Module 2
Module 2
COMPILER CONSTRUCTION
Module II
Syntax Analyzer (Parsing)
Dr A. K. Jayswal
ASET(CSE)
MTech(CSE)-JNU
PhD(CSE)-JNU
BSc(CS), MSc(CS)-BHU
GATE(CS), UGC-NET(CS)
1
Module II:
2
Amity School of Engineering and Technology
Recommended Reading
Textbooks:
• Alfred V. Aho, Monica S. Lam, Ravi Sethi, Jeffrey D. Ullman, “Compilers:
Principles Techniques and Tool”, Second Edition, Pearson Publication, 2007.
• A.A Putambekar, “Compiler Construction”, Technical Publications, 2009.
Reference Book:
• Des Watson, A Practical Approach to Compiler Construction, First
Edition, Springer, 2017
3
Amity School of Engineering and Technology
OBJECTIVES
After completing this section, you will be able to
1.1 Understand the Role of the parse
1.4 Analyze and Implement various Top-Down parsing and Bottom-up Parsing
4
Amity School of Engineering and Technology
Module Assessment
• Quiz (Conceptual and Numerical Based)-2 Marks
• Assignment (module-2)-2 marks
5
Amity School of Engineering and Technology
6
Amity School of Engineering and Technology
PHASES
OF
COMPILE
R
7
Amity School of Engineering and Technology
PHASES
OF
COMPILE
R
8
Amity School of Engineering and Technology
Parser for any grammar is program that takes as input string w (obtain set
PARSER of strings of tokens from the lexical analyzer) and produces as output
either a parse tree for w , if w is a valid sentences of grammar or error
message indicating that w is not a valid sentences of given grammar.
The goal of the parser is to determine the syntactic validity of a source
string is valid, a tree is built for use by the subsequent phases of the
computer. The tree reflects the sequence of derivations or reduction used
during the parser. Hence, it is called parse tree. If string is invalid, the
parse has to issue diagnostic message identifying the nature and cause of
the errors in string. Every elementary subtree in the parse tree
corresponds to a production of the grammar.
9
Amity School of Engineering and Technology
10
Amity School of Engineering and Technology
Role of
the Parser
(syntax
Analyzer)
11
Amity School of Engineering and Technology
Types of Parser
13
Amity School of Engineering and Technology
Types of
Parser
14
Amity School of Engineering and Technology
15
Amity School of Engineering and Technology
Hierarch
y of
Parsing
methods
16
Amity School of Engineering and Technology
1. Ambiguity
2. Backtracking
3. Left Recursion
4. Left Factoring
5. Sometimes TDP may reject some valid sentences
17
Amity School of Engineering and Technology
1. Ambiguity
2. Backtracking
3. Left Recursion
4. Left Factoring
5. Sometimes TDP may reject some valid sentences
2. G1 : S → aSbS | bSaS | λ
G2: s → aAB A → bBb B → A | λ
G3: s → SS | aSb | bSa | λ
18
Ambiguous Grammar
SA
SB
S AB
A aA
B bB
Ae
Be
Rules:
1. Check the precedence of the operators involved.
2. Different precedence operators are treated differently for removing ambiguity.
3. First remove the ambiguity for the minimum precedence operator and so on…
21
Amity School of Engineering and Technology
If the operator is left associative then change the right most symbol
Eg.- E->E*E|id
E->E*E’|E’
E’-> id
If the operator is right associative then change the Left most symbol
Eg.- E->E⬆E|id
E->E’⬆E|E’
E’-> id 22
Amity School of Engineering and Technology
2. Backtracking:
Backtracking is a technique in which for expansion of non-terminal symbol, we choose
alternative and if some mismatch occurs then we try another alternative if any.
If for a non-terminal, there are multiple production rules beginning with the same input symbol
then to get the correct derivation, we need to try all these alternatives.
Secondly, in backtracking, we need to move some levels upward in order to check the
possibilities. This increases lot of overhead in implementation of parsing.
Hence, it becomes necessary to eliminate the backtracking by modifying the grammar.
Example: ScAd
Aab/a
Consider w=cad
23
Amity School of Engineering and Technology
3. Left Recursion:
A Grammar G is said to be left recursive, if it has a derivation of the form
It can cause a top-down parser to go into an infinite loop. To use TDP, we have to eliminate left recursion.
24
Amity School of Engineering and Technology
25
Amity School of Engineering and Technology
26
Amity School of Engineering and Technology
27
Amity School of Engineering and Technology
28
Amity School of Engineering and Technology
29
Amity School of Engineering and Technology
30
Amity School of Engineering and Technology
Left Recursion-Example1
31
Amity School of Engineering and Technology
32
Amity School of Engineering and Technology
Left
Factoring
A TDP without backtracking (Predictive parser) insists that Grammar must be left factored.
33
Amity School of Engineering and Technology
Left
Factoring
34
Amity School of Engineering and Technology
Left Factoring
Example:
S->cAd
A->ab/a
After LF, it can be re-write
as:
S->cAd
A->aA’
A’->b/Null
35
Amity School of Engineering and Technology
36
Amity School of Engineering and Technology
37
Amity School of Engineering and Technology
38
Amity School of Engineering and Technology
39
Amity School of Engineering and Technology
Top-Down Parser
Top-down parser build parse tree from the top (root) to the bottom (leaves).
A left-most derivation is applied at each derivation step
Top-down parser may require (need) Backtracking, that is it may require repeated scan
over the input. However, backtracking is rarely needed to parse programming language
constructs. Backtracking is an extra overhead so generally backtracking parser are not
used frequently.
Example of TDP :
- Recursive descent parsing,
- Predictive parsing and
- LL(1) parsing.
Note : TDP does not allow ambiguous grammar, No left recursive grammar, no non-deterministic
grammar (can be removed using left factoring)
40
Amity School of Engineering and Technology
Top-Down Parser
41
Amity School of Engineering and Technology
42
Amity School of Engineering and Technology
43
Amity School of Engineering and Technology
E();
input++;
Main()
{
E();
If (input==‘$’)
Write production for each nonterminals. Printf(“ parsing successful);
}
44
Amity School of Engineering and Technology
Top-Down Parsing
Top-Down parsing does not support ambiguous , left recursive and left factoring.
45
Amity School of Engineering and Technology
Top-Down Parsing
Top Down Parsing involves construction of parse tree from root to leaves using left most
derivation.
46
Amity School of Engineering and Technology
47
Amity School of Engineering and Technology
Recursive Descent Parsing: It is top down method of syntax analysis which uses collection of recursive
procedures to process the input.
Predictive Parsing:
It is a special form of RDP, in which the look-ahead symbol unambiguously determines the procedure
selected for each non-terminal.
The look-ahead symbol guides the selection of the production to be used. Writing a grammar after
eliminating left-recursion and left-factoring guaranteed a RDP with no Backtracking.
It is also called as non recursive predictive parsing because it implements the recursive descent parsing by
maintaining a stack of activation records.
48
Amity School of Engineering and Technology
Predictive parsing:
49
Amity School of Engineering and Technology
Finally, Parse the given string w using Algorithm and parsing table created in step4.
50
FIRST and FOLLOW set:
FIRST and FOLLOW Set:
Note: FIRST and Follow set are useful in creating parse table in Top-Down and Bottom-Up parsing.
Amity School of Engineering and Technology
Calculating First(Y2Y3)
55
Amity School of Engineering and Technology
Example:
SACB/CbB/Ba
Ada/BC
Bg/∈
Ch/∈
Compute FIRST
set.
56
FOLLOW(A):
[where A is any nonterminal of G]
Follow(A) is a function which gives a set of all terminals that can appear immediately to the right of
symbol A. That is [to calculate follow we always focus on RHS of “arrow” nonterminal].
Example1:
SaSbS/bSaS/∈
58
Amity School of Engineering and Technology
Example2:
SABC
ADEF
B/b
C/c
D /d
E /e
F /f
Compute FIRST and
FOLLOW set.
59
Amity School of Engineering and Technology
Example3:
60
Amity School of Engineering and Technology
Note: if grammar is Left recursive then first remove it then calculate FIRST and FOLLOW SET,
before creating a TDP table (i.e. predictive parsing)
67
Amity School of Engineering and Technology
We have-
•The given grammar is left recursive.
•So, we first remove left recursion from the given grammar.
78
Amity School of Engineering and Technology
79
Amity School of Engineering and Technology
80
Amity School of Engineering and Technology
81
Amity School of Engineering and Technology
82
Amity School of Engineering and Technology
Finally, Parse the given string w using Algorithm and parsing table created in step4.
83
Amity School of Engineering and Technology
Predictive
Parser
84
Amity School of Engineering and Technology
Predictive
Parser
85
Amity School of Engineering and Technology
Parsing Table M:
It is a 2D table
where A is a
Nonterminal and a
is a terminal
Predictive symbol or .
Parser
86
Amity School of Engineering and Technology
87
Amity School of Engineering and Technology
88
Amity School of Engineering and Technology
Predictive Parser
89
Amity School of Engineering and Technology
Predictive Parser
90
Amity School of Engineering and Technology
Example:
Table-driven
Predictive
Parser
91
Amity School of Engineering and Technology
92
Amity School of Engineering and Technology
94
Amity School of Engineering and Technology
95
Amity School of Engineering and Technology
97
Amity School of Engineering and Technology
Moves made
by Predictive
Parser for
Input:
98
Example2:
Amity School of Engineering and Technology
Using a parse table, check whether the string belongs to grammar or not.
Q.2 Construct a predictive Parsing table for the following grammar. Also, check whether the grammar
is LL(1) or not?
LL(1) Grammar
Ambiguous Grammar or Left Recursive Grammar cannot be LL(1) grammar.
103
Amity School of Engineering and Technology
Note point
104
Amity School of Engineering and Technology
Q2.
Q.3
Q.5
Q.4. SiCtSS’/a
S’eS/
Cb
109
Amity School of Engineering and Technology
Types of Parsing
110
Amity School of Engineering and Technology
111
Amity School of Engineering and Technology
Bottom-Up Parsing
112
Amity School of Engineering and Technology
Bottom-Up Parsing
113
Amity School of Engineering and Technology
Bottom-Up Parsing
114
Amity School of Engineering and Technology
Bottom-Up Parsing
115
Amity School of Engineering and Technology
Bottom-Up Parsing
116
Amity School of Engineering and Technology
Bottom-Up Parsing
117
Amity School of Engineering and Technology
Bottom-Up Parsing
118
Amity School of Engineering and Technology
Practice Questions
Ques1: Consider the grammar
S->TL;
T->int|float
L->L,id|id
Parse the input string int id, id; using shift reduce parser.
119
Amity School of Engineering and Technology
Practice Questions
Ques3: Consider the grammar
E->E+T
T-> id|id[]|id[X]
X-> E, E|E
Find FIRST and FOLLOW of all the non-terminals.
Ques4: Consider the grammar
S->(A)|0
A->SB
B->,SB| e
Is the above grammar LL(1)? Justify your answer.
Ques5: Consider the grammar
E->+EE| *EE |num
Construct LL(1) parsing table for it.
120
Amity School of Engineering and Technology
Practice Questions
Ques 7: Construct the predictive parser for the following grammar
S->a|^|(T)
T->T,S| S
Show the behavior of the parser in the sentences:
i) (a,(a,a))
ii) (((a,a),^,(a),a)
Ques 8: Construct the predictive parser for the following grammar
S->(L)|a
L-> L,S| S
Parse the sentence (a,a) using predictive parsing for the above grammar.
121
Amity School of Engineering and Technology
122
Amity School of Engineering and Technology
123
Amity School of Engineering and Technology
124
Amity School of Engineering and Technology
125
Amity School of Engineering and Technology
126
Amity School of Engineering and Technology
127
Amity School of Engineering and Technology
Note: By using LEADING and TRAILING set, we can also construct a Precedence relation Table.
128
Amity School of Engineering and Technology
Note: By using LEADING and TRAILING set, we can also construct a Precedence relation Table.
129
Amity School of Engineering and Technology
Algorithm for constructing Operator Precedence Relation table (OPRT) using Leading and
Trailing Set
130
Amity School of Engineering and Technology
131
Amity School of Engineering and Technology
132
Amity School of Engineering and Technology
In other words
133
Amity School of Engineering and Technology
134
Amity School of Engineering and Technology
Consider a string w=
135
Amity School of Engineering and Technology
136
Amity School of Engineering and Technology
137
Amity School of Engineering and Technology
138
Amity School of Engineering and Technology
139
Amity School of Engineering and Technology
1. Table size is reduced from to O(n) since there are only 2n entries in operator
function table.
2. But the Error capability is less than the precedence table.
*************************************************************************
140
Amity School of Engineering and Technology
142