Download as pdf or txt
Download as pdf or txt
You are on page 1of 89

SYNTAX ANALYSIS

OR
PARSING
Lecture 05
FOLLOW SETS
 FOLLOW(A) is the set of terminals (including
end marker of input - $) that may follow non-
terminal A in some sentential form.
 FOLLOW(A) = {c | S ⇒+ …Ac…} ∪ {$} if S ⇒+
…A
 For example, consider L ⇒+ (())(L)L
Both ‘)’ and end of file can follow L
 NOTE: ε is never in FOLLOW sets

2
COMPUTING FOLLOW(A)
1. 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)

3
EXAMPLE
 FOLLOW(E) = {$}
 E  T E′  FOLLOW(E′) =
 E′  + T E ′ | ε  FOLLOW(T) =
 T  F T′  FOLLOW(T′) =
 T′  * F T ′ | ε  FOLLOW(F) =
 F  ( E ) | id

 FIRST(E) = {(, id} Using rule #1


 FIRST(E′) = {+, ε}
 FIRST(T) = {(, id} 1. If A is start symbol, put $ in FOLLOW(A)

 FIRST(T′) = {*, ε}
 FIRST(F) = {(, id}} 4
Assume the first non-terminal is the start symbol
EXAMPLE
 E  T E′  FOLLOW(E) = {$, )}
 E′  + T E ′ | ε  FOLLOW(E′) =
 T  F T′  FOLLOW(T) = {+}
 T′  * F T ′ | ε  FOLLOW(T′) =
 F  ( E ) | id  FOLLOW(F) = {*}

 FIRST(E) = {(, id}


 FIRST(E′) = {+, ε}
 FIRST(T) = {(, id}
 FIRST(T′) = {*, ε}
Using rule #2
 FIRST(F) = {(, id}}
2. Productions of the form B  α A β, 5
Add FIRST(β) – {ε} to FOLLOW(A)
EXAMPLE
 E  T E′  FOLLOW(E) = {$, )}
 E′  + T E ′ | ε  FOLLOW(E′) = FOLLOW(E)
= {$, )}
 T  F T′
 FOLLOW(T) = {+} ∪ FOLLOW(E′)
 T′  * F T ′ | ε
= {+, $, )}
 F  ( E ) | id
 FOLLOW(T′) = FOLLOW(T)
= {+, $, )}
 FIRST(E) = {(, id}
 FOLLOW(F) = {*} ∪ FOLLOW(T′)
 FIRST(E′) = {+, ε} = {*, +, $, )}
 FIRST(T) = {(, id}
 FIRST(T′) = {*, ε} Using rule #3
 FIRST(F) = {(, id}}
3. Productions of the form B  α A or
6
B  α A β where β ⇒* ε
Add FOLLOW(B) to FOLLOW(A)
EXAMPLE
 S  ( A) | ε
 ATE
 E&TE|ε
 T(A)|a|b|c

 FIRST(T) =
 FIRST(E) =
 FIRST(A) =
 FIRST(S) =

 FOLLOW(S) =
 FOLLOW(A) =

 FOLLOW(E) =

 FOLLOW(T) =
7
EXAMPLE
 S  ( A) | ε
 ATE
 E&TE|ε
 T(A)|a|b|c

 FIRST(T) = {(,a,b,c}
 FIRST(E) = {&, ε }
 FIRST(A) = {(,a,b,c}
 FIRST(S) = {(, ε}

 FOLLOW(S) = {$}
 FOLLOW(A) = { ) }
 FOLLOW(E) = FOLLOW(A) = { ) } 8

 FOLLOW(T) = FIRST(E) ∪ FOLLOW(E) = {&, )}


EXAMPLE
 SaSe|B  FOLLOW(C) =
 BbBCf|C
 CcCg|d| ε  FOLLOW(B) =

 FIRST(C) =  FOLLOW(S) = {$}


 FIRST(B) =
 FIRST(S) =
Assume the first non-terminal is the start symbol
1. 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
9
B  α A β where β ⇒* ε
Add FOLLOW(B) to FOLLOW(A)
EXAMPLE
 SaSe|B  FOLLOW(C) =
 BbBCf|C {f,g} ∪ FOLLOW(B)
 CcCg|d| ε = {c,d,e,f,g,$}

 FIRST(C) = {c,d,ε}  FOLLOW(B) =


 FIRST(B) = {b,c,d,ε}
{c,d,f} ∪ FOLLOW(S)
= {c,d,e,$ ,f}
 FIRST(S) = {a,b,c,d,ε}

 FOLLOW(S) = {$, e }

10
FOLLOW EXAMPLE
 SaSe|B  FOLLOW(C) =
 BbBCf|C
 CcCg|d| ε  FOLLOW(B) =

 FIRST(C) =  FOLLOW(S) = {$}


 FIRST(B) =
 FIRST(S) =
Assume the first non-terminal is the start symbol
1. 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
11
B  α A β where β ⇒* ε
Add FOLLOW(B) to FOLLOW(A)
FOLLOW EXAMPLE
 SaSe|B  FOLLOW(C) =
 BbBCf|C {f,g} ∪ FOLLOW(B)
 CcCg|d| ε = {c,d,e,f,g,$}

 FIRST(C) = {c,d,ε}  FOLLOW(B) =


{c,d} ∪ FOLLOW(S)
 FIRST(B) = {b,c,d,ε}
= {c,d,e,$}
 FIRST(S) = {a,b,c,d,ε}

 FOLLOW(S) = {$, e }

12
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
13

TABLE DRIVEN PREDICTIVE PARSING

14
PARSE TABLE CONSTRUCTION

15
TABLE DRIVEN PREDICTIVE PARSING

16
TABLE DRIVEN PREDICTIVE PARSING

17
PREDICTIVE PARSING ALGORITHM

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
PREDICTIVE PARSING

58
PREDICTIVE PARSING

59
PREDICTIVE PARSING

60
PREDICTIVE PARSING

61
PREDICTIVE PARSING

62
PREDICTIVE PARSING

63
PREDICTIVE PARSING

64
PREDICTIVE PARSING

65
RECONSTRUCTING THE PARSE TREE

66
RECONSTRUCTING THE PARSE TREE

67
RECONSTRUCTING THE PARSE TREE

68
EXAMPLE: THE “DANGLING ELSE” GRAMMAR

69
EXAMPLE: THE “DANGLING ELSE” GRAMMAR

70
EXAMPLE: THE “DANGLING ELSE” GRAMMAR

71
EXAMPLE: THE “DANGLING ELSE” GRAMMAR

72
EXAMPLE: THE “DANGLING ELSE” GRAMMAR

73
EXAMPLE: THE “DANGLING ELSE” GRAMMAR

74
EXAMPLE: THE “DANGLING ELSE” GRAMMAR

75
EXAMPLE: THE “DANGLING ELSE” GRAMMAR

76
EXAMPLE: THE “DANGLING ELSE” GRAMMAR

77
EXAMPLE: THE “DANGLING ELSE” GRAMMAR

78
EXAMPLE: THE “DANGLING ELSE” GRAMMAR

79
LL(1) GRAMMAR 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. 80
LL(1) GRAMMAR

81
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).

82
ERROR RECOVERY
When Do Errors Occur? Recall Predictive Parser Function:

a + b $ Input

Stack X Predictive Parsing Output


Program
Y
Z
Parsing Table
$ M[A,a]

1. If X is a terminal and it doesn’t match input.


2. If M[X, Input] is empty – No allowable actions 83
ERROR RECOVERY

84
ERROR RECOVERY: SKIP INPUT SYMBOLS

85
ERROR RECOVERY: POP THE STACK

86
ERROR RECOVERY: PANIC MODE

87
ERROR RECOVERY - TABLE ENTRIES

88
QUESTIONS ?

89

You might also like