[antlr-interest] Change type of current AST

Dimitrios Kolovos dskolovos at gmail.com
Fri Jun 6 14:37:51 PDT 2008


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