Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %language "c++"
- %require "3.0.4"
- %defines
- %define parser_class_name{ mlaskal_parser }
- %define api.token.constructor
- %define api.token.prefix{DUTOK_}
- %define api.value.type variant
- %define parse.assert
- %define parse.error verbose
- %locations
- %define api.location.type{ unsigned }
- %code requires
- {
- // this code is emitted to du3g.hpp
- // allow references to semantic types in %type
- #include "dutables.hpp"
- // avoid no-case warnings when compiling du3g.hpp
- #pragma warning (disable:4065)
- // adjust YYLLOC_DEFAULT macro for our api.location.type
- #define YYLLOC_DEFAULT(res,rhs,N) (res = (N)?YYRHSLOC(rhs, 1):YYRHSLOC(rhs, 0))
- // supply missing YY_NULL in bfexpg.hpp
- #define YY_NULL 0
- #define YY_NULLPTR 0
- }
- %param{ mlc::yyscan_t2 yyscanner } // formal name "yyscanner" is enforced by flex
- %param{ mlc::MlaskalCtx* ctx }
- %start mlaskal
- // why? %locations
- %code
- {
- // this code is emitted to du3g.cpp
- // declare yylex here
- #include "bisonflex.hpp"
- YY_DECL;
- // allow access to context
- #include "dutables.hpp"
- // other user-required contents
- #include <assert.h>
- #include <stdlib.h>
- /* local stuff */
- using namespace mlc;
- }
- %token EOF 0 "end of file"
- %token PROGRAM /* program */
- %token LABEL /* label */
- %token CONST /* const */
- %token TYPE /* type */
- %token VAR /* var */
- %token BEGIN /* begin */
- %token END /* end */
- %token PROCEDURE /* procedure */
- %token FUNCTION /* function */
- %token ARRAY /* array */
- %token OF /* of */
- %token GOTO /* goto */
- %token IF /* if */
- %token THEN /* then */
- %token ELSE /* else */
- %token WHILE /* while */
- %token DO /* do */
- %token REPEAT /* repeat */
- %token UNTIL /* until */
- %token FOR /* for */
- %token OR /* or */
- %token NOT /* not */
- %token RECORD /* record */
- /* literals */
- %token<mlc::ls_id_index> IDENTIFIER /* identifier */
- %token<mlc::ls_int_index> UINT /* unsigned integer */
- %token<mlc::ls_real_index> REAL /* real number */
- %token<mlc::ls_str_index> STRING /* string */
- /* delimiters */
- %token SEMICOLON /* ; */
- %token DOT /* . */
- %token COMMA /* , */
- %token EQ /* = */
- %token COLON /* : */
- %token LPAR /* ( */
- %token RPAR /* ) */
- %token DOTDOT /* .. */
- %token LSBRA /* [ */
- %token RSBRA /* ] */
- %token ASSIGN /* := */
- /* grouped operators and keywords */
- %token<mlc::DUTOKGE_OPER_REL> OPER_REL /* <, <=, <>, >=, > */
- %token<mlc::DUTOKGE_OPER_SIGNADD> OPER_SIGNADD /* +, - */
- %token<mlc::DUTOKGE_OPER_MUL> OPER_MUL /* *, /, div, mod, and */
- %token<mlc::DUTOKGE_FOR_DIRECTION> FOR_DIRECTION /* to, downto */
- %%
- mlaskal: PROGRAM
- IDENTIFIER
- SEMICOLON
- blockP
- DOT
- ;
- /* list of uints */
- uints: UINT
- | uints COMMA UINT ;
- /* list of identifiers */
- identifiers: IDENTIFIER |
- identifiers COMMA IDENTIFIER ;
- /* constants */
- unsigned_constant: IDENTIFIER |
- UINT |
- REAL |
- STRING ;
- uint_or_real: UINT |
- REAL ;
- constant: unsigned_constant |
- OPER_SIGNADD uint_or_real ;
- id_or_uint: UINT |
- IDENTIFIER ;
- sign_add_opt: OPER_SIGNADD |
- ;
- ordinal_constant: sign_add_opt id_or_uint ; /* id = integer constant identifier */
- var_opt: VAR |
- ;
- /* formal parameters */
- formal_parameter: var_opt identifiers COLON IDENTIFIER ; /* type identifier */
- formal_parameters: formal_parameters SEMICOLON formal_parameter |
- formal_parameter ;
- /* procedure and function */
- parenthesized_formal_params: /* parameters in parentheses */
- LPAR formal_parameters RPAR ;
- parenthesized_formal_params_opt:
- parenthesized_formal_params |
- ;
- /* types */
- field: identifiers COLON type ;
- field_list: field_list SEMICOLON field |
- field ;
- record_block: RECORD END |
- RECORD field_list END |
- RECORD field_list SEMICOLON END ;
- structured_type: record_block ;
- type: IDENTIFIER | /* type identifier */
- structured_type ;
- /* variable */
- variable: IDENTIFIER | /* variable identifier */
- variable DOT IDENTIFIER ; /* record variable . field identifier */
- /* factor */
- factors: factors OPER_MUL factor |
- factor ;
- factor: unsigned_constant |
- variable |
- IDENTIFIER parenthesized_real_parameters_opt | /* function identifier */
- LPAR expression RPAR |
- NOT factor ;
- /* term */
- oper_signadd_or: OPER_SIGNADD |
- OR ;
- term: factors ;
- terms: terms oper_signadd_or term |
- term ;
- /* expression */
- complete_oper_rel: OPER_REL |
- EQ ;
- expression: simple_expression complete_oper_rel simple_expression |
- simple_expression ;
- expressions: expression |
- expressions COMMA expression ; /* variable subset of expression */
- simple_expression: sign_add_opt terms ;
- /* real parameters */
- real_parameters: expressions ;
- parenthesized_real_parameters:
- LPAR real_parameters RPAR ;
- parenthesized_real_parameters_opt:
- parenthesized_real_parameters |
- ;
- /* statement */
- uint_opt: UINT |
- ;
- else_opt: ELSE statement |
- ;
- if_then_else: IF expression THEN statement else_opt ;
- statement_exclusive:
- variable ASSIGN expression | /* variable or function identifier, which is variable too */
- IDENTIFIER parenthesized_real_parameters_opt | /* procedure identifier */
- GOTO UINT |
- BEGIN statements END |
- if_then_else |
- WHILE expression DO statement |
- REPEAT statements UNTIL expression |
- FOR IDENTIFIER ASSIGN expression FOR_DIRECTION expression DO statement | /* ordinal type variable identifier, ordinal expression */
- ;
- statement: uint_opt statement_exclusive;
- statements: statements SEMICOLON statement |
- statement ;
- /* block */
- /* block label */
- b_label: LABEL uints SEMICOLON ;
- /* block const */
- const_def: IDENTIFIER EQ constant SEMICOLON ;
- const_defs: const_defs const_def |
- const_def ;
- b_const: CONST const_defs ;
- /* block type */
- type_def: IDENTIFIER EQ type SEMICOLON ;
- type_defs: type_defs type_def |
- type_def ;
- b_type: TYPE type_defs ;
- /* block variables */
- var_def: identifiers EQ type SEMICOLON ;
- var_defs: var_defs var_def |
- var_def ;
- b_var: VAR var_defs ;
- /* block of statements */
- b_statement: BEGIN statements END ;
- b_label_opt: b_label |
- ;
- b_const_opt: b_const |
- ;
- b_type_opt: b_type |
- ;
- b_var_opt: b_var |
- ;
- block: b_label_opt b_const_opt b_type_opt b_var_opt b_statement ;
- /* procedure and function header */
- procedure_header: PROCEDURE IDENTIFIER parenthesized_formal_params_opt ;
- function_header: FUNCTION IDENTIFIER parenthesized_formal_params_opt COLON IDENTIFIER ; /* scalar type identifier */
- procedure_or_function_header:
- procedure_header |
- function_header ;
- /* procedure and function */
- procedure_or_function_def:
- procedure_or_function_header SEMICOLON block SEMICOLON ;
- b_proc_or_func:
- b_proc_or_func SEMICOLON procedure_or_function_def |
- procedure_or_function_def ;
- b_proc_or_func_opt:
- b_proc_or_func |
- ;
- /* block P */
- blockP: b_label_opt b_const_opt b_type_opt b_var_opt b_proc_or_func_opt b_statement ;
- %%
- namespace yy {
- void mlaskal_parser::error(const location_type& l, const std::string& m)
- {
- message(DUERR_SYNTAX, l, m);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement