Advertisement
Guest User

Untitled

a guest
Jul 28th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.95 KB | None | 0 0
  1. %token EOF
  2. %token <string> ID
  3. %token <int> INT
  4. %token <string> STRING
  5. %token COMMA COLON SEMICOLON LPAREN RPAREN LBRACKET RBRACKET LBRACE RBRACE DOT PLUS MINUS EQ NEQ LT LE GT GE OR ARRAY IF THEN ELSE WHILE FOR TO DO LET IN END OF BREAK NIL FUNCTION VAR TYPE AND COLON_EQ
  6. %token UMINUS
  7. %token TIMES DIVIDE
  8.  
  9. %right OF
  10. %nonassoc COLON_EQ
  11. %nonassoc THEN
  12. %nonassoc ELSE
  13. %left AND OR
  14. %nonassoc EQ NEQ LT LE GT GE
  15. %left PLUS MINUS
  16. %left TIMES DIVIDE
  17. %nonassoc UMINUS
  18.  
  19. %type<unit>program
  20.  
  21. %start program
  22.  
  23. %%
  24.  
  25. decs:
  26. | {}
  27. | decs dec {}      
  28. ;
  29.  
  30. type_id:
  31. | ID {}
  32. ;
  33.  
  34. dec:
  35. | tydec {}
  36. | vardec {}  
  37. | fundec {}
  38. ;
  39.  
  40. tydec:
  41. | TYPE type_id EQ ty {}
  42. ;
  43.  
  44. ty:
  45. | type_id {}
  46. | LBRACE tyfields RBRACE {}
  47. | ARRAY OF type_id {}
  48. ;
  49.  
  50. tyfields:
  51. | {}
  52. | ID COLON type_id {}
  53. | tyfields COMMA ID COLON type_id {}
  54. ;
  55.  
  56. vardec:
  57. | VAR ID COLON EQ exp {}
  58. | VAR ID COLON type_id COLON EQ exp {}
  59. ;
  60.  
  61. procedure_declaration:
  62. | FUNCTION ID LPAREN tyfields RPAREN EQ exp {}
  63. ;
  64.  
  65. fundec:
  66. | procedure_declaration {}
  67. | FUNCTION ID LPAREN tyfields RPAREN COLON type_id EQ exp {}
  68. ;
  69. program:
  70. | exp EOF {}
  71. ;
  72.  
  73. exp:
  74. (*lvalue*)
  75. | ID  {}
  76. | lvalue_not_ID {}
  77. | valueless_exp {}
  78. | NIL {}
  79. | sequencing {}
  80. | no_value {}
  81. | integer_literal {}
  82. | string_literal {}
  83. | negation {}
  84. (*arithmetic*)
  85. | exp PLUS exp {}
  86. | exp MINUS exp {}
  87. | exp TIMES exp {}
  88. | exp DIVIDE exp {}
  89. (*comparison*)
  90. | exp EQ exp {}
  91. | exp NEQ exp {}
  92. | exp GE exp {}
  93. | exp LE exp {}
  94. | exp GT exp {}
  95. | exp LT exp {}
  96. | boolean_operators {}
  97. | IF exp THEN exp {}
  98. | IF exp THEN exp ELSE exp  {}
  99. | record_creation {}
  100. | array_creation {}
  101. | let_exp {}
  102. | parentheses {}
  103. (*assignment*)
  104. | lvalue COLON_EQ exp {}
  105. ;
  106.  
  107. valueless_exp:
  108. | function_call {(*que si la fonction est une procédure*)}
  109. | while_exp {}
  110. | for_exp {}
  111. | break {}
  112. ;
  113.  
  114. string_literal:
  115. | STRING {}
  116. ;
  117.  
  118. integer_literal:
  119. | INT {print_int $1}
  120. ;
  121.  
  122. negation:
  123. | MINUS exp  %prec UMINUS {}
  124. ;
  125.  
  126. indirect:
  127. | ID LBRACKET exp RBRACKET {}
  128. ;
  129.  
  130. lvalue:
  131. | ID {}
  132. | lvalue_not_ID {}
  133. ;
  134.  
  135. lvalue_not_ID:
  136. | lvalue DOT ID {}
  137. | ID LBRACKET exp RBRACKET {}
  138. | lvalue_not_ID LBRACKET exp RBRACKET {}
  139. ;
  140.  
  141. sequencing:
  142. | LPAREN exp_seq RPAREN {}
  143. ;
  144.  
  145. exp_seq:
  146. | exp SEMICOLON exp {}
  147. | exp_seq SEMICOLON exp {}
  148. ;
  149.  
  150. no_value:
  151. | LPAREN RPAREN {}
  152. ;
  153.  
  154. function_call:
  155. | ID LPAREN RPAREN {}
  156. | ID LPAREN function_params RPAREN {}
  157. ;
  158.  
  159. function_params:
  160. | exp {}
  161. | function_params COMMA exp {}
  162. ;
  163.  
  164. boolean_operators:
  165. | exp AND exp {}
  166. | exp OR exp {}
  167. ;
  168.  
  169. record_creation:
  170. type_id LBRACE record_fields RBRACE {}
  171. ;
  172.  
  173. record_fields:
  174. | {}
  175. | ID EQ exp {}
  176. | record_fields COMMA ID EQ exp {}
  177. ;
  178.  
  179. array_creation:
  180. | indirect OF exp {}
  181. ;
  182.  
  183.  
  184. while_exp:
  185. | WHILE exp DO valueless_exp {}
  186. ;
  187.  
  188. for_exp:
  189. | FOR ID COLON_EQ exp TO exp DO valueless_exp {}
  190. ;
  191.  
  192. break:
  193. | BREAK {}
  194. ;
  195.  
  196. let_exp:
  197. | LET decs IN expseq END {}
  198. ;
  199.  
  200. parentheses:
  201. | LPAREN exp RPAREN {}
  202. ;
  203.  
  204. expseq:
  205. | {}
  206. | exp {}
  207. | expseq SEMICOLON exp {}
  208. ;
  209.  
  210. (*Sub expressions*)
  211.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement