[antlr-interest] Problem with recursive rules

Johannes Luber jaluber at gmx.de
Sun Dec 16 15:34:23 PST 2007


Mark schrieb:
> Hi,
> 
> I've just discovered ANTLR and it looks like a fantastic tool, but I'd be
> grateful if anyone here could offer me any help with the following..
> 
> If I try to parse an IF statement in a language with a rule like this:
> 
> statement =
> '{' statement '}'
> |  ....
> |  'if' expression 'then' statement 'else' statement
> 
> Then I get a conflict warning.  Looking at it in Antlrworks shows that the
> problem is: if it finds a statement like "if (a) then if (b) then
> doSomething; else doSomethingElse", it doesn't know which 'if' the 'else'
> applies to.  It should always apply to the inner one (ie, the "if b" in this
> example), but how do I tell ANTLR that?

ANTLR does already that. You can suppress the warning by adding a
symantic predicate like:

if_statement
	:	IF OPEN_PARENS bool_expression CLOSE_PARENS embedded_statement
((ELSE)=> ELSE embedded_statement)?
	;

as I use it.

Johannes


More information about the antlr-interest mailing list