The role of the parser and Error recovery strategies ppt in compiler design
Module 11
1.
2.
3. THE ROLE OF THE PARSER Lexical Analyzer Parser source program token get next token parse tree Symbol table
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20. Ambiguity – “dangling else ” stmt if expr then stmt | if expr then stmt else stmt | other stmts if E 1 then if E 2 then S 1 else S 2 stmt if expr then stmt else stmt E 1 if expr then stmt S 2 E 2 S 1 stmt if expr then stmt E 1 if expr then stmt else stmt E 2 S 1 S 2 1 2
21.
22.
23. Immediate Left-Recursion A A | where does not start with A eliminate immediate left recursion A A ’ A ’ A ’ | an equivalent grammar A A 1 | ... | A m | 1 | ... | n where 1 ... n do not start with A eliminate immediate left recursion A 1 A ’ | ... | n A ’ A ’ 1 A ’ | ... | m A ’ | an equivalent grammar In general,
24. Immediate Left-Recursion -- Example E E+T | T T T*F | F F id | (E) E T E ’ E ’ +T E ’ | T F T ’ T ’ *F T ’ | F id | (E) eliminate immediate left recursion
43. LL(1) Parser – Example1 Outputs: S aBa B bB B bB B Derivation(left-most): S aBa abBa abbBa abba S B a a B B b b parse tree
44. LL(1) Parser – Example2 E TE ’ E ’ +TE ’ | T FT ’ T ’ *FT ’ | F (E) | id E E+T | T T T*F | F F id | (E) Input : id +id F (E) F id F T ’ T ’ T ’ *FT ’ T ’ T ’ T FT ’ T FT ’ T E ’ E ’ E ’ +TE ’ E ’ E TE ’ E TE ’ E $ ) ( * + id
45.
46.
47.
48.
49.
50.
51.
52.
53. LL(1) PARSING TABLE F (E) F id F T ’ T ’ T ’ *FT ’ T ’ T ’ T FT ’ T FT ’ T E ’ E ’ E ’ +TE ’ E ’ E TE ’ E TE ’ E $ ) ( * + id
54.
55.
56.
57.
58.
59.
60. Panic-Mode Error Recovery in LL(1) Parsing example synch synch F (E) synch synch F id F T ’ T ’ T ’ *FT ’ T ’ T ’ synch synch T FT ’ synch T FT ’ T E ’ E ’ E ’ +TE ’ E ’ synch synch E TE ’ E TE ’ E $ ) ( * + id
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79. LR Parsing Algorithm stack input output S 0 X 1 S 1 . . X m-1 S m-1 X m S m $ a n ... a i ... a 1 Goto Table non-terminal s t each item is a a state number t e s Action Table terminals and $ s t four different a actions t e s LR Parsing Algorithm
80.
81.
82. (SLR) Parsing Tables for Expression Grammar Action Table Goto Table 1) E E+T 2) E T 3) T T*F 4) T F 5) F (E) 6) F id r3 r3 r3 r3 10 r5 r5 r5 r5 11 r1 r1 s7 r1 9 s11 s6 8 10 s4 s5 7 3 9 s4 s5 6 r6 r6 r6 r6 5 3 2 8 s4 s5 4 r4 r4 r4 r4 3 r2 r2 s7 r2 2 acc s6 1 3 2 1 s4 s5 0 F T E $ ) ( * + id state
83.
84.
85.
86.
87.
88.
89.
90. Transition Diagram (DFA) of Goto Function I 0 I 1 I 2 I 3 I 4 I 5 I 6 I 7 I 8 to I 2 to I 3 to I 4 I 9 to I 3 to I 4 to I 5 I 10 to I 4 to I 5 I 11 to I 6 to I 7 id ( F * E E + T T T ) F F F ( id id ( * ( id +
103. d S->.S,$ S->.CC,$ C->.cC,c/d C->.d,c/d I 0 C->d.,c/d I 4 S->C.C,$ C->.Cc,$ C->.d,$ I 2 C->c.C,$ C->.cC,$ C->.d,$ I 6 S->CC.,$ I 5 C->.cC.,$ I 9 C->d.,$ I 7 C->cC.,c/d I 8 C->c.C,c/d C->.cC,c/d C->.d,c/d I 3 S’->S.,$ I 1 S C C C c d C c d c c
104. CLR PARSING TABLE r2 9 r2 r2 8 r3 7 9 s7 s6 6 r1 5 r3 r3 4 8 s4 s3 3 5 s7 s6 2 acc 1 2 1 s4 s3 0 C S $ d c goto action STATE
105.
106.
107.
108.
109.
110. LALR PARSING TABLE r2 r2 r2 89 r1 5 r3 r3 r3 47 89 s47 s36 36 5 s47 s36 2 acc 1 2 1 s47 s36 0 C S $ d c goto action STATE