Upload
amiyam
View
221
Download
0
Embed Size (px)
Citation preview
8/3/2019 Context-Free and Beyond
1/44
Finishing up context-free
languagesReview
properties of cflsmaterial covered in Chapters 5-8
8/3/2019 Context-Free and Beyond
2/44
Context-free Languages
Review
A language iscontext-free if it can be generatedby acontext-freegrammar (cfg) or recognized by
apushdownautomaton (pda) A context-free grammar is one in which every
production is of the form:X w
that is, of the form in which the left-hand side is asingle variable and the right-hand side is any stringof variables and terminals.
8/3/2019 Context-Free and Beyond
3/44
Context-free Languages
Review
a cfg productionXxXygenerates from the
center and produces an equal number ofxs and
ys
the cfg productionsX xY, Y xYy will
generate morexs thanys.
the cfg productionsX aXa | bXb generatespalindromic strings.
x y is the same asx > y or x < y
8/3/2019 Context-Free and Beyond
4/44
Context-free Languages
Review
Derivation trees
Derivations
Left-most and right-most derivations
Ambiguity (grammars)
Inherent Ambiguity (languages)
{an
bm
cm
} {an
bn
cm
} is INHERENTLY AMBIGUOUS! There has to be at least two ways to generateabc since it
could be in the language because theb's equal thec's, orbecause thea's equal theb's
8/3/2019 Context-Free and Beyond
5/44
Context-free Languages
Review
rules (X )
unit rules
useless productions
in general, hard to remove because of cycles
(XY, YX). But in simple cases can be removed
by merely crossing them out and adding productions
of the formX w where there is a production of
the form Yw
we can get rid of a rule by crossing it out and adding
new rules with the erased variable (X) removed foreach case where it appears on a right-hand side
a variable is said to be useless if the variable can not be
generated from the start symbol orif no string containing only
terminal symbols can be generated from it. Any production
mentioning a useless variable anywhere in it is said to be
useless and can be removed.
8/3/2019 Context-Free and Beyond
6/44
Context-free Languages
Review
Chomsky Normal Form
all rules are of the formX YZ whereX, YandZ are
all variables. (They dont have to be different variables)orXa. We also allow the rule S
I expect you to understand this and to be able to
transform simple cfgs into Chomsky Normal Form.
The technique is to create new variables wherever aterminal appears on the right-hand side and then to
break upX WYZ intoX VZ, V WY
8/3/2019 Context-Free and Beyond
7/44
Context-free Languages
Review
Greibach Normal Form
all rules are of the formX aY...Zwherea is a
terminal and Y...Z is any string of zero or morevariables. We also allow the rule S
I expect you to understand this and to be able totransform simple cfgs into Greibach Normal Form.
This is generally hard to do (when the right-hand side
begins with a variable), but in simple cases, technique isto remove unit rules and add variables corresponding toany terminal on the right-hand side that is not the firstsymbol.
8/3/2019 Context-Free and Beyond
8/44
Context-free Languages
Review
Pushdown automata
A pushdown automaton is merely a finite automaton
with a stack added to it. The stack allows for unbounded memorization.
The transition function maps (q,a,s) to a finite numberof possible pairs (q,s*). We also allowtransitions.The transition (q,a,s) (r, uts) means that if we are in
state q and the next input symbol isa and the symbolsis on top of the stack, then go into stater and replace thesymbols on the stack with the symbols u,t ands (one ata time); that is, push ut. The stack string read from leftto right is the same from top downward.
8/3/2019 Context-Free and Beyond
9/44
Context-free Languages
Review
Pushdown automata
A string is accepted by a pda if after reading that string,
the pda could wind up in an accepting state. Thecontents of the stack is irrelevant. We always start the
stack with a special stack symbol z0 to avoid the
situation of having an empty stack and therefore cant
write a transition rule for the situation.
8/3/2019 Context-Free and Beyond
10/44
Context-free Languages
Review
Pushdown automata
The language {anbmcn+m}
why does this work?
a, z0, xz0a, x, xx
, z0, z0
,x, x
b, z0, xz0
b, x, xx
, z0 , z0,x, x
c, x,
, z0 , z0
8/3/2019 Context-Free and Beyond
11/44
Context-free Languages
Review Pushdown automata
The language {anbm | n m}
why does this work?
a, z0, xz0a, x, xx
, z0, z0
,x, x
b, x,
, x, xb, z0, z0
too manyas
too manybs
8/3/2019 Context-Free and Beyond
12/44
Context-free Languages
Review
Pushdown automata
The language of balanced parentheses
why does this work?
(, z0, (z0
(, (, ((
), (,
, z0 , z0
8/3/2019 Context-Free and Beyond
13/44
Context-free Languages
Review
Pushdown automata
Adeterministicpushdownautomaton (dpda) only
allows one transition for each triple (q,a,s) and if thereare transitions (q,,s)then there are no corresponding
non- transitions (q,c,s)for anyc
Not every context-free language can be recognized by a
deterministic push-down automaton. How would adpda generate the language {wwR}, the
language of unmarked palindromes? It cant. Why?
8/3/2019 Context-Free and Beyond
14/44
Context-free grammars and Pushdown
Automata
Context-free grammars recognize exactly the
same class of languages as non-deterministic
pushdown automata.
Proof:
Consider any context-free grammar. There is an
equivalent Greibach Normal Form grammar. Construct
annpda that takes the form below:
,z0, Sz0
8/3/2019 Context-Free and Beyond
15/44
Context-free grammars and Pushdown
Automata
Proof(continued):
Consider any context-free grammar. There is an
equivalent Greibach Normal Form grammar. For each
productionY
aX
1X
2...X
n, wheren 1
, add the labela, Y,X1X2...Xnto the loop on the middle state. For each
production Ya , adda, Y, to the transition going to
the accepting state. This machine accepts the same
language as the original context-free grammar!!!
,z0, Sz0
8/3/2019 Context-Free and Beyond
16/44
Context-free grammars and Pushdown
Automata
Example:
Consider the grammar
SaXY
XaXB | aB YbYA | bA
Aa
Bb
L(G) = {aanbnbmam | m, n > 0}
a, S, XY
a, X, XB
a, X,B
b, Y, YA
b,Y, A
a, A,
b,B,
Consider aaabbba
S
,z0, Sz0
X
YA
B
z0
X
B
8/3/2019 Context-Free and Beyond
17/44
Context-free grammars and Pushdown
Automata
Context-free grammars recognize exactly the
same class of languages as non-deterministic
pushdown automata.
Proof:
We leave the proof that any push-down automaton can
be converted into a context-free grammar to the
"reader."
8/3/2019 Context-Free and Beyond
18/44
The Pumping Lemma for Context-Free
Languages
LetL be context-free. There exists some
integerm such that for all w inLwith |w| m,
w = uvxyz with |vxy|m and |vy|> 0 such that
uvixyizL for all i = 0, 1, 2, 3 ....
8/3/2019 Context-Free and Beyond
19/44
The Pumping Lemma for Context-Free
Languages
This is similar to our previous Pumping
Lemma, except we are allowed to pump
equally in two different spots in the middle of
the string. These two spots must be within a
distance ofm from one another.
We use this Pumping Lemma for a very
similar reason: to show that a language is NOTcontext-free
8/3/2019 Context-Free and Beyond
20/44
The Pumping Lemma for Context-Free
Languages
Show thatL = {anbncn: n 0} is not context-
free.
Proof: Assume thatL is context-free.
Then the Pumping Lemma applies.
Letmbe the magic number.
Consider the stringam
bm
cm
which is clearly inL and is longer thanm.
By the Pumping Lemma,
8/3/2019 Context-Free and Beyond
21/44
The Pumping Lemma for Context-Free
Languages
aaaaa....aaaaabbb...bbbbbccc...ccccc
m m m
u v x y z
8/3/2019 Context-Free and Beyond
22/44
The Pumping Lemma for Context-Free
Languages
ambmcmcan be written as uvxyz with |vxy| m and
|vy| > 0 such that uvixyiz is inL for all i = 0, 1, 2, 3 ....
There are only a limited number of possibilities for v
andy. v crosses the border between theas andbs (ory crosses
the border betweenbs andcs)
v is allas andy is allbs
v is allbs andyis allcs
etc. etc.
8/3/2019 Context-Free and Beyond
23/44
The Pumping Lemma for Context-Free
Languages
Take each case separately. Clearly ifv ory
crosses a boundary uv2xy2z will not be of the
form as followed by bs followed by cs
But ifv is all a's theny can not contain c's. So
pumping up will leave too few c's.
Ifv is all b's then pumping up will leave too
few a's.
Therefore the language is not context-free.
8/3/2019 Context-Free and Beyond
24/44
Context-free Languages
What are the properties of Context-Free
Languages?
There is an algorithm to determine if a given
Context-Free Language is empty.
Proof: Construct any grammar for the
language. Remove all useless productions. IfS
is found to be useless, then the language isempty; otherwise it is not.
8/3/2019 Context-Free and Beyond
25/44
Context-free Languages
What are the properties of Context-FreeLanguages?
There is an algorithm to determine if a given
Context-Free Language is infiniteProof: LetGbe a grammar forL that has no
unit rules, productions or useless productions.Let Ghaven variables. For each variable,
consider all the derivations of lengthn startingwith that variable. If any sentential form in anyof these derivations contains its starting symbol,the language is infinite.
8/3/2019 Context-Free and Beyond
26/44
Context-free Languages
What are the properties of Context-Free
Languages?
There exists an algorithm to determine if a
string belongs to a given context-free
language.
Proof: Construct a Greibach Normal Form
grammar. Construct the corresponding npda. Ifthe npda accepts the string the string is in the
language. If it doesn't, the string it isn't in the
language.
8/3/2019 Context-Free and Beyond
27/44
Context-free Languages
What are the properties of Context-Free
Languages?
Context-free Languages are closed under
union.
Proof: LetG1 be a grammar forL1and G2 a
grammar forL2. We can modify one of these
to ensure that they have no variables in commonwithout affecting the language. Change the start
symbol ofG1 to S1, and the start symbol ofG2
to S2. Add S S1 | S2. Voila!
8/3/2019 Context-Free and Beyond
28/44
Context-free Languages
What are the properties of Context-Free
Languages?
Context-free languages are not closed under
intersection.
Proof: Consider the following two grammars:
L1 = {anbncm, n > 0, m > 0}
L2 = {anbmcm, n > 0, m > 0}
L1 L2 = {anbncn, n > 0}. We will shortly show
that this language is not context-free.
8/3/2019 Context-Free and Beyond
29/44
Context-free Languages
Review
Context-free languages
What kinds of languages are beyond the scopeof context-free languages?
anbncn
wwanbmcnm
8/3/2019 Context-Free and Beyond
30/44
Homework
8.1/3, 7c and d, 8
8.2/12, 18
You are NOT responsible for any material
regarding linear languages.
8/3/2019 Context-Free and Beyond
31/44
Where do we go from here?
From the earlier example, we see that not all
languages are context-free.
Is there a mathematical model for even more
complex computations?
There are many.
We will study one in particular: an automaton
called a Turing machine.
8/3/2019 Context-Free and Beyond
32/44
Turing machines
A Turing machine (tm) is a finite automaton
with a twist:
The tape head can move in either direction (left
OR right)
A square on the input tape can be overwritten with
another symbol
By the way, there is no stack!
8/3/2019 Context-Free and Beyond
33/44
Turing machines
Remember our finite automaton?
inputtape
on/offswitch accept
or
rejecttape read
head
"guts"of the
machine
8/3/2019 Context-Free and Beyond
34/44
Turing machines
A Turing machine
inputtape
on/offswitch accept
or
rejecttape read
head
"guts"of the
machine
tape head
can move
left or right.
tape head
can read or
write!
8/3/2019 Context-Free and Beyond
35/44
Turing machines
A Turing machine (tm) is a finite automaton
with a twist:
The tape head can move in either direction (left
OR right)
A square on the input tape can be overwritten with
another symbol
By the way, there is no stack!
8/3/2019 Context-Free and Beyond
36/44
Turing machines
What can a Turing machine do? Clearly it can
accept any regular language. Just limit the
instructions to read from left to right and
never change the contents of the input tape.
Can it accept the language {anbn}? How?
How about {wcwR}?
How about {wwR}?
How about {ww}?
8/3/2019 Context-Free and Beyond
37/44
Turing machines
Can it accept the language {anbn}? How?
Lay out a strategy
Let's mark off eacha and matchingb with anX
Let's work with thea's andb's from left to right
Whenever we mark off ana, move right until we find a
b. Mark off theb and head back left looking for the
left-most remaininga. Repeat as long as you can.
If there are no morea's, go to the right and check thatthere are no moreb's.
Let the machine die if there is no matchingb for ana.
8/3/2019 Context-Free and Beyond
38/44
Turing machines
a, X, R
a, a, R
X, X, R
X, X, R
b, b, L
X, X, L
a, a, L
a, a, L
X, X, R
, , R
X, X, R
, , R
, , R
L = {anbn, n 0}
8/3/2019 Context-Free and Beyond
39/44
Turing machines
Can it accept the language {wcwR}? How?
Lay out a strategy
Let's find the centerc.
Find the next unmarked symbol on the left. Head right,remembering whether it was an a or a b.
If the next unmarked symbol on the right matches, head
back to the middle and start again.
If from the centerc, there are no more unmarkedsymbols on the left, make sure there are no more on the
right as well.
8/3/2019 Context-Free and Beyond
40/44
Turing machines
How about {wcwR}?
a,a,R
b,b,R
a,X,R c,c,R
X,X,R
a,X,LX,X,L
c,c,L
b,X,R
X,X,R
X,X,R
X,X,R
c,c,Rb,X,L
c,c,L
c,c,L
X,X,L
X,X,L
,,R
X,X,R
c,c,R
X,X,R
,,R
8/3/2019 Context-Free and Beyond
41/44
Turing machines
How about {wwR}?
Describe in words how a Turing machine can
recognize this language.
Instead of going to the middle and working
outwards like the last example, start with the
outermost symbols and keep matching until
you hit the middle.
8/3/2019 Context-Free and Beyond
42/44
Turing machines
How about {anbncn}?
YES!!!!!!!!!!!
consider our first machine and how it can be
extended to handle matchingcs as well.
Turing machines are more powerful thanpushdown automata!
8/3/2019 Context-Free and Beyond
43/44
Turing machines
The Church-Turing Thesis:
If something can be computed it can be
computed by a Turing machine.
Note that this is called a Thesis, not a theorem.
It cant be proved, because the term can be
computed is too vague.
But it is universally accepted as a true statement.
8/3/2019 Context-Free and Beyond
44/44
Turing machines
Given the Church-Turing Thesis:
What does this say about "computability"?
Are there things even a Turing machine can't do?
If there are, then there are things that simply can't
be "computed."
Not with a Turing machine
Not with your laptop Not with a supercomputer
There ARE things that a Turing machine can't do!!!