Syntax Analysis - Bottom Up Parsers - LR - Parsers - KR - Notes
Syntax Analysis - Bottom Up Parsers - LR - Parsers - KR - Notes
Every LR parser has a driver program. It reads input from left to right, uses a stack to
keep track of the states of LR automaton, an ACTION table, and a GOTO table. All LR
parsers have the same architecture and uses the same driver program. They differ only
in how they construct their tables.
The states of the LR automaton that describes that stack content are numbered by small
integers. So are the productions.
The value of ACTION[i, a], where i is a state and a is a grammar symbol, can have one of
four forms:
o Shift j, written sj, tells the parser to shift state j onto the stack.
o Reduce j, written rj, tells the parser to reduce by production j, i.e., popping off
as many states on the stack as there are symbols on the body of production j,
exposing state i on the stack. The parser then pushes onto to the stack the state
determined by GOTO[i, A] where A is the head of production j.
o Accept, written acc, tells the parser to accept the input and finish parsing.
o Error, shown as a blank entry, tells the parser there’s an error in the input and
that it should take a corrective action.
In summary, LR(0) parser is a simpler version of LR(1) parser that constructs a parsing table without
considering the lookahead symbols. This makes it more efficient but also less powerful. By following the
steps outlined above, we can construct a parsing table for a given grammar using LR(0) parser.
Canonical Collection of LR(0) items
If a state (Ii) is going to some other state (Ij) on a terminal then it corresponds to a shift move in the
action part.
If a state (Ii) is going to some other state (Ij) on a variable then it correspond to go to move in the
Go to part.
If a state (Ii) contains the final item like A → ab• which has no transitions to the next state then the
production is known as reduce production. For all terminals X in FOLLOW (A), write the reduce entry
along with their production numbers.
Example
1. S -> •Aa
2. A->αβ•
1. Follow(S) = {$}
2. Follow (A) = {a}
Example
Given grammar:
1. S → AA
2. A → aA | b
LR(0) Table
o If a state is going to some other state on a terminal then it correspond to a shift move.
o If a state is going to some other state on a variable then it correspond to go to move.
o If a state contain the final item in the particular row then write the reduce node completely.
Explanation:
o I0 on S is going to I1 so write it as 1.
o I0 on A is going to I2 so write it as 2.
o I2 on A is going to I5 so write it as 5.
o I3 on A is going to I6 so write it as 6.
o I0, I2and I3on a are going to I3 so write it as S3 which means that shift 3.
o I0, I2 and I3 on b are going to I4 so write it as S4 which means that shift 4.
o I4, I5 and I6 all states contains the final item because they contain • in the right most end. So
rate the production as production number.
Productions are numbered as follows:
1. S → AA ... (1)
2. A → aA ... (2)
3. A → b ... (3)
o I1 contains the final item which drives(S` → S•), so action {I1, $} = Accept.
o I4 contains the final item which drives A → b• and that production corresponds to the
production number 3 so write it as r3 in the entire row.
o I5 contains the final item which drives S → AA• and that production corresponds to the
production number 1 so write it as r1 in the entire row.
o I6 contains the final item which drives A → aA• and that production corresponds to the
production number 2 so write it as r2 in the entire row.
******* More Notes **********
SLR Parser
An LR(0) item (or just item) of a grammar G is a production of G with a dot at some position of the right
side indicating how much of a production we have seen up to a given point.
For example, for the production E -> E + T we would have the following items:
[E -> .E + T]
[E -> E. + T]
[E -> E +. T]
[E -> E + T.]
(1) E -> E * B The Action and Goto Table The two LR(0) parsing tables for this grammar look as follows:
(2) E -> E + B
(3) E -> B
(4) B -> 0
(5) B -> 1
Example LR(0) automaton
goto(I, X)
begin
let J be the set of items [A -> aX.b, a] such that
[A -> a.Xb, a] is in I
return closure(J);
end;
procedure items(G')
begin
C := {closure({S' -> .S, $})};
repeat
for each set of items I in C and each grammar symbol X such
that goto(I, X) is not empty and not in C do
add goto(I, X) to C
until no more sets of items can be added to C;
end;
1. S` → •S, $
2. S → •AA, $
3. A → •aA, a/b
4. A → •b, a/b
I0 State:
Add Augment production to the I0 State and Compute the Closure
I0 = Closure (S` → •S)
Add all productions starting with S in to I0 State because "." is followed by the non-terminal. So, the I0 State
becomes
I0 = S` → •S, $
S → •AA, $
Add all productions starting with A in modified I0 State because "." is followed by the non-terminal. So, the
I0 State becomes.
I0= S` → •S, $
S → •AA, $
A → •aA, a/b
A → •b, a/b
I1= Go to (I0, S) = closure (S` → S•, $) = S` → S•, $
I2= Go to (I0, A) = closure ( S → A•A, $ )
Add all productions starting with A in I2 State because "." is followed by the non-terminal. So, the I2 State
becomes
I2= S → A•A, $
A → •aA, $
A → •b, $
1.S → AA … (1) The placement of shift node in CLR (1) parsing table is same as the SLR
2. A → aA ....(2) (1) parsing table. Only difference in the placement of reduce node.
3. A → b ... (3)
I4 contains the final item which drives ( A → b•, a/b), so action {I4, a} =
R3, action {I4, b} = R3.
I5 contains the final item which drives ( S → AA•, $), so action {I5, $} =
R1.
I7 contains the final item which drives ( A → b•,$), so action {I7, $} = R3.
I8 contains the final item which drives ( A → aA•, a/b), so action {I8, a} =
R2, action {I8, b} = R2.
I9 contains the final item which drives ( A → aA•, $), so action {I9, $} =
R2.