le compilateur super performant
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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. %{
  2. #include "stdio.h"
  3. #include "stdlib.h"
  4. #include "symbol_table.h"
  5. enum Type current_type = TYPE_INT;
  6. SymbolTable symbol_table;
  7. %}
  8. %union {
  9. char* symbol_name;
  10. int nombre;
  11. }
  12. %token<nombre> tNB
  13. %token tADD
  14. %token tSUB
  15. %token tMUL
  16. %token tDIV
  17. %token tPO
  18. %token tPF
  19. %token tPV
  20. %token tVIRG
  21. %token tAO
  22. %token tAF
  23. %token tEQ
  24. %token tEQ2
  25. %token tNOTEQ
  26. %token tNOT
  27. %token tINF
  28. %token tINFEQ
  29. %token tSUP
  30. %token tSUPEQ
  31. %token tAND
  32. %token tOR
  33. %token tINT
  34. %token tCONST
  35. %token tMAIN
  36. %token tIF
  37. %token tELSE
  38. %token tELSIF
  39. %token tWHILE
  40. %token tPRINTF
  41. %token<symbol_name> tID
  42. %left tADD
  43. %left tSUB
  44. %left tMUL
  45. %left tDIV
  46. // %type<nombre> E
  47. %%
  48. S : Main { printf("S\n"); } ;
  49. Main : tINT tMAIN tPO Params tPF MainBody { printf("Main\n"); } ;
  50. Params : | Param SuiteParams { printf("Params\n"); } ;
  51. Param : tINT tID { printf("Param\n"); } ;
  52. SuiteParams : tVIRG Param SuiteParams { printf("SuiteParam\n"); } ;
  53. MainBody : tAO Declarations Instructions tAF { printf("MainBody\n"); } ;
  54. Body : tAO Instructions tAF { printf("Body\n"); } ;
  55. Instructions : Instruction Instructions | { printf("Instructions\n"); } ;
  56. Instruction : Aff | Printf { printf("Instruction\n"); } ;
  57. Aff : tID tEQ E tPV { mark_symbol_initialized(&symbol_table, $1); } ;
  58. E : tNB { printf("%d\n", $1); } | tID | E tADD E | E tMUL E | E tSUB E | E tDIV E | tPO E tPF | tSUB E ;
  59. Declarations : | Declaration Declarations { printf("Declarations\n"); } ;
  60. Declaration : DeclarationInt | DeclarationConst { printf("Declaration\n"); } ;
  61. DeclarationInt : tINT { current_type = TYPE_INT; } DeclarationBody tPV ;
  62. DeclarationConst : tCONST { current_type = TYPE_CONST; } DeclarationBody tPV ;
  63. DeclarationBody : VariableIdentifier tVIRG DeclarationBody | VariableIdentifier ;
  64. VariableIdentifier: tID { add_symbol(&symbol_table, current_type, $1); }
  65. // While : tWHILE tPO Cond tPF Body { printf("While\n"); } ;
  66. //
  67. // If : IfSimple | IfElse { printf("If\n"); } ;
  68. //
  69. // IfSimple : tIF tPO Cond tPF Body { printf("IfSimple\n"); } ;
  70. //
  71. // IfElse : IfSimple tELSE Body { printf("IfElse\n"); } ;
  72. //
  73. // Cond : tNOT Cond | Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | E tINFEQ E | E tSUP E | E tSUPEQ E | E tNOTEQ E | tNOT tPO Cond tPF { printf("Cond\n"); } ;
  74. Printf : tPRINTF tPO tID tPF tPV { printf("Printf\n"); } ;
  75. // If : tIF tPO Cond tPF Body ;
  76. //
  77. // Cond : Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | tNOT Cond ;
  78. // Invocation : tID tPO Args tPF ;
  79. // Args : .... cf params
  80. // Return : tRET E tPV ;
  81. %%
  82. #include <string.h>
  83. #define LABEL_SIZE 10
  84. // ADD @ + @ -> @
  85. // SUB @ - @ -> @
  86. // AFC nb -> @
  87. // CPY @ -> @
  88. // Exemple
  89. // y = 1 + 2;
  90. // AFC 1 32
  91. // AFC 2 33
  92. // ADD 32 33 32
  93. // CPY 32 @y
  94. /*
  95. table des symboles (tS) :
  96. sous forme de pile
  97. -> où sera la variable lors de l'exec
  98. |
  99. | nom | type | @ | init |
  100. |-------|--------|-----|--------|
  101. | a | | 0 | |
  102. | b | | 1 | |
  103. | c | | 2 | |
  104. | | | | |
  105. | . | | . | | -> entiers sur 1 octet
  106. | . | | . | |
  107. | . | | . | |
  108. | | | | |
  109. | y | | 26 | |
  110. Donc on remplace @y par 26 dans CPY 32 @y
  111. /!\
  112. E + E + E
  113. | | |
  114. -> -> -> vt variable temporaire (autre expression)
  115. -> -> -> variable -> ADD @E1 @E2 vt
  116. -> -> -> nombre
  117. Donc 9 possibilités (3 par E)
  118. On suppose que E est tout le temps une vt, donc on génère tout le temps le même code
  119. */
  120. void init() {
  121. init_table(&symbol_table);
  122. }
  123. void yyerror(char const* err) {
  124. printf("%s\n", err);
  125. exit(1);
  126. }
  127. void main(void) {
  128. init();
  129. yyparse();
  130. print_table(&symbol_table);
  131. }