0% found this document useful (0 votes)
137 views

LALR

The document discusses LALR parsing, which is a type of shift-reduce parsing. It involves two main steps: shift, which advances the input pointer and pushes the shifted symbol onto the stack, and reduce, which replaces grammar rules on the stack with their left-hand sides. The LALR parsing algorithm involves first designing an LR(1) parser, then merging states with the same productions to create the LALR parser. An example is provided to demonstrate the process of designing an LR(1) parser and converting it to an LALR parser.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views

LALR

The document discusses LALR parsing, which is a type of shift-reduce parsing. It involves two main steps: shift, which advances the input pointer and pushes the shifted symbol onto the stack, and reduce, which replaces grammar rules on the stack with their left-hand sides. The LALR parsing algorithm involves first designing an LR(1) parser, then merging states with the same productions to create the LALR parser. An example is provided to demonstrate the process of designing an LR(1) parser and converting it to an LALR parser.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

LALR Parsing

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|$

I89= A aA. , a|b|$


3) Transition Diagram for LALR Parser
Io I1
S
I2 I5
A
A

a I36
b I47
I36 I89
a

I36

I47 I47
b
4) LALR Parsing Table

State Action GOTO


a b $ A S
Io S36 S47 1 2
I1 Accept
I2 S36 S47 5
I36 S36 S47 89
I47 R3 R3 R3
I5 R1
I89 R2 R2 R2
5) Stack Implementation

Stack Input Action


$0 aabb$ Shift A to stack and GOTO
$0a3 abb$ Shift a to S3
$0a3a3 bb$ Shift b to S4
$0a3a3b4 b$ Reduce A to b
$0a3a3A8 b$ Reduce A to aA
$0a3A8 b$ Reduce A to aA
$0A2 b$ Shift b to S4
$0a2b4 $ Reduce A to b
$0A2A5 $ Reduce S to AA
$0S1 $ Accept
6) Parse Tree
QUESTIONS

You might also like