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.

as.y 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. %union {
  2. int nombre;
  3. char id[30];
  4. }
  5. %{
  6. #include "../Tables/Symboles/table_symboles.h"
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include "../Tables/Instructions/tab_instruc.h"
  11. #define TAILLE 1024
  12. enum type_t type_courant;
  13. int instructions_ligne_to_patch[10][20];
  14. int nbs_instructions_to_patch[10];
  15. %}
  16. %token tMAIN
  17. %token tOBRACKET tCBRACKET
  18. %token tOBRACE tCBRACE
  19. %token tINT
  20. %token tCONST
  21. %token tPV tCOMA
  22. %token tMUL tDIV tADD tSUB tEQ
  23. %token<nombre> tNB tNBEXP
  24. %token<id> tID
  25. %token tPRINTF
  26. %token tERROR
  27. %token<nombre> tIF tWHILE tELSE
  28. %token tLT tGT tEQCOND
  29. %token tAND tOR
  30. %left tAND tOR
  31. %left tNOT
  32. %left tLT tGT
  33. %left tEQCOND
  34. %left tADD tSUB
  35. %left tMUL tDIV
  36. %type<nombre> E Invocation
  37. //%type<nombre> E
  38. /******************************************** FAIRE LA GENERATION DU CODE ASSEMBLEUR DANS UN TABLEAU AVEC UN FPRINTF *******************/
  39. %%
  40. Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(); create_asm();} ;
  41. Params : { printf("Sans Params\n"); } ;
  42. Params : Param SuiteParams ;
  43. Param : DeclType tID { printf("Prametre : %s\n", $2); };
  44. SuiteParams : tCOMA Param SuiteParams ;
  45. SuiteParams : ;
  46. Body : tOBRACKET Instructions tCBRACKET { } ;
  47. Instructions : Instruction Instructions ;
  48. Instructions : ;
  49. Instruction : Aff {reset_temp_vars();};
  50. Instruction : Decl {reset_temp_vars();};
  51. Instruction : Invocation tPV{reset_temp_vars();};
  52. Instruction : If {reset_temp_vars();};
  53. Instruction : While {reset_temp_vars();};
  54. //On considère que la première ligne du code en ASM est la ligne 0
  55. If : tIF tOBRACE E tCBRACE { profondeur++;
  56. add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
  57. Body {int current = get_current_index();
  58. patch($1,current + 1);
  59. add_operation(JMP,0,0,0);
  60. instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
  61. nbs_instructions_to_patch[profondeur]++;}
  62. Else { printf("If reconnu\n"); reset_pronf(); profondeur--;};
  63. //On considère que la première ligne du code en ASM est la ligne 0
  64. ElseIf : tIF tOBRACE E tCBRACE {add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
  65. Body {int current = get_current_index();
  66. patch($1,current + 1);
  67. add_operation(JMP,0,0,0);
  68. instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
  69. nbs_instructions_to_patch[profondeur]++;}
  70. Else { printf("If reconnu\n");};
  71. Else : tELSE ElseIf { printf("Else if reconnu\n"); };
  72. Else : tELSE Body { printf("Else reconnu\n"); int current = get_current_index();
  73. for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
  74. patch(instructions_ligne_to_patch[profondeur][i],current);
  75. }
  76. nbs_instructions_to_patch[profondeur] = 0;
  77. };
  78. Else : {int current = get_current_index();
  79. for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
  80. patch(instructions_ligne_to_patch[profondeur][i],current);
  81. }
  82. nbs_instructions_to_patch[profondeur] = 0;};
  83. While : tWHILE tOBRACE E tCBRACE {profondeur++;
  84. add_operation(JMF,$3,0,0);
  85. $1 = get_current_index() - 1;}
  86. Body { printf("While reconnu\n");
  87. int current = get_current_index();
  88. patch($1,current + 1);
  89. add_operation(JMP,$1,0,0);
  90. reset_pronf(); profondeur--;};
  91. Aff : tID tEQ E tPV { printf("%s prend une valeur\n", $1); struct symbole_t * symbole = get_variable($1); symbole->initialized = 1; add_operation(COP, symbole->adresse, $3,0);} ; //besoin de get_address
  92. E : tNB { int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
  93. E : tNBEXP { printf("Nombre exp\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
  94. E : tID { printf("Id\n"); struct symbole_t * symbole = get_variable($1); int addr = allocate_mem_temp_var(symbole->type); add_operation(COP, addr,symbole->adresse,0); $$=addr;};
  95. E : E tMUL E { printf("Mul\n"); int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr,$1,$3); $$ = addr;};
  96. E : E tDIV E { printf("Div\n"); int addr = allocate_mem_temp_var(INT); add_operation(DIV, addr,$1,$3); $$ = addr;};
  97. E : E tSUB E { printf("Sub\n"); int addr = allocate_mem_temp_var(INT); add_operation(SOU, addr,$1,$3); $$ = addr;};
  98. E : E tADD E { printf("Add\n"); int addr = allocate_mem_temp_var(INT); add_operation(ADD, addr,$1,$3); $$ = addr;};
  99. E : Invocation { printf("Invoc\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
  100. E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
  101. E : tSUB E { printf("Moins\n"); int addr = allocate_mem_temp_var(INT); add_operation(SOU, 0,addr,0); $$ = addr;};
  102. E : E tEQCOND E { printf("==\n"); int addr = allocate_mem_temp_var(INT); add_operation(EQU, addr,$1,$3); $$ = addr;};
  103. E : E tGT E { printf(">\n"); int addr = allocate_mem_temp_var(INT); add_operation(SUP, addr,$1,$3); $$ = addr;};
  104. E : E tLT E { printf("<\n"); int addr = allocate_mem_temp_var(INT); add_operation(INF, addr,$1,$3); $$ = addr;};
  105. E : tNOT E { printf("!\n"); };
  106. E : E tAND E {int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr, $1, $3); $$=addr;};
  107. E : E tOR E {int addr = allocate_mem_temp_var(INT); add_operation(ADD, addr, $1, $3); $$=addr;} ;
  108. //Créer un champ isConst dans la table des symboles
  109. DeclType : tINT {type_courant = INT; printf("Type int\n");} ;
  110. Decl : DeclType SuiteDecl FinDecl ;
  111. Decl : tCONST DeclType SuiteDeclConst FinDeclConst;
  112. SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");};
  113. SuiteDecl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(COP, addr,$3,0);};
  114. FinDecl : tPV { printf("Fin Decl\n");};
  115. FinDecl : tCOMA SuiteDecl FinDecl ;
  116. SuiteDeclConst : tID tEQ E {int addr = push($1,1, type_courant); add_operation(COP, addr,$3,0);};
  117. FinDeclConst : tPV;
  118. FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
  119. /* //Créer un champ isConst dans la table des symboles
  120. DeclType : tINT {type_courant = INT; printf("Type int\n");} ;
  121. Decl : tCONST DeclType SuiteDeclConst { } ;
  122. SuiteDeclConst : tCOMA tID SuiteDeclConst ;
  123. SuiteDeclConst : tEQ E tPV { };
  124. SuiteDeclConst : tPV { };
  125. Decl : DeclType Decl SuiteDecl { } ;
  126. Decl : tID {push($1, 0, type_courant);};
  127. Decl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);} ;
  128. SuiteDecl : tCOMA Decl SuiteDecl { };
  129. SuiteDecl : tPV { };
  130. */
  131. Invocation : tPRINTF tOBRACE tID tCBRACE { printf("Appel de printf sur %s\n", $3); } ;
  132. /*S : E tPV
  133. { printf("RES: %d\n", $1); }
  134. S
  135. | { printf("END\n"); }
  136. ;
  137. E : E tADD E { $$ = $1 + $3; }
  138. | E tSUB E { $$ = $1 - $3; }
  139. | tOB E tCB { $$ = $2; }
  140. | tNB { $$ = $1; }
  141. ;*/
  142. %%
  143. void main(void) {
  144. init();
  145. yyparse();
  146. }