Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Construction Sohail Aslam Lecture 26. 2 Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production.

Similar presentations


Presentation on theme: "Compiler Construction Sohail Aslam Lecture 26. 2 Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production."— Presentation transcript:

1 Compiler Construction Sohail Aslam Lecture 26

2 2 Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production X →  A →  X  X →   

3 3 Finite Automaton of Items  The initial DFA state I 0 we computed is the  -closure of the set consisting of item S →  E

4 4  Recall the stage in the closure s ={ [S →  E, $], [E →  E + (E), $], [E →  int, $] } Finite Automaton of Items

5 5 NFA states [S →  E,$]  [E →  E+(E),$] [E →  int,$] 

6 6 Algorithm: Construction of collection of canonical sets of LR(1) items. Input: An augmented grammar G' Output: Collection of canonical ( CC ) sets of LR(1)

7 7 Algorithm: Construction of collection of canonical sets of LR(1) items. Input: An augmented grammar G' Output: Collection of canonical ( CC ) sets of LR(1)

8 8 Algorithm: Construction of collection of canonical sets of LR(1) items. Input: An augmented grammar G' Output: Collection of canonical ( CC ) sets of LR(1)

9 9 CC( G' ) I 0 ← {closure( [S' →  S, $] )} CC ← { I 0 } repeat for each unmarked set I j  CC mark I j as processed for each X following  in an item in I j I k ← goto( I j, X ) if I k  CC then CC ← CC  I k record transition from I j to I k on X until CC is not changing

10 10 Sets of LR(1) Items Augmented grammar G' S → E E → E + (E) | int

11 11 Sets of LR(1) Items  We now compute goto( I 0, X ) for various values of X  X can be E, int, +, ( and )

12 12 goto(s, y ) m  { } for each item [X →  y , b]  s m ← m  { [X →  y , b] } return closure(m)

13 13 I 1 = goto( I 0, int ) m ← {} for each [X →  int , b]  I 0  [E →  int, $ / +]  I 0 m = m  { [E → int , $ / +] } return closure({ [E → int ,$ / +] })

14 14 closure({ [E → int ,$ / +] }) No additional closure is possible since the dot is at the right end of the production

15 15  Thus I 1 = { [E → int , $ / +] }  And we have the transition from I 0 to I 1 on int

16 16  Thus I 1 = { [E → int , $ / +] }  And we have the transition from I 0 to I 1 on int I0I0 int I1I1

17 17 I 2 = goto( I 0, E ) m ← {} for each [X →  E , b]  I 0  [S →  E, $]  [E →  E+(E), $ / +]

18 18 m = m  { [S → E , $] }  { [E → E  +(E), $ / +] } return closure(m)

19 19 I 2 = { [S → E , $], [E → E  +(E), $ / +] }  No further closure for the first item because  is at the end  In the second item, a terminal + appears after  so no further closure

20 20 I 2 = { [S → E , $], [E → E  +(E), $ / +] }  No further closure for the first item because  is at the end  In the second item, a terminal + appears after  so no further closure

21 21  I 3 = goto( I 2, + ) = { [E → E +  (E), $ / +] }  I 4 = goto( I 3, ( ) = { [E → E + (  E), $ / +] [E →  E + (E), ) / +] [E →  int, ) / +] }

22 22  I 3 = goto( I 2, + ) = { [E → E +  (E), $ / +] }  I 4 = goto( I 3, ( ) = { [E → E + (  E), $ / +] [E →  E + (E), ) / +] [E →  int, ) / +] }

23 23  I 5 = goto( I 4, int ) = { [E → int , ) / +] }  I 6 = goto( I 4, E ) = { [E → E + (E  ), $ / +] [E → E  + (E), ) / +] }  and so on....

24 24  I 5 = goto( I 4, int ) = { [E → int , ) / +] }  I 6 = goto( I 4, E ) = { [E → E + (E  ), $ / +] [E → E  + (E), ) / +] }  and so on....

25 25  I 5 = goto( I 4, int ) = { [E → int , ) / +] }  I 6 = goto( I 4, E ) = { [E → E + (E  ), $ / +] [E → E  + (E), ) / +] }  and so on....

26 26 0 E → int on ), + int E + ( E E → int on $, + accept on $ S →  E, $ E →  E+(E), $/+ E →  int, $/+ 1 E → int , $/+ 2 S → E , $ E → E  +(E), $/+ 3 E → E+  (E), $/+ 4 E → E+(  E), $/+ E →  E+(E), )/+ E →  int, )/+ 6 E → E+(  E), $/+ E →  E+(E), )/+ E →  int, )/+ 5 E → int , )/+

27 27 NFA of LR(0) Items Consider the augmented grammar E ' → E E → E + n | n

28 28 E  E + E' → EE' → E E' → EE' → E E →  E+nE →  n E → E+  nE → E  +n E → n  E → E+n    n n 

29 29 E  E + E' → EE' → E E' → EE' → E E →  E+nE →  n E → E+  nE → E  +n E → n  E → E+n    n n 

30 30 E + E ' →  E E →  E+n E →  n E' → EE' → E E → E+  nE → E  +n E → n  E → E+n  n n E

31 31 E + E ' →  E E →  E+n E →  n E ' → E  E → E  +n n n E → E+  n E → n  E → E+n 

32 32 LR(1) Transitions Given an LR(1) item [ A →  B  a ] where B is a non-terminal, there are  -transitions to items [B →  b] for every production B →  and  for every token b in FIRST (  a )

33 33 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 I i  The parsing actions for state i are determined as follows:

34 34 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 I i  The parsing actions for state i are determined as follows:

35 35 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 I i  The parsing actions for state i are determined as follows:


Download ppt "Compiler Construction Sohail Aslam Lecture 26. 2 Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production."

Similar presentations


Ads by Google