Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. /*ExampleFSMcode*/
  2. /*Reasdsstringfromkeyboard*/
  3. /*
  4. RunsFSMoneachcharacterofthe
  5. string(callingfunctionPro)
  6. until#characterisfound
  7. */
  8. // ostream constructor
  9. #include <iostream>
  10. #include <fstream>
  11. #include<iostream>
  12. #include<string>
  13. #include <vector>
  14.  
  15. using namespace std;
  16. string state;
  17. vector<string> vectors;
  18. vector<int> lineSize;
  19. string Token = "";
  20. int lines = 0;
  21. int length = 0;
  22. int lengthCount = 0;
  23. int line = 0;
  24. int skip = 0;
  25. int longness = 0;
  26. string filename = "test.txt";
  27. bool stop = false;
  28.  
  29. //currentFSMstate
  30. /*DescribesanFSMwithfourstates:s0,s1,s2ands3*/
  31. /*s3isanacceptingstate*/
  32. /*returns1ifFSMisinacceptingstate*/
  33. void printskeletoncode(){//prints top and main for function, they stay constant
  34. std::filebuf fb;
  35. fb.open ("Generated_FSM.cpp",std::ios::app);
  36. std::ostream os(&fb);
  37. os <<"int main(){\n";
  38. os <<" string input;\n";
  39. os <<"int string_accepted = 0;\n";
  40. os <<"state = \"s0\"; //starting state\n";
  41. os <<"while ( 1 ){ \n";
  42. os <<"if( string_accepted == 1){break;}\n";
  43. os <<"cout<<\"enter input string:\";\n";
  44. os <<"cin>>input;\n";
  45. os <<"cout<<input<<endl;\n";
  46. os <<"string_accepted = Proc(input);\n";
  47. os <<" cout<<\"Input is \"<<input<<\" state is \"<<state<<endl;\n";
  48. os <<"}\n";
  49. os <<" cout<<\" accepted = \"<<string_accepted<<endl;\n";
  50. os <<"} \n";
  51.  
  52. os <<"\n";
  53.  
  54. fb.close();
  55. }
  56.  
  57. char testChar(char char1, char char2){
  58. if(char1 == ',' && char2 == ','){
  59.  
  60. return ',';
  61.  
  62. }
  63.  
  64. if(char1 == ':' && char2 == ':'){
  65.  
  66. return ':';
  67.  
  68. }
  69.  
  70. return ' ';
  71.  
  72. }
  73.  
  74.  
  75.  
  76. void ProcessChar(char in_char){//used for making inputs into tokens which can be printed into custom code
  77.  
  78. if(state == "s0"){
  79.  
  80. if(in_char != ',' && in_char != ':' && in_char != '#' && in_char != '\n' && in_char != ' '){
  81.  
  82. Token +=(in_char);
  83.  
  84. }
  85. else{
  86.  
  87. state = "s1";
  88.  
  89. }
  90.  
  91. lengthCount++;
  92.  
  93. }
  94. if(lengthCount == length){state = "s1";}
  95.  
  96. if(state == "s1"){
  97.  
  98.  
  99. if(Token != ""){
  100.  
  101. vectors.push_back(Token);
  102. longness++;
  103.  
  104. }
  105.  
  106. Token="";state= "s0";
  107.  
  108. }
  109. }
  110.  
  111.  
  112. void readFile(string filename){
  113. ifstream infile;
  114. infile.open(filename);
  115. string a;
  116.  
  117. while(getline(infile,a)){
  118. state="s0";
  119. longness = 0;
  120.  
  121. length = (int)a.length();
  122. lengthCount = 0;
  123. for(int i = 0; i< (int)a.length(); i++){
  124. if(i>0){
  125. if(testChar(a.at(i-1),a.at(i)) != ' '){
  126. cout<<"Too Many "<<testChar(a.at(i),a.at(i+1))<<" Program Shutting Down"<<endl;
  127. throw std::exception();
  128. }
  129. }
  130.  
  131. ProcessChar(a.at(i));
  132.  
  133. }
  134.  
  135. lineSize.push_back(longness);
  136. lines++;
  137.  
  138. //call break string method
  139. }
  140. infile.close();
  141. }
  142. void printMethod(){
  143.  
  144. std::filebuf fb;
  145. fb.open ("Generated_FSM.cpp",std::ios::out);
  146. std::ostream os(&fb);
  147. os <<"/*Example FSM code*/\n";
  148. os <<"/*Reasds string from keyboard*/\n";
  149. os <<"/*\n";
  150. os <<"Runs FSM on each character of the \n";
  151. os <<"string (calling function Pro) \n";
  152. os <<"until # character is found\n";
  153. os <<"*/\n";
  154. os <<"#include <iostream>\n";
  155. os <<"#include <string>\n";
  156. os <<"\n";
  157. os <<"using namespace std;\n";
  158. os <<"\n";
  159. os <<"string state; // current FSM state\n";
  160. os <<"\n";
  161. os <<"/*Describes an FSM with four states: s0, s1,s2 and s3 */\n";
  162. os <<"/* s3 is an accepting state*/\n";
  163. os <<"/*returns 1 if FSM is in accepting state*/\n";
  164. os <<"\n";
  165. os <<" int Proc(string input){\n";
  166.  
  167. int size = lineSize.at(line);
  168.  
  169. os <<"if(state==\""<<vectors.at(0)<<"\"){"<<"\n";
  170. os <<"if(input=="<<'"'<<vectors.at(1)<<"\"){state=\""<<vectors.at(2)<<"\";}"<<"\n";//prints given output line, inserts tokens
  171.  
  172. for(int i = 3; i < size; i +=2){
  173.  
  174. os <<"else if(input=="<<'"'<<vectors.at(i)<<"\"){state=\""<<vectors.at(i+1)<<"\";}"<<"\n";//prints given output line, inserts tokens
  175.  
  176. }
  177.  
  178. os <<"}"<<"\n";
  179. line = 0;
  180.  
  181. for(int o = 1; o < lines-1; o++){
  182.  
  183. size = lineSize.at(o);
  184. skip = 0;
  185.  
  186. for(int x = 0; x <= line; x++){
  187.  
  188. skip += lineSize.at(x);
  189.  
  190. }
  191.  
  192. os <<"else if(state==\""<<vectors.at(skip)<<"\"){"<<"\n";
  193. os <<"if(input=="<<'"'<<vectors.at(skip+1)<<"\"){state=\""<<vectors.at(skip+2)<<"\";}"<<"\n";
  194.  
  195. for(int y = 3; y < size; y=y+2){
  196.  
  197. os <<"else if(input=="<<'"'<<vectors.at(skip+y)<<"\"){state=\""<<vectors.at(skip+y+1)<<"\";}"<<"\n";//prints given output line, inserts tokens
  198. }
  199.  
  200. os <<"}"<<"\n";
  201. line++;
  202. }
  203.  
  204. skip = 0;
  205.  
  206. for(int x = 0; x<=line; x++){
  207. skip += lineSize.at(x);
  208.  
  209. }
  210.  
  211. os <<"if(state==\""<<vectors.at(skip)<<"\"){"<<"\n";
  212. os<<"cout<<\"Accepted\"<<endl; \n";
  213. os<<"return 1;";
  214. os <<"}"<<"\n";
  215. os<<"return 0; \n";
  216. os <<"}"<<"\n";
  217. fb.close();//close file
  218. }
  219.  
  220.  
  221. int main(){
  222. readFile(filename);
  223. printMethod();
  224. printskeletoncode();
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement