ANTLR Solution Mutual Left Recursive Rules

the "expr" rule in the ANTLR grammar below is obviously mutually left recursive. As a beginner, ANTLR is hard to solve my problem. I read "Conflict Resolution Without LL (*)" in the ANTLR directory, but I still don't see a solution. Any pointers?

LPAREN: ('(');
RPAREN: (')');
AND: ('AND' | '&' | 'EN');
OR: ('OR' | '|' | 'OF');
NOT: ('-' | 'NOT' | 'NIET');
WS: ('' | '\ t' | '\ r' | '\ n') {$ channel = HIDDEN;};
WORD: (~ ('' | '\ t' | '\ r' | '\ n' | '(' | ')' | '"')) *;

input: expr EOF;
expr: (andexpr | orexpr | notexpr | atom);
andexpr: expr AND expr;
orexpr: expr OR expr;
notexpr: NOT expr;
phrase: '"' WORD * '"';
atom: (phrase | WORD); 
+3
source share
1 answer

I would suggest looking at an example of grammarians on the antlr site. Explicit grammar does what you want.

Basically you can do something like this:

expr : andexpr;
andexpr : orexpr (AND andexpr)*;
orexpr : notexpr (OR orexpr)*;
notexpr : atom | NOT expr;

The key is that each expression can end like an atom.

+5
source

Source: https://habr.com/ru/post/1761306/


All Articles