Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. %language "c++"
  2. %require "3.0.4"
  3. %defines
  4. %define parser_class_name{ mlaskal_parser }
  5. %define api.token.constructor
  6. %define api.token.prefix{DUTOK_}
  7. %define api.value.type variant
  8. %define parse.assert
  9. %define parse.error verbose
  10.  
  11. %locations
  12. %define api.location.type{ unsigned }
  13.  
  14. %code requires
  15. {
  16. // this code is emitted to du3g.hpp
  17.  
  18. // allow references to semantic types in %type
  19. #include "dutables.hpp"
  20.  
  21. // avoid no-case warnings when compiling du3g.hpp
  22. #pragma warning (disable:4065)
  23.  
  24. // adjust YYLLOC_DEFAULT macro for our api.location.type
  25. #define YYLLOC_DEFAULT(res,rhs,N) (res = (N)?YYRHSLOC(rhs, 1):YYRHSLOC(rhs, 0))
  26. // supply missing YY_NULL in bfexpg.hpp
  27. #define YY_NULL 0
  28. #define YY_NULLPTR 0
  29. }
  30.  
  31. %param{ mlc::yyscan_t2 yyscanner } // formal name "yyscanner" is enforced by flex
  32. %param{ mlc::MlaskalCtx* ctx }
  33.  
  34. %start mlaskal
  35. // why? %locations
  36.  
  37. %code
  38. {
  39. // this code is emitted to du3g.cpp
  40.  
  41. // declare yylex here
  42. #include "bisonflex.hpp"
  43. YY_DECL;
  44.  
  45. // allow access to context
  46. #include "dutables.hpp"
  47.  
  48. // other user-required contents
  49. #include <assert.h>
  50. #include <stdlib.h>
  51.  
  52. /* local stuff */
  53. using namespace mlc;
  54.  
  55. }
  56.  
  57. %token EOF 0 "end of file"
  58.  
  59. %token PROGRAM /* program */
  60. %token LABEL /* label */
  61. %token CONST /* const */
  62. %token TYPE /* type */
  63. %token VAR /* var */
  64. %token BEGIN /* begin */
  65. %token END /* end */
  66. %token PROCEDURE /* procedure */
  67. %token FUNCTION /* function */
  68. %token ARRAY /* array */
  69. %token OF /* of */
  70. %token GOTO /* goto */
  71. %token IF /* if */
  72. %token THEN /* then */
  73. %token ELSE /* else */
  74. %token WHILE /* while */
  75. %token DO /* do */
  76. %token REPEAT /* repeat */
  77. %token UNTIL /* until */
  78. %token FOR /* for */
  79. %token OR /* or */
  80. %token NOT /* not */
  81. %token RECORD /* record */
  82.  
  83. /* literals */
  84. %token<mlc::ls_id_index> IDENTIFIER /* identifier */
  85. %token<mlc::ls_int_index> UINT /* unsigned integer */
  86. %token<mlc::ls_real_index> REAL /* real number */
  87. %token<mlc::ls_str_index> STRING /* string */
  88.  
  89. /* delimiters */
  90. %token SEMICOLON /* ; */
  91. %token DOT /* . */
  92. %token COMMA /* , */
  93. %token EQ /* = */
  94. %token COLON /* : */
  95. %token LPAR /* ( */
  96. %token RPAR /* ) */
  97. %token DOTDOT /* .. */
  98. %token LSBRA /* [ */
  99. %token RSBRA /* ] */
  100. %token ASSIGN /* := */
  101.  
  102. /* grouped operators and keywords */
  103. %token<mlc::DUTOKGE_OPER_REL> OPER_REL /* <, <=, <>, >=, > */
  104. %token<mlc::DUTOKGE_OPER_SIGNADD> OPER_SIGNADD /* +, - */
  105. %token<mlc::DUTOKGE_OPER_MUL> OPER_MUL /* *, /, div, mod, and */
  106. %token<mlc::DUTOKGE_FOR_DIRECTION> FOR_DIRECTION /* to, downto */
  107.  
  108. %%
  109. mlaskal: PROGRAM
  110. IDENTIFIER
  111. SEMICOLON
  112. blockP
  113. DOT
  114. ;
  115.  
  116.  
  117. /* list of uints */
  118.  
  119. uints: UINT
  120. | uints COMMA UINT ;
  121.  
  122. /* list of identifiers */
  123.  
  124. identifiers: IDENTIFIER |
  125. identifiers COMMA IDENTIFIER ;
  126.  
  127. /* constants */
  128.  
  129. unsigned_constant: IDENTIFIER |
  130. UINT |
  131. REAL |
  132. STRING ;
  133.  
  134. uint_or_real: UINT |
  135. REAL ;
  136.  
  137. constant: unsigned_constant |
  138. OPER_SIGNADD uint_or_real ;
  139.  
  140. id_or_uint: UINT |
  141. IDENTIFIER ;
  142.  
  143. sign_add_opt: OPER_SIGNADD |
  144. ;
  145.  
  146. ordinal_constant: sign_add_opt id_or_uint ; /* id = integer constant identifier */
  147.  
  148. var_opt: VAR |
  149. ;
  150.  
  151. /* formal parameters */
  152.  
  153. formal_parameter: var_opt identifiers COLON IDENTIFIER ; /* type identifier */
  154.  
  155. formal_parameters: formal_parameters SEMICOLON formal_parameter |
  156. formal_parameter ;
  157.  
  158. /* procedure and function */
  159.  
  160. parenthesized_formal_params: /* parameters in parentheses */
  161. LPAR formal_parameters RPAR ;
  162.  
  163. parenthesized_formal_params_opt:
  164. parenthesized_formal_params |
  165. ;
  166.  
  167. /* types */
  168.  
  169. field: identifiers COLON type ;
  170.  
  171. field_list: field_list SEMICOLON field |
  172. field ;
  173.  
  174. record_block: RECORD END |
  175. RECORD field_list END |
  176. RECORD field_list SEMICOLON END ;
  177.  
  178. structured_type: record_block ;
  179.  
  180. type: IDENTIFIER | /* type identifier */
  181. structured_type ;
  182.  
  183. /* variable */
  184.  
  185. variable: IDENTIFIER | /* variable identifier */
  186. variable DOT IDENTIFIER ; /* record variable . field identifier */
  187.  
  188. /* factor */
  189.  
  190. factors: factors OPER_MUL factor |
  191. factor ;
  192.  
  193. factor: unsigned_constant |
  194. variable |
  195. IDENTIFIER parenthesized_real_parameters_opt | /* function identifier */
  196. LPAR expression RPAR |
  197. NOT factor ;
  198.  
  199. /* term */
  200.  
  201. oper_signadd_or: OPER_SIGNADD |
  202. OR ;
  203.  
  204. term: factors ;
  205.  
  206. terms: terms oper_signadd_or term |
  207. term ;
  208.  
  209. /* expression */
  210.  
  211. complete_oper_rel: OPER_REL |
  212. EQ ;
  213.  
  214. expression: simple_expression complete_oper_rel simple_expression |
  215. simple_expression ;
  216.  
  217. expressions: expression |
  218. expressions COMMA expression ; /* variable subset of expression */
  219.  
  220. simple_expression: sign_add_opt terms ;
  221.  
  222. /* real parameters */
  223. real_parameters: expressions ;
  224.  
  225. parenthesized_real_parameters:
  226. LPAR real_parameters RPAR ;
  227.  
  228. parenthesized_real_parameters_opt:
  229. parenthesized_real_parameters |
  230. ;
  231.  
  232. /* statement */
  233. uint_opt: UINT |
  234. ;
  235.  
  236. else_opt: ELSE statement |
  237. ;
  238.  
  239. if_then_else: IF expression THEN statement else_opt ;
  240.  
  241. statement_exclusive:
  242. variable ASSIGN expression | /* variable or function identifier, which is variable too */
  243. IDENTIFIER parenthesized_real_parameters_opt | /* procedure identifier */
  244. GOTO UINT |
  245. BEGIN statements END |
  246. if_then_else |
  247. WHILE expression DO statement |
  248. REPEAT statements UNTIL expression |
  249. FOR IDENTIFIER ASSIGN expression FOR_DIRECTION expression DO statement | /* ordinal type variable identifier, ordinal expression */
  250. ;
  251.  
  252. statement: uint_opt statement_exclusive;
  253.  
  254. statements: statements SEMICOLON statement |
  255. statement ;
  256.  
  257. /* block */
  258.  
  259. /* block label */
  260. b_label: LABEL uints SEMICOLON ;
  261.  
  262. /* block const */
  263. const_def: IDENTIFIER EQ constant SEMICOLON ;
  264.  
  265. const_defs: const_defs const_def |
  266. const_def ;
  267.  
  268. b_const: CONST const_defs ;
  269.  
  270. /* block type */
  271. type_def: IDENTIFIER EQ type SEMICOLON ;
  272.  
  273. type_defs: type_defs type_def |
  274. type_def ;
  275.  
  276. b_type: TYPE type_defs ;
  277.  
  278. /* block variables */
  279. var_def: identifiers EQ type SEMICOLON ;
  280.  
  281. var_defs: var_defs var_def |
  282. var_def ;
  283.  
  284. b_var: VAR var_defs ;
  285.  
  286. /* block of statements */
  287. b_statement: BEGIN statements END ;
  288.  
  289. b_label_opt: b_label |
  290. ;
  291.  
  292. b_const_opt: b_const |
  293. ;
  294.  
  295. b_type_opt: b_type |
  296. ;
  297.  
  298. b_var_opt: b_var |
  299. ;
  300.  
  301. block: b_label_opt b_const_opt b_type_opt b_var_opt b_statement ;
  302.  
  303. /* procedure and function header */
  304.  
  305. procedure_header: PROCEDURE IDENTIFIER parenthesized_formal_params_opt ;
  306.  
  307. function_header: FUNCTION IDENTIFIER parenthesized_formal_params_opt COLON IDENTIFIER ; /* scalar type identifier */
  308.  
  309. procedure_or_function_header:
  310. procedure_header |
  311. function_header ;
  312.  
  313. /* procedure and function */
  314.  
  315. procedure_or_function_def:
  316. procedure_or_function_header SEMICOLON block SEMICOLON ;
  317.  
  318. b_proc_or_func:
  319. b_proc_or_func SEMICOLON procedure_or_function_def |
  320. procedure_or_function_def ;
  321.  
  322. b_proc_or_func_opt:
  323. b_proc_or_func |
  324. ;
  325.  
  326. /* block P */
  327. blockP: b_label_opt b_const_opt b_type_opt b_var_opt b_proc_or_func_opt b_statement ;
  328.  
  329. %%
  330.  
  331.  
  332. namespace yy {
  333.  
  334. void mlaskal_parser::error(const location_type& l, const std::string& m)
  335. {
  336. message(DUERR_SYNTAX, l, m);
  337. }
  338.  
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement