131 lines
1.9 KiB
Plaintext
131 lines
1.9 KiB
Plaintext
|
|
%{
|
|
|
|
#include <stdio.h>
|
|
#include <iostream>
|
|
#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: INT_LITERAL
|
|
;
|
|
|
|
primitive_type: BYTE
|
|
| UBYTE
|
|
| CHAR
|
|
| WCHAR
|
|
| INT
|
|
| UINT
|
|
| LONG
|
|
| ULONG
|
|
;
|
|
|
|
struct_type: STRUCT
|
|
;
|
|
|
|
ptr_type: type STAR
|
|
;
|
|
|
|
type: primitive_type
|
|
| ptr_type
|
|
| struct_type
|
|
;
|
|
|
|
%%
|
|
|
|
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);
|
|
}
|