lecture 06 syntax analysis 3
TRANSCRIPT
![Page 1: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/1.jpg)
SYNTAX ANALYSISORPARSINGLecture 06
![Page 2: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/2.jpg)
FOLLOW EXAMPLE S a S e | B B b B C f | C C c C g | d |
FIRST(C) = FIRST(B) = FIRST(S) =
FOLLOW(C) = FOLLOW(B) = FOLLOW(S) = {$}
Assume the first non-terminal is the start symbol1. If A is start symbol, put $ in FOLLOW(A)2. Productions of the form B A ,
Add FIRST() – {} to FOLLOW(A)3. Productions of the form B A or B A where *
Add FOLLOW(B) to FOLLOW(A)
2
![Page 3: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/3.jpg)
FOLLOW EXAMPLE S a S e | B B b B C f | C C c C g | d |
FIRST(C) = {c,d,} FIRST(B) = {b,c,d,} FIRST(S) = {a,b,c,d,}
FOLLOW(C) =
FOLLOW(B) =
FOLLOW(S) = { }$, e
{c,d} FOLLOW(S)
= {c,d,e,$}
{f,g} FOLLOW(B)
= {c,d,e,f,g,$}
3
![Page 4: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/4.jpg)
PREDICTIVE PARSING
4
![Page 5: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/5.jpg)
PREDICTIVE PARSING LL(1) Grammars
Can do predictive parsing Can select the right rule Looking at only the next 1 input symbol
First L : Left to Right Scanning Second L: Leftmost derivation 1 : one input symbol look-ahead for predictive decision
LL(k) Grammars Can do predictive parsing Can select the right rule Looking at only the next k input symbols
Techniques to modify the grammar: Left Factoring Removal of Left Recursion
LL(k) Language Can be described with an LL(k) grammar 5
![Page 6: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/6.jpg)
TABLE DRIVEN PREDICTIVE PARSING
6
![Page 7: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/7.jpg)
PARSE TABLE CONSTRUCTION
7
![Page 8: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/8.jpg)
TABLE DRIVEN PREDICTIVE PARSING
8
![Page 9: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/9.jpg)
TABLE DRIVEN PREDICTIVE PARSING
9
![Page 10: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/10.jpg)
PREDICTIVE PARSING ALGORITHM
10
![Page 11: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/11.jpg)
PREDICTIVE PARSING
11
![Page 12: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/12.jpg)
PREDICTIVE PARSING
12
![Page 13: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/13.jpg)
PREDICTIVE PARSING
13
![Page 14: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/14.jpg)
PREDICTIVE PARSING
14
![Page 15: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/15.jpg)
PREDICTIVE PARSING
15
![Page 16: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/16.jpg)
PREDICTIVE PARSING
16
![Page 17: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/17.jpg)
PREDICTIVE PARSING
17
![Page 18: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/18.jpg)
PREDICTIVE PARSING
18
![Page 19: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/19.jpg)
PREDICTIVE PARSING
19
![Page 20: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/20.jpg)
PREDICTIVE PARSING
20
![Page 21: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/21.jpg)
PREDICTIVE PARSING
21
![Page 22: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/22.jpg)
PREDICTIVE PARSING
22
![Page 23: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/23.jpg)
PREDICTIVE PARSING
23
![Page 24: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/24.jpg)
PREDICTIVE PARSING
24
![Page 25: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/25.jpg)
PREDICTIVE PARSING
25
![Page 26: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/26.jpg)
PREDICTIVE PARSING
26
![Page 27: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/27.jpg)
PREDICTIVE PARSING
27
![Page 28: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/28.jpg)
PREDICTIVE PARSING
28
![Page 29: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/29.jpg)
PREDICTIVE PARSING
29
![Page 30: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/30.jpg)
PREDICTIVE PARSING
30
![Page 31: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/31.jpg)
PREDICTIVE PARSING
31
![Page 32: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/32.jpg)
PREDICTIVE PARSING
32
![Page 33: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/33.jpg)
PREDICTIVE PARSING
33
![Page 34: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/34.jpg)
PREDICTIVE PARSING
34
![Page 35: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/35.jpg)
PREDICTIVE PARSING
35
![Page 36: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/36.jpg)
PREDICTIVE PARSING
36
![Page 37: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/37.jpg)
PREDICTIVE PARSING
37
![Page 38: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/38.jpg)
PREDICTIVE PARSING
38
![Page 39: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/39.jpg)
PREDICTIVE PARSING
39
![Page 40: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/40.jpg)
PREDICTIVE PARSING
40
![Page 41: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/41.jpg)
PREDICTIVE PARSING
41
![Page 42: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/42.jpg)
PREDICTIVE PARSING
42
![Page 43: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/43.jpg)
PREDICTIVE PARSING
43
![Page 44: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/44.jpg)
PREDICTIVE PARSING
44
![Page 45: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/45.jpg)
PREDICTIVE PARSING
45
![Page 46: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/46.jpg)
PREDICTIVE PARSING
46
![Page 47: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/47.jpg)
PREDICTIVE PARSING
47
![Page 48: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/48.jpg)
PREDICTIVE PARSING
48
![Page 49: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/49.jpg)
PREDICTIVE PARSING
49
![Page 50: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/50.jpg)
PREDICTIVE PARSING
50
![Page 51: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/51.jpg)
PREDICTIVE PARSING
51
![Page 52: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/52.jpg)
PREDICTIVE PARSING
52
![Page 53: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/53.jpg)
PREDICTIVE PARSING
53
![Page 54: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/54.jpg)
PREDICTIVE PARSING
54
![Page 55: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/55.jpg)
PREDICTIVE PARSING
55
![Page 56: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/56.jpg)
PREDICTIVE PARSING
56
![Page 57: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/57.jpg)
PREDICTIVE PARSING
57
![Page 58: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/58.jpg)
RECONSTRUCTING THE PARSE TREE
58
![Page 59: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/59.jpg)
RECONSTRUCTING THE PARSE TREE
59
![Page 60: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/60.jpg)
RECONSTRUCTING THE PARSE TREE
60
![Page 61: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/61.jpg)
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
61
![Page 62: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/62.jpg)
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
62
![Page 63: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/63.jpg)
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
63
![Page 64: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/64.jpg)
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
64
![Page 65: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/65.jpg)
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
65
![Page 66: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/66.jpg)
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
66
![Page 67: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/67.jpg)
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
67
![Page 68: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/68.jpg)
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
68
![Page 69: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/69.jpg)
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
69
![Page 70: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/70.jpg)
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
70
![Page 71: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/71.jpg)
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
71
![Page 72: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/72.jpg)
LL(1) GRAMMAR LL(1) grammars
Are never ambiguous. Will never have left recursion.
Furthermore... If we are looking for an “A” and the next symbol
is “b”, Then only one production must be possible
Although elimination of left recursion and left factoring is easy. Some grammar will never be a LL(1) grammar.
LL(1) Grammar
72
![Page 73: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/73.jpg)
LL(1) GRAMMAR
73
![Page 74: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/74.jpg)
PROPERTIES OF LL(1) GRAMMAR A grammar G is LL(1) if an only if whenever
A are two distinct productions of G the following conditions hold:1. For no terminal a do both and derive
strings beginning with a.2. At most one of and can derive the empty
string.3. If then * then does not derive any
string beginning with a terminal in FOLLOW(A).
74
![Page 75: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/75.jpg)
ERROR RECOVERY
a + b $
Y
X
$
Z
Input
Predictive Parsing Program
Stack Output
Parsing Table M[A,a]
When Do Errors Occur? Recall Predictive Parser Function:
1. If X is a terminal and it doesn’t match input.
2. If M[X, Input] is empty – No allowable actions 75
![Page 76: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/76.jpg)
ERROR RECOVERY
76
![Page 77: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/77.jpg)
ERROR RECOVERY: SKIP INPUT SYMBOLS
77
![Page 78: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/78.jpg)
ERROR RECOVERY: POP THE STACK
78
![Page 79: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/79.jpg)
ERROR RECOVERY: PANIC MODE
79
![Page 80: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/80.jpg)
ERROR RECOVERY - TABLE ENTRIES
80
![Page 81: Lecture 06 syntax analysis 3](https://reader036.vdocument.in/reader036/viewer/2022062503/589a962d1a28abae648b61f9/html5/thumbnails/81.jpg)
QUESTIONS ?
81