Search code examples
parsinglr-grammar

Is there a rule of thumb for which symbol to shift first when generating LR(0) parser table?


I'm reading about LR(0) parser from this book: Modern Compiler Implementation in Java. Below is how the parsing table looked like based on the book. http://postimg.org/image/hyowddu1h/

Start symbol: S --> E$

Productions:

(1) E --> T + E

(2) E --> T

(3) T --> x

I tried making a parsing table based on the productions given but I didn't get the same parsing table as the one in the book. I think I shifted the symbol correctly. It's just that I have different parsing table than the one in the book. (Note: I started with state 0 instead of state 1 like in the book)

So is the parsing table unique or is there any rule of thumbs on deciding which symbol to shift to the stack first or how to label the parsing state correctly? I always shift the terminal symbols first then nonterminal symbols as shown below: http://postimg.org/image/76vbu2vu3/

Thanks in advance!


Solution

  • State numbers are not that important. In fact, they're not important at all. What is important is state identity; how you label the state to show that identity is up to you. You could use names, like hurricanes and tornados.

    Two machines are the same if you can construct a one-to-one mapping between the two state sets such that all the labeled transition preserve the mapping. I'm pretty sure you can construct that mapping between the state machine in your textbook and the machine you built.