6
$\begingroup$

Me and my friends are studying for an upcoming test and this exercise is one of the harder ones for us.

We have been trying to solve it and we have been looking at similar exercises, the problem is that the explanations are too complicated for us to understand. Keep in mind we just started with this.

So, my question is: Can anyone explain this as simple as possible, I have included the exercise.

Exercise

$\endgroup$

migrated from stackoverflow.com May 12 '13 at 10:07

This question came from our site for professional and enthusiast programmers.

  • 1
    $\begingroup$ First, you should be able to simulate parsing with just the grammar and the string. Then you need to understand how the LR parser generator builds this table. With that understanding, using the table should be easy. Check out my SO answer on learning how to do LR parsing: stackoverflow.com/a/5200159/120163 $\endgroup$ – Ira Baxter May 11 '13 at 17:18
  • 2
    $\begingroup$ Bizarre closure here. Vote to reopen. $\endgroup$ – user207421 May 12 '13 at 3:24
  • $\begingroup$ @IraBaxter, you can safely forget almost all about the grammar when using the tables. You do have to understand how to interpret the tables, that's all. $\endgroup$ – vonbrand May 12 '13 at 14:38
4
$\begingroup$

The mechanics of shift-reduce parsing is pretty simple -- the hard part is understanding why it works or how to construct the tables. But since you already have the table, that is not relevent.

To do shift/reduce parsing, you need an action table and a goto table (which you have) and a stack of states. You start with just state 0 on the stack. At each step you look up in the action table with the top state on the stack and the next token of input and perform the corrsponding action.

  • for a SHIFT action you consume the token of input and push the state on the stack

  • for a REDUCE action, you find the SIZE of the right-hand-side of the rule to be reduced (number of symbols -- ε is zero size) and the SYMBOL on the left-hand-size. You pop SIZE states from the stack and then use the state now on top of the stack and the SYMBOL to lookup in the GOTO table and push the corresponding state.

That's it. You repeat the above until you either reach an ACCEPT action (the string matches) or a REJECT action (any blank in the action table -- the string is not matched). The reduce action is a bit complicated as it chains immediately to a goto action, so you can think of the reduce+goto as a single action or as two distinct actions as you prefer.

If you want your parser to construct the parse tree while you're at it, its a bit more complicated. You need to have your stack hold state × subtree pairs -- when you shift a token you include that token (as a single node subtree) along with the state, and when you reduce, you create a new subtree from the subtrees popped (as children to the LHS symbol) which you push along with the GOTO state. When you get to the ACCEPT state you do a 'reduce 1' action to build the complete parse tree.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy