Zahoori Nazo
Zahoori Nazo

Reputation: 53

Left recursion mutally in antlr 3

I try to parsing the grammar in the antlr 3 , but i have a problem with the left recursion , and I am a beginner in parsing grammar.

Upvotes: 0

Views: 142

Answers (1)

Jiri Tousek
Jiri Tousek

Reputation: 12440

The problem is that the roules b, e, t, f are referencing each other without consuming any input - e.g. a number could be accepted by multiple sequences:

b -> NUM
b -> e -> t -> f -> b -> NUM
...

the cycle you have there probably is meant to express a sub-expression - what's missing there then are parentheses:

start   : e;
e       : t (a t)*;
t       : f (m f)*;
f       : ID | NUM | '-'NUM | '(' e ')';
a       : '+' | '-';
m       : '*' | '/';

(I also changed e : t | t a t to e : t | e a t to allow 1 + 2 + 3)

Upvotes: 2

Related Questions