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

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