8 Operator Precedence Parsing 13-08-2024
8 Operator Precedence Parsing 13-08-2024
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).
Grammar:
E-->E + T | T
T-->T * F | F
F-->(E) | id
Parsing Methods
Parsing
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.
EE+E
EE*E String: id1+id2*id3
Eid
Rightmost Derivation Right sentential form Handle Production
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: EE+T | T Rule:
TT*F | F 1. If A αaβ and (α is a single variable or
ε) then Leading (A) = a
Fid 2. If A Ba|β and β is a variable (NT)
then Leading (A) = Leading (β)
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}
+ * id $
+ .> <. <. .>
Soln:
Step 1: Find Leading & Trailing of NT
a ^ ( ) , $
Step 2(a): Establish Relation
a .> .> .>
a ^ ( ) , $
• 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 .> .>
* <. .>
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 * .> .> <. .>
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 * .> .> <. .>
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 * .> .> <. .>
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 * .> .> <. .>
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
+ * 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$