Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton...

17

Click here to load reader

Transcript of Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton...

Page 1: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

Additional NPDA examplesAdditional NPDA examples

(From Linz 7.1 p. 183 Exercise 4.)

Page 2: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

Formal DefinitionNon-Deterministic Pushdown Automaton NPDA

)F,z,q,,,,Q(M 0δΓΣ=Final

Set of statesFinalstates

Inputalphabet Stack

startInitialstate

Stackalphabet

Transitionfunction

symbolstate

2

Page 3: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

Instantaneous DescriptionInstantaneous Description

)( suq ),,( suq

Currentstate Remaining

input

Currentstackcontentsinput contents

3

Page 4: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

1. Construct NPDA  that accepts the language L { R { b}} ith Σ { b }L = {wcwR : w  {a, b}} with Σ = {a, b, c}

The idea of machine construction*:

We construct M  that accepts  L. It reads w and after seeing c, it reads wR.We will need three states, for reading w, switching to the new state after 

di d th fi l ti t treading c, and the final accepting state.

First for each a or b seen we push an a or b respectively onto the stack  and stay  in state  q0. When  we encounter  a c, we do not push anything  on to the stack,  but go to the next  state,  q1. While being in state  q1 , if we encounter an a or b, we match it  with  the  a or b respectively  on the  stack  and  pop  one a for each a or one b for each b seen, so that it matches  wR

against the contents of the stack and the matching starts after it encountersagainst  the contents  of the stack and the matching  starts after it encounters  a c. Finally  it accepts  if the input  is done and there  are no more a’s or b’s on the stack.

*This is a variation on the example 7.5 on the p. 181 of Linz book, for the language  L = {wwR : w  {a, b}}

Page 5: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

NPDA

{ }{ }*R b,awwcwL ∈=

λ/bba/a λλλ

/a,a/b,b

b/,ba/,a

λλ

c, λ / λq q qz/z,λq0 q1 q2

5

Page 6: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

Instantaneous description of the automaton

Here is the machine  definition.

Instantaneous description of the automatonaccepting L = {wcwR : w  {a, b}}   Σ = {a, b, c} 

M   = ({q0 , q1, q2}, Σ, {a, b, z}, δ, q0, z, {q2})  

Instantaneous description (ID): (current state, remaining input, stack)

δ(q0 , a, a)  = {(q0 , aa)}, δ(q0, b, a)  = {(q0, ba)},  δ(q0, a, b)  = {(q0 , ab)},  δ( 0 b b) {( 0 bb)}

aa/a,aa/,a λ

δ(q0 , b, b)  = {(q0, bb)},  δ(q0, a, z)  = {(q0 , az)},  δ(q0, b, z)  = {(q0, bz)}, δ(q0, c, z) = {(q1, z)},  λ

λ/a,a/b,b

bb/b,bab/b,ab/,b λ

(q ) (q )δ(q0, c, a) = {(q1, a)}, δ(q0, c, b) = {(q1 , b)}, δ(q1, a, a) = {(q1, λ)},δ(q1 b b) = {(q1 λ)}

c, λ / λq0 q1 q2

z/z,λ

δ(q1 , b, b) = {(q1, λ)}, δ(q1, λ, z) = {(q2 , z)}.

Page 7: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

2 Construct NPDA that accepts the language2. Construct NPDA that accepts the  language L = {an bm cn+m : n ≥ 0, m ≥ 0} with Σ = {a, b, c}

The idea of machine construction:

We construct M that accepts L First it accepts the empty string (n=m=0)We construct M that  accepts L. First it accepts  the  empty string (n=m=0). Further,  for each a seen (if any)  it pushes an a onto the stack.  Then  it pushes an a onto the stack  for each b seen (if any).  Thus  after  reading all as and bs in the input,  the stack  has na (w) + nb (w) as on it.  It then  pops one a for each c seen guaranteeing that na (w) + nb (w) = nc(w)  and they  are in the right order. Finally  it accepts  if the input  is done and there  are no more as on the stackstack.

M  = ({q0, q1, q2, q3 , q4}, Σ, {a, z}, δ, q0, z, {q0, q4 })

Page 8: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

NPDA

L = anbmbn+m : n ≥ 0,m ≥ 0{ }

c a / λa λ / a b λ / a c, a / λa, λ / a

b, λ / a

λ, λ / λq0 q1 q3

z/z,λq2

λ, λ / λ

8

Page 9: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

Instantaneous description of the automatonaccepting L = {an bm cn+m : n ≥ 0, m ≥ 0} Σ = {a, b, c} Machine definition:M  = ({q0, q1, q2, q3 , q4}, Σ, {a, z}, δ, q0, z, {q0, q4 }) 

δ(q0 , λ, z) = {(q1, z)}, δ(q1 , a, z) = {(q1 , az)},

c, a / λa, λ / a b, λ / a

Instantaneous description (ID): (current state, remaining input, stack)

δ(q1 , a, z)   {(q1 , az)}, δ(q1 , a, a) = {(q1 , aa)}, δ(q1, λ, a) = {(q2, a)}, δ(q1, λ, z) = {(q2 , z)}, δ( 2 b ) {( 2 )}

λ, λ / λq0 q1 q3

z/z,λq2

λ, λ / λδ(q2, b, z) = {(q2, az)},δ(q2 , b, a) = {(q2, aa)}, δ(q2, c, a) = {(q3, λ)}, δ(q3, c, a) = {(q3, λ)}, 

q0 q1 q2

(q ) (q )δ(q3, λ, z) = {(q4, λ)}.

Page 10: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

3. Construct NPDA that accepts the language L = {an bm : n ≤ m ≤ 3n, n≥0} with Σ = {a, b, c}.

The idea of machine construction:The idea of machine construction:

We will construct M that accepts L.  It accepts empty string.  The state q0 keeps track  of the n as encountered in the first part  of the string  and provides a chance for state  q1 to match  the bs to anywhere  between n and 3n.  It proceeds to state  q2 from state  q1 if the number  of bsencountered is between n and 3n.

Page 11: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

NPDA

{ }03 ≥≤≤= n,nmn:baL mn

λ/aba/,a λ

λ/a,b

aaa/,aaa/,a

λλ

λ/a,bq0 q1 q2z/z,λ

11

Page 12: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

Instantaneous description of the automaton

Machine definition:

accepting L = {an bm : n ≤ m ≤ 3n, n≥0} with Σ = {a, b, c}.

Machine definition:M   = ({q0, q1, q2 }, Σ, {a, b, z}, δ, q0 , z, {q2 })

Instantaneous description (ID): (current state remaining input stack)

δ(q0, a, z)  = {(q0, az)},  δ(q0 , a, z)  = {(q0, aaz)}, δ(q0 a z) = {(q0 aaaz)}

Instantaneous description (ID): (current state, remaining input, stack)

δ(q0, a, z) = {(q0, aaaz)}, δ(q0 , a, a) = {(q0, aa)}, δ(q0, a, a) = {(q0 , aaa)}, δ(q0, a, a) = {(q0, aaaa)}, 

λ/a,b

aaa/,aaa/,aa/,a

λλλ

aaa/a,aaa/a,aa/a,a

δ(q0 , b, a) = {(q1, λ)}, δ(q1 , b, a) = {(q1, λ)}, δ(q1, λ, z) = {(q2 , z)}

λ/a,bq0 q1 q2z/z,λ

Page 13: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

4. Construct NPDA that accepts the language L = {w : na (w) = nb (w) + 1} with Σ = {a, b, c}.

{ }{ }h d f h

{ }{ }*b,aw,)w(n)w(n:wL ba ∈+== 1

The idea of machine construction:

Machine M  accepts L and it starts with one extra  a on the stack and then uses the machine given in Example  7.4 in the Linz book  to tell if the number of as g pand bs are the same.  If they are and we started with one more a on the stack  then  the condition of L is met.  Additionally, if it sees a c, it just reads it and does not modify the stack.

Page 14: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

We start with a simpler machine that accepts the language:

{ }{ })w(n)w(n:b,awL ba* =∈= { }{ })()( ba

Here we use Linz solution. He uses 0 to count a’s which will be poppedwhen b’s are found As sometimes in some prefix there may be more b’swhen b s are found. As sometimes in some prefix there may be more b sthan a’s we will find no 0 on the stack. In that case 1 is used that later on can be matched with a’s.

Page 15: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

NPDA for a simpler machine

{ }{ })w(n)w(n:bawL b* =∈= { }{ })w(n)w(n:b,awL ba∈

/bλ/,bz/z,b

01

0z/z,a λ/,a/,b

1111

000 /,a

z/z,λq0 fq

Page 16: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

NPDA

{ }{ }1+=∈= )w(n)w(n:bawL b*{ }{ }1+∈ )w(n)w(n:b,awL ba

/bλ/,bz/z,b

01

0z/z,a λ/,a/,b

1111

q

000 /,a

z/z,λa/,λλ1q 2qq0

,

Page 17: Additional NPDA examples - MDH of NP… · Formal Definition Non-Deterministic Pushdown Automaton NPDA M =(Q,Σ,Γ,δ,q 0 ,z,F) Final Set of states states Input alphabet Stack start

Instantaneous description of the automaton

M   = ({q0, q1, q2}, Σ, {a, b, z}, δ, q0, z, {q2})

accepting L = {w : na (w) = nb (w) + 1} with Σ = {a, b, c}.

δ(q0, λ, z)  = {(q1 , az)},  

Instantaneous description (ID): (current state, remaining input, stack)

(q , , ) {(q , )},δ(q1 , a, z)  = {(q1, az)},  δ(q1 , a, a)  = {(q1 , aa)},  δ(q1 , a, b)  = {(q1, λ)},  δ(q1 b ) {(q1 b )}

λ/,bz/z,b

01

0z/z,a λ/,a/,b

1111

δ(q1, b, z)  = {(q1, bz)},  δ(q1, b, a)  = {(q1 , λ)}, δ(q1, b, b) = {(q1, bb)}, δ(q1, λ, z) = {(q2, λ)},  1q 2q

000 /,a

z/z,λ

λ/,a

q0a/,λλ

δ(q1, c, z) = {(q1 , c)}, δ(q1, c, a) = {(qa , a)},δ(q1, c, b) = {(q1, b)}.

q0

In this solution a and b are used as counters instead of 0 and 1.