How to Parse a given LALR(1) Grammar

136 Views Asked by At

I'm having trouble parsing the following grammar using the LALR method.

s -> y
y -> dX | ydX
X -> e | Zd
z -> F | epsilon

I'm ok at the beginning, here is item state 0: (the , seperates the lookahead states)

s -> .y, $
y -> .dX, $d
y -> .ydX, $d

Now this is fine, but when you move to state 1 off of a d terminal I get confused. My book has state 2 as the following:

y -> d.X, $d
X -> .e, $d
X -> .Zd, $d
Z -> .f, d
Z -> ., d

Where does the lookahead terminal "d" come from in the X non-terminal? I thought that d.X came from .dX which had the lookahead terminals "$" and $d". But when doing the E-closure shouldnt the lookaheads be the first of $d which is "$"? why is it "$", or "d"? I thought it might come from another state since this is LALR but the state I end up merging state 1 with also doesn't have a d in the lookahead. Can somebody explain to me why there is a "d" paired with the "$" in the lookahead of this state? Thanks.

1

There are 1 best solutions below

2
On

S$->Y$->dX$ in this derivation sequence after d what follows X is {$}

S$->Y$->ydX$->dXdX$ in this derivation after first d what follows X is {d}

both derivation sequences are covered in same state after covering first symbol in the input'd'