Pushdown Automata and ContextFree Languages - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Pushdown Automata and ContextFree Languages

Description:

Department of Math and Computer Science. Bennett College. Goals ... G is the finite collection of stack symbols. T is a finite collection of transitions. ... – PowerPoint PPT presentation

Number of Views:189
Avg rating:3.0/5.0
Slides: 39
Provided by: jhy9
Category:

less

Transcript and Presenter's Notes

Title: Pushdown Automata and ContextFree Languages


1
Pushdown Automata and Context-Free Languages
  • Jianhua Yang
  • Department of Math and Computer Science
  • Bennett College

2
Goals
  • Pushdown automata
  • Context-free grammars
  • The limits of PDA
  • LL(k) parsers
  • LR(k) parsers

3
2.1 Pushdown Automata (PDA)
  • How to recognize L?
  • Lxnyn n?N

4
1. Definition of PDA
Input tape
Head moves in this direction
tape head
1
8
2
7
3
4
6
5
Control mechanism
5
Read 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.

6
Examples
  • (p, x, s q, y)
  • (p, ?, ? q, ?)
  • (p, ?, s q, ?)
  • (p, x, ? q, z)

7
PDA transition diagram
y, x ?
y, x ?
2
3
?, ?
?, ?
x, ? x
8
Formal 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.

9
2. 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, ?).
10
Example
  • Design a PDA to accept language Lxnyn n?N

2
3
11
One exception
  • A string might be accepted by a PDA but without
    emptying its stack.

x,?x
y,?x
y,?x
1
2
  • Lxmyn m,n?N, mn

12
Theorem 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.

14
2.2 Context-free Grammars
  • The question is what languages can be recognized
    by a PDA.

15
1. 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.
16
Examples
  • xNy?xzy Context-free?
  • S?zMNz
  • M?aMa
  • M?z
  • N?bNb
  • N?z
  • Context-free?

17
Derivations
  • Leftmost derivation
  • Rightmost derivation

18
Context-free Languages
  • The languages generated by context-free grammars
    are called context-free languages (CFL)

Lxnynn?N
S?xSy S??
19
2. 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)

20
Equivalence 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)

21
Theorem 2.2
  • For each CFG G, there is a pushdown automaton M
    such that L(G)L(M).

22
Proof
  • 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.

23
Proof (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,?)

24
Example
  • We have a CFG G

S?xMNz M?aMa M?z N?bNb N?z
25
The designed PDA M
?, ? S
q
p
?, ?
?, ?
?, S zMNz
?, M aMa
i
z, z ?
?, M z
b, b ?
?, N bNb
a, a ?
?, N z
26
M accepts zazabzbz
27
Theorem 2.3
  • For each PDA M, there is a CFG G such that
    L(M)L(G).

28
Proof
  • The idea of prove this is that for each PDA, we
    can design a CFG, and vise versa.

29
2.3 The limits of PDA
  • Are there any languages that are not context-free?

30
Pumping 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.

31
Proof
  • skip

32
Example
  • To prove that the language xnynzn n ?N is not
    CFL by using pumping lemma.

33
Deterministic PDA
  • It is a PDA in which one and only one transition
    is applicable at any time.

34
Definition
  • 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.

35
Theorem 2.6
  • There is a context-free language that is not the
    language accepted by any deterministic pushdown
    automata.

36
Proof
  • Self study.

37
2.4 Summary
  • Pushdown Automata
  • Context-free Languages
  • Context-free grammar

38
The hierarchy of languages introduced so far
Write a Comment
User Comments (0)
About PowerShow.com