Compiler Design 3
Compiler Design 3
Syntax Analysis
Zakia Zinat Choudhury
Lecturer
Department of Computer Science & Engineering
University of Rajshahi
CONTENTS
01. The Role of the Parser
02. Limitations of Syntax Analyzers
03. Syntax Error Handling
04. Context-Free Grammars
05. Description of Four Tuples of CFG
06. Derivations
07. Parse Tree
08. Ambiguity
09. Recursion
10. Elimination of Left Recursion
11. Left Factoring
1
2020-8-15
Regular Context-Free
Expression Grammar
2
2020-8-15
Context-Free Grammars
V → Variable or Non-Terminals
T → Termminals or Σ
P → Productions or Rules
S → Star Symbol
3
2020-8-15
Derivations
A derivation is basically a sequence of production rules, in order to get the input string. During
parsing, two decisions are taken for some sentential form of input:
Left-most Derivation
If the sentential form of an input is scanned and replaced from left to right, it is called left-most
derivation. The sentential form derived by the left-most derivation is called the left-sentential form.
Right-most Derivation
If we scan and replace the input with production rules, from right to left, it is known as right-most
derivation. The sentential form derived from the right-most derivation is called the right-sentential
form.
8
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
4
2020-8-15
Parse Tree
A parse tree is a graphical representation of a derivation. It is convenient to see how strings a
re derived from the start symbol. The start symbol of the derivation becomes the root of
the parse tree. When a CFG is given, a parse tree can be found accrding to the grammar
with following properties:
1. The root is labeled by the start symbol.
2. Each leaf is labeled by a terminal or by ꜫ.
3. Each interior node is labeled by a nonterminal.
4. If A is the nonterminal labeling some interior node and X1, X2, . . . , Xn are the labels
of the children of that node from left to right, then there must be a production
A → X1 X2 ….. Xn. Here, X1, X2, . . . , Xn, each stand for a symbol that is either a
terminal or a nonterminal. As a special case, if A → ꜫ is a production, then a node
labeled A may have a single child labeled ꜫ.
Ambiguity
A grammar that produces more than one parse tree for some sentence is said to be
ambiguous.
Put another way, an ambiguous grammar is one that produces more than one leftmost
derivation or more than one rightmost derivation for the same sentence.
For Example:
The Grammar is:
E→E+E
E→E-E
E→id
For the string id + id – id, how many parse tree will be geanerated?
5
2020-8-15
Recursion
The process in which a function calls itself directly or indirectly is called recursion
and the corresponding function is called as recursive function.
6
2020-8-15
Left Factoring
14
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
7
2020-8-15
Left Factoring
15
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
16
Zakia Zinat Choudhury, Lecturer, Dept. of CSE, RU
8
2020-8-15
THANK YOU