Title: Lecture 6: Context-Free Languages
1Lecture 6 Context-Free Languages
??????? ?????????
2Content
- Language Recognizer vs. Language Generator
- Context-Free Grammars
- Regular Languages vs. Context-Free Languages
- Pushdown Automata
- Pushdown Automata vs. Context-Free Grammars
- Properties of Context-Free Languages
- Context-Sensitive Grammars
3Lecture 6 Context-Free Languages
- Language Recognizer vs.
- Language Generator
??????? ?????????
4Recognizer vs. Generator
Parsing
Machines
Grammas
Generation
5Example Regular Expressions
Is the expression power of r. e. strong enough?
A rule (grammar) to generate strings ?
6Regular Expressions As Grammars
S, M, A, B
? nonterminals
Production Rules
S
? start symbol
a, b
? terminals
7Regular Expressions As Grammars
Example
8Lecture 6 Context-Free Languages
??????? ?????????
9Definition
A context-free grammar G is a quadruple
(V, ?, P, S)
where
V finite set of nonterminals
V ? ? ?
? finite set of terminals
P finite set of rules, i.e., subset of V? (V?
?)
S start symbol, S?V
In the form of A?? with A?V and ??(V? ?)
10Notation Conventions
- The capital letters A, B, C, D, E, and S denote
nonterminals S is the start symbol. - The lower-case letters a, b, c, d, e, digits and
boldface strings are terminals. - The capital letters X, Y, and Z that may be
either terminals or variables. - The lower-case letters u, v, w, x, y, and z
denote strings of terminals. - The lower-case Greek letters ?, ?, and ? denote
strings of variables and terminals.
11Notation Conventions
a derivation of G of ?m from ?1.
12Context-Free Languages
The language generated by G (V, ?, P, S),
denoted as L(G), is
13Example
L(G)?
G (V, ?, P, E)
E Expression T Term F Factor
14Example
L(G)?
R1
R2
R3
R4
R5
R6
R7
15Example
L(G)?
x1
x1(x2x1x1)
x1(x2(x1x1))
x1x2x1x2
?L(G)?
x1(
xx2
x2
2x2
16Example
L(G)?
G (V, ?, P, S)
VS ?a, b
L(G)anbnn?0
S ? aSb
? aaSbb
? a3Sb3
? ? ? ? ? anSbn
? anbn
17Example
L(G)?
G (V, ?, P, S)
Is the expression power of context-free grammar
stronger than regular expression?
VS ?a, b
L(G)anbnn?0
S ? aSb
? aaSbb
? a3Sb3
? ? ? ? ? anSbn
? anbn
18Lecture 6 Context-Free Languages
- Regular Languages
- vs.
- Context-Free Languages
??????? ?????????
19Definition ? Regular Grammar
A CFG G (V, ?, P, S) is regular iff
P ? V ? ?(V??).
tailing nonterminal
RHS
nonterminal
leading terminal(s)
LHS
20Example
Consider G (V, ?, P, S) with
G is regular.
VS, A, B ?a, b
21Regular Grammars vs. Regular Languages
Consider G (V, ?, P, S) with
G is regular.
VS, A, B ?a, b
? babaS
?
S ? bA
? ababS
S ? aB
?
Regular Expression
L(G)(abab baba)
22Theorem
Let L be a language.
L is regular iff L is generated by a regular
grammar.
accepted by a DFA
23Theorem
L is regular iff L is generated by a regular
grammar.
Pf)
?
Given a DFA M (K, ?, ?, s, F), we will
construct a regular grammar, say, G st. L(M)
L(G).
Consider G (K, ?, P, S) st.
From the correspondence btw. M and G, it is
easily seen that
Hence,
24Theorem
L is regular iff L is generated by a regular
grammar.
Pf)
?
Given a regular grammar G (V, ?, P, S), we will
construct an NFA, say, M st. L(G) L(M).
Consider M (K, ?, ?, s, F) st.
From the correspondence btw. M and G, it is
easily seen that
Hence,
25Example
Consider G (V, ?, P, S) with
G is regular.
VS, A, B ?a, b
f
B
A
26The Chomsky Hierarchy
Chomsky Hierarchy Languages Grammars Automaton
Type 0 Recursively enumerable unrestricted Turing Machine
Recursive unrestricted Decider
Type 1 Context-Sensitive Context-Sensitive Linear-Bounded Automaton
Type 2 Context-Free Context-Free Push-Down Automaton
Type 3 Regular Regular NFA or DFA
27The Chomsky Hierarchy
28Lecture 6 Context-Free Languages
??????? ?????????
29Basic Concept
How to accept language LwwR w???
30Definition
A pushdown automata is a sextuple
(K, ?, ?, ?, s, F)
where
K a finite set of states
? a finite set of input symbols
? a finite set of stack symbols
? the transition function K ???? ? K ??
s ?K the initial state
F ?K the set of final states
31Definition ? More Precise Version
A PDA is possibly nondeterministic.
A pushdown automata is a sextuple
(K, ?, ?, ?, s, F)
where
K a finite set of states
? a finite set of input symbols
? a finite set of stack symbols
? the transition function K ???? ? 2K ??
s ?K the initial state
F ?K the set of final states
32The Transition Function of PDA
K ???? ? K ??
33The Transition Function of PDA
?K ???? ? K ??
?(p, v, ?)? (q, ?)
34Stack Operations
Case 3 ? ?, ? ? ?
Case 1 ? ? ?, ? ? ?
replacement
push
Case 2 ? ? ?, ? ?
Case 4 ? ?, ? ?
pop
none
?(p, v, ?)? (q, ?)
35Yields in One Step
if ?(p, v, ?)? (q, ?)
36Yields
in some (including zero) steps
37Languages Accepted by PDAs
M (K, ?, ?, ?, s, F)
38Example
A deterministic PDA
39Example
40Example
How to design a PDA to accept L wwR?
41Example
?
-
s
abbbba
s
a
1
bbbba
s
ba
2
bbba
s
bba
2
bba
bba
3
f
bba
f
ba
5
ba
f
a
5
a
?
f
?
4
42Example
A nondeterministic PDA
43Lecture 6 Context-Free Languages
- Pushdown Automata
- vs.
- Context-Free Grammars
??????? ?????????
44Leftmost and Rightmost Derivations
- Given a context-free grammar there may be several
ways of rewriting its nonterminals to arrive at
precisely the same string. - Two systematic ways of derivations are
- Leftmost derivation
- Rightmost derivation
45Example
Note that there are other derivations that are
neither leftmost nor rightmost.
VS, A, B, ?a, b, c
Consider G (V, ?, P, S) with
? abcbabb
S ? aABb
? abAbBb
? abcbBb
? abcbaBb
S ? aABb
? aAaBb
? aAabb
? abAbabb
? abcbabb
46Lemma
For any context-free grammar G (V, ?, P, S) and
any string w? ?,
See textbook for the proof
47Theorem
The class of languages accepted by pushdown
automata is exactly the class of context-free
languages.
Pf)
? Pushdown Automaton
Context-Free Grammar
G (V, ?, P, S)
M ?
? Context-Free Grammar
Pushdown Automaton
M (K, ?, ?, ?, s, F)
G ?
48Theorem
The class of languages accepted by pushdown
automata is exactly the class of context-free
languages.
Pf)
Context-Free Grammar
? Pushdown Automaton
? M (p, q, ?, V? ?, ?, p, q)
G (V, ?, P, S)
M ?
49Theorem
The class of languages accepted by pushdown
automata is exactly the class of context-free
languages.
Pf)
Context-Free Grammar
? Pushdown Automaton
? M (p, q, ?, V, ?, p, q)
G (V, ?, P, S)
M ?
One can show that L(M)L(G).
(see text)
50Theorem
The class of languages accepted by pushdown
automata is exactly the class of context-free
languages.
Pf)
Pushdown Automaton
? Context-Free Grammar
M (K, ?, ?, ?, s, F)
G ?
see text
51Lecture 6 Context-Free Languages
- Properties of
- Context-Free Languages
??????? ?????????
52Review ? Properties of Regular Languages
- The regular languages are closed under
- union
- concatenation
- Keene star
- complementation
- intersection.
53Properties of Context-Free Languages
- The context-free languages are closed under
- union
- concatenation
- Keene star
- complementation
- intersection.
?
?
?
?
?
54Properties of Context-Free Languages
- The context-free languages are closed under
- union
- concatenation
- Keene star.
55Properties of Context-Free Languages
- The context-free languages are closed under
- union
- concatenation
- Keene star.
56Properties of Context-Free Languages
- The context-free languages are closed under
- union
- concatenation
- Keene star.
57Review The Pumping Theoremfor Regular Languages
Let L be an infinite regular set. Then there are
strings u,v, and w st. vgt0 and uvnw?L for all n
? 0.
58What CFGs Can? What CFGs cannot?
Which of the following languages are context-free?
59The Pumping Theoremfor Context-Free Languages
See textbook for the proof.
Let G be context-free gramma. Then there is a
number K, depending on G, such that any w ? L(G)
with wgtK can be rewritten as w uvxyz in such
a way that either v or y is nonempty and uvnxynz
? L(G) for all n ? 0.
60Theorem
Let G be context-free gramma. Then there is a
number K, depending on G, such that any w ? L(G)
with wgtK can be rewritten as w uvxyz in such
a way that either v or y is nonempty and uvnxynz
? L(G) for all n ? 0.
is not context-free.
Pf)
Let
It is impossible for us to rewrite w as w uvxyz
such that vygt0 and uvnxynz ? L(G) for all n ? 0.
61Theorem
The context-free languages is not closed under
intersection or complementation.
62Theorem
The context-free languages is not closed under
intersection or complementation.
Pf)
Facts
context-free
? not context-free
? CFLs are not closed under intersection.
Fact
- If CFLs are closed under complementation,
- they would be also closed under intersection.
63Theorem Algorithmic Properties
See textbook for the proof.
There are algorithms for answering the following
questions about context-free grammars
- Given a grammar G, and a string w, is w?L(G)?
- Is L(G)??
How?
What is compiler-compiler?
64The Chomsky Hierarchy
Chomsky Hierarchy Languages Grammars Automaton
Type 0 Recursively enumerable unrestricted Turing Machine
Recursive unrestricted Decider
Type 1 Context-Sensitive Context-Sensitive Linear-Bounded Automaton
Type 2 Context-Free Context-Free Push-Down Automaton
Type 3 Regular Regular NFA or DFA
65The Chomsky Hierarchy
66Lecture 6 Context-Free Languages
- Context-Sensitive Grammars
??????? ?????????
67Definition ?Context-Free Grammar (Review)
A context-free grammar G is a quadruple
(V, ?, P, S)
where
V finite set of nonterminals
V ? ? ?
? finite set of terminals
P finite set of rules in the form of
A?? with A?V and ??(V? ?)
S start symbol, S?V
68Definition ?Context-Sensitive Grammar (I)
A context-free grammar G is a quadruple
A context-sensitive grammar G is a quadruple
(V, ?, P, S)
where
V finite set of nonterminals
V ? ? ?
? finite set of terminals
? ? ?
P finite set of rules in the form of
A?? with A?V and ??(V? ?)
with ?, ??(V? ?) and ? ? ?.
S start symbol, S?V
69Definition ?Context-Sensitive Grammar (II)
A context-free grammar G is a quadruple
A context-sensitive grammar G is a quadruple
(V, ?, P, S)
where
V finite set of nonterminals
V ? ? ?
? finite set of terminals
? ? ?
?A? ? ???
P finite set of rules in the form of
A?? with A?V and ??(V? ?)
with ?, ??(V? ?) and ? ? ?.
with ?, ??(V? ?), A?V and ??(V? ?).
S start symbol, S?V
70Definition ?Context-Sensitive Grammar (II)
A language L is said to be context-sensitive if
there exists a context-sensitive grammar G, such
that
L L(G)
or
L L(G)??
71Review
is not context-free.
Is it context-sensitive?
72Example
Consider G (V, ?, P, S) with
VS, A, B ?a, b, c
L(G)?
73Example
Consider G (V, ?, P, S) with
VS, A, B ?a, b, c
74Example
Consider G (V, ?, P, S) with
VS, A, B ?a, b, c
75The Chomsky Hierarchy
Chomsky Hierarchy Languages Grammars Automaton
Type 0 Recursively enumerable unrestricted Turing Machine
Recursive unrestricted Decider
Type 1 Context-Sensitive Context-Sensitive Linear-Bounded Automaton
Type 2 Context-Free Context-Free Push-Down Automaton
Type 3 Regular Regular NFA or DFA
76The Chomsky Hierarchy
77Exercises
- Construct pushdown automata for the following
languages over the alphabet a, b - w w has an equal number of as and bs
- w the length of w is odd and its middle
symbol is a b - Prove that the following languages are not
context free - aibjck 0 lt i lt j lt k
- wwRw w ? a,b
- Construct context-free grammars that define the
following languages over a, b - ambn m ? n
- aibjck i j k