Title: Pushdown Automata and ContextFree Languages
1Pushdown Automata and Context-Free Languages
- Jianhua Yang
- Department of Math and Computer Science
- Bennett College
2Goals
- Pushdown automata
- Context-free grammars
- The limits of PDA
- LL(k) parsers
- LR(k) parsers
32.1 Pushdown Automata (PDA)
- How to recognize L?
- Lxnyn n?N
41. Definition of PDA
Input tape
Head moves in this direction
tape head
1
8
2
7
3
4
6
5
Control mechanism
5Read a symbol by a PDA
- Following the sequence
- Read a symbol from the input
- Pop a symbol from the stack
- Push a symbol into the stack
- Generate a new state.
6Examples
7PDA transition diagram
y, x ?
y, x ?
2
3
?, ?
?, ?
x, ? x
8Formal definition
- P(S, ?,G,T, i, F )
- S finite collection of states.
- ? is the machines alphabet.
- G is the finite collection of stack symbols.
- T is a finite collection of transitions.
- i (an element of S) is the initial state.
- F is the collection of accept states.
92. Pushdown automata as Language Accepters
- How to accept a language?
- L(M) denotes all the strings accepted by PDA M.
- The languages accepted by PDA include the regular
languages.
Why? Think about the case (p, x, ? q, ?).
10Example
- Design a PDA to accept language Lxnyn n?N
2
3
11One exception
- A string might be accepted by a PDA but without
emptying its stack.
x,?x
y,?x
y,?x
1
2
12Theorem 2.1
- For each PDA that accepts strings without
emptying its stack, there is a PDA that accepts
the same language but empties its stack before
reaching an accept state.
13- For this part, please read the book.
142.2 Context-free Grammars
- The question is what languages can be recognized
by a PDA.
151. Definition of CFG
- Definition
- Context-free
- Reflects the fact that since the left side of
each rewrite rule can contain only a single
nonterminal, the rule can be applied regardless
of the context in which that nonterminal is
found.
Left side of each rule must be a single
nonterminal, there are no restrictions on the
right side of each rule.
16Examples
- S?zMNz
- M?aMa
- M?z
- N?bNb
- N?z
- Context-free?
17Derivations
- Leftmost derivation
- Rightmost derivation
18Context-free Languages
- The languages generated by context-free grammars
are called context-free languages (CFL)
Lxnynn?N
S?xSy S??
192. CFG and PDA
- For convenience,
- We take the notation (p, a, s q, xyz) to
represent push more than one symbol on the stack. - It equals to (p, a, s q1, z), (q1, ?, ? q2, y),
and (q2, ?, ? q, x)
20Equivalence between CFG and PDA
- We need to show
- For any CFG G, there is a PDA M, such that
L(M)L(G) (Theorem 2.2) - And for any PDA M, there is a CFG G such that
L(G)L(M). (Theorem 2.3)
21Theorem 2.2
- For each CFG G, there is a pushdown automaton M
such that L(G)L(M).
22Proof
- Given G, we construct M as follows
- 1. ?terminals of G
- Gterminals, nonterminals,
- 2. Assume M has states i, p, q, f.
- i initial state, f accept state
- 3. Introduce the transition (i, ?,? p, )
- 4. Introduce a transition (p, ?,? q, S) where S
is the start symbol in G.
23Proof (Cont.)
- 5. Introduce a transition of the form (q,?, N
q, w) for each rule N?w - 6. Introduce a transition of the form (q, x, x
q,?) for each terminal x in G. - 7. Introduce the transition
- (q,?, f,?)
24Example
S?xMNz M?aMa M?z N?bNb N?z
25The designed PDA M
?, ? S
q
p
?, ?
?, ?
?, S zMNz
?, M aMa
i
z, z ?
?, M z
b, b ?
?, N bNb
a, a ?
?, N z
26M accepts zazabzbz
27Theorem 2.3
- For each PDA M, there is a CFG G such that
L(M)L(G).
28Proof
- The idea of prove this is that for each PDA, we
can design a CFG, and vise versa.
292.3 The limits of PDA
- Are there any languages that are not context-free?
30Pumping lemma
- Theorem 2.5 If L is a CFG containing infinitely
many strings, then there must be a string in L of
the form svuwt, where s, v, u, w, t are
substrings, at least one of v and w is nonempty,
and svnuwnt is in L for each n? N.
31Proof
32Example
- To prove that the language xnynzn n ?N is not
CFL by using pumping lemma.
33Deterministic PDA
- It is a PDA in which one and only one transition
is applicable at any time.
34Definition
- We define a deterministic PDA to be a pushdown
automata (S,?,G T, i, F ) such that for each
triple (p, x, y) in S x ? x G, there is one and
only one transition in T of the form (p, u, v q,
z), where (u, v) is in (x, y), (x, ?), (?,y),
(?, ?),, q is in S, and z is in G.
35Theorem 2.6
- There is a context-free language that is not the
language accepted by any deterministic pushdown
automata.
36Proof
372.4 Summary
- Pushdown Automata
- Context-free Languages
- Context-free grammar
38The hierarchy of languages introduced so far