L={a b , i>=1}

advertisement
Lec 18 : Computation Theory
Examples of PDA
1
First example
L={aibi, i>=1}
Basic idea for building a PDA:
- read chars “a” from the tape until you reah the “b”.
- as you read chars “a” push them on the stack.
- when you read “b” , check the top stack if “a” then pop it
and move to the new state
- read chars “b” from the tape and check the top stack, if “a”
then pop and stay at the same state, until the tape is empty
and on the stack “z0” go to final state
Continuing first Example
L={aibi, i>=1}
b,a / 
a,z0 / az0
a,a / aa
q0
b,a / 
q0
Describe transition function:
δ ( q 0 , a , Zo )  ( q 0 , aZo )
δ(q0 , a , a )  ( q 0 , a0 )
δ ( q 0 , b , a )  ( q1 , )
q1
,z0 / z0
q2
Lec 18 : Computation Theory
Examples of PDA
2
δ ( q1 , b , a )  ( q1 , )
δ(q1 ,  , Zo)  (q 2 , Zo)
Transition function for aabb
(q0 , aabb, z 0 )  (q0 , abb, az 0 ) push a
 (q0 , bb, aa)
push a
 (q1 , b, a)
pop a
 (q1 ,, z 0 )
pop a
 (q 2 ,, z 0 )
accept
Transition function for abb
(q0 , aab, z 0 )  (q0 , bb, az 0 ) push a
 (q1 , b, z 0 )
pop a
 (q1 , b, z 0 )
Reject
Second example:
r
*
L = { xcx | x { a,b } }
-Basic idea for building a PDA:
- read chars from the tape until you reach the “c” (marker).
- as you read chars push them on the stack.
- after reading the “c”, match the chars read with the chars on
the stack, if match then pop char from stack, until all chars
read.
-if at any point the char read does not match the char popped,
the machine “fails”.
Lec 18 : Computation Theory
Examples of PDA
3
Continuing our example:
r
*
L = { xcx | x { a,b } }
The PDA will have 4 states
• State
0 (initial) : reading before the „c‟
• State 1: read the „c‟
• State 2 :read after „c‟, comparing chars
• State 3: (accepting): move only after all chars read and stack
empty .



Describe transition function:
δ(q0 , a, z 0 )  (q0 , az 0 )
δ(q0 , b, z 0 )  (q0 , bz 0 )
δ(q0 , a, a)  (q0 , aa)
δ(q0 , b, b)  (q0 , bb)
δ(q0 , a, b)  (q0 , ab)
δ(q0 , b, a)  (q0 , ba)
Lec 18 : Computation Theory
δ(q0 , c,z 0 )  (q1 , z 0 )
δ(q1 , c,a)  (q1 , a)
δ(q0 , c,b)  (q1 , b)
δ(q1 ,, z 0 )  (q 2 , z 0 )
δ(q1 ,, a)  (q 2 , a)
δ(q1 ,, b)  (q 2 , b)
δ(q2 , b, b)  (q 2 ,)
δ(q2 , a, a)  (q 2 ,)
δ(q2 ,, z 0 )  (q3 , z0 )
Transition for abcba
(q0,abcba, z0)  (q0, bcba,az0) push a
 (q0, cba, ba)
push b
 (q1, ba, ba)
goto 2
 (q2, ba, a)
pop b
 (q2, a, z0)
pop a
 (q2, , z0)
Accept
Transition for abcb
(q0, abcba, z0)  (q0 ,bcb, az0) push a
 (q0, cb , ba) push b
 (q1, b,ba)
goto 2
 (q2, b,ba)
 (q2, ,a)
pop b
reject!
Examples of PDA
4
Lec 18 : Computation Theory
Examples of PDA
5
Another Example:
r
*
– L = { xx | x { a,b } }
Basic idea for building a PDA
Much like last example, except
- this time we don’t know when to start popping and
comparing
- since PDAs are non-deterministic, this is not a
problem
– The PDA will have 3 states
• State 0 (initial) : reading before the center of
string
• State 1: read after center of string, comparing
chars
• State 2 (accepting): after all chars read, stack
should be empty
– The machine can choose to go from state 0 to state 1 at
any
time:
• Will result in many “wrong” set of moves
• All you need is one “right” set of moves for a string to
be accepted.
Lec 18 : Computation Theory
Examples of PDA



Describe transition function:
δ(q0 , a, z 0 )  (q0 , az 0 )
δ(q0 , b, z 0 )  (q0 , bz 0 )
δ(q0 , a, a)  (q0 , aa)
δ(q0 , b, b)  (q0 , bb)
δ(q0 , a, b)  (q0 , ab)
δ(q0 , b, a)  (q0 , ba)
δ(q0 , , z 0 )  (q1 , z 0 )
δ(q1 , , a)  (q1 , a)
δ(q0 , , b)  (q1 , b)
δ(q1 , b, b)  (q1 ,)
δ(q1 , a, a)  (q1 ,)
6
Lec 18 : Computation Theory
Examples of PDA
δ(q1 ,, z 0 )  (q2 , z0 )
Let’s see a bad transition set for abba
– (q0, abba, Z) (q0, bba, a) // push a
– (q0, ba, ba) // push b
– (q0, a, bba) // push b
– (q1, a, bba) // transition
– Nowhere to go // Reject!
Let’s see a good transition set for abba
– (q0, abba, Z) (q0, bba, a) // push a
– (q0, ba, ba) // push b
– (q1, ba, ba) // transition
(q1, a, a) // pop b
– (q1, , Z) // pop a
– (q2, , Z) // Accept!
H.W
1-Build the PDAs that accept these languages:
a- L={ x  {a, b}* | na(x)>nb(x)}
b- L={0n1m, n,m  1,m=2n}
2- Formalize the PDA that accept the language
L={aibj, i>j  1}
7
Download