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.

analyse_syntaxique.y 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. %union {
  2. int nombre;
  3. char id[30];
  4. }
  5. %{
  6. #include <stdio.h>
  7. #include "table_symboles.h"
  8. enum Initialised_Variable init;
  9. enum Symbole_Type type;
  10. Table_Symboles table;
  11. %}
  12. %token<nombre> tENTIER
  13. %token<nombre> tENTIEREXP
  14. %token tADD
  15. %token tSUB
  16. %token tMUL
  17. %token tDIV
  18. %token tPO
  19. %token tPF
  20. %token tAO
  21. %token tAF
  22. %token tERROR
  23. %token tPV
  24. %token tVIRGULE
  25. %token tAFFECTATION
  26. %token tEGAL
  27. %token tDIFF
  28. %token tLT
  29. %token tGT
  30. %token tGTE
  31. %token tLTE
  32. %token tMAIN
  33. %token tINT
  34. %token tPRINT
  35. %token tRETURN
  36. %token tOR
  37. %token tAND
  38. %token tIF
  39. %token tELSE
  40. %token tWHILE
  41. %token tCONST
  42. %token<id> tVAR
  43. %token tNOT
  44. %left tADD
  45. %left tSUB
  46. %left tMUL
  47. %left tDIV
  48. %right tEGAL
  49. %%
  50. //C : Fonctions Main ;
  51. //Fonctions : Fonction Fonctions | ;
  52. //Fonction : tINT tVAR tPO Params tPF Body;
  53. Main : tINT tMAIN tPO Params tPF Body {};
  54. Params : {printf("Sans params\n");} ;
  55. Params : Param SuiteParams ;
  56. Param : tINT tVAR {printf("Parametre : %s\n", $2);} ;
  57. SuiteParams : tVIRGULE Param SuiteParams tPV ;
  58. SuiteParams : ;
  59. Body : tAO Instructions Return tAF {printf("Dans body\n");} ;
  60. Instructions : Instruction Instructions ;
  61. Instructions : ;
  62. Instruction : Aff ;
  63. Instruction : If ;
  64. Instruction : While ;
  65. Instruction : Print ;
  66. Instruction : Decl ;
  67. Instruction : Invocation tPV ;
  68. Decl : Type Valeur SuiteDecl tPV ;
  69. SuiteDecl: tVIRGULE Valeur SuiteDecl ;
  70. SuiteDecl: ;
  71. Type : tINT {type = TYPE_INT;} ;
  72. Type : tCONST tINT {type = TYPE_CONST_INT;} ;
  73. Valeur : tVAR tAFFECTATION E {add_symbole_top(&table, $1, type, INITIALISED, table.depth);};
  74. Valeur : tVAR {add_symbole_top(&table, $1, type, NOT_INITIALISED, table.depth);};
  75. Aff : tVAR tAFFECTATION E tPV {printf("Affectation : %s\n", $1);};
  76. E : tENTIER {printf("int %d\n", $1);};
  77. E : tVAR {printf("var %s\n", $1);};
  78. E : E tADD E {printf("Addition\n");} ;
  79. E : E tMUL E {printf("Multiplication\n");} ;
  80. E : E tSUB E {printf("Soustraction\n");} ;
  81. E : E tDIV E {printf("Division\n");} ;
  82. E : tSUB E {printf("Soustraction\n");} ;
  83. E : Invocation ;
  84. E : tPO E tPF {printf("Parenthèse\n");} ;
  85. Args : tVAR SuiteArgs ;
  86. Args : ;
  87. SuiteArgs : tVIRGULE tVAR SuiteArgs ;
  88. SuiteArgs : ;
  89. If : tIF tPO Cond tPF tAO Instructions tAF Else {printf("Dans if\n");};
  90. Else : tELSE tAO Instructions tAF {printf("else\n");} ;
  91. Else : tELSE tIF tPO Cond tPF tAO Instructions tAF Else {printf("elsif\n");} ;
  92. While : tWHILE tPO Cond tPF tAO Instructions tAF {printf("Dans while\n");};
  93. Cond : E tEGAL E {printf("Cond ==\n");} ;
  94. Cond : E tDIFF E {printf("Cond !=\n");} ;
  95. Cond : E tLT E {printf("Cond <\n");} ;
  96. Cond : E tGT E {printf("Cond >\n");} ;
  97. Cond : E tLTE E {printf("Cond <=\n");} ;
  98. Cond : E tGTE E{printf("Cond >=\n");} ;
  99. Cond : E tAND Cond {printf("Cond &&\n");} ;
  100. Cond : E tOR Cond {printf("Cond ||\n");} ;
  101. Cond : tNOT Cond {printf("Cond !\n");} ;
  102. Invocation : tVAR tPO Args tPF {printf("Dans invocation\n");};
  103. Print : tPRINT tPO tVAR tPF tPV {printf("printf de %s\n", $3);};
  104. Return : tRETURN E tPV {printf("return\n");};
  105. %%
  106. #include <stdio.h>
  107. void main(void){
  108. //TODO: rajouter gestion des erreurs
  109. initialise_table(&table);
  110. yyparse();
  111. print_table(&table);
  112. remove_symboles(&table, 0);
  113. print_table(&table);
  114. }