Question

I am new to the subject of compilation and have just started an exercise for Bottom -up parsing.

I have stuck on the following problem.

build a LR(0) parsing table for following grammar:

1) E –> E + T
2) E –> T
3) T –> (E)
4) T –> id


I0 :

   E' –> .E
   E –> .E + T
   E –> .T
   T –> .(E)
   T –> .id

on E the next state in the DFA would be :

 I1:

    E' -> E.
    E  -> E. + T

from what I have learned so far isn't this a S-R conflict? because the parser would not know whether to reduce or shift as it has no look-ahead variable? so this should not be LR(0) grammar?

but the PDF which I am reading have built the LR(0) table. So is there a mistake in the PDF or have I gone wrong some where understanding the concept?

Was it helpful?

Solution

This is indeed a shift/reduce conflict. This grammar isn't LR(0). You can also see this because it's not prefix-free; the grammar contains multiple strings that are prefixes of one another, so it can't be LR(0).

That said, you can still construct all the LR(0) configurating sets and make the LR(0) automaton. It just won't be deterministic because of the shift/reduce conflict. It's possible, therefore, that you're right and the handout is right.

Hope this helps!

OTHER TIPS

You augmented the grammar with E' -> E. Normally, you'd augment with a production like E' -> E $, where $ is a (terminal) symbol that doesn't otherwise occur in the grammar, and denotes end-of-input.

So I1 would actually be


E' -> E. $
E  -> E. + T

and there isn't a conflict. (And I believe the grammar is LR(0).)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top