Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*ExampleFSMcode*/
- /*Reasdsstringfromkeyboard*/
- /*
- RunsFSMoneachcharacterofthe
- string(callingfunctionPro)
- until#characterisfound
- */
- // ostream constructor
- #include <iostream>
- #include <fstream>
- #include<iostream>
- #include<string>
- #include <vector>
- using namespace std;
- string state;
- vector<string> vectors;
- vector<int> lineSize;
- string Token = "";
- int lines = 0;
- int length = 0;
- int lengthCount = 0;
- int line = 0;
- int skip = 0;
- int longness = 0;
- string filename = "test.txt";
- bool stop = false;
- //currentFSMstate
- /*DescribesanFSMwithfourstates:s0,s1,s2ands3*/
- /*s3isanacceptingstate*/
- /*returns1ifFSMisinacceptingstate*/
- void printskeletoncode(){//prints top and main for function, they stay constant
- std::filebuf fb;
- fb.open ("Generated_FSM.cpp",std::ios::app);
- std::ostream os(&fb);
- os <<"int main(){\n";
- os <<" string input;\n";
- os <<"int string_accepted = 0;\n";
- os <<"state = \"s0\"; //starting state\n";
- os <<"while ( 1 ){ \n";
- os <<"if( string_accepted == 1){break;}\n";
- os <<"cout<<\"enter input string:\";\n";
- os <<"cin>>input;\n";
- os <<"cout<<input<<endl;\n";
- os <<"string_accepted = Proc(input);\n";
- os <<" cout<<\"Input is \"<<input<<\" state is \"<<state<<endl;\n";
- os <<"}\n";
- os <<" cout<<\" accepted = \"<<string_accepted<<endl;\n";
- os <<"} \n";
- os <<"\n";
- fb.close();
- }
- char testChar(char char1, char char2){
- if(char1 == ',' && char2 == ','){
- return ',';
- }
- if(char1 == ':' && char2 == ':'){
- return ':';
- }
- return ' ';
- }
- void ProcessChar(char in_char){//used for making inputs into tokens which can be printed into custom code
- if(state == "s0"){
- if(in_char != ',' && in_char != ':' && in_char != '#' && in_char != '\n' && in_char != ' '){
- Token +=(in_char);
- }
- else{
- state = "s1";
- }
- lengthCount++;
- }
- if(lengthCount == length){state = "s1";}
- if(state == "s1"){
- if(Token != ""){
- vectors.push_back(Token);
- longness++;
- }
- Token="";state= "s0";
- }
- }
- void readFile(string filename){
- ifstream infile;
- infile.open(filename);
- string a;
- while(getline(infile,a)){
- state="s0";
- longness = 0;
- length = (int)a.length();
- lengthCount = 0;
- for(int i = 0; i< (int)a.length(); i++){
- if(i>0){
- if(testChar(a.at(i-1),a.at(i)) != ' '){
- cout<<"Too Many "<<testChar(a.at(i),a.at(i+1))<<" Program Shutting Down"<<endl;
- throw std::exception();
- }
- }
- ProcessChar(a.at(i));
- }
- lineSize.push_back(longness);
- lines++;
- //call break string method
- }
- infile.close();
- }
- void printMethod(){
- std::filebuf fb;
- fb.open ("Generated_FSM.cpp",std::ios::out);
- std::ostream os(&fb);
- os <<"/*Example FSM code*/\n";
- os <<"/*Reasds string from keyboard*/\n";
- os <<"/*\n";
- os <<"Runs FSM on each character of the \n";
- os <<"string (calling function Pro) \n";
- os <<"until # character is found\n";
- os <<"*/\n";
- os <<"#include <iostream>\n";
- os <<"#include <string>\n";
- os <<"\n";
- os <<"using namespace std;\n";
- os <<"\n";
- os <<"string state; // current FSM state\n";
- os <<"\n";
- os <<"/*Describes an FSM with four states: s0, s1,s2 and s3 */\n";
- os <<"/* s3 is an accepting state*/\n";
- os <<"/*returns 1 if FSM is in accepting state*/\n";
- os <<"\n";
- os <<" int Proc(string input){\n";
- int size = lineSize.at(line);
- os <<"if(state==\""<<vectors.at(0)<<"\"){"<<"\n";
- os <<"if(input=="<<'"'<<vectors.at(1)<<"\"){state=\""<<vectors.at(2)<<"\";}"<<"\n";//prints given output line, inserts tokens
- for(int i = 3; i < size; i +=2){
- os <<"else if(input=="<<'"'<<vectors.at(i)<<"\"){state=\""<<vectors.at(i+1)<<"\";}"<<"\n";//prints given output line, inserts tokens
- }
- os <<"}"<<"\n";
- line = 0;
- for(int o = 1; o < lines-1; o++){
- size = lineSize.at(o);
- skip = 0;
- for(int x = 0; x <= line; x++){
- skip += lineSize.at(x);
- }
- os <<"else if(state==\""<<vectors.at(skip)<<"\"){"<<"\n";
- os <<"if(input=="<<'"'<<vectors.at(skip+1)<<"\"){state=\""<<vectors.at(skip+2)<<"\";}"<<"\n";
- for(int y = 3; y < size; y=y+2){
- os <<"else if(input=="<<'"'<<vectors.at(skip+y)<<"\"){state=\""<<vectors.at(skip+y+1)<<"\";}"<<"\n";//prints given output line, inserts tokens
- }
- os <<"}"<<"\n";
- line++;
- }
- skip = 0;
- for(int x = 0; x<=line; x++){
- skip += lineSize.at(x);
- }
- os <<"if(state==\""<<vectors.at(skip)<<"\"){"<<"\n";
- os<<"cout<<\"Accepted\"<<endl; \n";
- os<<"return 1;";
- os <<"}"<<"\n";
- os<<"return 0; \n";
- os <<"}"<<"\n";
- fb.close();//close file
- }
- int main(){
- readFile(filename);
- printMethod();
- printskeletoncode();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement