[antlr-interest] "become root" operator ^ sometimes fails

Mark Langezaal mark.langezaal at xs4all.nl
Tue Oct 14 05:59:41 PDT 2008


Uhh, subtitles please...
Do you mean this is a kind of a bug, but there is an easy workaround by
removing the brackets?

Regards,
/Mark

> Hi. ('+')^ is probably better as '+'^ since I think there has been
> some subrule-as-set funkiness.
> Ter
> On Oct 13, 2008, at 6:12 PM, Mark Langezaal wrote:
>
>> Hello,
>>
>> During the construction of a grammar I encountered an anomaly in the
>> creation of root nodes when using the "become root" operator ^.
>> I use ANTLRWorks version 1.2
>> With the below simplified grammar it can be reproduced.
>> The okExpr rule generates the expected AST. for input: 1 + 2
>> But the badExpr rule does NOT generate a new root node for '+' token.
>>
>> Can one of you experts confirm (or deny) this is a genuine problem?
>>
>> Best regards,
>> Mark langezaal
>>
>> ===================================
>> grammar foo;
>>
>> options {
>>   output = AST;
>>   ASTLabelType = CommonTree;
>> }
>>
>> // Input: 1 + 2
>> // Generated AST: ^(+ 1 2)
>> okExpr
>>   :    NUM (('+'|'-')^ NUM)*
>>   ;
>>
>> // Input: 1 + 2
>> // Generated AST: (1 + 2)
>> badExpr
>>   :    NUM (('+')^ NUM)*
>>   ;
>>
>> NUM
>>   :   ('0'..'9')+
>>   ;
>>
>> WS
>>   : ('\r'|'\n'|'\t'|' ')+  {skip();}
>>   ;
>>
>>



More information about the antlr-interest mailing list