开发者

Program to convert context free language to push down automata?

开发者 https://www.devze.com 2023-01-27 17:48 出处:网络
I can\'t find any applet or program online to convert a context free language into a push down automata... any help would be gre开发者_StackOverflow中文版atly appreciated.It is very easy to do by hand

I can't find any applet or program online to convert a context free language into a push down automata... any help would be gre开发者_StackOverflow中文版atly appreciated.


It is very easy to do by hand. The PDA has start state s and final state f, the only two states it has. Make a transition ((s, empty, empty),(f, S)), where S is the start symbol of your CFG. For each rule X -> Y, where X is a non terminal symbol and Y is a possibly empty string of terminals and nonterminals, make a transition ((f, empty, X),(f,Y)). Finally, for each terminal symbol a, add the rule ((f, a, a),(f, empty)).

What this does is start by pushing the start symbol on the stack. Then it replaces any nonterminal it finds at the top of the stack with the right hand side of its production rule, and matches and pops any terminal characters at the top of the stack.


Please checkout the code at: https://github.com/P-Raj/AutomataPlus. It contains code not only for converting CFG into PDA, but also for other similar tasks.


Try this soft: https://github.com/navrkald/regularConvertor. You can step throw whole algorithm of conversion CFG to PDA. Its written in C++ using Qt and in section releases you have already builded self-executable binary for Windows.


#include<bits/stdc++.h> 
using namespace std;

 int main()
 {

int n; 
cout<<"Enter the no of Production Rules of CFG"; 

cin>>n; 
char s1; 
string s2; 
vector<pair<char,string>> v;

 cout<<"Please enter the Production rules of CFG \n";

  for(int i=0;i<n;i++)
    { cin>>s1>>s2;

      v.push_back(make_pair(s1,s2));}

cout<<"The Corresponding Production Rules For PDA are:-"<<endl; 



 cout<<"Rules For Non-Terminal Symbols are:- \n";

for(int i=0;i<n;i++){
 int flag=0; 
 cout<<"dl(q,null," <<v[i].first<<") --> ";

 string check=v[i].second;
 int si=check.size();

string ans="";
for(int i=0;i<si;i++){ char ch=check[i];


    if(ch == '|')
        { cout<<"dl(q,"<<ans<<") |";

              ans="";}

  else{ ans+=ch;}

  }


if(flag!=1) cout<<"dl(q,"<<ans<<")"<<endl;

}

cout<<"dl(q,0,0)--> dl(q,null)"<<endl; 
cout<<"dl(q,1,1) --> dl(q,null)"<<endl;
}

0

精彩评论

暂无评论...
验证码 换一张
取 消