Advertisement
Benjo_M

cisséplusplus bison

Nov 21st, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 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' << 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.  
  119. cout << "===== EXECUTION =====" << endl;
  120. ic = 0;
  121. while ( ic < instructions.size() ){
  122. auto ins = instructions[ic];
  123. cout << ic << '\t' << nom(ins.first) << "\t" << ins.second << endl;
  124.  
  125. switch(ins.first){
  126. case '+':
  127. x = depiler(pile);
  128. y = depiler(pile);
  129. pile.push_back(y+x);
  130. ic++;
  131. break;
  132.  
  133. case '*':
  134. x = depiler(pile);
  135. y = depiler(pile);
  136. pile.push_back(y*x);
  137. ic++;
  138. break;
  139.  
  140. case '-':
  141. x = depiler(pile);
  142. y = depiler(pile);
  143. pile.push_back
  144. break;
  145.  
  146. case '=':
  147. x = depiler(pile);
  148. pile.push_back(x);
  149. ic++;
  150. break;
  151.  
  152. case NUMBER :
  153. pile.push_back(ins.second);
  154. ic++;
  155. break;
  156.  
  157. case IDENTIFIER :
  158. pile.push_back
  159. break;
  160.  
  161. case JMP :
  162. ic = ins.second;
  163. break;
  164.  
  165. case JNZ :
  166. x = depiler(pile);
  167. ic = ( x ? ic + 1 : ins.second);
  168. break;
  169.  
  170. case OUT :
  171. cout << "Résultat : " << depiler(pile) << endl;
  172. ic++;
  173. break;
  174. }
  175. }
  176. cout << "=====================" << endl;
  177. }
  178.  
  179. int main(int argc, char **argv) {
  180.  
  181. if ( argc > 1 )
  182. yyin = fopen( argv[1], "r" );
  183. else
  184. yyin = stdin;
  185. yyparse();
  186.  
  187. print_program();
  188.  
  189. run_program();
  190.  
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement