No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

analyse_syntaxique.y 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. %union {
  2. int nombre;
  3. char id[30];
  4. }
  5. %{
  6. #include <stdio.h>
  7. #include "table_symboles.h"
  8. #include "table_fonctions.h"
  9. #include "gen_assembleur.h"
  10. enum Initialised_Variable init;
  11. enum Symbole_Type type;
  12. enum Return_Type return_type;
  13. Table_Symboles table;
  14. Table_Fonctions table_fonctions;
  15. instructions_array array;
  16. int whileCondition;
  17. int return_value;
  18. %}
  19. %token<nombre> tENTIER
  20. %token<nombre> tENTIEREXP
  21. %type<nombre> E
  22. %type<nombre> Return Instructions
  23. %type<nombre> Cond
  24. %type<nombre> While Else Invocation
  25. %token tADD
  26. %token tSUB
  27. %token tMUL
  28. %token tDIV
  29. %token<nombre> tPO
  30. %token tPF
  31. %token tAO
  32. %token tAF
  33. %token tERROR
  34. %token tAPPERSAND
  35. %token tPV
  36. %token tVIRGULE
  37. %token tAFFECTATION
  38. %token tEGAL
  39. %token tDIFF
  40. %token tLT
  41. %token tGT
  42. %token tGTE
  43. %token tLTE
  44. %token tMAIN
  45. %token tINT
  46. %token tPRINT
  47. %token tRETURN
  48. %token tOR
  49. %token tAND
  50. %token<nombre> tIF
  51. %token tELSE
  52. %token<nombre> tWHILE
  53. %token tCONST
  54. %token<id> tVAR
  55. %token tNOT
  56. %left tADD
  57. %left tSUB
  58. %left tMUL
  59. %left tDIV
  60. %right tEGAL
  61. %%
  62. /*C : Fonctions Main ;
  63. Fonctions : ;
  64. Fonctions : Fonction Fonctions ;
  65. Fonction : tINT tVAR tPO Params tPF Body;*/
  66. C : {generate_instruction_1(&array, JMP, -1);} Fonctions;
  67. Fonctions: Main;
  68. Fonctions: Fonction Fonctions;
  69. Main : tINT tMAIN {update_jmp(&array, 0, array.index); add_function(&table_fonctions, "Main", RET_INT, array.index); table.depth++;} tPO Params tPF Body {print_table(&table);remove_symboles(&table); table.depth--;};
  70. Fonction : Function_type tVAR {{add_function(&table_fonctions, $2, return_type, array.index); table.depth++;}} tPO Params tPF Body {print_table(&table);remove_symboles(&table); table.depth--;};
  71. Function_type: tINT {type = TYPE_INT;} ;
  72. Function_type: tINT tMUL {type = TYPE_INT_PTR;};
  73. Params : {} ;
  74. Params : Param SuiteParams ;
  75. Param : Param_type tVAR {add_symbole_top(&table, $2, type, INITIALISED, table.depth);} ;
  76. Param_type: tINT {type = TYPE_INT;} ;
  77. Param_type: tINT tMUL {type = TYPE_INT_PTR;};
  78. SuiteParams : tVIRGULE Param SuiteParams ;
  79. SuiteParams : ;
  80. Body : tAO Instructions Return tAF {} ;
  81. Instructions : Instruction Instructions {$$ = array.index;};
  82. Instructions : {$$ = array.index;};
  83. Instruction : Aff ;
  84. Instruction : If ;
  85. Instruction : While ;
  86. Instruction : Print ;
  87. Instruction : Decl ;
  88. Instruction : Invocation tPV ;
  89. Decl : Type Valeur SuiteDecl tPV ;
  90. SuiteDecl: tVIRGULE Valeur SuiteDecl ;
  91. SuiteDecl: ;
  92. Type : tINT {type = TYPE_INT;} ;
  93. Type : tCONST tINT {type = TYPE_CONST_INT;} ;
  94. Type : tINT tMUL {type = TYPE_INT_PTR;};
  95. Valeur : tVAR {add_symbole_top(&table, $1, type, INITIALISED, table.depth);} tAFFECTATION E {int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, varAddr, $4); free_temp(&table);};
  96. Valeur : tVAR {add_symbole_top(&table, $1, type, NOT_INITIALISED, table.depth);};
  97. Aff : tVAR tAFFECTATION E tPV {int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, varAddr, $3); free_temp(&table); };
  98. Aff : tMUL tVAR tAFFECTATION E tPV {int varAddr = variable_exists(&table, $2); generate_instruction_2(&array, COP_STR, varAddr, $4); free_temp(&table); };
  99. E : tENTIER {int vt = new_temp(&table); generate_instruction_2(&array, AFC, vt, $1); $$ = vt;};
  100. E : tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $1); generate_instruction_2(&array, COP, vt, varAddr); $$ = vt;};
  101. E : E tADD E {generate_instruction_3(&array, ADD, $1, $1, $3); free_temp(&table); $$ = $1;} ;
  102. E : E tMUL E {generate_instruction_3(&array, MUL, $1, $1, $3); free_temp(&table); $$ = $1;} ;
  103. E : E tSUB E {generate_instruction_3(&array, SOU, $1, $1, $3); free_temp(&table); $$ = $1;} ;
  104. E : E tDIV E {generate_instruction_3(&array, DIV, $1, $1, $3); free_temp(&table); $$ = $1;} ;
  105. E : tSUB E {printf("Variable negative\n");} ;
  106. E : Invocation {
  107. //int vt = new_temp(&table);
  108. //generate_instruction_2(&array, COP, vt, $1);
  109. remove_symboles(&table);
  110. table.depth--;
  111. $$ = $1;};
  112. E : tPO E tPF {printf("Parenthèse\n"); $$ = $2; } ;
  113. E : tAPPERSAND tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $2); generate_instruction_2(&array, LEA, vt, varAddr); $$ = vt;};
  114. E : tMUL tVAR {int vt = new_temp(&table); int varAddr = variable_exists(&table, $2); generate_instruction_2(&array, COP, vt, varAddr); generate_instruction_2(&array, COP_LD, vt, vt); $$ = vt;};
  115. If : tIF tPO Cond tPF {
  116. //gen_jmpf(&table, &array, $3, -1);
  117. generate_instruction_2(&array, JMF, $3, -1);
  118. free_temp(&table);
  119. $1 = array.index;
  120. }
  121. tAO {table.depth++;} Instructions {generate_instruction_1(&array, JMP, -1);} tAF {remove_symboles(&table); table.depth--;}
  122. {
  123. int adr_jmp = array.index;
  124. update_jmf(&array, $1, adr_jmp);
  125. }
  126. Else {printf("updating jump\n"); update_jmp(&array, $8, $13);};
  127. Else : tELSE tAO {table.depth++;} Instructions tAF {remove_symboles(&table); table.depth--;} {$$ = array.index;} ;
  128. Else : {$$ = array.index;};
  129. Else : tELSE If {$$ = array.index;} ;
  130. While : tWHILE tPO {
  131. $2 = array.index ;
  132. } Cond tPF {
  133. //gen_jmpf(&table, &array, $4, -1);
  134. generate_instruction_2(&array, JMF, $4, -1);
  135. free_temp(&table);
  136. $1 = array.index;
  137. }
  138. tAO {table.depth++;} Instructions tAF {remove_symboles(&table); table.depth--;} {
  139. int adr_jmp = array.index;
  140. update_jmf(&array, $1, adr_jmp);
  141. //gen_jmpf(&table, &array, $1, $2);
  142. generate_instruction_1(&array, JMP, $2);
  143. };
  144. Cond : E tEGAL E {generate_instruction_3(&array, EQ, $1, $1, $3); free_temp(&table); $$ = $3;};
  145. Cond : E tDIFF E {generate_instruction_3(&array, NEQ, $1, $1, $3); free_temp(&table); $$ = $3;} ;
  146. Cond : E tLT E {generate_instruction_3(&array, LT, $1, $1, $3); free_temp(&table); $$ = $3;} ;
  147. Cond : E tGT E {generate_instruction_3(&array, GT, $1, $1, $3); free_temp(&table); $$ = $3;} ;
  148. Cond : E tLTE E {generate_instruction_3(&array, LTE, $1, $1, $3); free_temp(&table); $$ = $3;} ;
  149. Cond : E tGTE E {generate_instruction_3(&array, GTE, $1, $1, $3); free_temp(&table); $$ = $3;} ;
  150. Cond : E tAND E {generate_instruction_3(&array, AND, $1, $1, $3); free_temp(&table); $$ = $3;} ;
  151. Cond : E tOR E {generate_instruction_3(&array, OR, $1, $1, $3); free_temp(&table); $$ = $3;} ;
  152. Cond : tNOT Cond {generate_instruction_2(&array, NOT, $2, $2); $$ = $2;} ;
  153. Cond : E {$$ = $1; };
  154. Invocation : tVAR tPO {table.depth++; prepare_function_call(&table); return_value = (table.indexAvailableBottom);} Args tPF
  155. {int function_index = function_exists(&table_fonctions, $1);
  156. int jmp_addr = (table_fonctions.array[function_index]).start_addr;
  157. generate_instruction_2(&array, CALL, jmp_addr, table.indexAvailableTop);
  158. $$ = return_value;
  159. };
  160. Args : Arg SuiteArgs ;
  161. Args :
  162. Arg : E {int arg_addr = prepare_argument_push(&table); generate_instruction_2(&array, COP, arg_addr, $1); free_temp(&table);};
  163. SuiteArgs : tVIRGULE Arg SuiteArgs ;
  164. SuiteArgs : ;
  165. Print : tPRINT tPO E tPF tPV {generate_instruction_1(&array, PRI, $3); free_temp(&table);};
  166. Return : tRETURN E tPV {$$ = generate_instruction_1(&array, RET, $2); free_temp(&table);};
  167. %%
  168. #include <stdio.h>
  169. void main(void){
  170. //TODO: rajouter gestion des erreurs
  171. initialise_table(&table);
  172. initialise_function_table(&table_fonctions);
  173. initialise_asm(&array);
  174. yyparse();
  175. print_table(&table);
  176. printf("\n");
  177. print_fonction_table(&table_fonctions);
  178. //remove_symboles(&table, 0);
  179. //print_table(&table);
  180. exportInstructions(&array);
  181. }