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 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 tOCROCH tCCROCH
  20. %token tINT
  21. %token tCONST
  22. %token tPV tCOMA
  23. %token tMUL tDIV tADD tSUB tEQ
  24. %token<nombre> tNB tNBEXP
  25. %token<id> tID
  26. %token tPRINTF
  27. %token tERROR
  28. %token<nombre> tIF tWHILE tELSE
  29. %token tLT tGT tEQCOND
  30. %token tAND tOR
  31. %token tADDR
  32. %left tLT tGT
  33. %left tEQCOND
  34. %left tAND tOR
  35. %left tNOT
  36. %left tADD tSUB
  37. %left tMUL tDIV
  38. %type<nombre> E Invocation DebutAff SuiteAffPointeur DebutAffPointeur EBeforeAddr
  39. //%type<nombre> E
  40. /******************************************** FAIRE LA GENERATION DU CODE ASSEMBLEUR DANS UN TABLEAU AVEC UN FPRINTF *******************/
  41. %%
  42. Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(); create_asm();} ;
  43. Params : { printf("Sans Params\n"); } ;
  44. Params : Param SuiteParams ;
  45. Param : Type tID { printf("Prametre : %s\n", $2); };
  46. SuiteParams : tCOMA Param SuiteParams ;
  47. SuiteParams : ;
  48. Body : tOBRACKET {inc_prof();} Instructions tCBRACKET {print(); reset_prof();} ;
  49. Instructions : Instruction Instructions ;
  50. Instructions : ;
  51. Instruction : Aff {};
  52. Instruction : Decl {};
  53. Instruction : Invocation tPV{};
  54. Instruction : If {};
  55. Instruction : While {};
  56. //On considère que la première ligne du code en ASM est la ligne 0
  57. If : tIF tOBRACE E tCBRACE {
  58. add_operation(JMF,$3,0,0); $1 = get_current_index() - 1;}
  59. Body {int current = get_current_index();
  60. patch($1,current + 1);
  61. add_operation(JMP,0,0,0);
  62. instructions_ligne_to_patch[get_prof()][nbs_instructions_to_patch[get_prof()]] = current;
  63. nbs_instructions_to_patch[get_prof()]++;
  64. pop();}
  65. Else {printf("If reconnu\n");};
  66. Else : tELSE If { printf("Else if reconnu\n"); };
  67. Else : tELSE Body { printf("Else reconnu\n"); int current = get_current_index();
  68. for (int i = 0; i< nbs_instructions_to_patch[get_prof()]; i++){
  69. patch(instructions_ligne_to_patch[get_prof()][i],current);
  70. }
  71. nbs_instructions_to_patch[get_prof()] = 0;
  72. };
  73. Else : {int current = get_current_index();
  74. for (int i = 0; i< nbs_instructions_to_patch[get_prof()]; i++){
  75. patch(instructions_ligne_to_patch[get_prof()][i],current);
  76. }
  77. nbs_instructions_to_patch[get_prof()] = 0;};
  78. While : tWHILE tOBRACE E tCBRACE {
  79. add_operation(JMF,$3,0,0);
  80. $1 = get_current_index() - 1;
  81. pop();}
  82. Body { printf("While reconnu\n");
  83. int current = get_current_index();
  84. patch($1,current + 1);
  85. add_operation(JMP,$1,0,0);};
  86. Aff : DebutAff tEQ E tPV {add_operation(COP, $1, $3,0); pop();} ;
  87. Aff : DebutAffPointeur tEQ E tPV {add_operation(WR,$1,$3,0); pop(); pop();};
  88. DebutAff : tID {struct symbole_t * symbole = get_variable($1); symbole->initialized = 1; $$=symbole->adresse; printf("%s prend une valeur\n", $1);};
  89. DebutAffPointeur : tMUL SuiteAffPointeur {add_operation(READ, $2, $2, 0); $$=$2;};
  90. DebutAffPointeur : SuiteAffPointeur {$$=$1;};
  91. SuiteAffPointeur : tMUL tID {struct symbole_t * symbole = get_variable($2); int addr = push("0_TEMPORARY", 1, symbole->type); add_operation(COP, addr,symbole->adresse,0); $$=addr;};
  92. SuiteAffPointeur : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1); int addr = push("0_TEMPORARY", 1, symbole->type); add_operation(AFC, addr,symbole->adresse,0); int addr2 = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3); add_operation(ADD,$3,addr,$3); $$=$3; pop(); pop();};
  93. E : tNB { int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
  94. E : tNBEXP { printf("Nombre exp\n"); int addr = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,$1,0); $$ = addr;};
  95. E : E tMUL E { printf("Mul\n"); add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
  96. E : E tDIV E { printf("Div\n"); add_operation(DIV, $1,$1,$3); $$ = $1; pop();};
  97. E : E tSUB E { printf("Sub\n"); add_operation(SOU,$1,$1,$3); $$ = $1; pop();};
  98. E : E tADD E { printf("Add\n"); add_operation(ADD,$1,$1,$3); $$ = $1; pop();};
  99. E : Invocation { printf("Invoc\n"); int addr = push("0_TEMPORARY", 1, integer); 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 = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr,0,0); add_operation(SOU, $2,$2,addr); $$ = $2; pop();};
  102. E : E tEQCOND E { printf("==\n"); add_operation(EQU,$1,$1,$3); $$ = $1; pop();};
  103. E : E tGT E { printf(">\n"); add_operation(SUP,$1,$1,$3); $$ = $1; pop();};
  104. E : E tLT E { printf("<\n"); add_operation(INF,$1,$1,$3); $$ = $1; pop();};
  105. E : tNOT E { printf("!\n"); };
  106. E : E tAND E {add_operation(MUL,$1,$1,$3); $$ = $1; pop();};
  107. E : E tOR E {add_operation(ADD,$1,$1,$3); $$ = $1; pop();} ;
  108. E : tMUL E { add_operation(READ, $2, $2, 0); $$=$2;};
  109. E : tADDR EBeforeAddr {add_operation(AFCA,$2, $2,0); $$=$2;};
  110. E : EBeforeAddr {$$=$1;};
  111. EBeforeAddr : tID tOCROCH E tCCROCH {struct symbole_t * symbole = get_variable($1); int addr = push("0_TEMPORARY", 1, symbole->type); add_operation(AFC, addr,symbole->adresse,0); int addr2 = push("0_TEMPORARY", 1, integer); add_operation(AFC, addr2, taille_types[symbole->type.base],0); add_operation(MUL,$3,addr2,$3); add_operation(ADD,$3,addr,$3); add_operation(READ,$3,$3,0); $$=$3; pop(); pop();};
  112. EBeforeAddr : tID { printf("Id\n"); struct symbole_t * symbole = get_variable($1); int addr = push("0_TEMPORARY", 1, symbole->type); add_operation(COP, addr,symbole->adresse,0); $$=addr;};
  113. //Créer un champ isConst dans la table des symboles
  114. Type : tINT {type_courant.base = INT; type_courant.pointeur_level = 0; type_courant.nb_blocs = 1; printf("Type int\n");} ;
  115. Type : Type tMUL {type_courant.pointeur_level++; printf("Type int *\n");};
  116. //SuiteType : tMUL SuiteType {type_courant.pointeur_level++; printf(" * en plus\n");} ;
  117. //SuiteType : ;
  118. Decl : Type SuiteDecl FinDecl ;
  119. Decl : tCONST Type SuiteDeclConst FinDeclConst;
  120. SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");};
  121. SuiteDecl : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
  122. SuiteDecl : tID tOCROCH tNB tCCROCH {type_courant.pointeur_level++; type_courant.nb_blocs = $3; push($1, 0, type_courant);} ;
  123. FinDecl : tPV { printf("Fin Decl\n");};
  124. FinDecl : tCOMA SuiteDecl FinDecl ;
  125. SuiteDeclConst : tID tEQ E {pop(); int addr = push($1,1, type_courant);};
  126. FinDeclConst : tPV;
  127. FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
  128. /* //Créer un champ isConst dans la table des symboles
  129. DeclType : tINT {type_courant = INT; printf("Type int\n");} ;
  130. Decl : tCONST DeclType SuiteDeclConst { } ;
  131. SuiteDeclConst : tCOMA tID SuiteDeclConst ;
  132. SuiteDeclConst : tEQ E tPV { };
  133. SuiteDeclConst : tPV { };
  134. Decl : DeclType Decl SuiteDecl { } ;
  135. Decl : tID {push($1, 0, type_courant);};
  136. Decl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);} ;
  137. SuiteDecl : tCOMA Decl SuiteDecl { };
  138. SuiteDecl : tPV { };
  139. */
  140. Invocation : tPRINTF tOBRACE tID tCBRACE { printf("Appel de printf sur %s\n", $3); } ;
  141. /*S : E tPV
  142. { printf("RES: %d\n", $1); }
  143. S
  144. | { printf("END\n"); }
  145. ;
  146. E : E tADD E { $$ = $1 + $3; }
  147. | E tSUB E { $$ = $1 - $3; }
  148. | tOB E tCB { $$ = $2; }
  149. | tNB { $$ = $1; }
  150. ;*/
  151. %%
  152. void main(void) {
  153. init();
  154. yyparse();
  155. }