




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





LL parsing is easy to use, but unless the grammar is just right, you may need to perform all kinds of tedious^1 transformations. For this reason, it is much more common to use LR parsing.
An LR(1) Parser
Contrast to an LL(1) Parser
Consider the grammar S → E E → id E → ∗ E E and the input * id * * id id id. What is the resulting parse tree? (^1) Well, you won’t do it yourself, you’ll make the computer do it for you.
These states correspond to different stages of the production.
Consider the grammar A → x y A → z
Consider the grammar
A → x y A → q A → x z
(d) If s is already part of our set of states, discard it. Otherwise add it and continue with the next item.
Generate the Characteristic Finite State Machine for the following grammar. S → b A e A → x E A → x I E → a N I → c N N → y
Action Go To State + * int $ E T 0 S 3 1 2 1 S 4 2 R 2 S 5 R 2 3 R 4 R 4 R 4 4 S 3 6 5 R 3 R 3 R 3 6 R 1 S 5 R 1
Action Go To State + * int $ E T 0 S 3 1 2 1 S 4 2 R 2 S 5 R 2 3 R 4 R 4 R 4
...
In other words, how does state 2 of this machine know what to do?
It’s easiest to make the tables while you are making the CFSM.
(a) Looking at a terminal? Add a shift operation for that terminal in the Action table. Put the destination state in the Goto table. (b) At the end of a rule X → Y? Add a reduce operation for each terminal in the Follow set of X. (c) Looking at a non-terminal? Add the destination state to the Goto table.
Activity! Make the LR Table, and think about what should happen at state 5.
There are two ways that shift-reduce conflicts are usually handled.
In this example, since * has higher precedence, the reducing rule will be used, and the shifting rule ignored.
E → if E then E • E → if E then E • else E
A → x y B → x y
What will happen in the CFSM?
A → • x y B → • x y I 1 : S → A • I 2 : S → B • I 3 : A → x • y B → x • y I 4 : A → x y • B → x y • State 4 has a reduce-reduce conflict. This grammar has serious problems.