Docsity

LR Table Construction - Compiler Construction - Lecture Notes, Study notes of Compiler Construction

Toggle sidebar
bg1
Sohail Aslam Compiler Construction Notes Set:2-1
1
L
Le
ec
ct
tu
ur
re
e
2
27
7
LR Table Construction
Construct CC = {I0,I1,I2, ... , In}, for G'. State i of the parser is constructed from the set Ii
The parsing actions for state i are determined as follows:
for each item Ii ∈ CC
if [A → α•a β, b] ∈ Ii and goto(Ii, a) == Ij then
Action[i,a] ← “shift j”
else if [A → •, a] ∈ Ii and A ≠ S' then
Action[i,a] ← “reduce A → α ”
else if [S' → S•, $] ∈ Ii then
Action[i,a] ← “accept”
end for
// the goto table
for each non-terminal A ∈ G
if goto(Ii, A) = Ij then
Goto[i, A] ← j
The initial state is the one that contains the item [S' → •S, $]. All remaining entries are
marked “error”. Let us go through an example and construct the LR table for the
augmented grammar
1. S' → E
2. E → T – E
3. E → T
4. T → F × T
5. T → F
6. F → id
The FIRST sets we would need are
Symbol FIRST
S' { id }
E { id }
T { id }
F { id }
id { id }
× { × }
– { – }
pf3
pf4
pf5

Partial preview of the text

Download LR Table Construction - Compiler Construction - Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

Le Leccttuurree 2 277

LR Table Construction

Construct CC = {I 0 ,I 1 ,I 2 , ... , I (^) n }, for G'. State i of the parser is constructed from the set Ii The parsing actions for state i are determined as follows:

for each item I (^) i ∈ CC if [A → α•a β, b] ∈ I (^) i and goto(I (^) i, a) == I (^) j then Action[i,a] ← “shift j” else if [A →  • , a] ∈ I (^) i and A ≠ S' then Action[i,a] ← “reduce A → α ” else if [S' → S • , $] ∈ I (^) i then Action[i,a] ← “accept” end for

// the goto table for each non-terminal A ∈ G if goto(I (^) i , A) = I (^) j then Goto[i, A] ← j

The initial state is the one that contains the item [S' → • S , $]. All remaining entries are marked “error”. Let us go through an example and construct the LR table for the augmented grammar

  1. S' → E
  2. E → T – E
  3. E → T
  4. T → F × T
  5. T → F
  6. F → id

The FIRST sets we would need are

Symbol FIRST S' { id } E { id } T { id } F { id } id { id } × { × }

  • { – }

We construct the canonical collection of set of LR(1)

I 0 = {closure([S' → • E, $])} { [S' → • E, $], [E → • T – E, $], [E → • T, $], [T → • F×T, $], [T → • F, $] [T → • F×T, – ], [T → • F, – ], [F → • id, $], [F → • id, – ], [F → • id, ×] }

I 1 = {goto(I 0 , E)} = {[S' → E•, $] }

I 2 = {goto(I 0 , T)} = {[E → T• – E,$], [E →T•,$]}

I 3 = {goto(I 0 , F)} = { [T → F• ×T, $], [T → F•, $], [T → F• ×T, – ], [T → F•, – ] }

I 4 = {goto(I 0 , id)} = { [F → id • , $], [F → id • , – ], [F → id • , ×] }

I 5 = {goto(I 2 , –)} = { [E →T– • E, $], [E → • T–E, $], [E → • T, $], [T → • F×T, $], [T → • F×T, – ], [T → • F, $], [T → • F, – ], [F → • id, $], [F → • id, – ], [F → • id, ×] }

I 6 = {goto(I 3 , ×)} = { [T → F× • T, $], [T → F× • T, – ], [T → • F×T, $], [T → • F×T, – ], [T → • F, $], [T → • F, – ], [F → • id, $], [F → • id, – ], [F → • id, ×] }

I 7 = {goto(I 5 , E)} = {[E → T – E • , $] }

I 2 = {goto(I 5 , T)}, i.e., goto(I 5 , T) yields the same set as I 2.

I 3 = {goto(I 5 , F)}

I 4 = {goto(I 5 , id)}

I 4 = {[F → id•, $], [F → id•, – ], [F → id•, ×] } ⇒ Action[4, $] ← reduce 6 ⇒ Action[4, –] ← reduce 6 ⇒ Action[4, ×] ← reduce 6

I 7 = {[E → T – E•, $] } ⇒ Action[7, $] ← reduce 2

I 8 = { [T → F×T•, $], [T → F×T•, – ] } ⇒ Action[8, $] ← reduce 4 ⇒ Action[8, –] ← reduce 4

Apply

  1. if [S' → S • , $] ∈ I (^) i then set action[i,$] to “accept”

I 1 = {[S' → E•, $] } ⇒ Action[1, $] ← accept

Apply for each non-terminal A ∈ G if goto(I (^) i , A) = I (^) j then goto[i, A] ← j.

goto(I 0 , E) = I 1 ⇒ goto[0,E] ← 1 goto(I 0 , T) = I 2 ⇒ goto[0,T] ← 2 goto(I 0 , F) = I 3 ⇒ goto[0,F] ← 3 goto(I 5 , E) = I 7 ⇒ goto[5,E] ← 7 goto(I 5 , T) = I 2 ⇒ goto[5,T] ← 2 goto(I 5 , F) = I 3 ⇒ goto[5,F] ← 3 goto(I 6 , T) = I 8 ⇒ goto[6,T] ← 8 goto(I 6 , F) = I 3 ⇒ goto[6,F] ← 3

The final table we get is

Action Goto id – (^) × $ E T F 0 s4 1 2 3 1 acc 2 s5 r 3 r5 s6 r 4 r6 r6 r 5 s4 7 2 3 6 s4 8 3 7 r 8 r4 r

Let us parse the expression x – y × z using the LR(1) table. The scanner will encode the input string as id – id × id $ where $ is the EOF marker

Stack Input ¤0 id – id × id $ s ¤0id4 – id × id $ r6 F→ id ¤0F3 – id × id $ r5 T→ F ¤0T2 (^) – id × id $ s ¤0T2–5 id × id $ s ¤0T2–5id4 × id $ r6 F→ id ¤0T2–5F3 × id $ s ¤0T2–5F3× 6 id $ s ¤0T2–5F3×6id4 $ r6 F→^ id ¤0T2–5F3×6F3 $ r5 T→ F ¤0T2–5F3×6T8 $ r4 T→ F×T ¤0T2–5T2 $ r3 E→T ¤0T2 –5E7 $ r2 E→T–E

¤0E1 $ accept