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

Unit Ii - 2020

The document discusses operator precedence parsing. It defines three precedence relations between terminals - a ≐ b means a and b have equal precedence, a ⋖ b means b has higher precedence than a, and a ⋗ b means b has lower precedence than a. It also discusses how to construct the operator precedence parsing table based on these relations and use it to parse strings according to the algorithm.

Uploaded by

KANCHAN -
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)
53 views

Unit Ii - 2020

The document discusses operator precedence parsing. It defines three precedence relations between terminals - a ≐ b means a and b have equal precedence, a ⋖ b means b has higher precedence than a, and a ⋗ b means b has lower precedence than a. It also discusses how to construct the operator precedence parsing table based on these relations and use it to parse strings according to the algorithm.

Uploaded by

KANCHAN -
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/ 43

15CS314J – COMPILER DESIGN

UNIT II
Prepared by:
Dr.R.I.Minu
Associate Professor

1
Follow(E) = {$,+,)}
Follow(T) ={$,+,),*}
Follow(F) ={$,+,),*}

E  E+T (1)
E  T (2)
T  T*F (3)
• For shifting use the state number
T  F (4)
• For reduction use the equation number and write
F  (E) (5)
across the Follow() symbol
F  id (6)
E  E+T (1)
E  T (2)
T  T*F (3)
T  F (4)
F  (E) (5)
F  id (6)
Construction of LR(1) Sets of Items
• For shifting use the state number
• For reduction use the equation number and write
across the symbol written in the second half
Operator Precedence Parser
• Operator Grammar
• Small, but an important class of grammars
• We may have efficient operator precedence parser (a shift reduce parser)for
an operator grammar

• In an operator grammar, no production rule can have:


•  at the right side
• Two adjacent non-terminals at the right side
Operator Precedence Grammar
Let G be an  free operator grammar. For each terminal symbols a and
b, the following conditions need to be satisfies
1. 𝑎 ≐ 𝑏, 𝑖𝑓 ∃ a production in RHS of the form ab, where  is
either  or a single non terminal. Ex: SiCtSeS implies i ≐ t and t ≐
e
2. 𝑎 ⋖ 𝑏, 𝑖𝑓 for some non-terminal A ∃ a production in RHS of the
form A ab and A b, where  is either  or a single non
terminal. Ex: SiCtS and C  b implies i⋖ b
Operator Precedence Grammar
3. 𝑎 ⋗ 𝑏, 𝑖𝑓 for some non-terminal A ∃ a production in RHS of the
form A ab and A b, where  is either  or a single non
terminal. Ex: SiCtS and C  b implies b⋗t
Precedence relations

• In operator-precedence parsing, we define three disjoint precedence


relations between certain pairs of terminals.
• 𝑎 ≐ 𝑏 - b has same precedence as a
• 𝑎 ⋖ 𝑏 - b has higher precedence than a
• 𝑎 ⋗ 𝑏 – b has lower precedence than a
• The determination of correct precedence relation between terminals
are based on the traditional notion of associativity and precedence of
operators.
Operator Precedence
• The intention of the precedence relation is to find the handle of a
right-sentential form
• ≐ appearing in the interior of the handle
• ⋖ with marking the left end
• ⋗ marking the right hand
Parsing
1. Scan the string form left end until the first ⋗ is encountered
2. Then scan backward (to the left) over any ≐ until a ⋖ is
encountered
3. The handle contains everything to left to the first ⋗ and to the right
of the ⋖ is encountered
Parsing
stack Rule Input
$⋖ id⋗+⋖ id⋗*⋖ id⋗ $ E id $id + id * id$

$⋖ +⋖ id⋗*⋖ id⋗ $ E id $E + id * id$

$⋖ +⋖ *⋖ id⋗ $ E id $E+E * id$

$⋖ +⋖*⋗ $ E E*E $E + E * E$

$⋖ +⋗ $ EE+E $E+E$

$$
Operator Precedence Parsing
• Ensure the Grammar satisfies the pre-requisite
• Compute Leading and Trailing
• Construct the operator precedence parsing table
• Parse the string based on the algorithm
Leading and Trailing
• LEADING : For each NT , those terminals that can be the first terminal
in a string derived from that NT
• TRAILING: For each NT, those terminals that can be the last terminal
in a string derived from that NT
Leading
• Based on two rules
• a is in Leading(A) if Aa, where  is  or any Non-Terminal
• If a is in Leading(B) and AB, then a in Leading(A)
Trailing
• Based on two rules
• a is in Trailing(A) if Aa, where  is  or any Non-Terminal
• If a is in Trailing (B) and A  B, then a in Trailing(A)
Example
Symbol Leading Trailing
E  E+T (1)
E  T (2)
E {+,*,(,id} {+,*,),id}
T  T*F (3)
T  F (4) T {*,(,id} {*,),id}
F  (E) (5)
F  id (6) F {(,id} {),id}
Operator precedence relation
𝐹𝑜𝑟 𝑒𝑎𝑐ℎ 𝑝𝑟𝑜𝑑𝑢𝑐𝑡𝑖𝑜𝑛 𝐴 → 𝑋1 𝑋2 𝑋3 𝑋4 ---𝑋𝑁
• 𝐼𝐹𝑋𝑖 𝑎𝑛𝑑𝑋𝑖+1 𝑎𝑟𝑒 𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 𝑋𝑖 ≐ 𝑋𝑖+1
• 𝐼𝐹𝑋𝑖 𝑎𝑛𝑑𝑋𝑖+2 𝑎𝑟𝑒 𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 𝑎𝑛𝑑 𝑋𝑖+1 𝑖𝑠 𝑛𝑜𝑛 𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 𝑋𝑖 ≐ 𝑋𝑖+2
• 𝐼𝐹𝑋𝑖 𝑖𝑠 𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 𝑎𝑛𝑑 𝑋𝑖+1 𝑎𝑟𝑒 𝑛𝑜𝑛 −
𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 𝑡ℎ𝑒𝑛 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑠𝑒𝑡 𝑜𝑓 𝑎 𝐿𝑒𝑎𝑑𝑖𝑛𝑔 𝑋𝑖+1 𝑠𝑒𝑡 𝑋𝑖 ⋖ 𝑎
• 𝐼𝐹𝑋𝑖 𝑖𝑠 𝑛𝑜𝑛 −
𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 𝑎𝑛𝑑 𝑋𝑖+1 𝑎𝑟𝑒 𝑡𝑒𝑟𝑚𝑖𝑛𝑎𝑙 𝑡ℎ𝑒𝑛 𝑓𝑜𝑟 𝑎𝑙𝑙 𝑠𝑒𝑡 𝑜𝑓 𝑎 𝑇𝑟𝑎𝑖𝑙𝑖𝑛𝑔 𝑋𝑖+1 𝑠𝑒𝑡 𝑎 ⋖
𝑋𝑖+1
Operator precedence table

You might also like