Lecture_11
Lecture_11
Front-End: Parser
source tokens IR
scanner parser
code
errors
errors
errors
Guides context-sensitive
(“semantic”) analysis (type
checking)
4
Front-End: Parser
source tokens IR
scanner parser
code
errors
5
Syntactic Analysis
Natural language analogy:
consider the sentence
He wrote the program
6
Syntactic Analysis
7
Syntactic Analysis
8
Syntactic Analysis
Natural language analogy
He wrote the program
noun verb article noun
subject predicate object
sentence
9
Syntactic Analysis
Programming language
if ( b <= 0 ) a = b
bool expr assignment
if-statement
10
Syntactic Analysis
syntax errors
} not a keyword
13
Semantic Analysis
Grammatically correct
sentence
14
Semantic Analysis
semantically (meaning) wrong!
sentence
15
Semantic Analysis
int* foo(int i, int j)
{
for(k=0; i < j; j++ )
if( i < j-2 )undeclared var
sum = sum+i
return type
return sum;
} mismatch
16
Role of the Parser
Not all sequences of tokens
are program.
Parser must distinguish
between valid and invalid
sequences of tokens.
17
Role of the Parser
Not all sequences of tokens
are program.
Parser must distinguish
between valid and invalid
sequences of tokens.
18
Role of the Parser
What we need
An expressive way to
describe the syntax
An acceptor mechanism that
determines if input token
stream satisfies the syntax
19
Role of the Parser
What we need
An expressive way to
describe the syntax
An acceptor mechanism that
determines if input token
stream satisfies the syntax
20
Role of the Parser
What we need
An expressive way to
describe the syntax
An acceptor mechanism that
determines if input token
stream satisfies the syntax
21
Study of Parsing
Parsing is the process of
discovering a derivation for
some sentence
22
Study of Parsing
Mathematical model of
syntax – a grammar G.
Algortihm for testing
membership in L(G).
23
Study of Parsing
Mathematical model of
syntax – a grammar G.
Algortihm for testing
membership in L(G).
24
Context Free Grammars
A CFG is a four tuple
G=(S,N,T,P)
S is the start symbol
N is a set of non-terminals
T is a set of terminals
P is a set of productions
25
Why Not Regular
Expressions?
Reason:
regular languages do not
have enough power to
express syntax of
programming languages.
26
Limitations of Regular
Languages
Finite automaton can’t
remember number of
times it has visited a
particular state
27
Example of CFG
Context-free syntax is
specified with a CFG
28
Example of CFG
Example
30
Example of CFG
SheepNoise → SheepNoise baa
| baa
37
Backus-Naur Form (BNF)
The notation was developed
by John Backus and
adapted by Peter Naur for
the Algol60 report.
Thus the term Backus-Naur
Form (BNF)
38
Backus-Naur Form (BNF)
The notation was developed
by John Backus and
adapted by Peter Naur for
the Algol60 report.
Thus the term Backus-Naur
Form (BNF)
39
Derivation:
Let us use the expression
grammar to derive the
sentence
x–2*y
40
Derivation: x – 2 * y
Rule Sentential Form
- expr
1 expr op expr
2 <id,x> op expr
5 <id,x> – expr
1 <id,x> – expr op
expr
41
Derivation: x – 2 * y
Rule Sentential Form
2 <id,x> – <num,2> op
expr
6 <id,x> – <num,2>
expr
3 <id,x> – <num,2>
<id,y>
42
Derivation
Such a process of rewrites
is called a derivation.
Process or discovering a
derivations is called parsing
43
Derivation
Such a process of rewrites
is called a derivation.
Process or discovering a
derivations is called parsing
44
Derivation
expr →* id – num * id
45
Derivations
At each step, we choose a
non-terminal to replace
Different choices can lead to
different derivations.
46
Derivations
At each step, we choose a
non-terminal to replace
Different choices can lead to
different derivations.
47
Derivations
Two derivations are of
interest
1. Leftmost derivation
2. Rightmost derivation
48
Derivations
Leftmost derivation:
replace leftmost non-
terminal (NT) at each step
Rightmost derivation:
replace rightmost NT at
each step
49
Derivations
Leftmost derivation:
replace leftmost non-
terminal (NT) at each step
Rightmost derivation:
replace rightmost NT at
each step
50
Derivations
The example on the
preceding slides was
leftmost derivation
There is also a rightmost
derivation
51
Rightmost Derivation
Rule Sentential Form
- expr
1 expr op expr
3 expr op <id,x>
6 expr <id,x>
1 expr op expr
<id,x>
52
Derivation: x – 2 * y
Rule Sentential Form
2 expr op <num,2>
<id,x>
5 expr – <num,2>
<id,x>
3 <id,x> – <num,2>
<id,y> 53
Derivations
In both cases we have
expr →* id – num id
54
Derivations
The two derivations produce
different parse trees.
The parse trees imply
different evaluation orders!
55
Derivations
The two derivations produce
different parse trees.
The parse trees imply
different evaluation orders!
56