[antlr-interest] How do I use a custom token object?

Johannes Luber jaluber at gmx.de
Thu Jul 24 11:23:56 PDT 2008


Tom MacEllen schrieb:
> Hi Johannes,
> 
> thank you for your answer. I just tried what you suggested.
> Unfortunately "INT.x" doesn't work.
> 
> I looked at the generated source code and found out that the variable(s) which are named after the rule-names are public final integers. 
> So the expression "INT.x" results in an compiler error: "int cannot be dereferenced".
> 
> Tom

Then you could try to add a member variable which you set in the action 
and refer in Emit():

@members{
int x;

public override Emit() {
     return new MyToken(x);
}
}

ID : 'a'..'z'+ {this.x = 2; }

You may have to change the source code a bit to get it working.

Johannes
> 
>>> Is there a way to alter this value from the action in the grammar.
>>> Something like:
>>> ID : 'a'..'z'+  {/* set the integer x from here */};
>>> INT : '0'..'9'+ {/* and here */};
>>>
>>> I am just curious if that is possible?
>> Doesn't ID.x or INT.x work?
>>
>> Johannes
> 



More information about the antlr-interest mailing list