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

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