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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. %union {
  2. int nombre;
  3. char id[30];
  4. }
  5. %{
  6. #include "../Symboles/table_symboles.h"
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #define TAILLE 1024
  11. int addr = 0;
  12. enum type_t type_courant;
  13. int * tab_instruc = malloc(sizeof(int)*TAILLE);
  14. int index_instruc = 0;
  15. %}
  16. %token tMAIN
  17. %token tOBRACKET tCBRACKET
  18. %token tOBRACE tCBRACE
  19. %token tINT
  20. %token tCONST
  21. %token tPV tCOMA#include <string.h>
  22. %token tMUL tDIV tADD tSUB tEQ
  23. %token<nombre> tNB tNBEXP
  24. %token<id> tID
  25. %token tPRINTF
  26. %token tERROR
  27. %token 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
  37. /******************************************** FAIRE LA GENERATION DU CODE ASSEMBLEUR DANS UN TABLEAU AVEC UN FPRINTF *******************/
  38. %%
  39. Main : tINT tMAIN tOBRACE Params tCBRACE Body { print(pile); printf("addr = %d\n",addr);} ;
  40. Params : { printf("Sans Params\n"); } ;
  41. Params : Param SuiteParams ;
  42. Param : DeclType tID { printf("Prametre : %s\n", $2); };
  43. SuiteParams : tCOMA Param SuiteParams ;
  44. SuiteParams : ;
  45. Body : tOBRACKET Instructions tCBRACKET { struct symbole_t symbole = {"Salut", 0x77b58af, INT, 1}; push(symbole, pile); } ;
  46. Instructions : Instruction Instructions ;
  47. Instructions : ;
  48. Instruction : Aff ;
  49. Instruction : Decl ;
  50. Instruction : Invocation tPV ;
  51. Instruction : If;
  52. Instruction : While;
  53. If : tIF tOBRACE Cond tCBRACE Body Else { printf("If reconnu\n"); };
  54. Else : tELSE If { printf("Else if reconnu\n"); };
  55. Else : tELSE Body { printf("Else reconnu\n"); };
  56. Else : ;
  57. While : tWHILE tOBRACE Cond tCBRACE Body { printf("While reconnu\n"); };
  58. Cond : E SuiteCond ;
  59. SuiteCond : ;
  60. SuiteCond : tAND E SuiteCond;
  61. SuiteCond : tOR E SuiteCond;
  62. Aff : tID tEQ E tPV { printf("%s prend une valeur\n", $1);} ; //besoin de get_address
  63. E : tNB { printf("Nombre\n");
  64. struct symbole_t symbole = ("", addr, INT, 1};
  65. push(symbole, pile);
  66. $$=addr;
  67. addr++;
  68. printf("AFC %d %d",addr,$1); } ;
  69. E : tNBEXP { printf("Nombre exp\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("AFC %d %d",addr,$1); };
  70. E : tID { printf("Id\n"); /*Faire un get_address sur la pile*/};
  71. E : E tMUL E { printf("Mul\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("MUL %d %d %d",addr, $1,$2);};
  72. E : E tDIV E { printf("Div\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("DIV %d %d %d",addr, $1,$2);};
  73. E : E tSUB E { printf("Sub\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("SOU %d %d %d",addr, $1,$2);};
  74. E : E tADD E { printf("Add\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("ADD %d %d %d",addr, $1,$2);}};
  75. E : Invocation { printf("Invoc\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("AFC %d %d",addr, $1);};
  76. E : tOBRACE E tCBRACE { printf("Parentheses\n"); $$=$2};
  77. E : tSUB E { printf("Moins\n"); printf("SUB %d 0 %d",addr,$2);};
  78. E : E tEQCOND E { printf("==\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("EQU %d %d %d",addr, $1,$3);};
  79. E : E tGT E { printf(">\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("SUP %d %d %d",addr, $1,$3);};
  80. E : E tLT E { printf("<\n"); struct symbole_t symbole = {"", addr, INT, 1}; push(symbole, pile); $$=addr; addr++; printf("SUP %d %d %d",addr, $1,$3);};
  81. E : tNOT E { printf("!\n"); };
  82. //Créer un champ isConst dans la table des symboles
  83. Decl : tCONST DeclType SuiteDeclConst { } ;
  84. SuiteDeclConst : tCOMA tID SuiteDeclConst ;
  85. SuiteDeclConst : tEQ E tPV { };
  86. SuiteDeclConst : tPV { };
  87. DeclType : tINT {type_courant = INT;} ;
  88. Decl : DeclType Decl SuiteDecl { } ;
  89. Decl : tID {push($1, 0, type_courant);};
  90. Decl : tID tEQ E {push($1,1, type_courant);} ;
  91. SuiteDecl : tCOMA Decl SuiteDecl { };
  92. SuiteDecl : tPV { };
  93. Invocation : tPRINTF tOBRACE tID tCBRACE { printf("Appel de printf sur %s\n", $3); } ;
  94. /*S : E tPV
  95. { printf("RES: %d\n", $1); }
  96. S
  97. | { printf("END\n"); }
  98. ;
  99. E : E tADD E { $$ = $1 + $3; }
  100. | E tSUB E { $$ = $1 - $3; }
  101. | tOB E tCB { $$ = $2; }
  102. | tNB { $$ = $1; }
  103. ;*/
  104. %%
  105. void main(void) {
  106. init();
  107. yyparse();
  108. }