[antlr-interest] rewrite rules cont.

Trevor John Thompson tijet at mac.com
Tue Oct 26 20:07:25 PDT 2010


Greetings.
I continue to wrestle with rewrite rules for AST construction. I am trying to treat semicolon and newline as equivalent separators, and gather a sequence of expressions as children of a single AST node. The grammar looks like
=======
grammar Test;
options {output=AST;}
prog:	expr EOF!;
expr:	(term->term) (((NL|SC) term)+ -> ^(NL $expr term+))?;
term:	ID
	|	->ID	// empty treated as no-name ID
	;
fragment
SP	:	' '|'\t';
SC	:	';';
ID	:	SP*
		('a'..'z'|'A'..'Z'|'_')
		('0'..'9'|'a'..'z'|'A'..'Z'|'_')*
	;
NL	:	('\r'|'\n')+;
=======
The problem is that if the sequence does *not* include newline, then i get RewriteEmptyStreamException on the NL in the rewrite rule; i.e. "a;\n" works, but "a;" does not.

What particularly baffles me is that if i build the node with any token other than NL or SC (e.g. SP), then the rule *always* works.

Could someone please explain what is going on?
Thanks
TJ
--
Trevor John Thompson    net: tijet at mac.com

Quidquid Latine dictum sit, altum videtur.



More information about the antlr-interest mailing list