Reputation: 3
I am struggling to understand why an LL(1) parser would not be able to parse this.
A ::= B PLUS A | B
B ::= NUM | ID
Upvotes: 0
Views: 155
Reputation: 1
The grammar is not LL. The two productions (alternatives) of non-terminal A share the same prefix, which is not accepted in LL parsing, i.e, the grammar should be factorized first.
Upvotes: 0
Reputation: 56925
A ::= B PLUS A | B
has to look ahead to figure out which rule to use. The B
is ambiguous.
You could add a new rule A'
that goes to epsilon to remove the ambiguity:
A ::= B A'
A' ::= PLUS A | epsilon
B ::= NUM | ID
Upvotes: 2