[antlr-interest] Rewrite with predicate -> default?

Tobias Diez webmaster at altertoby.de
Sat Jul 23 13:05:59 PDT 2011


Hi,

It is possible to rewrite something based on a predicate but not always?
What I want to do is the following (pseudo code)

^(TOMATCH)
	-> ^(RESULT1)   if TOMATCH.Text = 1
	-> ^(RESULT2)   if TOMATCH.Text = 2
	LEAVE IT UNCHANGED otherwise

The closest thing I could archive is the following code, which works in the
sense that the output-AST is correct. But this solution has the flaw that
ANTLR apparently thinks something has changed and reruns the rule (and
because nothing changed so far it match again) and so ACTION would be called
two times!

command_single_argument
	: ^(COMMAND c=CONTROL_WORD ^(BLOCK b=.)) {ACTION}
		-> {$c.Text == @"\begin"}? ^(ENVIRONMENT_BEGIN $b)
		-> {$c.Text == @"\end"}? ^(ENVIRONMENT_END $b)
		-> ^(COMMAND $c ^(BLOCK $b))
	;

Is there a better solution?

Thanks!



More information about the antlr-interest mailing list