Professional Documents
Culture Documents
Parser
Parser
source tokens IR
scanner parser
code
errors
errors
errors
Guides context-sensitive
(“semantic”) analysis (type
checking)
3
Front-End: Parser
source tokens IR
scanner parser
code
errors
4
Syntactic Analysis
Natural language analogy:
consider the sentence
He wrote the program
5
Syntactic Analysis
6
Syntactic Analysis
7
Syntactic Analysis
Natural language analogy
He wrote the program
noun verb article noun
subject predicate object
sentence
8
Syntactic Analysis
Programming language
if ( b <= 0 ) a = b
bool expr assignment
if-statement
9
Syntactic Analysis
syntax errors
sentence
13
Semantic Analysis
semantically (meaning) wrong!
sentence
14
Semantic Analysis
int* foo(int i, int j)
{
for(k=0; i < j; j++ )
if( i < j-2 )undeclared var
sum = sum+i
return sum; return type
mismatch
}
15
Role of the Parser
Not all sequences of tokens
are program.
Parser must distinguish
between valid and invalid
sequences of tokens.
16
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
17
Study of Parsing
Parsing is the process of
discovering a derivation for
some sentence
18
Study of Parsing
Mathematical model of
syntax – a grammar G.
Algortihm for testing
membership in L(G).
19
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
20
Why Not Regular
Expressions?
Reason:
regular languages do not
have enough power to
express syntax of
programming languages.
21
Limitations of Regular
Languages
Finite automaton can’t
remember number of
times it has visited a
particular state
22
Example of CFG
Context-free syntax is
specified with a CFG
23
Example of CFG
Example
25
Example of CFG
SheepNoise → SheepNoise baa
| baa
31
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)
32
Derivation:
Let us use the expression
grammar to derive the
sentence
x–2*y
33
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
34
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>
35
Derivation
Such a process of rewrites
is called a derivation.
Process or discovering a
derivations is called parsing
36
Derivation
expr →* id – num * id
37
Derivations
At each step, we choose a
non-terminal to replace
Different choices can lead to
different derivations.
38
Derivations
Two derivations are of
interest
1. Leftmost derivation
2. Rightmost derivation
39
Derivations
Leftmost derivation:
replace leftmost non-
terminal (NT) at each step
Rightmost derivation:
replace rightmost NT at
each step
40
Derivations
The example on the
preceding slides was
leftmost derivation
There is also a rightmost
derivation
41
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>
42
Derivation: x – 2 * y
43
Derivations
In both cases we have
expr →* id – num id
44
Derivations
The two derivations produce
different parse trees.
The parse trees imply
different evaluation orders!
45