meljun cortes -- automata theory lecture - 10

Upload: meljun-cortes-mbampa

Post on 04-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    1/23

    CSC 3130: Automata theory and formal languages

    Andrej Bogdanov

    http://www.cse.cuhk.edu.hk/~andrejb/csc3130

    Pushdown automata

    Fall 2008

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    2/23

    Motivation

    We had two ways to describe regular languages

    How about context-free languages?

    regular

    expressionDFANFA

    syntactic computational

    CFG pushdown automaton

    syntactic computational

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    3/23

    Pushdown automata versus NFA

    Since context-free is more powerful than regular,pushdown automata must generalize NFAs

    state control0 1 0 0

    input

    NFA

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    4/23

    Pushdown automata

    A pushdown automaton has access to a stack,which is a potentially infinite supply of memory

    state control0 1 0 0

    input

    pushdown automaton (PDA)

    stack

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    5/23

    Pushdown automata

    As the PDA is reading the input, it can push / popsymbols in / out of the stack

    state control0 1 0 0

    input

    pushdown automaton (PDA)

    Z0 0 1

    stack

    1

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    6/23

    Rules for pushdown automata

    The transitions are nondeterministic Stack is always accessed from the top

    Each transition can pop a symbol from the stack

    and / orpush another symbol onto the stack Transitions depend on input symbol and on last

    symbol popped from stack

    Automaton accepts if after reading whole input, it

    can reach an accepting state

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    7/23

    Example

    L= {0n#1n: n 0}state control

    0 0 0 #

    input

    Z0 a a

    stack

    a

    1 1 1

    read 0push a

    read #

    read 1pop a

    pop Z0

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    8/23

    Shorthand notation

    read 0push a

    read #

    read 1pop a

    pop Z0

    0, e / a

    #, e / e

    e, Z0 / e

    1, a / e

    read, pop / push

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    9/23

    Formal definition

    A pushdown automaton is (Q, ,, , q0, Z0, F): Q is a finite set ofstates;

    is the input alphabet;

    is the stack alphabet, including a special symbol Z0;

    q0in Q is the initial state;

    Z0in is the start symbol;

    F Q is a set of final states;

    is the transition function

    : Q ( {e}) ( {e}) subsets of Q ( {e})

    state input symbol pop symbol state push symbol

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    10/23

    Notes on definition

    We use slightly different definition than textbook Example

    0, e / a

    #, e / e e, Z0 / e

    1, a / e

    : Q ( {e}) ( {e}) subsets of Q ( {e})

    q0 q1 q2

    (q0, 0, e) = {(q0, a)}(q0, 1, e) =

    (q0, #, e) = {(q1, e)}

    (q0, 0, a) = ...

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    11/23

    A convention

    Sometimes we denote transitions by:

    This will mean:

    Intuitively, pop b, then push c1, c2, and c3

    a, b/ c1c2c3q0 q1

    e, e / c2

    q0 q1a, b/ c1 e, e / c3

    intermediatestates

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    12/23

    Examples

    Describe PDAs for the following languages: L = {w#wR: w *}, = {0, 1, #}

    L = {wwR: w *}, = {0, 1}

    L = {w: whas same number of 0s and 1s}, = {0, 1}

    L = {0i1j: ij 2i}, = {0, 1}

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    13/23

    Main theorem

    A language Lis context-free if and only if it

    is accepted by some pushdown automaton.

    context-free grammar pushdown automaton

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    14/23

    From CFGs to PDAs

    Idea: Use PDA to simulate (rightmost) derivationsA 0A1

    A B

    B #

    A 0A1 00A11 00B11 00#11

    PDA control:

    CFG:

    write start variable

    stack:

    Z0A

    replace production in reverse Z01A0

    pop terminals and match Z01A

    e, e / A

    0, 0 / e

    e, A / 1A0

    input:

    00#11

    00#11

    0#11

    replace production in reverse Z011A0e, A / 1A0 0#11

    pop terminals and match Z011A0, 0 / e #11

    replace production in reverse Z011Be, A / B #11

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    15/23

    From CFGs to PDAs

    If, after reading whole input, PDA ends up with anempty stack, derivation must be valid

    Conversely, if there is no valid derivation, PDAwill get stuck somewhere

    Either unable to match next input symbol,

    Or match whole input but stack non empty

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    16/23

    Description of PDA for CFGs

    Repeat the following steps: If the top of the stack is a variableA:

    Choose a ruleA a and substituteA with a

    If the top of the stack is a terminal a:

    Read next input symbol and compare to aIf they dont match, reject (die)

    If top of stack is Z0, go to accept state

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    17/23

    Description of PDA for CFGs

    q0 q1 q2e, e / S

    a, a / efor every terminal a

    e, A / ak...a1for every productionA a1...ak

    e, Z0 / e

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    18/23

    From PDAs to CFGs

    First, we simplify the PDA:

    It has a single accept stateqf Z0 is always popped exactly before accepting

    Each transition is either a push, or a pop, but not both

    context-free grammar pushdown automaton

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    19/23

    From PDAs to CFGs

    We look at the stack in an acceptingcomputation:

    a

    Z0 Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0 Z0

    b a c c c

    a

    portions that preserve the stack

    q0 q1 q3 q1 q7 q0 q1 q2 q1 q3 q7

    A03 = {x: xleads from q0 to q3 and preserves stack}

    e 1 1 e 0 1 e e 0 00input

    state

    stack

    qf

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    20/23

    From PDAs to CFGs

    a

    Z0 Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0 Z0

    b a c c c

    a

    q0 q1 q3 q1 q7 q1 q2 q1 q7e 1 1 e 0 1 e e 0 00input

    state

    stack

    A11

    A03

    0 eA11 0A03e

    q0 q3 qf

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    21/23

    From PDAs to CFGs

    a

    Z0 Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0

    a

    Z0 Z0

    b a c c c

    a

    q0 q1 q3 q1 q7 q1 q2 q1 q7e 1 1 e 0 1 e e 0 00input

    state

    stack

    A03

    A13

    A13 A10A03

    q0 q3

    A10

    qf

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    22/23

    From PDAs to CFGs

    qiqj

    qi

    qj Aij aAijb

    qi qj qk Aik AijAjk

    qi Aii e

    variables:Aij

    qfa, Z0 /eqi A0f A0ia

    start variable:A0f

  • 7/31/2019 MELJUN CORTES -- AUTOMATA THEORY LECTURE - 10

    23/23

    Example

    0, e / a

    #, e / e e, Z0 / e

    1, a / e

    q0 q1 q2

    start variable: A02productions:

    A00 A00A00A00 A01A10

    A00 A03A30A01 A01A11A01 A02A21

    A00 e

    ...

    A11 eA

    22

    e

    A01 0A011A01 #A33

    A33 e

    0, e / a

    #, e / $ e, Z0 / e

    1, a / e

    q0 q1 q2q3e, $ / e

    A02 A01