I was trying to implement a LL(1) top-down parser for a calculator language. It only allows us to sum, subtract, divide and multiply numbers. No parentheses.
S -> A
A -> B + A
| B - A
| B
B -> int * B
| int / B
| int
As this grammar is not suited to a LL(1) parser, I had to change it quite a bit:
S -> A
A -> B A'
A'-> + A
| - A
| λ
B -> int B'
B'-> * B
| / B
| λ
The problem is that now the grammar is not left associative for the 4 shown operators, and I need it to be so. How to solve this problem? Is it even possible to accomplish so?
LL(1)
parsers are not a good match for parsing expressions; if you don't want to useLR(1)
for some reason, write a Pratt parser or a operator precedence parser (see "Shunting Yard algorithm") – Pinko