# Construct Pushdown automata for L = {0(n+m)1m2n | m, n = 0} in C++

C++Server Side ProgrammingProgramming

We are given with a language “L” and the task is to construct a pushdown automata for the given language which explains that the occurrences of 0’s will be the addition of occurrences of 1’s and 2’s and also, occurrence of 1 and 2 will be minimum one which can also makes the string NULL and it should be accepted by the automata.

## What is pushdown Automata?

A pushdown automata or pushdown automaton or PDA is a technique to implement a context−free grammar in a similar way we design Deterministic Finite Automaton or DFA for a regular grammar. A DFA can operate on finite data, but a PDA can operate on infinite data. We can understand pushdown automata as the combination of “finite state machine” and a “stack”.

A pushdown automaton has three components −

• an input tape,

• a control unit, and

• a stack with infinite size.

A PDA can be formally described as a 7−tuple (Q, Ʃ, S, δ, q0, I, F) −

• Q is the finite number of states

• Ʃ is input alphabet

• S is stack symbols

• δ is the transition function: Q × (Ʃ υ {ε}) × S × Q × S*

• q0 is the initial state (q0 ε Q)

• I is the initial stack top symbol (I ε S)

• F is a set of accepting states (F ε Q)

Let’s construct a pushdown Automata for the given language The strings that are acceptable by this PDA are of the form −

• 1. 0n2n : 02, 0022, 000222 etc. No. of 0s is equal to no. of 2s. When m is 0 we will have no 1s. Keep pushing 0s and as soon as the first 2 is encountered then pop 0s. If we reach the end of the string and are left with no 0s then the string is accepted.

• 0m1m: 01, 0011, 000111 etc. No. of 0s is equal to no. of 1s. When n is 0 we will have no 2s. Keep pushing 0s and as soon as the first 1 is encountered then pop 0s. If we reach the end of the string and are left with no 0s then the string is accepted.

• 0n+m1m2n: 0012, 000112, 000122 etc. No. of 0s is equal to sum of no. of 1s and 2s. Keep pushing 0s and as first 1 is encountered then pop those 0s until no 1s are left. Then keep pushing 0s again and as the first 2 is encountered then pop those 0s until no 2s are left. The string will be accepted.

• NULL string is also accepted. 001020

## Let’s understand the machine

• Transitions for state q0 :

• 0, I/0I ) − If top of stack is I and current input symbol is 0 then push 0 to top of stack and remain at q0. Stack becomes 0I...

• ( 0, 0/00 ) − If top of stack is 0 and current input symbol is also 0 then push 0 to top of stack and remain at q0. Stack becomes 00.... Keep pushing 0s until the next 1 or 2.

• ( 1, 0/$) − If top of stack is 0 and current input symbol is 1 then pop 0 and move to q1. • ( 2, 0/$ ) − If top of stack is 0 and current input symbol is 2 then pop 0 and move to q2.

• ( $, I/I ) − If top of stack is I and there is no input then do nothing and move to qf. For NULL string. • Transitions for state q1 − • ( 1, 0/$ ) − If top of stack is 0 and current input symbol is 1 then pop 0 to and remain at q1.

• ( $, I/I ) − If top of stack is I and there is no input then do nothing and move to qf. • ( 2, 0/$ ) − If top of stack is 0 and current input symbol is 2 then pop 0 and move to q2.

• Transitions for state q2 −

• ( 2, 0/$) − If top of stack is 0 and current input symbol is 2 then pop 0 and remain at q2. • ($, I/I ) − If top of stack is I and there is no input then do nothing and move to qf.