Search code examples
parsinglr-grammar

difficulty solving this example in LR parsing?


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


Solution

  • 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.