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