LALR
LALR
LOOKING BACKWARD
SHIFT-REDUCE PARSING
Shift-reduce parsing uses two unique steps for bottom-up parsing. These
steps are known as shift-step and reduce-step.
• Shift step: The shift step refers to the advancement of the input
pointer to the next input symbol, which is called the shifted symbol.
This symbol is pushed onto the stack. The shifted symbol is treated
as a single node of the parse tree.
• Reduce step : When the parser finds a complete grammar rule (RHS)
and replaces it to (LHS), it is known as reduce-step. This occurs when
the top of the stack contains a handle. To reduce, a POP function is
performed on the stack which pops off the handle and replaces it
with LHS non-terminal symbol.
LR PARSER
The LR parser is a non-recursive, shift-reduce, bottom-up parser. It uses a
wide class of context-free grammar which makes it the most efficient
syntax analysis technique. LR parsers are also known as LR(k) parsers,
where L stands for left-to-right scanning of the input stream; R stands for
the construction of right-most derivation in reverse, and k denotes the
number of look ahead symbols to make decisions.
It has further types:
• SLR •LR(0)
• LALR •LR(1)
• CLR
HISTORY
LR parser was introduced by Donald knuth.
1st time LALR parser was introduced by Frank DeRamar in 1969 in his PhD.
1st algorithm was developed in 1973.
1st time published in 1982 by DeRamar and Pennello.
LALR
Look ahead left to right (LALR) parser
Step 1: Design LR(1) Parser
• Augmented grammar
• Calculation s of 1st set
• Transition diagram
• LR (1) Parsing table
Step 2: Design LALR Parser
• Find states having same production and merge both the states
• Transition diagram
• LALR parsing table
SOLING THROUGH EXAMPLE
Question:
S AA
A aA|b
Solution: Step 1: Design of LR(1) Parser
1) Augmented Grammar
S’ .S
S .AA
A .aA
A .b
2) Calculation of 1st set
Here, we will calculate Firsts of the given grammar
First of (S) = {a,b}
First of (A) = {a,b}
3) Transition Diagram
Io I1
S
I2 I5
A
A
I6 I9
a A
a I6
I3
a b
I7 I7
b
I8
A
I4
b a I3
b I4
4) LR (1) Parsing Table
state Action GOTO
a b $ S A
Io S3 S4 1 2
I1 Accept
I2 S6 S7 5
I3 S3 S4 8
I4 R3 R3
I5 R1
I6 S6 S7 9
I7 R3
I8 R2 R2
I9 R2
Step 2: Design of LALR Parser
In this step, we get common transition states from transition table of
LR(1) transition Diagram, and combine them.
I36 = A a.A , a|b|$
S .aA , a|b|$
A .b , a|b|$
I47= A b. a|b|$
a I36
b I47
I36 I89
a
I36
I47 I47
b
4) LALR Parsing Table