Advertisement
Benjo_M

bison

Dec 11th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. %{
  2. #include <iostream>
  3. #include <map>
  4. #include <string>
  5. #include <vector>
  6. using namespace std;
  7. extern FILE *yyin;
  8. extern int yylex ();
  9. int yyerror(char *s) { printf("%s\n", s); }
  10.  
  11. map<string, int> variables; //map string et int adresse en memoire avec un compteur memoire entier 0,1,2,3,4
  12. map<string,int>::iterator itvar;
  13. //memoire vector double qu'on utilisera ensuite dans le run (une adresse memoire en gros)
  14. vector<double> memory;
  15. vector<string> fMemory;
  16. int incmemory = 0;
  17. int incFmemory = 0;
  18.  
  19. vector<pair<int,double>> instructions;
  20. int ic = 0; // compteur instruction
  21. inline ins(int c, double d) { instructions.push_back(make_pair(c,d)); ic++;};
  22.  
  23. // structure pour stocker les adresses pour les sauts condistionnels et autres...
  24. typedef struct adr {
  25. int ic_goto;
  26. int ic_false;
  27. } t_adresse;
  28.  
  29. %}
  30.  
  31. %union
  32. {
  33. double valeur;
  34. char nom[50];
  35. t_adresse adresse;
  36. }
  37.  
  38. %token <valeur> NUMBER
  39. %token <nom> VARIABLE
  40. %token <nom> TEXT
  41. %type <valeur> expression
  42. %token <adresse> SI
  43. %token ALORS
  44. %token SINON
  45. %token FINSI
  46. %token REPEAT
  47.  
  48. %token JMP
  49. %token JNZ
  50. %token OUT
  51.  
  52. %token PRINT
  53. %left ASK
  54.  
  55. %token DOUBLE_EGAL
  56. %token SUP_EGAL
  57. %token SUP_STRICT
  58. %token INF_EGAL
  59. %token INF_STRICT
  60.  
  61.  
  62. %left '+' '-' /* associativité à gauche */
  63. %left '*' '/' /* associativité à gauche */
  64. %token '=' '&'
  65.  
  66. %%
  67. bloc : bloc instruction '\n'
  68. | /* Epsilon */
  69. ;
  70. instruction : expression { ins (OUT,0); /* imprimer le résultat de l'expression */ }
  71. | VARIABLE '=' expression { if ( variables.find($1) == variables.end() ) {
  72. variables[$1] = incmemory;
  73. memory.push_back(0) ;
  74. incmemory++;
  75. }
  76. ins('=', variables[$1]);}
  77. | SI expression '\n' { $1.ic_goto = ic;
  78. ins (JNZ,0); }
  79. ALORS '\n' bloc { $1.ic_false = ic;
  80. ins (JMP,0);
  81. instructions[$1.ic_goto].second = ic;
  82. }
  83. SINON '\n' bloc { instructions[$1.ic_false].second = ic; }
  84. FINSI { }
  85.  
  86. | REPEAT '(' expression ')' expression {
  87.  
  88. }
  89. | ASK '(' VARIABLE ')' { if ( variables.find($3) == variables.end() ) {
  90. variables[$3] = incmemory;
  91. memory.push_back(0) ;
  92. incmemory++;
  93. }
  94. ins(ASK,variables[$3]);
  95. }
  96. | /* Ligne vide*/
  97. ;
  98. expression: expression '+' expression { ins('+', 0);}
  99. | expression '-' expression { ins('-', 0);}
  100. | expression '*' expression { ins('*', 0);}
  101. | expression '/' expression { ins('/', 0);}
  102.  
  103. | expression DOUBLE_EGAL expression { ins(DOUBLE_EGAL, 0);}
  104. | expression SUP_EGAL expression { ins(SUP_EGAL,0);}
  105. | expression SUP_STRICT expression { ins(SUP_STRICT,0);}
  106. | expression INF_EGAL expression { ins(INF_EGAL,0);}
  107. | expression INF_STRICT expression { ins(INF_STRICT,0);}
  108. | '(' expression ')' { }
  109. | PRINT '(' expression ')' {ins(PRINT,$3);}
  110. | NUMBER { ins(NUMBER, $1);}
  111. | VARIABLE { if ( variables.find($1) == variables.end() ) {
  112. variables[$1] = incmemory;
  113. memory.push_back(0) ;
  114. incmemory++;
  115. }
  116. ins(VARIABLE, variables[$1]);
  117. }
  118. | TEXT {
  119. }
  120. ;
  121. %%
  122.  
  123.  
  124. // Pour imprimer le code généré de manière plus lisible
  125. string nom(int instruction){
  126. switch (instruction){
  127. case '+' : return "ADD";
  128. case '*' : return "MUL";
  129. case '-' : return "MIN";
  130. case '/' : return "DIV";
  131. case '=' : return "EQU";
  132. case DOUBLE_EGAL : return "EG?";
  133. case SUP_EGAL : return "SE?";
  134. case SUP_STRICT : return "SS?";
  135. case INF_EGAL : return "IE?";
  136. case INF_STRICT : return "IS?";
  137. case NUMBER : return "NUM";
  138. case VARIABLE : return "VAR";
  139. case TEXT : return "TXT";
  140. case OUT : return "OUT";
  141. case JNZ : return "JNZ"; // Jump if not zero
  142. case JMP : return "JMP"; // Unconditional Jump
  143. case ASK : return "ASK";
  144. case PRINT : return "PRT";
  145. default : return to_string (instruction);
  146. }
  147. }
  148.  
  149. void print_program(){
  150. cout << "==== CODE GENERE ====" << endl;
  151. int i = 0;
  152. for (auto ins : instructions )
  153. cout << i++ << '\t' << nom(ins.first) << "\t" << ins.second << endl;
  154. cout << "=====================" << endl;
  155. }
  156.  
  157. double depiler(vector<double> &pile) {
  158. double t = pile[pile.size()-1];
  159. cout << "Dépiler " << t << endl;
  160. pile.pop_back();
  161. return t;
  162. }
  163.  
  164. void run_program(){
  165. vector<double> pile;
  166. double x,y;
  167. int memoryMark;
  168. string varName;
  169.  
  170. cout << "===== EXECUTION =====" << endl;
  171. ic = 0;
  172. while ( ic < instructions.size() ){
  173. auto ins = instructions[ic];
  174. //cout << ic << '\t' << nom(ins.first) << "\t" << ins.second << endl;
  175.  
  176. switch(ins.first){
  177. case '+':
  178. x = depiler(pile);
  179. y = depiler(pile);
  180. pile.push_back(y+x);
  181. ic++;
  182. break;
  183.  
  184. case '*':
  185. x = depiler(pile);
  186. y = depiler(pile);
  187. pile.push_back(y*x);
  188. ic++;
  189. break;
  190.  
  191. case '-':
  192. x = depiler(pile);
  193. y = depiler(pile);
  194. pile.push_back(y-x);
  195. ic++;
  196. break;
  197.  
  198. case '/':
  199. x=depiler(pile);
  200. y=depiler(pile);
  201. pile.push_back(y/x);
  202. ic++;
  203. break;
  204.  
  205. case '=':
  206. x = depiler(pile);
  207. memory[ins.second] = x;
  208. pile.push_back(memory[(int)ins.second]);
  209. ic++;
  210. break;
  211.  
  212. case DOUBLE_EGAL:
  213. x=depiler(pile);
  214. y=depiler(pile);
  215. if(y == x){pile.push_back(1);}
  216. else{pile.push_back(0);}
  217. ic++;
  218. break;
  219.  
  220. case SUP_EGAL:
  221. x=depiler(pile);
  222. y=depiler(pile);
  223. if(y >= x){pile.push_back(1);}
  224. else{pile.push_back(0);}
  225. ic++;
  226. break;
  227.  
  228. case SUP_STRICT:
  229. x=depiler(pile);
  230. y=depiler(pile);
  231. if(y > x){pile.push_back(1);}
  232. else{pile.push_back(0);}
  233. ic++;
  234. break;
  235.  
  236. case INF_EGAL:
  237. x=depiler(pile);
  238. y=depiler(pile);
  239. if(y <= x){pile.push_back(1);}
  240. else{pile.push_back(0);}
  241. ic++;
  242. break;
  243.  
  244. case INF_STRICT:
  245. x=depiler(pile);
  246. y=depiler(pile);
  247. if(y < x){pile.push_back(1);}
  248. else{pile.push_back(0);}
  249. ic++;
  250. break;
  251.  
  252. case NUMBER :
  253. x = ins.second;
  254. pile.push_back(x);
  255. ic++;
  256. break;
  257.  
  258. case VARIABLE :
  259. memoryMark = (int)ins.second;
  260. cout << "pos = " << memoryMark << endl;
  261. x = memory[memoryMark];
  262. pile.push_back(x);
  263. ic++;
  264. break;
  265.  
  266. case PRINT:
  267. cout << ins.second << endl;
  268. ic++;
  269. break;
  270.  
  271. case JMP :
  272. ic = ins.second;
  273. break;
  274.  
  275. case JNZ :
  276. x = depiler(pile);
  277. ic = ( x ? ic + 1 : ins.second);
  278. break;
  279.  
  280. case ASK :
  281. for (auto it = variables.begin(); it != variables.end(); ++it)
  282. { if (it->second == ins.second){ varName = it->first; }; }
  283. cout << varName << " : ";
  284. cin >> memory[ins.second];
  285. ic++;
  286. break;
  287.  
  288. case OUT :
  289. cout << "Résultat : " << depiler(pile) << endl;
  290. ic++;
  291. break;
  292. }
  293. }
  294. cout << "=====================" << endl;
  295. }
  296.  
  297. int main(int argc, char **argv) {
  298.  
  299. if ( argc > 1 )
  300. yyin = fopen( argv[1], "r" );
  301. else
  302. yyin = stdin;
  303. yyparse();
  304.  
  305. print_program();
  306.  
  307. run_program();
  308.  
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement