Why do these two rules result in an infinite-recursion error from ANTLR?

Terence Parr

Think of these rules as methods, hence, if rule 'a' calls rule 'b', the first thing it may do is call rule 'a', resulting in infinite recursion. In fact, you can rewrite the grammar to the more obvious, but equivalent:

b : b B
  | C

This works fine in bottom-up (LALR) parser generators like YACC, but cause problems for top-down (LL) tools like ANTLR. Use the following grammar (the bonus is that it's more readable):

d : C (B)* ;