COS3701
Assignment 2
(COMPLETE
ANSWERS)
2024 (365071) -
DUE 27 June
2024
, Question 1 [15] Build a DPDA to show that the language L = {(ba)na(ab)n-2 | n
> 2} is deterministic context free.
To show that the language �={(��)�(��)�−2∣�>2}L={(ba)n(ab)n−2∣n>2}
is deterministic context-free, we can construct a deterministic pushdown automaton (DPDA) that
recognizes it. Here's the high-level idea of the DPDA:
1. The DPDA will push symbols onto the stack for each occurrence of 'b' and 'a' in the input
string until it reads the second occurrence of 'a'.
2. Upon reading the second 'a', the DPDA will start popping symbols from the stack for
each occurrence of 'a' and 'b' until the stack is empty. If the stack becomes empty before
reaching the end of the input string, it accepts; otherwise, it rejects.
Here's the detailed construction:
1. State q0: The initial state. The DPDA starts here.
2. State q1: Push 'b' onto the stack for each 'b' encountered in the input.
3. State q2: Transition to this state when the first 'a' is encountered.
4. State q3: Push 'a' onto the stack for each 'a' encountered in the input until the second 'a' is
encountered.
5. State q4: Transition to this state upon reading the second 'a'.
6. State q5: Pop symbols from the stack for each 'a' or 'b' encountered in the input until the
stack is empty. If the end of the input is reached, accept; otherwise, reject.
The transitions for the DPDA are as follows:
�(�0,�,�)={(�1,�)}δ(q0,b,ε)={(q1,b)} (Push 'b' onto the stack)
�(�1,�,�)={(�1,��)}δ(q1,b,b)={(q1,bb)} (Push 'b' onto the stack)
�(�1,�,�)={(�2,�)}δ(q1,a,ε)={(q2,ε)} (Transition to q2 upon
encountering the first 'a')
�(�2,�,�)={(�3,�)}δ(q2,a,ε)={(q3,a)} (Push 'a' onto the stack)
�(�3,�,�)={(�3,��)}δ(q3,a,a)={(q3,aa)} (Push 'a' onto the stack)
�(�3,�,�)={(�3,�)}δ(q3,b,ε)={(q3,ε)} (Ignore additional 'b's until the
second 'a' is encountered)
�(�3,�,�)={(�4,�)}δ(q3,a,ε)={(q4,ε)} (Transition to q4 upon
encountering the second 'a')
�(�4,�,�)={(�5,�)}δ(q4,a,ε)={(q5,ε)} (Transition to q5 upon
encountering any 'a' after the second 'a')
�(�4,�,�)={(�5,�)}δ(q4,b,ε)={(q5,ε)} (Transition to q5 upon
encountering any 'b' after the second 'a')
�(�5,�,�)={(�5,�)}δ(q5,a,ε)={(q5,ε)} (Pop 'a' from the stack)
�(�5,�,�)={(�5,�)}δ(q5,b,ε)={(q5,ε)} (Pop 'b' from the stack)
Assignment 2
(COMPLETE
ANSWERS)
2024 (365071) -
DUE 27 June
2024
, Question 1 [15] Build a DPDA to show that the language L = {(ba)na(ab)n-2 | n
> 2} is deterministic context free.
To show that the language �={(��)�(��)�−2∣�>2}L={(ba)n(ab)n−2∣n>2}
is deterministic context-free, we can construct a deterministic pushdown automaton (DPDA) that
recognizes it. Here's the high-level idea of the DPDA:
1. The DPDA will push symbols onto the stack for each occurrence of 'b' and 'a' in the input
string until it reads the second occurrence of 'a'.
2. Upon reading the second 'a', the DPDA will start popping symbols from the stack for
each occurrence of 'a' and 'b' until the stack is empty. If the stack becomes empty before
reaching the end of the input string, it accepts; otherwise, it rejects.
Here's the detailed construction:
1. State q0: The initial state. The DPDA starts here.
2. State q1: Push 'b' onto the stack for each 'b' encountered in the input.
3. State q2: Transition to this state when the first 'a' is encountered.
4. State q3: Push 'a' onto the stack for each 'a' encountered in the input until the second 'a' is
encountered.
5. State q4: Transition to this state upon reading the second 'a'.
6. State q5: Pop symbols from the stack for each 'a' or 'b' encountered in the input until the
stack is empty. If the end of the input is reached, accept; otherwise, reject.
The transitions for the DPDA are as follows:
�(�0,�,�)={(�1,�)}δ(q0,b,ε)={(q1,b)} (Push 'b' onto the stack)
�(�1,�,�)={(�1,��)}δ(q1,b,b)={(q1,bb)} (Push 'b' onto the stack)
�(�1,�,�)={(�2,�)}δ(q1,a,ε)={(q2,ε)} (Transition to q2 upon
encountering the first 'a')
�(�2,�,�)={(�3,�)}δ(q2,a,ε)={(q3,a)} (Push 'a' onto the stack)
�(�3,�,�)={(�3,��)}δ(q3,a,a)={(q3,aa)} (Push 'a' onto the stack)
�(�3,�,�)={(�3,�)}δ(q3,b,ε)={(q3,ε)} (Ignore additional 'b's until the
second 'a' is encountered)
�(�3,�,�)={(�4,�)}δ(q3,a,ε)={(q4,ε)} (Transition to q4 upon
encountering the second 'a')
�(�4,�,�)={(�5,�)}δ(q4,a,ε)={(q5,ε)} (Transition to q5 upon
encountering any 'a' after the second 'a')
�(�4,�,�)={(�5,�)}δ(q4,b,ε)={(q5,ε)} (Transition to q5 upon
encountering any 'b' after the second 'a')
�(�5,�,�)={(�5,�)}δ(q5,a,ε)={(q5,ε)} (Pop 'a' from the stack)
�(�5,�,�)={(�5,�)}δ(q5,b,ε)={(q5,ε)} (Pop 'b' from the stack)