Title: Finishing up contextfree languages
1Finishing up context-free languages
- Review
- properties of cfls
- material covered in Chapters 5-8
2Context-free Languages
- Review
- A language is context-free if it can be generated
by a context-free grammar (cfg) or recognized by
a pushdown automaton (pda) - A context-free grammar is one in which every
production is of the form X ? wthat is, of
the form in which the left-hand side is a single
variable and the right-hand side is any string of
variables and terminals.
3Context-free Languages
- Review
- a cfg production X ? xXy generates from the
center and produces an equal number of xs and
ys - the cfg productions X ? xY, Y ? xYy will generate
more xs than ys. - the cfg productions X ? aXa bXb generates
palindromic strings. - x ? y is the same as x gt y or x lt y
4Context-free Languages
- Review
- Derivation trees
- Derivations
- Left-most and right-most derivations
- Ambiguity (grammars)
- Inherent Ambiguity (languages)
- anbmcm ? anbncm is INHERENTLY AMBIGUOUS!
- There has to be at least two ways to generate abc
since it could be in the language because the b's
equal the c's, or because the a's equal the b's
5Context-free Languages
- Review
- ? rules (X ? ?)
- unit rules
- useless productions
- we can get rid of a ? rule by crossing it out
and adding new rules with the erased variable
(X) removed for each case where it appears on a
right-hand side
- in general, hard to remove because of cycles
(X?Y, Y?X). But in simple cases can be removed
by merely crossing them out and adding
productions of the form X ? w where there is a
production of the form Y ? w
- a variable is said to be useless if the
variable can not be generated from the start
symbol or if 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.
6Context-free Languages
- Review
- Chomsky Normal Form
- all rules are of the form X ? YZ where X, Y and Z
are all variables. (They dont have to be
different variables) or X ? a. 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 a terminal appears on the right-hand
side and then to break up X ? WYZ into X ? VZ, V
? WY
7Context-free Languages
- Review
- Greibach Normal Form
- all rules are of the form X ? aY...Z where a is a
terminal and Y...Z is any string of zero or more
variables. We also allow the rule S ? ? - I expect you to understand this and to be able to
transform 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 is to remove unit rules and add
variables corresponding to any terminal on the
right-hand side that is not the first symbol.
8Context-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 number of possible pairs (q, s). We also
allow ? transitions. The transition (q, a, s) ?
(r, uts) means that if we are in state q and the
next input symbol is a and the symbol s is on top
of the stack, then go into state r and replace
the symbol s on the stack with the symbols u, t
and s (one at a time) that is, push ut. The
stack string read from left to right is the same
from top downward.
9Context-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. The contents 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.
10Context-free Languages
- Review
- Pushdown automata
- The language anbmcnm
- why does this work?
11Context-free Languages
- Review
- Pushdown automata
- The language anbm n? m
- why does this work?
?, x, x b, z0, z0
too many as
?, z0 , z0 ? ,x, x
a, z0 , xz0 a, x, xx
b, x, ?
too many bs
12Context-free Languages
- Review
- Pushdown automata
- The language of balanced parentheses
- why does this work?
?, z0 , z0
(, z0 , (z0 (, (, (( ), (, ?
13Context-free Languages
- Review
- Pushdown automata
- A deterministic pushdown automaton (dpda) only
allows one transition for each triple (q, a, s)
and if there are ? transitions (q, ?, s) then
there are no corresponding non-? transitions (q,
c, s) for any c - Not every context-free language can be recognized
by a deterministic push-down automaton. - How would a dpda generate the language wwR, the
language of unmarked palindromes? It cant. Why?
14Context-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 an npda that takes the form below
?, z0, Sz0
15Context-free grammars and Pushdown Automata
- Proof (continued)
- Consider any context-free grammar. There is an
equivalent Greibach Normal Form grammar. For
each production Y ? aX1X2...Xn, where n 1 , add
the label a, Y, X1X2...Xn to the loop on the
middle state. For each production Y ? a , add a,
Y, ? to the transition going to the accepting
state. This machine accepts the same language as
the original context-free grammar!!!
?, z0, Sz0
16Context-free grammars and Pushdown Automata
- ExampleConsider the grammar
- S ? aXY
- X ? aXB aB
- Y ? bYA bA
- A ? a
- B ? b
L(G) aanbnbmam m, n gt 0
Consider aaabbba
B
X
a, S, XY
b, Y, YA
a, X, XB
X
B
b, Y, A
a, X, B
S
Y
A
a, A, ?
z0
?, z0, Sz0
b, B, ?
17Context-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."
18The Pumping Lemma for Context-Free Languages
- Let L be context-free. There exists some
integer m such that for all w in L with w m, - w uvxyz with vxy m and vy gt 0 such that
- uvixyiz ? L for all i 0, 1, 2, 3 ....
19The 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 of m
from one another. - We use this Pumping Lemma for a very similar
reason to show that a language is NOT
context-free
20The Pumping Lemma for Context-Free Languages
- Show that L anbncn n 0 is not
context-free. - Proof Assume that L is context-free.
- Then the Pumping Lemma applies.
- Let m be the magic number.
- Consider the string ambmcm which is clearly in L
and is longer than m. - By the Pumping Lemma,
21The Pumping Lemma for Context-Free Languages
u
x
z
v
y
aaaaa....aaaaabbb...bbbbbccc...ccccc
m
m
m
22The Pumping Lemma for Context-Free Languages
- ambmcm can be written as uvxyz with vxy m and
vy gt 0 such that uvixyiz is in L for all i 0,
1, 2, 3 .... - There are only a limited number of possibilities
for v and y. - v crosses the border between the as and bs (or
y crosses the border between bs and cs) - v is all as and y is all bs
- v is all bs and y is all cs
- etc. etc.
23The Pumping Lemma for Context-Free Languages
- Take each case separately. Clearly if v or y
crosses a boundary uv2xy2z will not be of the
form as followed by bs followed by cs - But if v is all a's then y can not contain c's.
So pumping up will leave too few c's. - If v is all b's then pumping up will leave too
few a's. - Therefore the language is not context-free.
24Context-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. If S is found to
be useless, then the language is empty otherwise
it is not.
25Context-free Languages
- What are the properties of Context-Free
Languages? - There is an algorithm to determine if a given
Context-Free Language is infinite - Proof Let G be a grammar for L that has no unit
rules, ? productions or useless productions. Let
G have n variables. For each variable, consider
all the derivations of length n starting with
that variable. If any sentential form in any of
these derivations contains its starting symbol,
the language is infinite.
26Context-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. If the npda
accepts the string the string is in the language.
If it doesn't, the string it isn't in the
language.
27Context-free Languages
- What are the properties of Context-Free
Languages? - Context-free Languages are closed under union.
- Proof Let G1 be a grammar for L1 and G2 a
grammar for L2. We can modify one of these to
ensure that they have no variables in common
without affecting the language. Change the start
symbol of G1 to S1, and the start symbol of G2 to
S2. Add S ? S1 S2. Voila!
28Context-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 gt 0, m gt 0
- L2 anbmcm, n gt 0, m gt 0
- L1 ? L2 anbncn, n gt 0. We will shortly show
that this language is not context-free.
29Context-free Languages
- Review
- Context-free languages
- What kinds of languages are beyond the scope of
context-free languages? - anbncn
- ww
- anbmcnm
30Homework
- 8.1/3, 7c and d, 88.2/12, 18
- You are NOT responsible for any material
regarding linear languages.
31Where 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.
32Turing 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!
33Turing machines
"guts" of the machine
on/off switch
accept or reject
tape read head
input tape
Remember our finite automaton?
34Turing machines
"guts" of the machine
on/off switch
accept or reject
tape head can move left or right.
tape read head
input tape
tape head can read or write!
A Turing machine
35Turing 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!
36Turing 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?
37Turing machines
- Can it accept the language anbn? How?
- Lay out a strategy
- Let's mark off each a and matching b with an X
- Let's work with the a's and b's from left to
right - Whenever we mark off an a, move right until we
find a b. Mark off the b and head back left
looking for the left-most remaining a. Repeat as
long as you can. - If there are no more a's, go to the right and
check that there are no more b's. - Let the machine die if there is no matching b for
an a.
38Turing machines
L anbn, n 0
a, a, R
X, X, R
a, X, R
X, X, R
X, X, L
?, ?, R
b, b, L
X, X, R
a, a, L
?, ?, R
?, ?, R
a, a, L
X, X, R
39Turing machines
- Can it accept the language wcwR? How?
- Lay out a strategy
- Let's find the center c.
- 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 center c, there are no more unmarked
symbols on the left, make sure there are no more
on the right as well.
40Turing machines
X,X,R
X,X,L
X,X,R
X,X,L
a,X,L
c,c,R
a,X,R
c,c,L
c,c,L
c,c,L
b,X,R
?,?,R
c,c,R
a,a,R b,b,R
b,X,L
c,c,R
X,X,R
X,X,L
?,?,R
X,X,R
X,X,R
X,X,R
41Turing 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.
42Turing machines
- How about anbncn?
- YES!!!!!!!!!!!
- consider our first machine and how it can be
extended to handle matching cs as well. - Turing machines are more powerful than pushdown
automata!
43Turing 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.
44Turing 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!!!