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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. struct 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. %token tADDR
  31. %left tAND tOR
  32. %left tNOT
  33. %left tLT tGT
  34. %left tEQCOND
  35. %left tADD tSUB
  36. %left tMUL tDIV
  37. %type<nombre> E Invocation DebutAff
  38. //%type<nombre> E
  39. /******************************************** FAIRE LA GENERATION DU CODE ASSEMBLEUR DANS UN TABLEAU AVEC UN FPRINTF *******************/
  40. %%
  41. Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(); create_asm();} ;
  42. Params : { printf("Sans Params\n"); } ;
  43. Params : Param SuiteParams ;
  44. Param : Type tID { printf("Prametre : %s\n", $2); };
  45. SuiteParams : tCOMA Param SuiteParams ;
  46. SuiteParams : ;
  47. Body : tOBRACKET {profondeur++;} Instructions tCBRACKET {print(); reset_pronf(); profondeur--;} ;
  48. Instructions : Instruction Instructions ;
  49. Instructions : ;
  50. Instruction : Aff {reset_temp_vars();};
  51. Instruction : Decl {reset_temp_vars();};
  52. Instruction : Invocation tPV{reset_temp_vars();};
  53. Instruction : If {reset_temp_vars();};
  54. Instruction : While {reset_temp_vars();};
  55. //On considère que la première ligne du code en ASM est la ligne 0
  56. If : tIF tOBRACE E tCBRACE {
  57. add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
  58. Body {int current = get_current_index();
  59. patch($1,current + 1);
  60. add_operation(JMP,0,0,0);
  61. instructions_ligne_to_patch[profondeur][nbs_instructions_to_patch[profondeur]] = current;
  62. nbs_instructions_to_patch[profondeur]++;}
  63. Else { printf("If reconnu\n");};
  64. Else : tELSE If { printf("Else if reconnu\n"); };
  65. Else : tELSE Body { printf("Else reconnu\n"); int current = get_current_index();
  66. for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
  67. patch(instructions_ligne_to_patch[profondeur][i],current);
  68. }
  69. nbs_instructions_to_patch[profondeur] = 0;
  70. };
  71. Else : {int current = get_current_index();
  72. for (int i = 0; i< nbs_instructions_to_patch[profondeur]; i++){
  73. patch(instructions_ligne_to_patch[profondeur][i],current);
  74. }
  75. nbs_instructions_to_patch[profondeur] = 0;};
  76. While : tWHILE tOBRACE E tCBRACE {
  77. add_operation(JMF,$3,0,0);
  78. $1 = get_current_index() - 1;}
  79. Body { printf("While reconnu\n");
  80. int current = get_current_index();
  81. patch($1,current + 1);
  82. add_operation(JMP,$1,0,0);};
  83. Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0);} ; //besoin de get_address
  84. DebutAff : tID {struct symbole_t * symbole = get_variable($1); symbole->initialized = 1; $$=symbole->adresse; printf("%s prend une valeur\n", $1);};
  85. DebutAff : tMUL E { add_operation(GET, $2, $2, 0); $$=$2;};
  86. DebutAff : tADDR tID { int addr = allocate_mem_temp_var(INT); struct symbole_t * symbole = get_variable($2); add_operation(AFC,addr, symbole->adresse,0); $$=addr;};
  87. E : tNB { int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
  88. E : tNBEXP { printf("Nombre exp\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
  89. E : tID { printf("Id\n"); struct symbole_t * symbole = get_variable($1); int addr = allocate_mem_temp_var(symbole->type.base); add_operation(COP, addr,symbole->adresse,0); $$=addr;};
  90. E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();};
  91. E : E tDIV E { printf("Div\n"); add_operation(DIV, $1,$1,$3); $$ = $1; decrement_temp_var();};
  92. E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; decrement_temp_var();};
  93. E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; decrement_temp_var();};
  94. E : Invocation { printf("Invoc\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
  95. E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
  96. E : tSUB E { printf("Moins\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,0,0); add_operation(SOU, $2,$2,addr); $$ = $2; decrement_temp_var();};
  97. E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; decrement_temp_var();};
  98. E : E tGT E { printf(">\n"); add_operation(SUP,$1,$1,$3); $$ = $1; decrement_temp_var();};
  99. E : E tLT E { printf("<\n"); add_operation(INF,$1,$1,$3); $$ = $1; decrement_temp_var();};
  100. E : tNOT E { printf("!\n"); };
  101. E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; decrement_temp_var();};
  102. E : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; decrement_temp_var();} ;
  103. E : tMUL E { add_operation(GET, $2, $2, 0); $$=$2;};
  104. E : tADDR tID { int addr = allocate_mem_temp_var(INT); struct symbole_t * symbole = get_variable($2); add_operation(AFC,addr, symbole->adresse,0); $$=addr;};
  105. //Créer un champ isConst dans la table des symboles
  106. Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; printf("Type int\n");} ;
  107. Type : Type tMUL {type_courant.pointeur_level++; printf("Type int *\n");};
  108. //SuiteType : tMUL SuiteType {type_courant.pointeur_level++; printf(" * en plus\n");} ;
  109. //SuiteType : ;
  110. Decl : Type SuiteDecl FinDecl ;
  111. Decl : tCONST Type 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. }