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

Module 4 -Operator Precedence Parser

Uploaded by

ankurvatsa3
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

Module 4 -Operator Precedence Parser

Uploaded by

ankurvatsa3
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

CSI2005

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
Ab
Program
Bd

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
Ab
Program
Bd 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
Ab
Program
Bd 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
Ab
Program
Bd 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
Ab
Program
Bd 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
Ab
Program
Bd 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
Ab
Program
Bd 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
Ab
Program
Bd 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
Ab
Program
Bd 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
Ab
Program
Bd b

This parser is known as an LR Parser because


it scans the input from Left to right, and it constructs
a Rightmost derivation in reverse order.
13
Operator grammar

A grammar with a property that no production right hand side is  or


has two adjacent non-terminals is called operator grammar.
Example:
E → E + E / E * E / id
A small class of expression grammar can be parsed using operator-
precedence techniques.
Precedence Relations
a<b a has less precedence than b
a=b a has the same precedence as b
a>b a has higher precedence than b
Operator-Precedence Relations From Associativity and Precedence

1. If operator ϴ1 has higher precedence than operator ϴ2 , make


ϴ1 > ϴ2 and ϴ2 < ϴ1
For example, if * has higher precedence than + , make
* > + and + < *.
These relations ensure that, in an expression of the form
E + E * E + E , the middle E * E is the handle that will be reduced first.
2. If ϴ1 and ϴ2 are operators of equal precedence (they may in fact be
the same operator), then make ϴ1 > ϴ2 and ϴ2 > ϴ1 if the operators
are left-associative, or make ϴ1 < ϴ2 and ϴ2 < ϴ1 if the operators
are right-associative.
For example, if + and – are left-associative, then make
+ > + , + > – , – > – and – > +
if ↑ is right-associative, then make ↑ < ↑.

These relations ensure that, in an expression of the form

E – E + E , will have handle E – E is reduced first and

E ↑ E ↑ E , will have handle E ↑ E is reduced first.


3. Make ϴ < id , id > ϴ (both left and right associative)
ϴ< ( , ( < ϴ (right-associative)
ϴ> ) , ) > ϴ (left-associative)
ϴ > $ , $ < ϴ (both left and right associative)
for all operators ϴ. Also, let
(=) , $ <( , $ < id

(<( , id > $ , ) >$

( < id , id > ) , ) >)


Operator-Precedence
Relations
Algorithm: (Operator precedence parsing)
Set ip to point the first symbol of w$ ; repeat forever
if $ is on top of the stack and ip points to $ then
return;
else
begin
let a be the top most symbol on the stack and let b be the symbol pointed by ip;
if a < b or a = b then
push b onto the stack;
advance ip to the next input symbol;
else if a > b then
repeat
pop the stack;
until the top of the stack terminal < to the terminal most recently poped;
else error();
end;
Example
Consider the grammar
E  E + E / E * E / id
Input string : id + id * id
Operator-precedence table

id + * $

id > > >

+ < > < >

* < > > >

$ < < <


parsing
STACK INPUT ACTION
$ id + id * id $ a = $ b = id
$id + id * id $ a < b , push b
$id + id * id $ a = id b = +
$ + id * id $ a > b, pop stack
$ < id , so stop the pop
operation
$ + id * id $ a=$ b=+
$+ id * id $ a < b , push b
$+ id * id $ a = + b = id
$+id *id $ a < b , push b
Parsing
STACK INPUT ACTION
$+ id * id $ a = id b = *
$+ * id $ a > b , pop
+ < id , stop pop operation
$+ * id $ a=+ b=*
$+ * id $ a < b, push b
$+ * id $ a = * b = id
$+ * id $ a < b , push b
$+ * $ a = id b = $ , a > b, pop
id > *, stop pop operation
$+ * $ a=* b=$
Parsing
STACK INPUT ACTION
$+ $ a > b , pop
+ < * , stop pop operation
$+ $ a=+ b=$
$ $ a > b , pop
$ < + , stop pop operation
$ $ a = $ b = $ halt
Precedence Functions
• Compilers using operator precedence parsers need not store the table of
precedence relations.
• Precedence table can be encoded by two functions f and g that maps the
terminal symbols to integers.
• For symbols a and b
1. f(a) < g(b) whenever a < b
2. f(a) = g (b) whenever a = b
3. f(a) > g(b) whenever a > b
Precedence relation between a and b can be determined by a numerical
comparison between f(a) and g(b).
Constructing precedence functions
Input : An operator precedence matrix
1. Create symbols fa and ga for each a is a terminal or $.
2. Partition the created symbols into as many groups as possible.
i ) if a = b then fa and gb are in the same group.
ii) if a = b and c = b then fa and fc must be in the same group.
3. Create a directed graph whose nodes are the groups found in (2).
i) if a < b , place an edge from the group of gb to the group of fa
ii) if a > b , place an edge from the group of fa to the group of gb
4. If the constructed graph has a cycle, then no precedence function exist.
5. If there are no cycles, let f(a) be the length of the longest path beginning at
the group of fa . Let g(b) be the longest path from the group of gb
Precedence Functions and Precedence Graph

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)

You might also like