Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.65 KB | None | 0 0
  1. %{
  2. #include <iostream>
  3. #include <string>
  4. #include <map>
  5. #include <math.h>
  6. #include <vector>
  7. using namespace std;
  8.  
  9. // structure pour stocker les adresses pour les sauts condistionnels et autres...
  10. typedef struct addr {
  11. int ic_goto;
  12. int ic_false;
  13. } t_address;
  14.  
  15. // Stockage des variables
  16. map<string,double> variable;
  17. vector<pair<int,double>> instruction;
  18. int ic = 0; // compteur instruction
  19. inline ins(int c, double d) { instruction.push_back(make_pair(c, d)); ic++;};
  20.  
  21. extern FILE *yyin;
  22. extern int yyerror(char *);
  23. extern int yylex(void);
  24. void Div0Error(void);
  25. void UnknownVarError(string s);
  26. %}
  27.  
  28. %union {
  29. double aNumber;
  30. char aString[256];
  31. t_address address;
  32. }
  33.  
  34. /* Variable et adresses */
  35. %token<aNumber> NUMBER
  36. %token<aString> VARIABLE
  37.  
  38. /* CONDITIONS */
  39. %token<address> IF
  40. %token ELSE REPEAT JMP JNZ OUT
  41.  
  42. /* Math */
  43. %token PLUS MINUS MULTIPLY DIVIDE
  44. %token EQUAL
  45.  
  46. /* Parentheses */
  47. %token LP RP /* ( ) */
  48. %token LA RA /* { } */
  49.  
  50. /* Autre ... */
  51. %token SEPARATOR PRINT
  52.  
  53. /* Gestion de l'associativité */
  54. %left PLUS MINUS
  55. %left MULTIPLY DIVIDE
  56.  
  57. /* Gestion des types */
  58. %type<aNumber> number
  59. %type<aNumber> instruction
  60.  
  61. %start bloc
  62.  
  63. /*** GESTION DES REGLES A RESPECTER ***/
  64. %%
  65. bloc:
  66. bloc line
  67. | line
  68. ;
  69.  
  70. line:
  71. PRINT number SEPARATOR { ins (OUT,0); /* imprimer le résultat de l'expression */ }
  72. | IF instruction { $1.ic_goto = ic; ins (JNZ,0); }
  73. LA bloc RA { $1.ic_false = ic; ins (JMP,0); instruction[$1.ic_goto].second = ic; }
  74. ELSE LA bloc RA { instruction[$1.ic_false].second = ic; }
  75. | VARIABLE EQUAL number SEPARATOR { variable[$1] = $3; }
  76. ;
  77.  
  78. number:
  79. NUMBER { ins(NUMBER, $1); }
  80. | LP number RP { }
  81. | instruction { }
  82. | VARIABLE { ins(VARIABLE, variable[$1]); } /*if(!variable.count($1)) UnknownVarError($1); else $$ = variable[$1];*/
  83. ;
  84.  
  85. instruction:
  86. number PLUS number { ins('+', 0); }
  87. | number MINUS number { ins('-', 0); }
  88. | number MULTIPLY number { ins('*', 0); }
  89. | number DIVIDE number { ins('/', 0); }
  90. ;
  91.  
  92. %%
  93.  
  94. void Div0Error(void) {
  95. printf("[Erreur] Division par zero impossible\n");
  96. exit (0);
  97. }
  98. void UnknownVarError(string s) {
  99. printf("[Erreur] La variable %s n'existe pas.\n", s.c_str ());
  100. exit (0);
  101. }
  102.  
  103. double unstack(vector<double> &stack) {
  104. double t = stack[stack.size()-1];
  105. stack.pop_back();
  106. return t;
  107. }
  108.  
  109. void start(){
  110. vector<double> stack;
  111. double x,y;
  112.  
  113. ic = 0;
  114. while ( ic < instruction.size() ){
  115. auto ins = instruction[ic];
  116.  
  117. switch(ins.first){
  118. case '+':
  119. x = unstack(stack);
  120. y = unstack(stack);
  121. stack.push_back(y+x);
  122. ic++;
  123. break;
  124.  
  125. case '*':
  126. x = unstack(stack);
  127. y = unstack(stack);
  128. stack.push_back(y*x);
  129. ic++;
  130. break;
  131.  
  132. case NUMBER :
  133. stack.push_back(ins.second);
  134. ic++;
  135. break;
  136.  
  137. case JMP :
  138. ic = ins.second;
  139. break;
  140.  
  141. case JNZ :
  142. x = unstack(stack);
  143. ic = ( x ? ic + 1 : ins.second);
  144. break;
  145.  
  146. case PRINT :
  147. cout << unstack(stack) << endl;
  148. ic++;
  149. break;
  150. }
  151. }
  152. }
  153.  
  154. int main(int argc, char **argv) {
  155. srand (time(NULL));
  156. if(argc != 2) {
  157. printf("[Erreur] Entrez la commande suivante: ./agnes.Ag nomFichier.a\n");
  158. exit (0);
  159. }
  160. FILE* file = fopen(argv[1],"r");
  161. if(file == NULL) {
  162. printf("[Erreur] Aucun fichier %s trouvé, passage en mode console", argv[1]);
  163. yyin = stdin;
  164. }
  165. else{
  166. yyin = file; // now flex reads from file
  167. }
  168. yyparse();
  169. start();
  170. fclose(file);
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement