Module 4 -Operator Precedence Parser
Module 4 -Operator Precedence Parser
Principles of Compiler
Design
MODULE - 3
Dr. WI. Sureshkumar
Associate Professor
School of Computer Science and Engineering (SCOPE)
VIT Vellore
[email protected]
SJT413A34
Bottom-up Parsing
• Bottom up parsing attempts to construct a parse tree for an input
beginning at the leaves and working up towards root.
• Reducing a string w to S, the start symbol of the grammar.
• At each step, a particular substring matching the right side of a
production is replaced by the symbol on the left of that production.
• A rightmost derivation is traced out in reverse.
• Bottom-up parsers
• A bottom-up parser, or a shift-reduce parser,
begins at the leaves and works up to the top of the tree.
• The reduction steps trace a rightmost derivation on reverse.
Grammar parse
S aABe The input string : abbcde.
A Abc | b
B d
12/20/2024 3
Bottom-Up Parser Example
Shift a
INPUT: a b b c d e $ OUTPUT:
Production
S aABe
Bottom-Up Parsing
A Abc
Ab
Program
Bd
4
Bottom-Up Parser Example
Shift b
Reduce from b to A
INPUT: a b b c d e $ OUTPUT:
Production
S aABe
Bottom-Up Parsing
A Abc A
Ab
Program
Bd b
5
Bottom-Up Parser Example
Shift A
INPUT: a A b c d e $ OUTPUT:
Production
S aABe
Bottom-Up Parsing
A Abc A
Ab
Program
Bd b
6
Bottom-Up Parser Example
Shift b
INPUT: a A b c d e $ OUTPUT:
Production
S aABe
Bottom-Up Parsing
A Abc A
Ab
Program
Bd b
7
Bottom-Up Parser Example
Shift c
Reduce from Abc to A
INPUT: a A b c d e $ OUTPUT:
Production
A
S aABe
Bottom-Up Parsing
A Abc A b c
Ab
Program
Bd b
8
Bottom-Up Parser Example
Shift A
INPUT: a A d e $ OUTPUT:
Production
A
S aABe
Bottom-Up Parsing
A Abc A b c
Ab
Program
Bd b
9
Bottom-Up Parser Example
Shift d
Reduce from d to B
INPUT: a A d e $ OUTPUT:
Production
A B
S aABe
Bottom-Up Parsing
A Abc A b c d
Ab
Program
Bd b
10
Bottom-Up Parser Example
Shift B
INPUT: a A B e $ OUTPUT:
Production
A B
S aABe
Bottom-Up Parsing
A Abc A b c d
Ab
Program
Bd b
11
Bottom-Up Parser Example
Shift e
Reduce from aABe to S
INPUT: a A B e $ OUTPUT:
S
Production e
a A B
S aABe
Bottom-Up Parsing
A Abc A b c d
Ab
Program
Bd b
12
Bottom-Up Parser Example
Shift S
Hit the target $
INPUT: S $ OUTPUT:
S
Production e
a A B
S aABe
Bottom-Up Parsing
A Abc A b c d
Ab
Program
Bd b
id + * $
gid fid
+ * id $
f* g* f 2 4 4 0
g 1 3 5 0
g+ f+
f$ g$
2. Find the operator-precedence function for the following table.
a ( ) , $
a > > >
( < < = <
) > > >
, < < > >
$ < <
f( , g)
ga fa
f) g(
g, f, fa →g( → f, → g$ (3)
fa →g, → f$ (2)
fa →g( → f, → f(, g) (3)
f$ g$ fa →g( → f$ (2)