Lect 4 IntroSyntaxAnalysis
Lect 4 IntroSyntaxAnalysis
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 2
Syntax Analysis
Different Formalisms
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 4
Example
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 5
Syntax Diagram
type id ;
varDclr:
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 6
Exercise
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 7
Context-Free Grammar
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 8
Exercise
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 9
Backus-Naur Form
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 10
Exercise
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 11
Note
;
0 2 4
type 1 id
,
id
3
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 12
Exercise
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 13
Note
Exercise
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 15
Note
Note
Specification to Recognizer
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 18
Context-Free Grammar
Language of a Grammar
✫ ✪
abuse the same notation.
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 22
Parse Tree
Parse Tree
Example
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 25
Example
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 26
Ambiguous Grammar
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 28
Example
+ T
E
F
T T *
id
F F
id ic
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 30
E E
E + E E *
E
E E id
id E * E
+
id ic
ic
id
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 31
Note
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 32
if-else Ambiguity
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 33
if-else Ambiguity
S S
if ( E ) S if ( E ) S
else S
if ( E ) S else if ( E ) S
S
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 34
if-else Modified
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 35
if-else Unambiguous
if ( E ) S
if ( E ) S else
S
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 36
Note
E → T +E |T −E |T
T → F ∗ T | F/T | F
F → id | ic | (E)
Is L(G) = L(G1 )? What difference does the
grammar make?
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 37
Problem
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 38
Problem
G and G1
G: G1:
E E
E − T T − E
− T F T − E
E F
F <ic,10> <ic,25> F T
T
<ic,2> F
F <ic,2>
<ic,10>
<ic,25>
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 40
G and G2
G:
E G2: E
E + T E T
*
F
T T * F
T
<ic, 10>
F F
T + F <ic, 10>
<ic, 5>
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 41
• G: (25 − 2) − 10 = 13
G1 : 25 − (2 − 10) = 33
• G: 5 + (2 ∗ 10) = 25
G2 : (5 + 2) ∗ 10 = 70
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 42
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 43
Useless Symbols
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 44
Useless Symbols
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 45
ε-Production
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 46
Example
S → 0A0 | 1B1 | BB
A → C
B → S|A
C → S|ε
All non-terminals are nullable.
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 47
Example
S → 0A0 | 1B1 | BB | 00 | 11 | B | ε
A → C
B → S|A
C → S
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 48
Unit Production
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 49
Normal Forms
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 50
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 51
Immediate Left-Recursion
✫ ✪
A will be a useless symbol.
Parse Tree
A
α
β
The yield is βα.
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 53
Rotation
A’
β
α
The new rules are A → βA′ and A′ → αA′ | ε.
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 54
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 55
Example
Original grammar:
E → E+T |T
T → T ∗F |F
F → (E) | ic
E → T E′ E ′ → +T E ′ | ε
T → FT′ T ′ → ∗F T ′ | ε
F → (E) | ic
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 56
T E’
E + T
E’
F F + T
T T *
ε
ic T’
ic F
F F
* T’
ic F
ic ic ε
ic
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 57
A → Aab | Ba | Cb | b
B → Aa | Db
C → Ab | Da
D → Bb | Ca
The grammar has indirect left-recursion:
A → Ba → Aaa etc.
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 58
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 59
Algorithm
for i = 1 to n
for j = 1 to i − 1
replace rule of the form Ai → Aj γ
by Ai → δ1 γ | · · · | δk γ, where
Aj → δ1 | · · · | δk are the current
Aj productions
remove immediate left-recursion of
Ai -productions.
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 60
Example
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 63
Example
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 64
Example
′ ′ ′
A → BaA | CbA | bA
A′ → abA′ | ε
B → DbB ′ | bA′ aB ′ | CbA′ aB ′
B ′ → aA′ aB ′ | ε
C → Ab | Da
··· ··· ···
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 65
Example
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 66
Example
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 67
Left Factoring
Example
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 69
Substitution
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 70
Example
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 71
Parsing
Top-Down Parsing
Top-Down Parsing
Top-Down Parsing
Top-Down Parsing
S
a S a a S a
b S b
Input: abcba
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 76
Non-Determinism
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 77
Note: Top-Down
Note: Top-Down
Example
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 80
Example
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 81
Bottom-Up Parsing
Bottom-Up Parsing
S S
1 2 3
a b c c b c b
Input: abcba
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 83
Note: Bottom-Up
Note: Bottom-Up
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas
✬ ✩
BS-MS & MS-PhD in Maths and Computing: SMCS (IACS) 85
Example
E → E + E | E ∗ E | ic
Given the input ic+ic*ic· · · , many reductions
are possible and in this case all of them will
finally lead to the start symbol. The previous
sentential form can be any one of the following
three, and there are many more:
E+ic*ic· · · , ic+E*ic· · · , ic+ic*E· · · etc. The
first one is the right sentential form.
✫ ✪
Lect IV: COM 5202: Compiler Construction Goutam Biswas