Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI3130: Formal Languages and Automata Theory Tutorial 4 Tutor: William Chow.

Similar presentations


Presentation on theme: "CSCI3130: Formal Languages and Automata Theory Tutorial 4 Tutor: William Chow."— Presentation transcript:

1 CSCI3130: Formal Languages and Automata Theory Tutorial 4 Tutor: William Chow

2 Pushdown Automata (PDA) Regular language  Finite Automata (FA) Context-free language  Pushdown Automata (PDA) 2 FA string Accept / Reject PDA string Accept / Reject

3 Pushdown Automata (PDA) PDA: NFA with stack M = FA ( Q, Σ, δ, q 0, F ) M = PDA ( Q, Σ, Γ, δ, q 0, Ζ 0, F ) 3 pushpop stack

4 Pushdown Automata (PDA) Difference in transitions: FA:δ (state, input) = {(state')} PDA:δ (state, input, stack) = {(state', stack')} Push example:δ(q 0, a, 0) = {(q 1, 00)} Pop example: δ(q 0, a, 0) = {(q 1, ε)} 4

5 Pushdown Automata (PDA) Acceptance state: FA:stay at any state in F (final state) PDA: F={} and stack is empty; or F≠{} and stay at any state in F 5

6 Example 1 L={wcw R | w  (a+b)*} 6

7 Example 1 – the PDA L={wcw R | w  (a+b)*} 7 q0q0 q1q1 a, 0/00 a, 1/01 a, S/0S b, 0/10 b, 1/11 b, S/1S a, 0/ε b, 1/ε ε, S/ε c, 0/0 c, 1/1 c, S/S if a, then push 0 if b, then push 1 if a, the pop must be 0 if b, the pop must be 1 at the end, the pop must be S δ(q 0, a, 0) = {(q 0, 00)} δ(q 0, a, 1) = {(q 0, 01)} δ(q 0, a, S) = {(q 0, 0S)} δ(q 0, b, 0) = {(q 0, 10)} δ(q 0, b, 1) = {(q 0, 11)} δ(q 0, b, S) = {(q 0, 1S)} δ(q 0, c, 0) = {(q 0, 0)} δ(q 0, c, 1) = {(q 0, 1)} δ(q 0, c, S) = {(q 0, S)} δ(q 1, a, 0) = {(q 1, ε)} δ(q 1, b, 1) = {(q 1, ε)} δ(q 1, ε, S) = {(q 1, ε)} δ(q 1, a, 1) = {} δ(q 1, a, S) = {} δ(q 1, b, 0) = {} δ(q 1, b, S) = {} M=({ q 0, q 1 }, { a, b, c }, { 0, 1, S }, δ, q 0, S, {}) QΣΓ F Z0Z0 δ(q 0, a, 0) = {(q 0, 00)}

8 Example 1 – How it works L={wcw R | w  (a+b)*} 8 δ(q 0, a, 0) = {(q 0, 00)} δ(q 0, a, 1) = {(q 0, 01)} δ(q 0, a, S) = {(q 0, 0S)} δ(q 0, b, 0) = {(q 0, 10)} δ(q 0, b, 1) = {(q 0, 11)} δ(q 0, b, S) = {(q 0, 1S)} δ(q 0, c, 0) = {(q 0, 0)} δ(q 0, c, 1) = {(q 0, 1)} δ(q 0, c, S) = {(q 0, S)} δ(q 1, a, 0) = {(q 1, ε)} δ(q 1, b, 1) = {(q 1, ε)} δ(q 1, ε, S) = {(q 1, ε)} (q 0, aabcbaa, S ) (q 0, abcbaa, 0S ) (q 0, bcbaa, 00S ) (q 0, cbaa, 100S ) (q 1, baa, 100S ) (q 1, aa, 00S ) (q 1, a, 0S ) (q 1, ε, S ) (q 1, ε, ε ) input : aabcbaa M=({ q 0, q 1 }, { a, b, c }, { 0, 1, S }, δ, q 0, S, {}) Current state Remaining input Current stack Instantaneous descriptions (ID)

9 Example 2 L={ww R | w  (a+b)*} 9

10 Example 2 L={ww R | w  (a+b)*} 10 q0q0 q1q1 a, 0/00 a, 1/01 a, S/0S b, 0/10 b, 1/11 b, S/1S ε, S/ε a, 0/ε b, 1/ε ε, S/ε a, 0/ε b, 1/ε δ(q 0, a, 0) = {(q 0, 00), (q 1, ε)} δ(q 0, a, 1) = {(q 0, 01)} δ(q 0, a, S) = {(q 0, 0S)} δ(q 0, b, 0) = {(q 0, 10)} δ(q 0, b, 1) = {(q 0, 11), (q 1, ε)} δ(q 0, b, S) = {(q 0, 1S)} δ(q 0, ε, S) = {(q 0, ε)} δ(q 1, a, 0) = {(q 1, ε)} δ(q 1, b, 1) = {(q 1, ε)} δ(q 1, ε, S) = {(q 1, ε)}

11 Example 2 L={ww R | w  (a+b)*} 11 δ(q 0, a, 0) = {(q 0, 00), (q 1, ε)} δ(q 0, a, 1) = {(q 0, 01)} δ(q 0, a, S) = {(q 0, 0S)} δ(q 0, b, 0) = {(q 0, 10)} δ(q 0, b, 1) = {(q 0, 11), (q 1, ε)} δ(q 0, b, S) = {(q 0, 1S)} δ(q 0, ε, S) = {(q 0, ε)} δ(q 1, a, 0) = {(q 1, ε)} δ(q 1, b, 1) = {(q 1, ε)} δ(q 1, ε, S) = {(q 1, ε)} (q 0, abba, S ) (q 0, bba, 0S ) (q 0, ba, 10S ) (q 0, a, 110S )(q 1, a, 0S ) (q 1, ε, 0110S )(q1, ε, S) (die)(q1, ε, ε) input : abba

12 Example 3 L={w | w=w R, w  (a+b)*} 12

13 Example 3 L={w | w=w R, w  (a+b)*} 13 q0q0 q1q1 a, 0/00 a, 1/01 a, S/0S b, 0/10 b, 1/11 b, S/1S ε, S/ε a, 0/ε b, 1/ε ε, S/ε a, 0/ε b, 1/ε a, 0/0 a, 1/1 b, 0/0 b, 1/1 δ(q 0, a, 0) = {(q 0, 00), (q 1, ε), (q 1, 0)} δ(q 0, a, 1) = {(q 0, 01), (q 1, 1)} δ(q 0, a, S) = {(q 0, 0S)} δ(q 0, b, 0) = {(q 0, 10), (q 1, 0)} δ(q 0, b, 1) = {(q 0, 11), (q 1, ε), (q 1, 1)} δ(q 0, b, S) = {(q 0, 1S)} δ(q 0, ε, S) = {(q 0, ε)} δ(q 1, a, 0) = {(q 1, ε)} δ(q 1, b, 1) = {(q 1, ε)} δ(q 1, ε, S) = {(q 1, ε)}

14 Example 3 L={w | w=w R, w  (a+b)*} 14 δ(q 0, a, 0) = {(q 0, 00), (q 1, ε), (q 1, 0)} δ(q 0, a, 1) = {(q 0, 01), (q 1, 1)} δ(q 0, a, S) = {(q 0, 0S)} δ(q 0, b, 0) = {(q 0, 10), (q 1, 0)} δ(q 0, b, 1) = {(q 0, 11), (q 1, ε), (q 1, 1)} δ(q 0, b, S) = {(q 0, 1S)} δ(q 0, ε, S) = {(q 0, ε)} δ(q 1, a, 0) = {(q 1, ε)} δ(q 1, b, 1) = {(q 1, ε)} δ(q 1, ε, S) = {(q 1, ε)} (q 0, ababa, S ) (q 0, baba, 0S ) (q 0, aba, 10S )(q1, aba, 0S)(die) (q 1, ba, 10S)(q 0, ba, 010S)(die) (q1, a, 0S) (q1, ε, S) (q1, ε, ε) input : ababa

15 Example 4 L={w | w has same number of a and b, w  (a+b)*} 15

16 Example 4 L={w | w has same number of a and b, w  (a+b)*} 16 q0q0 a, 0/00 a, 1/ε a, S/0S b, 0/ε b, 1/11 b, S/1S ε, S/ε δ(q 0, a, 0) = {(q 0, 00)} δ(q 0, a, 1) = {(q 0, ε)} δ(q 0, a, S) = {(q 0, 0S)} δ(q 0, b, 0) = {(q 0, ε)} δ(q 0, b, 1) = {(q 0, 11)} δ(q 0, b, S) = {(q 0, 1S)} δ(q 0, ε, S) = {(q 0, ε)} number of 0 in stack = max(0, number of a – number of b) number of 1 in stack = max(0, number of b – number of a)

17 Example 4 L={w | w has same number of a and b, w  (a+b)*} 17 δ(q 0, a, 0) = {(q 0, 00)} δ(q 0, a, 1) = {(q 0, ε)} δ(q 0, a, S) = {(q 0, 0S)} δ(q 0, b, 0) = {(q 0, ε)} δ(q 0, b, 1) = {(q 0, 11)} δ(q 0, b, S) = {(q 0, 1S)} δ(q 0, ε, S) = {(q 0, ε)} (q 0, abbbbabaaa, S ) (q 0, bbbbabaaa, 0S ) (q 0, bbbabaaa, S ) (q 0, bbabaaa, 1S ) (q 1, babaaa, 11S ) (q 1, abaaa, 111S ) (q 1, baaa, 11S ) (q 1, aaa, 111S ) (q 1, aa, 11S ) (q 1, a, 1S ) (q 1, ε, S ) (q 1, ε, ε ) input : abbbbabaaa

18 Example 5 L={w | w has (num. of a) less than (num. of b + num. of c), w  (a+b+c)*} 18

19 Example 5 L={w | w has (num. of a) less than (num. of b + num. of c), w  (a+b+c)*} 19 q0q0 a, 0/00 a, 1/ε a, S/0S b, 0/ε b, 1/11 b, S/1S c, 0/ε c, 1/11 c, S/1S δ(q 0, a, 0) = {(q 0, 00)} δ(q 0, a, 1) = {(q 0, ε)} δ(q 0, a, S) = {(q 0, 0S)} δ(q 0, b, 0) = {(q 0, ε)} δ(q 0, b, 1) = {(q 0, 11)} δ(q 0, b, S) = {(q 0, 1S)} δ(q 0, c, 0) = {(q 0, ε)} δ(q 0, c, 1) = {(q 0, 11)} δ(q 0, c, S) = {(q 0, 1S)} δ(q 0, ε, 1) = {(q 1, ε)} δ(q 1, ε, 1) = {(q 1, ε)} δ(q 1, ε, S) = {(q 1, ε)} Accept the string when there have 1 left in stack q1q1 ε, 1/ε ε, S/ε Basically the same as Example 4!

20 Example 5 L={w | w has (num. of a) less than (num. of b + num. of c), w  (a+b+c)*} 20 δ(q 0, a, 0) = {(q 0, 00)} δ(q 0, a, 1) = {(q 0, ε)} δ(q 0, a, S) = {(q 0, 0S)} δ(q 0, b, 0) = {(q 0, ε)} δ(q 0, b, 1) = {(q 0, 11)} δ(q 0, b, S) = {(q 0, 1S)} δ(q 0, c, 0) = {(q 0, ε)} δ(q 0, c, 1) = {(q 0, 11)} δ(q 0, c, S) = {(q 0, 1S)} δ(q 0, ε, 1) = {(q 1, ε)} δ(q 1, ε, 1) = {(q 1, ε)} δ(q 1, ε, S) = {(q 1, ε)} (q 0, abccbabaa, S ) (q 0, bccbabaa, 0S ) (q 0, ccbabaa, S ) (q 0, cbabaa, 1S ) (q 1, babaa, 11S ) (q 1, abaa, 111S ) (q 1, baa, 11S ) (q 1, aa, 111S ) (q 1, a, 11S ) (q 1, ε, 1S ) (q 1, ε, S ) (q 1, ε, ε ) input : abccbabaa


Download ppt "CSCI3130: Formal Languages and Automata Theory Tutorial 4 Tutor: William Chow."

Similar presentations


Ads by Google