Lecture 6: Context-Free Languages - PowerPoint PPT Presentation

1 / 77
About This Presentation
Title:

Lecture 6: Context-Free Languages

Description:

Title: Lecture 2: Limiting Models of Instruction Obeying Machine Author: Yue Tai-Wen Last modified by: Tai-Wen Yue Created Date: 2/8/2004 8:30:05 AM – PowerPoint PPT presentation

Number of Views:202
Avg rating:3.0/5.0
Slides: 78
Provided by: YueTa4
Category:

less

Transcript and Presenter's Notes

Title: Lecture 6: Context-Free Languages


1
Lecture 6 Context-Free Languages
  • ???

??????? ?????????
2
Content
  • 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

3
Lecture 6 Context-Free Languages
  • Language Recognizer vs.
  • Language Generator

??????? ?????????
4
Recognizer vs. Generator
Parsing
Machines
Grammas
Generation
5
Example Regular Expressions
Is the expression power of r. e. strong enough?
A rule (grammar) to generate strings ?
6
Regular Expressions As Grammars
S, M, A, B
? nonterminals
Production Rules
S
? start symbol
a, b
? terminals
7
Regular Expressions As Grammars
Example
8
Lecture 6 Context-Free Languages
  • Context-Free Grammars

??????? ?????????
9
Definition
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? ?)
10
Notation 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.

11
Notation Conventions
a derivation of G of ?m from ?1.
12
Context-Free Languages
The language generated by G (V, ?, P, S),
denoted as L(G), is
13
Example
L(G)?
G (V, ?, P, E)
E Expression T Term F Factor
14
Example
L(G)?
R1
R2
R3
R4
R5
R6
R7
15
Example
L(G)?
x1
x1(x2x1x1)
x1(x2(x1x1))
x1x2x1x2
?L(G)?
x1(
xx2
x2
2x2
16
Example
L(G)?
G (V, ?, P, S)
VS ?a, b
L(G)anbnn?0
S ? aSb
? aaSbb
? a3Sb3
? ? ? ? ? anSbn
? anbn
17
Example
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
18
Lecture 6 Context-Free Languages
  • Regular Languages
  • vs.
  • Context-Free Languages

??????? ?????????
19
Definition ? Regular Grammar
A CFG G (V, ?, P, S) is regular iff
P ? V ? ?(V??).
tailing nonterminal
RHS
nonterminal
leading terminal(s)
LHS
20
Example
Consider G (V, ?, P, S) with
G is regular.
VS, A, B ?a, b
21
Regular 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)
22
Theorem
Let L be a language.
L is regular iff L is generated by a regular
grammar.
accepted by a DFA
23
Theorem
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,
24
Theorem
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,
25
Example
Consider G (V, ?, P, S) with
G is regular.
VS, A, B ?a, b
f
B
A
26
The 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
27
The Chomsky Hierarchy
28
Lecture 6 Context-Free Languages
  • Pushdown Automata

??????? ?????????
29
Basic Concept
How to accept language LwwR w???
30
Definition
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
31
Definition ? 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
32
The Transition Function of PDA
K ???? ? K ??
33
The Transition Function of PDA
?K ???? ? K ??
?(p, v, ?)? (q, ?)
34
Stack Operations
Case 3 ? ?, ? ? ?
Case 1 ? ? ?, ? ? ?
replacement
push
Case 2 ? ? ?, ? ?
Case 4 ? ?, ? ?
pop
none
?(p, v, ?)? (q, ?)
35
Yields in One Step
if ?(p, v, ?)? (q, ?)
36
Yields
in some (including zero) steps
37
Languages Accepted by PDAs
M (K, ?, ?, ?, s, F)
38
Example
A deterministic PDA
39
Example
40
Example
How to design a PDA to accept L wwR?
41
Example
?
-
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
42
Example
A nondeterministic PDA
43
Lecture 6 Context-Free Languages
  • Pushdown Automata
  • vs.
  • Context-Free Grammars

??????? ?????????
44
Leftmost 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

45
Example
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
46
Lemma
For any context-free grammar G (V, ?, P, S) and
any string w? ?,
See textbook for the proof
47
Theorem
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 ?
48
Theorem
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 ?
49
Theorem
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)
50
Theorem
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
51
Lecture 6 Context-Free Languages
  • Properties of
  • Context-Free Languages

??????? ?????????
52
Review ? Properties of Regular Languages
  • The regular languages are closed under
  • union
  • concatenation
  • Keene star
  • complementation
  • intersection.

53
Properties of Context-Free Languages
  • The context-free languages are closed under
  • union
  • concatenation
  • Keene star
  • complementation
  • intersection.

?
?
?
?
?
54
Properties of Context-Free Languages
  • The context-free languages are closed under
  • union
  • concatenation
  • Keene star.

55
Properties of Context-Free Languages
  • The context-free languages are closed under
  • union
  • concatenation
  • Keene star.

56
Properties of Context-Free Languages
  • The context-free languages are closed under
  • union
  • concatenation
  • Keene star.

57
Review 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.
58
What CFGs Can? What CFGs cannot?
Which of the following languages are context-free?
59
The 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.
60
Theorem
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.
61
Theorem
The context-free languages is not closed under
intersection or complementation.
62
Theorem
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.

63
Theorem Algorithmic Properties
See textbook for the proof.
There are algorithms for answering the following
questions about context-free grammars
  1. Given a grammar G, and a string w, is w?L(G)?
  2. Is L(G)??

How?
What is compiler-compiler?
64
The 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
65
The Chomsky Hierarchy
66
Lecture 6 Context-Free Languages
  • Context-Sensitive Grammars

??????? ?????????
67
Definition ?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
68
Definition ?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
69
Definition ?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
70
Definition ?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)??
71
Review
is not context-free.
Is it context-sensitive?
72
Example
Consider G (V, ?, P, S) with
VS, A, B ?a, b, c
L(G)?
73
Example
Consider G (V, ?, P, S) with
VS, A, B ?a, b, c
74
Example
Consider G (V, ?, P, S) with
VS, A, B ?a, b, c
75
The 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
76
The Chomsky Hierarchy
77
Exercises
  • 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
Write a Comment
User Comments (0)
About PowerShow.com