[antlr-interest] rewrite rule like AST construction operator '^'

John B. Brodie jbb at acm.org
Sun Oct 24 07:07:23 PDT 2010


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))* ;





More information about the antlr-interest mailing list