Advertisement
Benjo_M

re bison

Nov 21st, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 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, double> variables;
  12.  
  13. vector<pair<int,double>> instructions;
  14. int ic = 0; // compteur instruction
  15. inline ins(int c, double d) { instructions.push_back(make_pair(c, d)); ic++;};
  16.  
  17. // structure pour stocker les adresses pour les sauts condistionnels et autres...
  18. typedef struct adr {
  19. int ic_goto;
  20. int ic_false;
  21. } t_adresse;
  22.  
  23. %}
  24.  
  25. %union
  26. {
  27. double valeur;
  28. char nom[50];
  29. t_adresse adresse;
  30. }
  31.  
  32. %token <valeur> NUMBER
  33. %token <nom> VARIABLE
  34. %type <valeur> expression
  35. %token <adresse> SI
  36. %token ALORS
  37. %token SINON
  38. %token FINSI
  39. %token REPEAT
  40. %token JMP
  41. %token JNZ
  42. %token OUT
  43. %token PRINT
  44.  
  45.  
  46. %left '+' '-' /* associativité à gauche */
  47. %left '*' '/' /* associativité à gauche */
  48. %token '='
  49.  
  50. %%
  51. bloc : bloc instruction '\n'
  52. | /* Epsilon */
  53. ;
  54. instruction : expression { ins (OUT,0); /* imprimer le résultat de l'expression */ }
  55.  
  56. | SI expression '\n' { $1.ic_goto = ic;
  57. ins (JNZ,0); }
  58. ALORS '\n' bloc { $1.ic_false = ic;
  59. ins (JMP,0);
  60. instructions[$1.ic_goto].second = ic;
  61. }
  62. SINON '\n' bloc { instructions[$1.ic_false].second = ic; }
  63. FINSI { }
  64.  
  65. | REPEAT '(' expression ')' expression { /* TO DO */ }
  66.  
  67. | /* Ligne vide*/
  68. ;
  69. expression: expression '+' expression { ins('+', 0);}
  70. | expression '-' expression { ins('-', 0);}
  71. | expression '*' expression { ins('*', 0);}
  72. | expression '/' expression { ins('/', 0);}
  73. | VARIABLE '=' expression { variables[$1] = $3;}
  74. | '(' expression ')' { }
  75. | PRINT '(' expression ')' { ins(PRINT, $3);}
  76. | NUMBER { ins(NUMBER, $1);}
  77. | VARIABLE { ins(VARIABLE, variables[$1]);}
  78. ;
  79. %%
  80.  
  81.  
  82. // Pour imprimer le code généré de manière plus lisible
  83. string nom(int instruction){
  84. switch (instruction){
  85. case '+' : return "ADD";
  86. case '*' : return "MUL";
  87. case '-' : return "MIN";
  88. case '/' : return "DIV";
  89. case '=' : return "EQU";
  90. case NUMBER : return "NUM";
  91. case VARIABLE : return "VAR";
  92. case OUT : return "OUT";
  93. case JNZ : return "JNZ"; // Jump if not zero
  94. case JMP : return "JMP"; // Unconditional Jump
  95. case PRINT : return "PRT";
  96. default : return to_string (instruction);
  97. }
  98. }
  99.  
  100. void print_program(){
  101. cout << "==== CODE GENERE ====" << endl;
  102. int i = 0;
  103. for (auto ins : instructions )
  104. cout << i++ << '\t' << "name : " << nom(ins.first) << "\t" << ins.second << endl;
  105. cout << "=====================" << endl;
  106. }
  107.  
  108. double depiler(vector<double> &pile) {
  109. double t = pile[pile.size()-1];
  110. cout << "Dépiler " << t << endl;
  111. pile.pop_back();
  112. return t;
  113. }
  114.  
  115. void run_program(){
  116. vector<double> pile;
  117. double x,y;
  118. string cle;
  119.  
  120. cout << "===== EXECUTION =====" << endl;
  121. ic = 0;
  122. while ( ic < instructions.size() ){
  123. auto ins = instructions[ic];
  124. cout << ic << '\t' << nom(ins.first) << "\t" << ins.second << endl;
  125.  
  126. switch(ins.first){
  127. case '+':
  128. x = depiler(pile);
  129. y = depiler(pile);
  130. pile.push_back(y+x);
  131. ic++;
  132. break;
  133.  
  134. case '*':
  135. x = depiler(pile);
  136. y = depiler(pile);
  137. pile.push_back(y*x);
  138. ic++;
  139. break;
  140.  
  141. case '-':
  142. x = depiler(pile);
  143. y = depiler(pile);
  144. pile.push_back(y-x);
  145. ic++;
  146. break;
  147.  
  148. case '=':
  149. x = depiler(pile);
  150. y = depiler(pile);
  151. pile.push_back(x);
  152. ic++;
  153. break;
  154.  
  155. case NUMBER :
  156. pile.push_back(ins.second);
  157. ic++;
  158. break;
  159.  
  160. case VARIABLE :
  161. x = depiler(pile);
  162. pile.push_back(x);
  163. ic++;
  164. break;
  165.  
  166. case JMP :
  167. ic = ins.second;
  168. break;
  169.  
  170. case JNZ :
  171. x = depiler(pile);
  172. ic = ( x ? ic + 1 : ins.second);
  173. break;
  174.  
  175. case OUT :
  176. cout << "Résultat : " << depiler(pile) << endl;
  177. ic++;
  178. break;
  179. }
  180. }
  181. cout << "=====================" << endl;
  182. }
  183.  
  184. int main(int argc, char **argv) {
  185.  
  186. if ( argc > 1 )
  187. yyin = fopen( argv[1], "r" );
  188. else
  189. yyin = stdin;
  190. yyparse();
  191.  
  192. print_program();
  193.  
  194. run_program();
  195.  
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement