Title: COSC 341 Lecture 11 Pushdown Automata
1COSC 341 Lecture 11 Pushdown Automata
- Context-free grammars A ? bBcC
- Regular languages are context-free, but some
context-free languages are not regular - Palindrome has the context-free grammar
- S ? aSa ? bSb ? a ? b ? ?.
- anbn n 0 has the CFG
- S ? aSb ? ?.
- We can design machines to accept the context-free
languages. - But there are languages that are not context-free.
2Pushdown automata (PDA)
- PDA NFA stack for memory
- PDAs may be non-deterministic - those that are
deterministic accept only some of the
context-free languages. - Need finite set Q of states, input alphabet ?, qo
in Q, F ? Q, a stack alphabet ?, and a
transition function ? Q?(???)?(???)
??(Q?(???)). - What does it mean if
- qn, B ? ?(qm, a, A)?
- It means that if the state is qm and the reading
head sees input letter a while A is at the top of
the stack, then the machine should - change the state from qm to qn
- process input letter a
- pop A off the stack
- push B onto the stack.
3The role of ?
- What does qk, ? ? ?(qm, a, A) mean? Change
from qm to qk, process a, pop A, and dont push
anything onto the stack. - What does qk, B ? ?(qm, a, ?) mean? Change
from qm to qk, process a, dont pop anything from
the stack, and push B on. - What does qk, B ? ?(qm, ?, A) mean? Change
from qm to qk, dont process anything from input
string (and so dont move reading head to next
square on right), pop A off and push B onto
stack. - What does qk, ? ? ?(qm, ?, ?) mean? Just
change from qm to qk....!
4Deterministic PDAs
- The string w is accepted if there is a
computation from q0, w, ? that ends in qi, ?,
? with qi ? F. - L anbn n ? o is accepted by PDA
Note this is a deterministic PDA (though not
total).
- Palindrome the set of strings over a, b,
of the form wz where z is w spelled
backward and w? a, b, e.g. abba,
aabaabaa, ...
(Note no choices, so deterministic PDA.)
5Non-deterministic PDAs
- The set of strings over a, b of even length
and spelled the same forwards and backwards.
(We need a non-deterministic transition from q0
to q1 because unlike Palindrome strings have no
middle marker telling machine when to change
state.)
b A/ ?
a ?/A
b A/ ?
? ?/?
? A/?
q2
6The pumping lemma for PDA
- Suppose L is context-free. There is a number k
(which depends on L) such that if z ? L with
length(z) k, then z can be written uvwxy, with - length (vwx) k
- length(v) length(x) gt 0
- uvnwxny ? L for all n 0.
- Why? Let G be a grammar for L and let m be the
maximum number of symbols on the right of a rule. - A node in a parse tree has m children, so a
parse tree of depth d produces a string of length
md. - Pick any string of length gt mj where G has j
non-terminals. A parse tree for that string has
depth gt j so some nonterminal A is used twice
along the same path. Suppose the subtree
generated by the first occurrence of A produces
vAx. We may use A again to produce vvAxx from
vAx! This pumps v and x, which are not both empty
(if we assume the grammar to be in Chomsky Normal
Form).
7Application
L anbncn ? n 0 is not context-free. Suppos
e L is context-free let k be the number in the
pumping lemma. Let z akbkck. We show that, no
matter what choices are made for u, v, w, x, and
y, the string uv2wx2y ? L. Fact every string in
L has form anbncn and so has only one substring
ab only one substring bc no substrings ac, or
ba, or ca, or cb. Either v and x are solid blocks
of one letter (or ?) or not. If they are solid
blocks, then one or two of a, b, c will be
increased in uv2wx2y while the remaining
letter(s) will not increase, so uv2wx2y ? L. If
they are not solid blocks, then uv2wx2y will have
more of the 2-letter substrings ab, ac, ba, bc,
ca, cb than it is supposed to.
8Properties of CF languages
- If L1, L2 are context-free, then L1?L2, L1L2 and
L1. - However, if L1 and L2 are context-free then it
is not necessarily so that L1?L2 is context-free,
nor that the complement of L1 is context-free.
Counterexample - L1 anbncm ? n, m 0
- L2 ambncn ? n, m 0
- L1 and L2 are both context-free with grammars
- G1 S?BC G2 S?AB
- B?aBb?? A?aA??
- C?cC?? B?bBc??
- But L1?L2 anbncn ? n 0 which we have just
proven not to be context free! - (If complement were context-free, then also
intersection - complement of the union of
complements - contradiction!) - (Exception if L1 is context-free and L2 is
regular, then L1?L2 is context-free. Proof
omitted.)