PushDown Automata (PDA)

PDA: In computer science, a pushdown automaton (PDA) is a type of automaton that employs a stackPushdown automata are used in theories about what can be computed by machines. They are more capable than finite-state machines but less capable than Turing machines.*

*Reference: Wikipedia

Sample Code:

Q: Program For PDA Which Accpets Strings Of (0^n)(1^n)

Solution:

#include<iostream>
#include<string.h>
using namespace std;
int top;
char s[10];
class Stack
{
    public:
    void push(int x)
    {
        s[top++]=x;
    }
    void pop(int x)
    {
        s[top--]=x;
    }
};
int main()
{
    int i,n;
    char a[10];
    cout<<"\nProgram For PDA Which Accpets Strings Of (0^n)(1^n)\n";
    cout<<"\nEnter String::";
    cin>>a;
    n=strlen(a);
    Stack st;
    top=-1;
    for(i=0;i<n;i++) 
    {
        if(a[i]=='0' || a[i]=='1')
        {
            if(a[i]=='0')
            {
                st.push(a[i]);
            }
            else
            {
                st.pop(a[i]);
            }    
        }
        else
        {
            break;    
        }    
    }
    if(top==-1)
    {
        cout<<"\nString Accepted.\n";
    }
    else
    {
        cout<<"\nString Rejected.\n";
    }    
    return 0;
}


6 Comments

  1. Replies
    1. All 0's should come first and all 1's should come after 0's.
      You have entered incorrect string

      Delete
  2. this code is not showing any output just take input and terminate why?

    ReplyDelete
  3. 110100 is Accepting here but it should reject actually

    ReplyDelete
  4. Write a same program for PDA that accept well formed paranthesis

    ReplyDelete
Previous Post Next Post