Dillon Drobena
Dillon Drobena

Reputation: 921

How do I make this grammar LL(1)?

Lets say I have this grammar

E -> T+Ex | F
T -> T*Fy | w
F -> E | z | ε

Now I need to make it LL(1). I've been following the steps but the solution I came up with doesn't seem quite right. Fist lets eliminate ε-productions

E -> T+Ex | F | T+x
T -> T*Fy | w | T*y
F -> E | z

Now we'll eliminate cycles

E -> T+Ex | T+x | z
T -> T*Fy | w | T*y
F -> T+Ex | T+x | z

No we'll eliminate immediate left recursion

E -> T+Ex | T+x | z
T -> wT'
T' -> *FyT' | *yT' | ε
F -> T+Ex | T+x | z

Finally we'll replace certain RHS productions where T occurred

E -> wT'+Ex | wT'+x | z
T -> wT'
T' -> *FyT' | *yT' | ε
F -> wT'+Ex | wT'+x | z

Now this doesn't seem to be LL(1) to me since the parse table generated by this will have multiple entries for several of the terminals. What do I seem to be missing?

Upvotes: 1

Views: 381

Answers (1)

Dillon Drobena
Dillon Drobena

Reputation: 921

I figured out how to do it, so out last step we have this configuration

E -> wT'+Ex | wT'+x | z
T -> wT'
T' -> *FyT' | *yT' | ε
F -> wT'+Ex | wT'+x | z

We now need to perform left-factorization to remove productions of the form

S -> aB | aC

And make them into the proper form

S -> aA
A -> B | C

Using this we can transform our grammar into

E -> wT'+E' | z
E' -> Ex | x
T -> wT'
T' -> *T'' | ε
T'' -> FyT' | yT'
F -> wT'+F' | z
F' -> Ex | x

Since F and F' are the same as E and E' we can just remove this production so we are left with.

E -> wT'+E' | z
E' -> Ex | x
T -> wT'
T' -> *T'' | ε
T'' -> EyT' | yT'

Upvotes: 1

Related Questions