Unit Ii - 2020
Unit Ii - 2020
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
$⋖ +⋖*⋗ $ E E*E $E + E * E$
$⋖ +⋗ $ EE+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 Aa, where is or any Non-Terminal
• If a is in Leading(B) and AB, then a in Leading(A)
Trailing
• Based on two rules
• a is in Trailing(A) if Aa, 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