[antlr-interest] Recursive rule invocation

David-Sarah Hopwood david-sarah at jacaranda.org
Sat Aug 29 11:50:13 PDT 2009


> On Aug 29, 2009 2:20pm, Martijn Reuvers <martijn.reuvers at gmail.com> wrote:
>> I would rewrite the markupStatement like this:
>
>> markupStatement :
>>   markup+ (expression | regularStatement | embedding)? ';'
   ;

jeroen.v.schagen at gmail.com wrote:
> Thanks for the response. I'm aware of the left factoring approach,
> however then the alternative:
> 
> markup+ ';'
> 
> will have a higher priority then:
> 
> markup+ expression ';'
> 
> which is invalid for my language.

The left-factored version is completely equivalent to the original unless
you have an ambiguity. We don't have enough of the grammar to be sure that
there isn't an ambiguity, but if there were, ANTLR would warn about it.

(Strictly speaking, left-factoring alone would give you

   markupStatement :
     markup+ (expression ';' | regularStatement ';' | embedding ';' | ';')
   ;

but the difference doesn't matter here.)

> Is there any way to specify priorities other then the ordering of alternatives?

At the risk of oversimplifying, alternatives are normally only prioritized
if at least one of them has a predicate. "The Definitive ANTLR Reference"
goes into detail about this in chapter 12.

Incidentally, you are matching ';' at the end of regularStatement as
well as in markupStatement, which seems likely to be wrong.

-- 
David-Sarah Hopwood  ⚥  http://davidsarah.livejournal.com



More information about the antlr-interest mailing list