Upload
iffat-anjum
View
413
Download
0
Embed Size (px)
Citation preview
SYNTAX ANALYSISORPARSINGLecture 06
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
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
PREDICTIVE PARSING
4
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
TABLE DRIVEN PREDICTIVE PARSING
6
PARSE TABLE CONSTRUCTION
7
TABLE DRIVEN PREDICTIVE PARSING
8
TABLE DRIVEN PREDICTIVE PARSING
9
PREDICTIVE PARSING ALGORITHM
10
PREDICTIVE PARSING
11
PREDICTIVE PARSING
12
PREDICTIVE PARSING
13
PREDICTIVE PARSING
14
PREDICTIVE PARSING
15
PREDICTIVE PARSING
16
PREDICTIVE PARSING
17
PREDICTIVE PARSING
18
PREDICTIVE PARSING
19
PREDICTIVE PARSING
20
PREDICTIVE PARSING
21
PREDICTIVE PARSING
22
PREDICTIVE PARSING
23
PREDICTIVE PARSING
24
PREDICTIVE PARSING
25
PREDICTIVE PARSING
26
PREDICTIVE PARSING
27
PREDICTIVE PARSING
28
PREDICTIVE PARSING
29
PREDICTIVE PARSING
30
PREDICTIVE PARSING
31
PREDICTIVE PARSING
32
PREDICTIVE PARSING
33
PREDICTIVE PARSING
34
PREDICTIVE PARSING
35
PREDICTIVE PARSING
36
PREDICTIVE PARSING
37
PREDICTIVE PARSING
38
PREDICTIVE PARSING
39
PREDICTIVE PARSING
40
PREDICTIVE PARSING
41
PREDICTIVE PARSING
42
PREDICTIVE PARSING
43
PREDICTIVE PARSING
44
PREDICTIVE PARSING
45
PREDICTIVE PARSING
46
PREDICTIVE PARSING
47
PREDICTIVE PARSING
48
PREDICTIVE PARSING
49
PREDICTIVE PARSING
50
PREDICTIVE PARSING
51
PREDICTIVE PARSING
52
PREDICTIVE PARSING
53
PREDICTIVE PARSING
54
PREDICTIVE PARSING
55
PREDICTIVE PARSING
56
PREDICTIVE PARSING
57
RECONSTRUCTING THE PARSE TREE
58
RECONSTRUCTING THE PARSE TREE
59
RECONSTRUCTING THE PARSE TREE
60
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
61
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
62
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
63
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
64
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
65
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
66
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
67
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
68
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
69
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
70
EXAMPLE: THE “DANGLING ELSE” GRAMMAR
71
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
LL(1) GRAMMAR
73
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
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
ERROR RECOVERY
76
ERROR RECOVERY: SKIP INPUT SYMBOLS
77
ERROR RECOVERY: POP THE STACK
78
ERROR RECOVERY: PANIC MODE
79
ERROR RECOVERY - TABLE ENTRIES
80
QUESTIONS ?
81