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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 ;
  45. Main : tINT tMAIN tPO Params tPF MainBody ;
  46. Params : | Param SuiteParams ;
  47. Param : tINT tID ;
  48. SuiteParams : tVIRG Param SuiteParams ;
  49. MainBody : tAO Declarations Instructions tAF ;
  50. Body : tAO Instructions tAF ;
  51. Instructions : Instruction Instructions | ;
  52. Instruction : Aff | Printf | If ;
  53. Aff : tID tEQ E tPV ;
  54. E : tNB | tID | E tADD E | E tMUL E | E tSUB E | E tDIV E | tPO E tPF | tSUB E ;
  55. Declarations : | Declaration Declarations ;
  56. Declaration : DeclarationInt | DeclarationConst ;
  57. DeclarationInt : tINT DeclarationBody tPV ;
  58. DeclarationConst : tCONST DeclarationBody tPV ;
  59. DeclarationBody : tID tVIRG DeclarationBody | tID ;
  60. If : IfSimple | IfElse ;
  61. IfSimple : tIF tPO Cond tPF Body ;
  62. IfElse : IfSimple tELSE Body ;
  63. 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 Cond | tNOT tPO Cond tPF ;
  64. Printf : tPRINTF tPO tID tPF tPV
  65. // If : tIF tPO Cond tPF Body ;
  66. //
  67. // Cond : Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | tNOT Cond ;
  68. // Invocation : tID tPO Args tPF ;
  69. // Args : .... cf params
  70. // Return : tRET E tPV ;
  71. /* S -> E ; S
  72. * S ->
  73. */
  74. // S : E tPV
  75. // { printf("RES: %d\n", $1); }
  76. // S
  77. // | { printf("END\n"); }
  78. // ;
  79. //
  80. // E : E tADD E { $$ = $1 + $3; }
  81. // | E tSUB E { $$ = $1 - $3; }
  82. // | tOB E tCB { $$ = $2; }
  83. // | tNB { $$ = $1; }
  84. // ;
  85. %%
  86. void yyerror(char const* err) {
  87. printf("%s\n", err);
  88. exit(1);
  89. }
  90. void main(void) {
  91. yyparse();
  92. }