Compiler Construction CS-4207: Instructor Name: Atif Ishaq
Compiler Construction CS-4207: Instructor Name: Atif Ishaq
CS-4207
Syntax Definition
Derivation of strings
Parsing
Ambiguous Grammar
2
Syntax Definition
What is characteristics to such inductive definition is that they start from some
constructs (e.g. expression), and then provide rules by which constructs can be
simply grammar) 3
Syntax Definition
Arrow is read as “can have the form”, such rule is called a production
Variables like expr and stmt represents sequence of terminals and are called
nonterminal
4
Context-Free Grammars
A → B1…….Bk | C1…….Ck
replacing a non terminal symbol by the right side of a production for that non
terminal. The token (terminal) strings that can be derived from the start symbol is
L→L+D|L–D|D
D → 0 | 1 | 2| 3 | 4 | 5 |6 | 7 | 8 | 9
6
The process inverse to derivation is recognition or parsing.
Parsing
Parsing is the problem of taking string of terminals and figuring out how to
derive it from the start symbol of the grammar, and if it can not be derived from
the start symbol of the grammar, then reporting syntax error within the string.
lexical analyzer into tokens, whose first components are the terminals processed
by the parser.
7
Parse Tree
A parse tree pictorially shows how the start symbol of a grammar derives a string
in the language
If nonterminal A has a production A → XYZ , then a parse tree may have interior
nodes labelled A with three children labeled X,Y, and Z from left to right
X Y Z
8
Parse Tree
properties
4. If A is the non terminal labeling some interior nodes X1,X2,…..Xn are the
lables of the children of that node from left to right then there must be a
production
A → X1X2…..Xn
9
Parse Tree
Given is parse tree of the derivation of the string 9 – 5 + 2 by the grammar for list
expression a = ( b = c )
letter → a | b | c | ………..| z 13
Associativity of Operators
14
Precedence of Operators
(9 + 5) * 2 or 9 + (5 * 2)
The associativity rules for + and * apply to occurrence of same operator, so they
do not resolve this ambiguity
17
Lecture Outcome
18
19