Title: Context-Free 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
31So far, we concentrated on generative aspect of
grammars.How about analytical aspect?Parsing..
32Parser
input string
derivation
grammar
33Example
Parser
derivation
input
?
34Exhaustive Search
Phase 1
Find derivation of
All possible derivations of length 1
35(No Transcript)
36Phase 2
Phase 1
37Phase 2
Phase 3
38Final result of exhaustive search
(top-down parsing)
Parser
input
derivation
39Time complexity of exhaustive search
Suppose there are no productions of the form
Number of phases for string is Why?
40For grammar with rules
Time for phase 1
possible derivations
41Time for phase 2
possible derivations
42Time for phase
possible derivations which is exponential in the
length of w
43Total time needed for parsing
phase 1
phase 2w
phase 2
Extremely bad!!!
44There exist faster algorithms for specialized
grammars
S-grammar
symbol
string of variables
appears once
Pair
45S-grammar example
Each string has a unique derivation
46For S-grammars
In the exhaustive search parsing there is only
one choice in each phase
Time for a phase
Total time for parsing string
47For general context-free grammars
There exists a parsing algorithm that parses a
string in time
(we will show it in the next class)
48Ambiguity
49leftmost derivation
50leftmost derivation
51(No Transcript)
52The grammar
is ambiguous
since there is a string, namely
, which has two derivation trees
53The grammar
is ambiguous
since string
has two leftmost derivations
54Definition
A context-free grammar is ambiguous if
some string has two or more
distinct derivation trees
55Alternatively we may say
Ambiguity of a grammar implies the existence
of two or more leftmost (or rightmost)
derivations
56Why do we care about ambiguity?
take
57(No Transcript)
58(No Transcript)
59Correct result
60- Ambiguity is bad for programming languages
- We want to remove ambiguity
61We may be able to fix the ambiguity
New non-ambiguous grammar
62(No Transcript)
63Unique derivation tree
64The grammar
is non-ambiguous
Every string has a unique
derivation tree
65Another Ambiguous Grammar
IF_STMT
if EXPR then STMT
if EXPR then STMT else STMT
66if 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
67Inherent Ambiguity
- Some context-free languages
- have only ambiguous grammars
Example
68The string
has two derivation trees
69Compilers
70Machine 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
71Compiler
Lexical analyzer
parser
input
output
machine code
program
72A parser knows the grammar of the programming
language
73Parser
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
74The 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
75derivation 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
76derivation tree
E
machine code
E
E
mult a, 2, 5 add b, 10, a
10
E
E
2
5