%{ #include #include #include "parser.tab.hh" /* bison-generated header with YY[SL]TYPE */ using namespace std; #define yyerror(msg) errFunc(msg, &yylloc) int yylex(YYSTYPE *, YYLTYPE *); extern FILE * yyin; void errFunc(const char * str, YYLTYPE * yyllocp); int yywrap() { return 1; } %} %pure-parser %locations %error-verbose /* operators */ %token ASSIGN; %token DEQUALS; %token DIVIDE; %token EQUALS; %token GREATER; %token LESS; %token MATCH; %token MINUS; %token MOD; %token PLUS; %token STAR; /* punctuation */ %token COLON; %token COMMA; %token DCOLON; %token DOLLAR; %token DOT; %token FROM; %token QUESTION; %token SEMICOLON; %token LCURLY; %token RCURLY; %token LBRACKET; %token RBRACKET; %token LPAREN; %token RPAREN; /* literals */ %token INT_LITERAL; %token REAL_LITERAL; /* primitive types */ %token BYTE; %token UBYTE; %token CHAR; %token WCHAR; %token INT; %token UINT; %token LONG; %token ULONG; /* keywords */ %token IMPORT; %token MODULE; %token RETURN; %token STRUCT; /* identifiers */ %token IDENTIFIER; %% program_items: /* empty */ | program_item program_items ; program_item: module_declaration | import | variable_declaration | function ; module_declaration: MODULE module_name SEMICOLON ; module_name: IDENTIFIER module_name_more ; module_name_more: /* empty */ | DOT IDENTIFIER module_name_more ; import: IMPORT import_name SEMICOLON ; import_name: IDENTIFIER import_name_more ; import_name_more: /* empty */ | DOT import_name_more_more ; import_name_more_more: IDENTIFIER import_name_more | STAR ; function: type IDENTIFIER LPAREN parameter_list RPAREN LCURLY function_items RCURLY ; function_items: /* empty */ | statement function_items ; parameter_list: /* empty */ | variable_spec parameter_list_more ; parameter_list_more: /* empty */ | COMMA variable_spec parameter_list_more ; primitive_type: BYTE | UBYTE | CHAR | WCHAR | INT | UINT | LONG | ULONG ; struct_type: STRUCT LCURLY struct_items RCURLY ; struct_items: /* empty */ | struct_item struct_items ; struct_item: variable_declaration ; variable_declaration: variable_spec SEMICOLON; ; variable_spec: type IDENTIFIER ; ptr_type: type STAR ; array_type: type LBRACKET RBRACKET ; type: primitive_type | ptr_type | struct_type | array_type | IDENTIFIER ; statement: expression SEMICOLON | return_statement SEMICOLON ; return_statement: RETURN expression ; expression: assign_expr ; assign_expr: lvalue ASSIGN expression ; lvalue: IDENTIFIER ; %% void parse(const char * fileName) { yyin = fopen(fileName, "r"); if (yyin == NULL) { cerr << "Failed to open file '" << fileName << "'" << endl; return; } if (yyparse()) { cerr << "Aborting." << endl; exit(1); } } void errFunc(const char * str, YYLTYPE * yyllocp) { fprintf(stderr, "error: %s: line %d, column %d\n", str, yyllocp->first_line, yyllocp->first_column); }