Context-Free Languages
1
Fall 2003
Regular Languages
2
Fall 2003
Regular Languages
Regular Languages
3
Fall 2003
Regular Languages
Context-Free Languages
4
Fall 2003
Context-Free Languages
Pushdown
Automata
Context-Free
Grammars
stack
automaton
Context-Free Grammars
5
Fall 2003
Example
6
Fall 2003
A context-free grammar :
A derivation:
7
Fall 2003
A context-free grammar :
Another derivation:
8
Fall 2003
(((( ))))
Describes parentheses:
9
Fall 2003
A context-free grammar :
A derivation:
Example
10
Fall 2003
A context-free grammar :
Another derivation:
11
Fall 2003
12
Fall 2003
A context-free grammar :
A derivation:
Example
13
Fall 2003
A context-free grammar :
A derivation:
14
Fall 2003
() ((( ))) (( ))
Describes
matched
parentheses:
Definition: Context-Free Grammars
15
Fall 2003
Grammar
Productions of the form:
String of variables
and terminals
Variables
Terminal
symbols
Start
variable
Variable
16
Fall 2003
Definition: Context-Free Languages
A language is context-free
if and only if
there is a context-free grammar
with
17
Fall 2003
Derivation Order
18
Fall 2003
Leftmost derivation:
Rightmost derivation:
19
Fall 2003
Leftmost derivation:
Rightmost derivation:
Derivation Trees
20
Fall 2003
21
Fall 2003
22
Fall 2003
23
Fall 2003
24
Fall 2003
25
Fall 2003
Derivation Tree
26
Fall 2003
yield
Derivation Tree
Partial Derivation Trees
27
Fall 2003
Partial derivation tree
28
Fall 2003
Partial derivation tree
29
Fall 2003
Partial derivation tree
sentential
form
yield
30
Fall 2003
Same derivation tree
Sometimes, derivation order doesn’t matter
Leftmost:
Rightmost:
Ambiguity
31
Fall 2003
32
Fall 2003
leftmost derivation
33
Fall 2003
leftmost derivation
34
Fall 2003
Two derivation trees
35
Fall 2003
The grammar
is ambiguous:
string
has two derivation trees
36
Fall 2003
string
has two leftmost derivations
The grammar
is ambiguous:
37
Fall 2003
Definition:
A context-free grammar is ambiguous
if some string has:
two or more derivation trees
38
Fall 2003
In other words:
A context-free grammar is ambiguous
if some string has:
two or more leftmost derivations
(or rightmost)
39
Fall 2003
Why do we care about ambiguity?
take
40
Fall 2003
41
Fall 2003
42
Fall 2003
Correct result:
43
Fall 2003
44
Fall 2003
We fix the ambiguous grammar:
New non-ambiguous grammar:
45
Fall 2003
46
Fall 2003
Unique derivation tree
47
Fall 2003
The grammar :
is non-ambiguous:
Every string has
a unique derivation tree
Another Ambiguous Grammar
48
Fall 2003
IF_STMT
if EXPR then STMT
if EXPR then STMT else STMT
49
Fall 2003
If expr1 then if expr2 then stmt1 else stmt2
IF_STMT
expr1
then
else
if
expr2
then
STMT
stmt1
if
IF_STMT
expr1
then
else
if
expr2
then
STMT
stmt2
if
stmt1
stmt2
Inherent Ambiguity
Some context free languages
have only ambiguous grammars
50
Fall 2003
Example:
51
Fall 2003
The string
has two derivation trees
Compilers
52
Fall 2003
53
Fall 2003
Compiler
Program
v = 5;
if (v>5)
x = 12 + v;
while (x !=3) {
x = x - 3;
v = 10;
}
......
Add v,v,0
cmp v,5
jmplt ELSE
THEN:
add x, 12,v
ELSE:
WHILE:
cmp x,3
...
Machine Code
54
Fall 2003
Lexical
analyzer
parser
Compiler
program
machine
code
input
output
55
Fall 2003
A parser knows the grammar
of the programming language
56
Fall 2003
Parser
PROGRAM STMT_LIST
STMT_LIST STMT; STMT_LIST | STMT;
STMT EXPR | IF_STMT | WHILE_STMT
| { STMT_LIST }
EXPR EXPR + EXPR | EXPR - EXPR | ID
IF_STMT if (EXPR) then STMT
| if (EXPR) then STMT else STMT
WHILE_STMT while (EXPR) do STMT
57
Fall 2003
The parser finds the derivation
of a particular input
10 + 2 * 5
Parser
E -> E + E
| E * E
| INT
E => E + E
=> E + E * E
=> 10 + E*E
=> 10 + 2 * E
=> 10 + 2 * 5
input
derivation
58
Fall 2003
10
E
2
5
E => E + E
=> E + E * E
=> 10 + E*E
=> 10 + 2 * E
=> 10 + 2 * 5
derivation
derivation tree
E
E
E
E
+
*
59
Fall 2003
10
E
2
5
derivation tree
E
E
E
E
+
*
mult a, 2, 5
add b, 10, a
machine code
Parsing
60
Fall 2003
61
Fall 2003
grammar
Parser
input
string
derivation
62
Fall 2003
Example:
Parser
derivation
input
?
63
Fall 2003
Exhaustive Search
Phase 1:
All possible derivations of length 1
Find derivation of
64
Fall 2003
65
Fall 2003
Phase 2
Phase 1
66
Fall 2003
Phase 2
Phase 3
67
Fall 2003
Final result of exhaustive search
Parser
derivation
input
(top-down parsing)
68
Fall 2003
Time complexity of exhaustive search
Suppose there are no productions of the form
Number of phases for string :
69
Fall 2003
Time for phase 1:
possible derivations
For grammar with rules
70
Fall 2003
Time for phase 2:
possible derivations
71
Fall 2003
Time for phase :
possible derivations
72
Fall 2003
Total time needed for string :
Extremely bad!!!
phase 1
phase 2
phase 2|w|
73
Fall 2003
There exist faster algorithms
for specialized grammars
S-grammar:
symbol
string
of variables
appears once
Pair
74
Fall 2003
S-grammar example:
Each string has a unique derivation
75
Fall 2003
In the exhaustive search parsing
there is only one choice in each phase
For S-grammars:
Total time for parsing string :
Time for a phase:
76
Fall 2003
For general context-free grammars:
There exists a parsing algorithm
that parses a string
in time
(we will show it in the next class)