VOOZH about

URL: https://www.geeksforgeeks.org/theory-of-computation/construct-pushdown-automata-for-l-0n1m2nm-mn-%e2%89%a5-0/

⇱ Construct Pushdown automata for L = {0<sup>n</sup>1<sup>m</sup>2<sup>n+m</sup> | m, n ≥ 0} - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Construct Pushdown automata for L = {0n1m2n+m | m, n ≥ 0}

Last Updated : 21 Apr, 2025

Prerequisite : PDA plays a very important role in task of compiler designing. Therefore, there is a need to have good practice on PDA.

Problem

Construct a PDA which accepts a string of the form { 0 n 1 m 2 m+n | m , n >=0 }

Examples

Input: 00001111 ( case 1 )
Output: Accepted

Input: 111222 (case 2)
Output: Accepted

Input: 00001112222222 (case 3)
Output: Accepted

Input : 𝟄 (case 4)
Output: Accepted

Input: 00011112222
Output: Not Accepted

Approach used in this PDA

There can be four cases while processing the given input string:

Case 1 :m=0: In this cases the input string will be of the form {0n2n}. In this condition, keep on pushing 0's in the stack until we encounter with 2. On receiving 2 check if top of stack is 0, then pop it from the stack. Keep on popping 0's until all the 2's of the string are processed. If we reach to the end of input string and stack becomes empty, then reached to the final state i.e. Accepts the input string else move to dead state.

Case 2: n=0: In this cases the input string will be of the form {1m2m}. In this condition, keep on pushing 1's in the stack until we encounter with 2. On receiving 2 check if top of stack is 1, then pop it from the stack. Keep on popping 1's until all the 2's of the string are processed. If we reach to the end of input string and stack becomes empty, then reached to the final state i.e. Accepts the input string else move to dead state.

Case 3 : m, n>0: In this cases the input string will be of the form {0n 1 m 2 n+m } . In this condition, keep on pushing 0’s and 1’s in the stack until we encounter with 2. On receiving 2 check if top of stack is 1 or 0, then pop it (1 or 0) from the stack. Keep on popping 1’s or 0's until all the 2’s of the input string are processed. If we reach to the end of input string and stack becomes empty, then reach to final state i.e. accept the input string else move to dead state.

Case 4 : m=0, n=0: In this case the input string will be empty. Therefore, directly jumps to final state. 👁 Image

Comment
Article Tags:

Explore