Lecture 05
Lecture 05
CS143
Lecture 5
• Parser overview
• Derivations
• Ambiguity
2
Languages and Automata
• Regular languages
– The weakest formal languages widely used
– Many applications
3
Beyond Regular Languages
{( ) | i ≥ 0}
i i
4
What Can Regular Languages Express?
5
The Functionality of the Parser
6
Example
• Cool
if x = y then 1 else 2 fi
• Parser input
IF ID = ID THEN INT ELSE INT FI
• Parser output
IF-THEN-ELSE
= INT INT
ID ID
7
Comparison with Lexical Analysis
8
The Role of the Parser
• We need
– A language for describing valid strings of tokens
– A method for distinguishing valid from invalid strings of
tokens
9
Context-Free Grammars
• An EXPR is
if EXPR then EXPR else EXPR fi
while EXPR loop EXPR pool
…
10
CFGs (Cont.)
• A CFG consists of
– A set of terminals T
– A set of non-terminals N
– A start symbol S (a non-terminal)
– A set of productions
X → Y1Y2 … Yn
where X ∊ N and Yi ∊T ∪ N ∪ {ε}
11
Notational Conventions
12
Examples of CFGs
A fragment of Cool:
13
Examples of CFGs (cont.)
E → E ∗E
| E+E
| (E )
| id
(Running example this week and next)
14
The Language of a CFG
X → Y1 … Yn
15
Key Idea
16
The Language of a CFG (Cont.)
if there is a production
Xi → Y1 … Ym
17
The Language of a CFG (Cont.)
Write
X1 … Xn →* Y1 ... Ym
if
X1 … Xn → … → … → Y1 ... Ym
in 0 or more steps
18
The Language of a CFG
19
Terminals
20
Examples
Two grammars:
S → (S ) S → (S )
OR
S → ε | ε
21
Cool Example
A fragment of Cool:
22
Cool Example (Cont.)
id
if id then id else id fi
while id loop id pool
if while id loop id pool then id else id fi
if if id then id else id fi then id else id fi
23
Arithmetic Example
25
More Notes
26
Derivations and Parse Trees
27
Derivation Example
• Grammar
E → E+E | E ∗ E | (E) | id
• String
id ∗ id + id
28
Derivation Example (cont.)
E
E
→ E+E
E + E
→ E ∗ E+E
→ id ∗ E + E E * E id
→ id ∗ id + E
id id
→ id ∗ id + id
29
Derivation in Detail (1)
30
Derivation in Detail (2)
E + E
E
→ E+E
31
Derivation in Detail (3)
E E + E
→ E+E
E * E
→ E ∗ E+E
32
Derivation in Detail (4)
E
E + E
→ E+E
→ E ∗ E+E E * E
→ id ∗ E + E
id
33
Derivation in Detail (5)
E
E
→ E+E E + E
→ E ∗ E+E
E * E
→ id ∗ E + E
→ id ∗ id + E id id
34
Derivation in Detail (6)
E
E
→ E+E
E + E
→ E ∗ E+E
→ id ∗ E + E E * E id
→ id ∗ id + E
id id
→ id ∗ id + id
35
Notes on Derivations
36
Left-most and Right-most Derivations
38
Right-most Derivation in Detail (2)
E + E
E
→ E+E
39
Right-most Derivation in Detail (3)
E E + E
→ E+E
id
→ E+id
40
Right-most Derivation in Detail (4)
E
E + E
→ E+E
→ E+id E * E id
→ E ∗ E + id
41
Right-most Derivation in Detail (5)
E
E
→ E+E E + E
→ E+id
E * E id
→ E ∗ E + id
→ E ∗ id + id id
42
Right-most Derivation in Detail (6)
E
E
→ E+E
E + E
→ E+id
→ E ∗ E + id E * E id
→ E ∗ id + id
id id
→ id ∗ id + id
43
Derivations and Parse Trees
44
Summary of Derivations
45
Ambiguity
46
Ambiguity (Cont.)
E + E E * E
E * E id id E + E
id id id id
47
Ambiguity (Cont.)
• Ambiguity is BAD
– Leaves meaning of some programs ill-defined
48
Dealing with Ambiguity E → E+E | E ∗ E | (E) | id
49
Ambiguity in Arithmetic Expressions
E + E E * E
E * E int int E + E
51
The Dangling Else: Example
• The expression
if E1 then if E2 then E3 else E4
has two parse trees
if if
E1 if E4 E1 if
E2 E3 E2 E3 E4
52
E → if E then E
| if E then E else E
The Dangling Else: A Fix | OTHER
53
The Dangling Else: Example Revisited
if if
E1 if E1 if E4
E2 E3 E4 E2 E3
55
Precedence and Associativity Declarations
• Examples …
56
Associativity Declarations
E + E E + E
E + E int int E + E
E * E E + E
E + E int int E * E