0% found this document useful (0 votes)
11 views30 pages

CD - Operator - Precedence Praser

The document covers the topic of Bottom-up Parsing in Compiler Design, focusing on syntax analysis and operator precedence grammar. It explains the classification of parsers, the generation of operator relation tables, and the process of operator precedence parsing. Additionally, it provides examples and discusses the construction of operator function tables for operator precedence parsing.

Uploaded by

sahusurabhi02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views30 pages

CD - Operator - Precedence Praser

The document covers the topic of Bottom-up Parsing in Compiler Design, focusing on syntax analysis and operator precedence grammar. It explains the classification of parsers, the generation of operator relation tables, and the process of operator precedence parsing. Additionally, it provides examples and discusses the construction of operator function tables for operator precedence parsing.

Uploaded by

sahusurabhi02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

Compiler Design

• Course Code: CS105101CS


• Unit 2: Syntax Analysis
• Lecture :Bottom-up Parsing
Outcome
§ Understanding the bottom-up approach of parsing
§ Revisiting the classification of the parsers
§ Understanding the Operator Precedence Grammar
§ How to convert a CFG in Operator Grammar
§ Generation of Operator Relation Table
§ Operator Precedence Parser
Generation of Parse Tree: Bottom-up Approach
S à aABe, A à Abc | a, B à d aabcde

S S à aABe
à aAde
à aAbcde
A B à aabcde
A
Right-most derivation
a a b c d e In reverse

Decision:
When to reduce
Classification of Parsers
Parsers Unambiguous

Top-Down Parsers Bottom-Up Parsers


Deterministic (Shift-Reduce Parsers)
Ambiguous
With Without
Backtracking Backtracking Operator
LR Parsers
Precedence Parsers

Brute Forcing
Predictive
Recursive
Parsers
Descent LR(0) SLR(1) LALR(1) CLR(1)
LL(1),
Parsers
LL(K)
Classification of Parsers
Bottom-Up Parsers

Ambiguous (Shift-Reduce Parsers)

Operator
LR Parsers
Precedence Parsers

LR(0) < SLR(1) < LALR(1) < CLR(1)


Operator Precedence Parser
§ It is mainly used for mathematical expressions.
§ It processes Operator Grammar.
§ It can handle Ambiguous Grammars.
§ It uses Operator Relation Table.
Operator Grammar
§ Used to define the operators.
Restrictions:
§ No adjacent non-terminals.
§ No epsilon (ε) productions.

E à EAE | id
Aà+|* E à E + E | E * E | id
Conversion to Operator Grammar
S à SAS | a
A à bSb | b

S à S bSbS | SbS | a
A à bSb | b

S à SbSbS | SbS | a
Construction of Operator Relation Table
E à E + E | E * E | id
Precedence Associativity
§ id à Highest Precedence *, /, % Left to Right
§ $ à Lowest Precedence
+, - Left to Right

id + * $
id --- > > > + > +
+ < > < > Higher precedence due to associativity
* < > > >
* > *
$ < < < ---
Higher precedence due to associativity
Operator Precedence Parsing
E à E + E | E * E | id id + * $
Rules: id --- > > >
§ If TOS < LookAhead, + < > < >
PUSH(shift) LookAhead in Stack.
id + id * id $
* < > > >
§ If TOS > LookAhead,
POP(reduce)
$ < < < ---

id
$
Stack
Operator Precedence Parsing
E à E + E | E * E | id id + * $
Rules: id --- > > >
E
§ If TOS < LookAhead, + < > < >
PUSH(shift) LookAhead in Stack. id + id * id $ * < > > >
§ If TOS > LookAhead,
POP(reduce)
$ < < < ---

id
$
Stack
Operator Precedence Parsing
E à E + E | E * E | id id + * $
Rules: id --- > > >
E
§ If TOS < LookAhead, + < > < >
PUSH(shift) LookAhead in Stack. id + id * id $ * < > > >
§ If TOS > LookAhead,
POP(reduce)
$ < < < ---

+
$
Stack
Operator Precedence Parsing
E à E + E | E * E | id id + * $
Rules: id --- > > >
E
§ If TOS < LookAhead, + < > < >
PUSH(shift) LookAhead in Stack. id + id * id $ * < > > >
§ If TOS > LookAhead,
POP(reduce)
$ < < < ---

id
+
$
Stack
Operator Precedence Parsing
E à E + E | E * E | id id + * $
Rules: id --- > > >
E E
§ If TOS < LookAhead, + < > < >
PUSH(shift) LookAhead in Stack. id + id * id $ * < > > >
§ If TOS > LookAhead,
POP(reduce)
$ < < < ---

id
+
$
Stack
Operator Precedence Parsing
E à E + E | E * E | id id + * $
Rules: id --- > > >
E E
§ If TOS < LookAhead, + < > < >
PUSH(shift) LookAhead in Stack. id + id * id $ * < > > >
§ If TOS > LookAhead,
POP(reduce)
$ < < < ---

*
+
$
Stack
Operator Precedence Parsing
E à E + E | E * E | id id + * $
Rules: id --- > > >
E E
§ If TOS < LookAhead, + < > < >
PUSH(shift) LookAhead in Stack. id + id * id $ * < > > >
§ If TOS > LookAhead,
POP(reduce)
$ < < < ---

id
*
+
$
Stack
Operator Precedence Parsing
E à E + E | E * E | id id + * $
Rules: id --- > > >
E E E
§ If TOS < LookAhead, + < > < >
PUSH(shift) LookAhead in Stack. id + id * id $ * < > > >
§ If TOS > LookAhead,
POP(reduce)
$ < < < ---

id
*
+
$
Stack
Operator Precedence Parsing
E à E + E | E * E | id id + * $
E
Rules: id --- > > >
E E * E
§ If TOS < LookAhead, + < > < >
PUSH(shift) LookAhead in Stack. id + id * id $ * < > > >
§ If TOS > LookAhead,
POP(reduce)
$ < < < ---

*
+
$
Stack
Operator Precedence Parsing
E
E à E + E | E * E | id id + * $
+ E
Rules: id --- > > >
E E * E
§ If TOS < LookAhead, + < > < >
PUSH(shift) LookAhead in Stack. id + id * id $ * < > > >
§ If TOS > LookAhead,
POP(reduce)
$ < < < ---

+
$
Stack
Operator Precedence Parsing
E
E à E + E | E * E | id id + * $
+ E
Rules: id --- > > >
E E * E
§ If TOS < LookAhead, + < > < >
PUSH(shift) LookAhead in Stack. id + id * id $ * < > > >
§ If TOS > LookAhead,
POP(reduce)
$ < < < ---

$
Stack
Another Way of Representation (Operator
Precedence Parsing)
id + id * id $
E à E + E | E * E | id id + * $
Stack Relational Operator Input Buffer Action id --- > > >
$ < id + id * id $ Shift id
+ < > < >
$ id > + id * id $ Reduce E à id
$E < + id * id $ Shift + * < > > >
$E+ < id * id $ Shift id $ < < < ---
$ E + id > * id $ Reduce E à id
E
$E+E < * id $ Shift *
$E+E* < id $ Shift id E E
+
$ E + E * id > $ Reduce E à id
$E+E*E > $ Reduce E à E * E id E * E
$E+E > $ Reduce E à E + E
$E $ Accept id id
EàE+E|T
Example TàT*F|F
a+b*c*d$
Fàa|b|c|d a b c d + * $
Stack Relational Input Buffer Action a -- -- -- -- > > >
Operator b -- -- -- -- > > >
$ < a+b*c*d$ Shift a
c -- -- -- -- > > >
$a > +b*c*d$ Reduce F à a
$F < +b*c*d$ Shift + d -- -- -- -- > > >
$F+ < b*c*d$ Shift b + < < < < > < >
$F+b > *c*d$ Reduce F à b * > < < < > > >
$F+F < *c*d$ Shift *
$ < < < < < < --
$F+F* < c*d$ Shift c
$F+F*c > *d$ Reduce F à c
$F+F*F > *d$ Reduce T à F
$F+F*T > *d $ Error
Problem with Operator Precedence Parser
Operator Relation Table
§ Number of Terminal = n
id + * $ n

id --- > > >


+ < > < >
* < > > > n
$ < < < ---

Table size = O(n2)


Operator Function Table
E à E + E | E * E | id
Operator Relation Table

id + * $ g fid gid
id --- > > >
+ < > < > f+ g+
* < > > >
$ < < < --- f* g*

f$ g$
f
Operator Function Table E à E + E | E * E | id

fid g* f+ g+ f$
fid gid
f+ g+ f$

f+ g+
f* g* f+ g+ f$

gid f* g+ f* g*
g* f+ f$

g+ f$ f$ g$

g* f+ g+ f$
Operator Function Table E à E + E | E * E | id

fid g* f+ g+ f$

f+ g+ f$ id + * $
f 4 2 4 0
f* g* f+ g+ f$ g 5 1 3 0

gid f* g* f+ g+ f$

g+ f$

g* f+ g+ f$
Operator Function Table E à E + E | E * E | id

§ Number of Terminal = n
n
id + * $
f 4 2 4 0
g 5 1 3 0
n

Table size = O(2n)

Table size = O(n2)


Example
Construct the operator function table for operator precedence parsing from the following CFG.

P à SR | S Paragraph: Sentence followed by Recursive Sentence or a Sentence


R à bSR | bS Recursive Sentence: a blank followed by a Sentence & Recursive Sentence, or a blank
S à WbS | W followed by a Sentence
WàL*W|L Sentence: a Word followed by a blank and Sentence or a Word
L à id Word: a Letter concatenated with a Word or Letter
Letter: identifier
Example
Construct the operator function table for operator precedence parsing from the following CFG.

P à SR | S id b * $ g
R à bSR | bS id -- > > >
S à WbS | W fid gid
b < < < >
WàL*W|L
L à id * < > < >
$ < < < -- fb gb
P à SbSR | SbS |S
S à WbS | W
WàL*W|L f* g*
L à id f

f$ g$
Example
Construct the operator function table for operator precedence parsing from the following CFG.
P à SR | S
R à bSR | bS fid g* f* gb fb g$
S à WbS | W
WàL*W|L
L à id g f g f g
id * b b $

fid gid

fb gb

id b * $
f* g* f 5 1 3 0
g 4 2 4 0
f$ g$

You might also like