3

I have been given a task to transform this grammar to LL(1)

E → E+E | E-E | E*E | E/E | E^E | -E | (E)| id | num

So for first step I eliminated ambiguity and got this:

E → E+T | E-T | T 
T → T*P | T/P | P
P → P ^ F | F
F → id | num | -E | (E)

And after that I have eliminated left-recursion and got this:

E  → TE'
E' → +TE' | -TE' | ɛ
T  → PT'
T' → *PT' | /PT' | ɛ
P  → FP'
P' → ^FP' | ɛ
F  → id | num | -E | (E)

When I put this into JFLAP and click 'Build LL(1) parse table' I get warning that grammar above is not LL(1).

Why this grammar is not LL(1), and what do I need to change to be LL(1).

Am_I_Helpful
  • 18,735
  • 7
  • 49
  • 73
clzola
  • 1,925
  • 3
  • 30
  • 49

1 Answers1

4

Your grammar is still ambiguous, so it can't be LL(1).

This production F → -E makes it possible to mix an expression with lower precedence operators in a level (unary operator) where they shouldn't appear.

Note that id + - id + id has two derivation trees.

You shouldn't use E there, but a symbol that represents an atomic value. You could replace

F → id | num | -E | (E)

with

F → -A | A
A → id | num | (E)

Or F → -F | A if you want to allow multiple unary minuses.

1010
  • 1,779
  • 17
  • 27