228 lines
3.9 KiB
Plaintext
228 lines
3.9 KiB
Plaintext
|
|
%{
|
|
|
|
#include <stdio.h>
|
|
#include <iostream>
|
|
#include "parser.tab.hh" /* bison-generated header with YY[SL]TYPE */
|
|
#include "parser.h"
|
|
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 DASSIGN;
|
|
%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;
|
|
%token STRING_LITERAL;
|
|
|
|
/* primitive types */
|
|
%token BYTE;
|
|
%token UBYTE;
|
|
%token CHAR;
|
|
%token WCHAR;
|
|
%token SHORT;
|
|
%token USHORT;
|
|
%token INT;
|
|
%token UINT;
|
|
%token LONG;
|
|
%token ULONG;
|
|
%token FLOAT;
|
|
%token DOUBLE;
|
|
|
|
/* keywords */
|
|
%token C;
|
|
%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
|
|
| c_statement
|
|
;
|
|
|
|
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: IDENTIFIER LPAREN parameter_list RPAREN COLON type 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
|
|
| SHORT
|
|
| USHORT
|
|
| 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: IDENTIFIER COLON type
|
|
;
|
|
|
|
ptr_type: type STAR
|
|
;
|
|
|
|
array_type: type LBRACKET RBRACKET
|
|
;
|
|
|
|
type: primitive_type
|
|
| ptr_type
|
|
| struct_type
|
|
| array_type
|
|
| IDENTIFIER
|
|
;
|
|
|
|
statement: expression SEMICOLON
|
|
| return_statement
|
|
| c_statement
|
|
;
|
|
|
|
return_statement: RETURN expression SEMICOLON
|
|
;
|
|
|
|
expression: assign_expr
|
|
;
|
|
|
|
assign_expr: lvalue ASSIGN expression
|
|
| lvalue DASSIGN expression
|
|
;
|
|
|
|
lvalue: IDENTIFIER
|
|
;
|
|
|
|
c_statement: C LPAREN STRING_LITERAL RPAREN SEMICOLON {
|
|
}
|
|
;
|
|
|
|
%%
|
|
|
|
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);
|
|
}
|