Operator Precedence Parsing
Types of Parser
Parser
Top Down Bottom Up
Backtracking Non Operator LR Parser
Backtracking Precedence
Predictive
Parser
Recursive Table SLR CLR LALR
Descent Driven Parser Parser Parser
Requirement for the Grammar
• Operator precedence grammar is kinds of shift
reduce parsing method. It is applied to a small
class of operator grammars.
• A grammar is said to be operator precedence
grammar if it has two properties:
• No Right Hand Side of any production has a∈.
• No two non-terminals are adjacent.
• Compilers uses operator precedence parsing
technique for parsing expression
Example
This is not a
Operator Grammar
This is a Operator Grammar
Precedence Relation
• Three disjoint precedence relation ⋖, ≐, ⋗
Between certain pairs of terminals
• These precedence relation guide the
selection of handlers and have the
following meaning
a ⋗ b means that terminal "a" has the higher
precedence than terminal "b".
a ⋖ b means that terminal "a" has the lower
precedence than terminal "b".
a ≐ b means that the terminal "a" and "b" both
have same precedence.
Rules to determine precedence relation from
associativity and precedence
• If operator Ɵ1 has higher precedence than Ɵ2
Ɵ1 ⋗ Ɵ2 , Ɵ2 ⋖ Ɵ1
* ⋗ + , + ⋖ *
• If operator Ɵ1 and Ɵ2 has equal precedence 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
precedence relation for Other
operators
• Ɵ ⋖ id id ⋗ Ɵ
• Ɵ⋖ ( (⋖ Ɵ
• )⋗ Ɵ Ɵ⋗)
• Ɵ⋗ $ $⋖ Ɵ
• (≐ )
• $ ⋖ id id ⋗ $
• )⋗$ $⋖ (
• (⋖ id id ⋗ )
• )⋗ ) (⋖ (
Precedence table:
Parsing
1. Insert $ symbol at the beginning and ending of the
input string.
2. Insert Precedence operator between every two
symbols of the string by referring the operator
precedence table.
3. Start scanning the string from LHS in the forward
direction until ⋗ symbol is encountered.
4. Start scanning the string from RHS in the backward
direction over any ≐ , until ⋖ symbol is encountered.
5. Everything that lies in the middle of ⋖ and ⋗ forms
the handle.
6. Replace the handle with the head of the respective
production.
7. Keep repeating the cycle from Step-03 to Step-06 until
the start symbol is reached.
Problem
• Construct the operator precedence parser and
parse the string id + id x id.
• E → EAE | id
• A→+|x
• E → E + E | E x E | id
• We insert $ symbol at both ends of the string as-
• $ id + id x id $
• We insert precedence operators between the
string symbols as-
• $ ⋖ id ⋗ + ⋖ id ⋗ x ⋖ id ⋗ $
• $ ⋖ id ⋗ + ⋖ id ⋗ * ⋖ id ⋗ $
• $ E + ⋖ id ⋗ * ⋖ id ⋗ $
• $ ⋖ E + ⋖ id ⋗ * ⋖ id ⋗ $
• $ ⋖ E + E * ⋖ id ⋗ $
• $ ⋖ E + ⋖ E * ⋖ id ⋗ $
• $⋖E+⋖E*E$
• $⋖E+ ⋖E*E⋗ $
• $ ⋖E+E⋗ $
• $E$
Construction of Operator Precedence Table
1. Ensure the Grammar satisfies the pre-
requisite
2. Compute the functions Leading and Trailing
3. Using the computed leading and trailing,
construct the Operator precedence parsing
table
LEADING and TRAILING computation
• LEADING is defined for each non-terminal
such that, terminals that can be the first
terminal in a string derived from that non-
terminal.
• TRAILING for each non-terminal are those
terminals that can be the last terminal in a
string derived from that non-terminal.
LEADING computation
1. ‘a’ is in Leading(A) if A γaδ where γ is ε or
any Non-Terminal
2. If’ ‘a’ is in Leading(B) and A Bα, then a in
Leading(A)
TRAILING computation
1. a is in Trailing(A) if A γaδ where δ is ε or
any Non-Terminal
2.If a is in Trailing(B) and A αB, then a in
Trailing(A)
Parsing Table Construction
• For each production A X1X2X3 ...Xn
• if Xi and Xi+1 are terminals set Xi ≐ Xi+1
• if i ≤ n-2 and Xi and Xi+2 are terminals and
Xi+1 is a non-terminal
• set Xi ≐ Xi+2
• if Xi is a terminal and Xi+1 is a non-terminal then
for all ‘a’ in Leading(Xi+1)
set Xi ⋖ a
• if Xi is a non-terminal and Xi+1 is a terminal then
for all ‘a’ in trailing(Xi)
set a ⋗ X
+ * id ( ) $
+ ⋗ ⋖ ⋖ ⋖ ⋗ ⋗
* ⋗ ⋗ ⋖ ⋖ ⋗ ⋗
id ⋗ ⋗ ⋗ ⋗
( ⋖ ⋖ ⋖ ⋖ ≐
) ⋗ ⋗ ⋗ ⋗
$ ⋖ ⋖ ⋖ ⋖
Operator Precedence Functions
• Precedence functions perform the mapping of
terminal symbols to the integers.
• To decide the precedence relation between
symbols, a numerical comparison is
performed.
• It reduces the space complexity to a large
extent.
• Create a group fa or ga for each terminal and $
• If a ≐ b then fa & gb are in the same group
• If a ≐ b and c ≐ b, then fa, fc & gb are in the same
group
• Create a directed graph,
• if a ⋖ b , then draw directed edge from gb to fa
• if a ⋗ b , then draw directed edge from fa to gb
• If No cycle exist then precedence function exist
• Let f(a) is the length of the longest path beginning at
the group of fa
• Let g(a) is the length of the longest path beginning at
the group of g
Graph - Operator Precedence
Functions
Operator Precedence Functions