Advertisement
Benjo_M

Untitled

Nov 21st, 2019
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.92 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,string>> instructions;
  14. int ic = 0; // compteur instruction
  15. inline ins(int c, string s) { instructions.push_back(make_pair(c, s)); 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.  
  68. | /* Ligne vide*/
  69. ;
  70. expression: expression '+' expression { ins('+', "0");}
  71. | expression '-' expression { ins('-', "0");}
  72. | expression '*' expression { ins('*', "0");}
  73. | expression '/' expression { ins('/', "0");}
  74. | VARIABLE '=' expression { ins('=',$1);}
  75. | '(' expression ')' { }
  76. | PRINT '(' expression ')' { }
  77. | NUMBER { ins(NUMBER, to_string($1));}
  78. | VARIABLE { ins(VARIABLE, $1);}
  79. ;
  80. %%
  81.  
  82.  
  83. // Pour imprimer le code généré de manière plus lisible
  84. string nom(int instruction){
  85. switch (instruction){
  86. case '+' : return "ADD";
  87. case '*' : return "MUL";
  88. case '-' : return "MIN";
  89. case '/' : return "DIV";
  90. case '=' : return "EQU";
  91. case NUMBER : return "NUM";
  92. case VARIABLE : return "VAR";
  93. case OUT : return "OUT";
  94. case JNZ : return "JNZ"; // Jump if not zero
  95. case JMP : return "JMP"; // Unconditional Jump
  96. case PRINT : return "PRT";
  97. default : return to_string (instruction);
  98. }
  99. }
  100.  
  101. void print_program(){
  102. cout << "==== CODE GENERE ====" << endl;
  103. int i = 0;
  104. for (auto ins : instructions )
  105. cout << i++ << '\t' << nom(ins.first) << "\t" << ins.second << endl;
  106. cout << "=====================" << endl;
  107. }
  108.  
  109. double depiler(vector<double> &pile) {
  110. double t = pile[pile.size()-1];
  111. cout << "Dépiler " << t << endl;
  112. pile.pop_back();
  113. return t;
  114. }
  115.  
  116. void run_program(){
  117. vector<double> pile;
  118. double x,y;
  119. string xs,ys;
  120. string::size_type sz;
  121.  
  122. cout << "===== EXECUTION =====" << endl;
  123. ic = 0;
  124. while ( ic < instructions.size() ){
  125. auto ins = instructions[ic];
  126. cout << ic << '\t' << nom(ins.first) << "\t" << ins.second << endl;
  127.  
  128. switch(ins.first){
  129. case '+':
  130. x = depiler(pile);
  131. y = depiler(pile);
  132. pile.push_back(y+x);
  133. ic++;
  134. break;
  135.  
  136. case '*':
  137. x = depiler(pile);
  138. y = depiler(pile);
  139. pile.push_back(y*x);
  140. ic++;
  141. break;
  142.  
  143. case '-':
  144. x = depiler(pile);
  145. y = depiler(pile);
  146. pile.push_back(y-x);
  147. ic++;
  148. break;
  149.  
  150. case '=':
  151. x = depiler(pile);
  152. variables[ins.second] = x;
  153. ic++;
  154. break;
  155.  
  156. case NUMBER :
  157. x = stod(ins.second,&sz);
  158. pile.push_back(x);
  159. ic++;
  160. break;
  161.  
  162. case VARIABLE :
  163. x = depiler(pile);
  164. x = variables[ins.second];
  165. pile.push_back(x);
  166. ic++;
  167. break;
  168.  
  169. case JMP :
  170. ic = stoi(ins.second);
  171. break;
  172.  
  173. case JNZ :
  174. xs = depiler(pile);
  175. x = stod(xs,&sz);
  176. ic = ( x ? ic + 1 : stoi(ins.second));
  177. break;
  178.  
  179. case OUT :
  180. cout << "Résultat : " << depiler(pile) << endl;
  181. ic++;
  182. break;
  183. }
  184. }
  185. cout << "=====================" << endl;
  186. }
  187.  
  188. int main(int argc, char **argv) {
  189.  
  190. cout << "test1" << endl;
  191.  
  192. if ( argc > 1 )
  193. yyin = fopen( argv[1], "r" );
  194. else
  195. yyin = stdin;
  196. cout << "test2" << endl;
  197. yyparse();
  198.  
  199. cout << "test3" << endl;
  200.  
  201. print_program();
  202.  
  203. //run_program();
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement