[antlr-interest] Change type of current AST

Dimitrios Kolovos dskolovos at gmail.com
Fri Jun 6 15:39:36 PDT 2008


I actually have the book in front of me and have spent quite a few hours 
looking at several v3 examples but I can't seem to figure out how to do 
something which looks dead simple (at least from my v2 experience) :(

Just FYI, the .token thing is syntactically ok but $tree at that point 
is null and thus, $tree.token throws an NPE. I'll start looking at the 
generated code to see if I can figure out how I can do it there and then 
copy the solution into the grammar.

In any case thanks a lot for your time and for providing this excellent 
tool for free.

Cheers,
Dimitrios

Terence Parr wrote:
> ok, i'm not going to predict code anymore for you...please start 
> reading the source or book or examples:
>
>     /** A single token is the payload */
>     public Token token;
>
> try it with that field.
> Ter
> On Jun 6, 2008, at 3:15 PM, Dimitrios Kolovos wrote:
>
>> Unfortunately this doesn't work as it generates
>>
>> if ( state.backtracking==0 ) {
>>  ((Token)((CommonTree)retval.tree).payload).setType(Operator);
>> }
>>
>> which doesn't compile as CommonTree doesn't have a payload field...
>>
>> Cheers,
>> Dimitrios
>>
>> On Fri, Jun 6, 2008 at 10:46 PM, Terence Parr <parrt at cs.usfca.edu> 
>> wrote:
>>> Oh, right.  ((Token)$tree.payload).setType(Operator) would do it.
>>> Ter
>>> On Jun 6, 2008, at 2:37 PM, Dimitrios Kolovos wrote:
>>>
>>>> If I set ASTLabelType=CommonType, $tree.type = Operator; it generates
>>>>
>>>> if ( state.backtracking==0 ) {
>>>> ((CommonTree)retval.tree).type = Operator;
>>>> }
>>>>
>>>> which is incorrect as CommonTree doesn't have a "type" field
>>>>
>>>> (without an ASTLabelType = it generates
>>>>
>>>> if ( state.backtracking==0 ) {
>>>> ((Object)retval.tree).type = Operator;
>>>> }
>>>>
>>>> which is also (profoundly) incorrect
>>>> )
>>>>
>>>> Cheers,
>>>> Dimitrios
>>>>
>>>> Terence Parr wrote:
>>>>>
>>>>> Oh, well $tree.type = foo; is what you want.
>>>>> Ter
>>>>> On Jun 6, 2008, at 1:50 PM, Dimitrios Kolovos wrote:
>>>>>
>>>>>> Thanks for your prompt reply. It seems we are getting closer (but 
>>>>>> still
>>>>>> no cigar :)
>>>>>>
>>>>>> both $tree = and $additiveExpression.tree = produce
>>>>>>
>>>>>> if ( state.backtracking==0 ) {
>>>>>> retval.tree = Operator;
>>>>>> }
>>>>>>
>>>>>> which does not seem to change the type of the AST (the getType() 
>>>>>> returns
>>>>>> the value of a generated field named T_97). Everything else in 
>>>>>> the grammar
>>>>>> seems to be working fine (no warnings, expected ASTs).
>>>>>>
>>>>>> Cheers,
>>>>>> Dimitrios
>>>>>>
>>>>>>
>>>>>> Terence Parr wrote:
>>>>>>>
>>>>>>> Try $tree = or $additiveExpression.tree =  I believe.No doubt 
>>>>>>> you are
>>>>>>> getting an error from antlr below?
>>>>>>> Ter
>>>>>>> On Jun 6, 2008, at 1:17 PM, Dimitrios Kolovos wrote:
>>>>>>>
>>>>>>>> Hi Ter,
>>>>>>>>
>>>>>>>> Unfortunately, that doesn't seem to work either.
>>>>>>>>
>>>>>>>> additiveExpression
>>>>>>>> :    multiplicativeExpression (('+'|'-')^ multiplicativeExpression
>>>>>>>> {$additiveExpression.setType(Operator);})*
>>>>>>>> ;
>>>>>>>>
>>>>>>>> in the parser grammar produces
>>>>>>>>
>>>>>>>> if ( state.backtracking==0 ) {
>>>>>>>> $additiveExpression.setType(Operator);
>>>>>>>> }
>>>>>>>>
>>>>>>>> in the parser class.
>>>>>>>>
>>>>>>>> (I'm using 3.1b in case this makes a difference.)
>>>>>>>>
>>>>>>>> Cheers,
>>>>>>>> Dimitrios
>>>>>>>>
>>>>>>>> Terence Parr wrote:
>>>>>>>>>
>>>>>>>>> Use $ not #
>>>>>>>>> On Jun 6, 2008, at 6:27 AM, Dimitrios Kolovos wrote:
>>>>>>>>>
>>>>>>>>>> Hi,
>>>>>>>>>>
>>>>>>>>>> In ANTLR2 I was able to change the type of the current AST like
>>>>>>>>>> this:
>>>>>>>>>>
>>>>>>>>>> additiveExpression
>>>>>>>>>> :    multiplicativeExpression ((PLUS^|MINUS^)
>>>>>>>>>> multiplicativeExpression 
>>>>>>>>>> {#additiveExpression.setType(OPERATOR);})*
>>>>>>>>>> ;
>>>>>>>>>>
>>>>>>>>>> but I haven't been able to find how to do the same in v3. Any 
>>>>>>>>>> hints?
>>>>>>>>>>
>>>>>>>>>> Cheers,
>>>>>>>>>> Dimitrios
>>>>>>>>>
>>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>
>>>>>
>>>
>>>
>
>


More information about the antlr-interest mailing list