[antlr-interest] rewrite rule like AST construction operator '^'
Trevor John Thompson
tijet at mac.com
Sun Oct 24 10:21:17 PDT 2010
On 2010 Oct 24, at 07:07, John B. Brodie wrote:
> Greetings!
>
> On Sun, 2010-10-24 at 02:35 -0700, Trevor John Thompson wrote:
>> I have a language in which one expression may immediately follow another. I am trying to construct an AST with an imaginary node representing the "concatenation" operator. I want the moral equivalent of
>> expr: term (SP^ term)*;
>> except that SP does *not* actually appear in the token stream.
>> When i try
>> expr: term (term->(^SP $expr term))*;
>> i get RewriteEmptyStreamException in that rule on input like
>> a b c
>> Here is a tiny grammar that demonstrates the problem:
>> grammar Test;
>> options {output=AST;}
>> prog: expr NL! EOF!;
>> expr: term (term->^(SP $expr term))*;
>> term: ID;
>> fragment
>> SP : ' '|'\t';
>> ID : SP*
>> ('a'..'z'|'A'..'Z'|'_'|'\u0080'..'\uFFFE')
>> ('0'..'9'|'a'..'z'|'A'..'Z'|'_'|'\u0080'..'\uFFFE')*
>> ;
>> NL : ('\r'|'\n')+;
>>
>> I would appreciate anyone pointing out what i am doing wrong.
>
> need to initialize the expr.tree before entering the loop, so try:
>
> expr: (term->term) (term->^(SP $expr term))* ;
That is exactly the fix. Thank you very much.
--
Trevor John Thompson net: tijet at mac.com
Quidquid Latine dictum sit, altum videtur.
More information about the antlr-interest
mailing list