0

The Grammar is:

S → ( L ) | id
L → S | L , S

I'm trying to Compute CLOSURE and GOTO on the given grammer using LR parsing.

Our teacher solved this, but in the first step he didn't use the second production L-->S|L,S, I don't know why. So I solved the same example but uisng full grammer in firsr step.
By this, the orginal was only 9 steps but mine was 10 steps.

My question is, does my solution is correct? I think I did LR(1).


1) Solution in lecture -------- 2) solution of mine.

enter image description here

Community
  • 1
  • 1
Caffe Latte
  • 1,693
  • 1
  • 14
  • 32

1 Answers1

1

Your solution is not correct.

The starting configuration is:

S' → . S $

That state is then expanded recursively to include all productions A → ω where A appears immediately following the position marker. Initially, the only non-terminal which follows the dot is S, so we add all of the productions for S:

S' → . S $
S  → . ( L )
S  → . id

There are no more non-terminals following the dot, so the state is complete.

Now, for each symbol following the ., we construct a follow state by moving the dot over the symbol. The only interesting one here is the second one, which will illustrate the closure rule. We start with:

S → ( . L )

Now we have a production where L immediately follows the dot, so we expand L into the state:

S → ( . L )
L → . S
L → . L , S

We're not done yet, because now there is another non-terminal following the dot, S. Consequently, we have to add those productions as well:

S → ( . L )
L → . S
L → . L , S
S → . ( L )
S → . id

Now the productions for every non-terminal which immediately follows the . is included in the state, so the state is complete.

rici
  • 234,347
  • 28
  • 237
  • 341
  • +1, thanks for the answer. I constructed the parsing table & parsed the sentence `id+id*(id)` in the exam. Should the teacher mark the entire question wrong or only the first part? Thanks again for the notes. – Caffe Latte Sep 22 '14 at 05:22
  • 1
    @Caffè: I have no comment on what your teacher's marking algorithm might be :) Your state table will (probably) construct the correct parse because although you incorrectly put `L->(L)|id` into state 1, no reduction will ever attempt `GOTO(L)` on that state. (Can you see why not?). Thus, your state 10 is unreachable and, in this particular case, the error is irrelevant. In general, however, this error could result in apparent shift/reduce or reduce/reduce conflicts, which obviously would affect parsing. Good luck, anyway. – rici Sep 22 '14 at 05:34
  • Yes, you're right. I noticed that too, it didn't affect my parsing table. I hope he understand what I did. – Caffe Latte Sep 22 '14 at 05:40