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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. %}
  14. %token tMAIN
  15. %token tOBRACKET tCBRACKET
  16. %token tOBRACE tCBRACE
  17. %token tINT
  18. %token tCONST
  19. %token tPV tCOMA
  20. %token tMUL tDIV tADD tSUB tEQ
  21. %token<nombre> tNB tNBEXP
  22. %token<id> tID
  23. %token tPRINTF
  24. %token tERROR
  25. %token tIF tWHILE tELSE
  26. %token tLT tGT tEQCOND
  27. %token tAND tOR
  28. %left tAND tOR
  29. %left tNOT
  30. %left tLT tGT
  31. %left tEQCOND
  32. %left tADD tSUB
  33. %left tMUL tDIV
  34. %type<nombre> E Invocation
  35. //%type<nombre> E
  36. /******************************************** FAIRE LA GENERATION DU CODE ASSEMBLEUR DANS UN TABLEAU AVEC UN FPRINTF *******************/
  37. %%
  38. Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(); create_asm();} ;
  39. Params : { printf("Sans Params\n"); } ;
  40. Params : Param SuiteParams ;
  41. Param : DeclType tID { printf("Prametre : %s\n", $2); };
  42. SuiteParams : tCOMA Param SuiteParams ;
  43. SuiteParams : ;
  44. Body : tOBRACKET Instructions tCBRACKET { } ;
  45. Instructions : Instruction Instructions ;
  46. Instructions : ;
  47. Instruction : Aff {reset_temp_vars();};
  48. Instruction : Decl {reset_temp_vars();};
  49. Instruction : Invocation tPV{reset_temp_vars();};
  50. Instruction : If {reset_temp_vars();};
  51. Instruction : While {reset_temp_vars();};
  52. If : tIF tOBRACE Cond tCBRACE Body Else { printf("If reconnu\n"); };
  53. Else : tELSE If { printf("Else if reconnu\n"); };
  54. Else : tELSE Body { printf("Else reconnu\n"); };
  55. Else : ;
  56. While : tWHILE tOBRACE Cond tCBRACE Body { printf("While reconnu\n"); };
  57. Cond : E SuiteCond ;
  58. SuiteCond : ;
  59. SuiteCond : tAND E SuiteCond;
  60. SuiteCond : tOR E SuiteCond;
  61. Aff : tID tEQ E tPV { printf("%s prend une valeur\n", $1);} ; //besoin de get_address
  62. E : tNB { int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
  63. E : tNBEXP { printf("Nombre exp\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
  64. E : tID { printf("Id\n"); /*Faire un get_address sur la pile*/};
  65. E : E tMUL E { printf("Mul\n"); int addr = allocate_mem_temp_var(INT); add_operation(MUL, addr,$1,$3); $$ = addr;};
  66. E : E tDIV E { printf("Div\n"); int addr = allocate_mem_temp_var(INT); add_operation(DIV, addr,$1,$3); $$ = addr;};
  67. E : E tSUB E { printf("Sub\n"); int addr = allocate_mem_temp_var(INT); add_operation(SOU, addr,$1,$3); $$ = addr;};
  68. E : E tADD E { printf("Add\n"); int addr = allocate_mem_temp_var(INT); add_operation(ADD, addr,$1,$3); $$ = addr;};
  69. E : Invocation { printf("Invoc\n"); int addr = allocate_mem_temp_var(INT); add_operation(AFC, addr,$1,0); $$ = addr;};
  70. E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2;};
  71. E : tSUB E { printf("Moins\n"); int addr = allocate_mem_temp_var(INT); add_operation(SOU, 0,addr,0); $$ = addr;};
  72. E : E tEQCOND E { printf("==\n"); int addr = allocate_mem_temp_var(INT); add_operation(EQU, addr,$1,$3); $$ = addr;};
  73. E : E tGT E { printf(">\n"); int addr = allocate_mem_temp_var(INT); add_operation(SUP, addr,$1,$3); $$ = addr;};
  74. E : E tLT E { printf("<\n"); int addr = allocate_mem_temp_var(INT); add_operation(INF, addr,$1,$3); $$ = addr;};
  75. E : tNOT E { printf("!\n"); };
  76. //Créer un champ isConst dans la table des symboles
  77. DeclType : tINT {type_courant = INT; printf("Type int\n");} ;
  78. Decl : DeclType SuiteDecl FinDecl ;
  79. Decl : tCONST DeclType SuiteDeclConst FinDeclConst;
  80. SuiteDecl : tID {push($1, 0, type_courant); printf("Suite Decl\n");};
  81. SuiteDecl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);};
  82. FinDecl : tPV { printf("Fin Decl\n");};
  83. FinDecl : tCOMA SuiteDecl FinDecl ;
  84. SuiteDeclConst : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);};
  85. FinDeclConst : tPV;
  86. FinDeclConst : tCOMA SuiteDeclConst FinDeclConst;
  87. /* //Créer un champ isConst dans la table des symboles
  88. DeclType : tINT {type_courant = INT; printf("Type int\n");} ;
  89. Decl : tCONST DeclType SuiteDeclConst { } ;
  90. SuiteDeclConst : tCOMA tID SuiteDeclConst ;
  91. SuiteDeclConst : tEQ E tPV { };
  92. SuiteDeclConst : tPV { };
  93. Decl : DeclType Decl SuiteDecl { } ;
  94. Decl : tID {push($1, 0, type_courant);};
  95. Decl : tID tEQ E {int addr = push($1,1, type_courant); add_operation(AFC, addr,$3,0);} ;
  96. SuiteDecl : tCOMA Decl SuiteDecl { };
  97. SuiteDecl : tPV { };
  98. */
  99. Invocation : tPRINTF tOBRACE tID tCBRACE { printf("Appel de printf sur %s\n", $3); } ;
  100. /*S : E tPV
  101. { printf("RES: %d\n", $1); }
  102. S
  103. | { printf("END\n"); }
  104. ;
  105. E : E tADD E { $$ = $1 + $3; }
  106. | E tSUB E { $$ = $1 - $3; }
  107. | tOB E tCB { $$ = $2; }
  108. | tNB { $$ = $1; }
  109. ;*/
  110. %%
  111. void main(void) {
  112. init();
  113. yyparse();
  114. }