0

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
Ryan Foster
  • 103
  • 9
  • `$d` here is not a two-symbol lookahead; it's shorthand for the set `{$, d}`. `FIRST` of a set consists of all the symbols which are in `FIRST` of an element of the set, so `FIRST({$, d})` is precisely `{$, d}`. – rici Nov 05 '19 at 16:26

1 Answers1

0

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'

  • I'm still confused. I get that when going through the grammar what follows X is {d}, but I have always been taught that when doing the E-closure, you take the first of the string that comes after that non-terminal. Why doesn't that work in this case? – Ryan Foster Nov 05 '19 at 05:46
  • 1
    There are two cases: case1: A->aBβ we will take the FIRST(β) but if FIRST(β) contains null or A->aB then what ever that follows A should be considered. – Venkatesh Nandigama Nov 05 '19 at 05:49