0% found this document useful (0 votes)
9 views40 pages

8 Operator Precedence Parsing 13-08-2024

Uploaded by

vedhvirat
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)
9 views40 pages

8 Operator Precedence Parsing 13-08-2024

Uploaded by

vedhvirat
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/ 40

Module 2 – Syntax Analysis

Bottom Up Parsing
Bottom-Up Parsing: Reductions
• The process of “reducing" a string w to the start symbol of the
grammar.
• At each reduction step, a specific substring matching the body of a
production is replaced by the nonterminal at the LHS/Head of that
production.
• The key decisions during bottom-up parsing are about when to
reduce and about what production to apply, as the parse proceeds.
• The reductions will be discussed in terms of the sequence of strings
id * id; F * id; T * id; T * F; T; E
Bottom-Up Parsing
• A bottom-up parsing corresponds to the construction of a parse
tree for an input string beginning at the leaves (the bottom) and
working up towards the root (the top).

• Ex: A bottom-up parse for id * id

Grammar:
E-->E + T | T
T-->T * F | F
F-->(E) | id
Parsing Methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking LR parsing
(predictive Parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
Handle & Handle pruning
• Handle: A “handle” of a string is a substring of the string that matches the
right side of a production, and whose reduction to the non-terminal of the
production is one step along the reverse of rightmost derivation.
• Handle pruning: The process of discovering a handle and reducing it to
appropriate left hand side non-terminal is known as handle pruning.
EE+E
EE*E String: id1+id2*id3
Eid
Rightmost Derivation Right sentential form Handle Production

E id1+id2*id3 id1 Eid


E+E E+id2*id3 id2 Eid
E+E*E E+E*id3 id3 Eid
E+E*id3 E+E*E E*E EE*E
E+id2*id3 E+E E+E EE+E
id1+id2*id3 E
Shift reduce parser
• The shift reduce parser performs following basic operations:
1. Shift: Moving of the symbols from input buffer onto the stack, this
action is called shift.
2. Reduce: If handle appears on the top of the stack then reduction of it by
appropriate rule/production is done. This action is called reduce action.
3. Accept: If stack contains start symbol only and input buffer is empty at
the same time then that action is called accept action.
4. Error: A situation in which parser cannot either shift or reduce the
symbols, it cannot even perform accept action then it is called error
action.
Example: Shift reduce parser
Grammar: Stack Input Buffer Action
EE+T | T $ id+id*id$ Shift
TT*F | F $id +id*id$ Reduce Fid
Fid $F +id*id$ Reduce TF
String: id+id*id $T +id*id$ Reduce ET
$E +id*id$ Shift
$E+ id*id$ Shift
$E+id *id$ Reduce Fid
$E+F *id$ Reduce TF
$E+T *id$ Shift
$E+T* id$ Shift
$E+T*id $ Reduce Fid
$E+T*F $ Reduce TT*F
$E+T $ Reduce EE+T
$E $ Accept
Example 2
Consider the grammar
E –> 2E2
E –> 3E3
E –> 4
Perform Shift Reduce parsing for input string “32423”.
Example 2 - Solution
Example 3
Consider the grammar
S –> ( L ) | a
L –> L , S | S
Perform Shift Reduce parsing for input string “( a, ( a, a ) ) “.
Example 3 - Solution
Parsing Methods
Parsing

Top down parsing Bottom up parsing (Shift reduce)

Back tracking Operator precedence

Parsing without
backtracking (predictive LR parsing
Parsing)
SLR
LL(1)
CLR
Recursive
descent LALR
Operator precedence parsing
• An operator precedence parser is a bottom-up parser that interprets an
operator grammar
• Operator Grammar: A Grammar in which there is no Є in RHS of any
production or no adjacent non-terminals is called operator grammar.
• Example: E EAE | (E) | id
A + | * | -
• Above grammar is not operator grammar because right side EAE has
consecutive non terminals.
• In operator precedence parsing we define following disjoint relations:
Relation Meaning
a<.b a “yields precedence to” b
a=b a “has the same precedence as” b
a.>b a “takes precedence over” b
Precedence & associativity of operators
Operator Precedence Associative
↑ 1 right
*, / 2 left
+, - 3 left
Steps of operator precedence parsing
1. Find Leading and trailing of non terminal
2. Establish relation and Creation of table
3. Parse the string
Leading & Trailing
Leading:- Leading of a non-terminal is the first terminal or operator in
production of that non-terminal.
Trailing:- Trailing of a non-terminal is the last terminal or operator in
production of that non-terminal.
Example 1: EE+T | T Rule:
TT*F | F 1. If A αaβ and (α is a single variable or
ε) then Leading (A) = a
Fid 2. If A Ba|β and β is a variable (NT)
then Leading (A) = Leading (β)

Non terminal Leading Trailing


E {+,*,id} {+,*,id}
T {*,id} {*,id}
F {id} {id}
Leading & Trailing
Example 2: S-->a | ^ | (T)
T-->T,S | S

Rule:
Non terminal Leading Trailing
1. If A αaβ and (α is a single variable
S {a,^, (} {a,^,)} or ε) then Leading (A) = a
T {,,a,^, (} {,,a,^,)} 2. If A Ba|β and β is a variable (NT)
then Leading (A) = Leading (β)
Rules to establish a relation
1. For a = b, ⇒ aAb, where A is ϵ or a single non-terminal [e.g : (E)]
2. a <.b ⇒Op.NT then Op <. Leading NT [e.g : +T]
3. a .>b ⇒NT.Op then Trailing(NT ) .>Op [e.g : E+]
4. $ <. Leading (start symbol)
5. Trailing (start symbol) .> $
Example: Operator precedence parsing
Step 1: Find Leading & Trailing of NT
E E+T| T
Nonterminal Leading Trailing T T*F | F
E {+,*,id} {+,*,id} F id
T {*,id} {*,id}
F {id} {id}

Step 2 (a): Establish Relation Step2(b): Creation of Table


+ * id $
1. a <.b + .> <. <. .>

.> .> <. .>


2. 𝑂𝑝 . 𝑁𝑇 𝑂𝑝 <. 𝐿𝑒𝑎𝑑𝑖𝑛𝑔 𝑁𝑇 *
id .> .> .>
3. +𝑇 + <. ∗, 𝑖𝑑 $ <. <. <.
4. ∗ 𝐹 ∗ <. {𝑖𝑑}
Example: Operator precedence parsing
Step 1: Find Leading & Trailing of NT
E E+T| T
Nonterminal Leading Trailing T T* F| F
E {+,*,id} {+,*,id} F id
T {*,id} {*,id}
F {id} {id}

Step 2 (a): Establish Relation Step2(b): Creation of Table


+ * id $
1. a .>b + .> <. <. .>

.> .> <. .>


2. 𝑁𝑇 . 𝑂𝑝 𝑇𝑟𝑎𝑖𝑙𝑖𝑛𝑔(𝑁𝑇 ). > 𝑂𝑝 *
id .> .> .>
3. 𝐸 + +,∗, 𝑖𝑑 . > + $ <. <. <.
4. 𝑇 ∗ {∗, 𝑖𝑑}. >∗
Example: Operator precedence parsing
Step 1: Find Leading & Trailing of NT
E E+T| T
Nonterminal Leading Trailing T T* F| F
E {+,*,id} {+,*,id} F id
T {*,id} {*,id}
F {id} {id}

Step 2 (a): Establish Relation Step2(b): Creation of Table


+ * id $
1. $<. Leading (start symbol) + .> <. <. .>
2. $ <. +,∗, 𝑖𝑑 * .> .> <. .>
3. Trailing (start symbol) .> $ id .> .> .>
4. +,∗, 𝑖𝑑 . > $ $ <. <. <. Accept
Example: Operator precedence parsing
Step 3: Parse the string using precedence table

+ * id $
+ .> <. <. .>

* .> .> <. .>

id .> .> .>

$ <. <. <.


Example 2: Operator precedence parsing
Construct Precedence parsing table for the following grammar:
S-->a | ^ | (T)
T-->T,S | S

Soln:
Step 1: Find Leading & Trailing of NT

Non terminal Leading Trailing


S {a,^, (} {a,^,)}
T {,,a,^, (} {,,a,^,)}
Example 2: Operator precedence parsing
S-->a | ^ | (T)
T-->T,S | S

Step 2(b): Creation of Table

a ^ ( ) , $
Step 2(a): Establish Relation
a .> .> .>

^ .> .> .>

( <. <. <. = <.

) .> .> .>

, <. <. <. .> .>

$ <. <. <.


Example 2: Operator precedence parsing
Step 3: Parse the string using precedence table

a ^ ( ) , $

a .> .> .>

^ .> .> .>

( <. <. <. = <.

) .> .> .>

, <. <. <. .> .>

$ <. <. <.


Precedence function
• Disadvantage of precedence table
• If we have ‘n’ operators then size of table will be n*n and
complexity will be O(n2).
• In order to decrease the size of table, we use operator function
table.

• Precedence function
• Used to store precedence relation into encoded form
• Two functions ‘f’ and ‘g’ are used to map terminal symbols into
integers
• There exists symbols ‘fa’ and ‘ga’ for each terminal ‘a’
• The precedence relations between the symbols are implemented
by numerical comparison
Operator precedence function
Algorithm for constructing precedence functions
1. Create functions 𝑓𝑎 and 𝑔𝑎 for each ′a’ that is terminal or $.
2. Partition the symbols in as many as groups possible, in such a way that 𝑓𝑎 and 𝑔𝑏 are in
the same group if 𝑎 = 𝑏.
3. Create a directed graph whose nodes are in the groups, next for each symbols
𝑎 𝑎𝑛𝑑 𝑏 do:
a) if 𝑎 <· 𝑏, place an edge from the group of 𝑔𝑏 to the group of 𝑓𝑎
b) if 𝑎 ·> 𝑏, place an edge from the group of 𝑓𝑎 to the group of 𝑔𝑏
4. If the constructed graph has a cycle then no precedence functions exist. When there are
no cycles collect the length of the longest paths from the groups of 𝑓𝑎 and
𝑔𝑏 respectively.
Operator precedence function
1. Create functions fa and ga for each a that is terminal or $. E E+T | T
T T*F | F
F id
𝑎 = {+,∗, 𝑖𝑑} 𝑜𝑟 $

f+ f* fid f$

g+ g* gid g$
Operator precedence function
• Partition. the symbols in as many as groups possible, in such a way that fa
and gb are in the same group if a = b.

+ * id $
+ .> <. <. .>
gid fid .> .>
* <. .>

id .> .> .>

$ <. <. <.


f* g*

g+ f+

f$ g$
Operator precedence function
3. if a <· b, place an edge from the group of gb to the group of fa
if a ·> b, place an edge from the group of fa to the group of gb

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

id .> .> .>

f* g* $ <. <. <.

f+ .> g+ f+-->g+
g+ f+
f* .> g+ f*-->g+
fid .> g+ fid-->g+
f$ g$ f$ <. g+ f$  g+
Operator precedence function
3. if a <· b, place an edge from the group of gb to the group of fa
if a ·> b, place an edge from the group of fa to the group of gb

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

id .> .> .>

f* g* $ <. <. <.

g+ f+
f+ <. g* f+  g*
f* .> g* f*-->g*
fid .> g* fid-->g*
f$ g$ f$ <. g* f$  g*
Operator precedence function
3. if a <· b, place an edge from the group of gb to the group of fa
if a ·> b, place an edge from the group of fa to the group of gb

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

id .> .> .>

f* g* $ <. <. <.

g+ f+
f+ <. gid f+  gid
f* <. gid f*  gid
f$ <. gid f$  gid
f$ g$
Operator precedence function
3. if a <· b, place an edge from the group of gb to the group of fa
if a ·> b, place an edge from the group of fa to the group of gb

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

id .> .> .>

f* g* $ <. <. <.

f+ > g$ f+-->g$
g+ f+
f* > g$ f*-->g$
fid >. g$ fid-->g$
f$ g$
Operator precedence function
+ * id $
f 2
gid fid g

f* g* 4. If the constructed graph


has a cycle then no
precedence functions
g+ f+ exist. When there are no
cycles collect the length of
the longest paths from the
f$ g$
groups of fa and gb
respectively.
Operator precedence function

+ * id $
f 2
gid fid
g 1

f* g*

g+ f+

f$ g$
Operator precedence function

+ * id $
f 2 4
gid fid
g 1

f* g*

g+ f+

f$ g$
Operator precedence function

+ * id $
f 2 4
gid fid
g 1 3

f* g*

g+ f+

f$ g$
Operator precedence function

+ * id $
f 2 4 4
gid fid
g 1 3

f* g*

g+ f+

f$ g$
Operator precedence function

+ * id $
f 2 4 4
gid fid
g 1 3 5

f* g*

g+ f+

f$ g$
Operator precedence function

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

f* g*

g+ f+

f$ g$

The complexity is reduced to O(2*n).

You might also like