-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
executable file
·190 lines (162 loc) · 5.36 KB
/
parser.y
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//---- DEFINITIONS ----------------------------------------------
%{
#include <stdarg.h>
#include <stdio.h>
#include "parser.tab.h"
#include "lexer.yy.h"
// Defined below in the User Code section.
extern void yyerror( YYLTYPE *, void *, void *, const char * );
extern void printfLoc( YYLTYPE loc, char *fmt, ... );
extern void fprintLoc( FILE *fp, YYLTYPE loc );
%}
%define api.pure full
%code provides
{
#define YY_DECL \
int yylex (YYSTYPE* yylval, YYLTYPE *yylloc, void* scanner)
YY_DECL;
}
%code requires {
#include "node.h"
typedef void *yyscan_t;
}
%lex-param { yyscan_t scanner }
%parse-param { yyscan_t scanner }
%parse-param { void **result }
%defines "parser.tab.h"
%locations
%union {
Node *node;
NodeStatement *stmt;
StatementList *stmtList;
NodeExpression *expr;
NodeInteger *intVal;
NodeDouble *doubleVal;
NodeString *stringVal;
NodeIdentifier *id;
NodeMethodCall *methodCall;
NodeBinaryOperator *bop;
NodeAssignment *assignment;
NodeBlock *block;
NodeExpressionStatement *exprStmt;
NodeReturnStatement *returnStmt;
NodeVariableDeclaration *varDecl;
NodeIfDeclaration *ifDecl;
NodeElifDeclaration *ElifDecl;
}
%token TOKEN_LET
%token TOKEN_BOP_ASSIGN
%token TOKEN_INTEGER TOKEN_REAL TOKEN_STRING
%token TOKEN_BREAK TOKEN_CONTINUE TOKEN_REPEAT TOKEN_WHILE
%token TOKEN_IF TOKEN_ELIF TOKEN_ELSE
%token '+' '-' '*' '/'
%token <id> TOKEN_ID
%token <intVal> TOKEN_LIT_INT
%token <doubleVal> TOKEN_LIT_REAL
%token <stringVal> TOKEN_LIT_STR
%type <block> block
%type <expr> expr
%type <stmt> stmt
%type <stmtList> stmtList
%type <exprStmt> exprStmt
%type <ifDecl> ifStmt
%type <varDecl> declStmt
%left '+' '-' // tok_UOP_NOT
%left '/' '*' '%'
%right '^'
%right NEGATE POSITE PERCENT
%% //---- RULES --------------------------------------------------
start
: block { *result = $1; }
;
block
: '{' stmtList ';' '}' { $$ = $2; }
| '{' '}' { $$ = new NodeBlock(); }
;
//-- Statements --------------------------------------------------
stmt
: block
| declStmt
| exprStmt
| ifStmt
;
stmtList
: stmt { $$ = new NodeBlock(); $$->StmtList.push_back($<stmt>1); }
| stmtList ';' stmt { $1->StmtList.push_back($<stmt>2); }
;
//-- Declaration -------------------------------------------------
declStmt
: TOKEN_LET TOKEN_ID ':' TOKEN_INTEGER TOKEN_BOP_ASSIGN TOKEN_LIT_INT {
$$ = new NodeVariableDeclaration( *$2, TOKEN_INTEGER, $6 );
}
| TOKEN_LET TOKEN_ID ':' TOKEN_REAL TOKEN_BOP_ASSIGN TOKEN_LIT_REAL {
$$ = new NodeVariableDeclaration( *$2, TOKEN_REAL, $6 );
}
| TOKEN_LET TOKEN_ID ':' TOKEN_STRING TOKEN_BOP_ASSIGN TOKEN_LIT_STR {
$$ = new NodeVariableDeclaration( *$2,TOKEN_STRING, $6 );
}
| TOKEN_LET TOKEN_ID ':' TOKEN_INTEGER {
$$ = new NodeVariableDeclaration( *$2, TOKEN_INTEGER, NULL );
}
| TOKEN_LET TOKEN_ID ':' TOKEN_REAL {
$$ = new NodeVariableDeclaration( *$2, TOKEN_REAL, NULL );
}
| TOKEN_LET TOKEN_ID ':' TOKEN_STRING {
$$ = new NodeVariableDeclaration( *$2, TOKEN_STRING, NULL );
}
;
//-- Expr statement ---------------------------------------------
exprStmt
: expr { $$ = new NodeExpressionStatement( *$1 ); }
;
//-- If statement ---------------------------------------------
ifStmt
: TOKEN_IF expr block TOKEN_ELSE block { $$ = new NodeIfDeclaration( $2, $3, $5 ); }
| TOKEN_IF expr block { $$ = new NodeIfDeclaration( $2, $3, NULL ); }
;
//-- Expressions -------------------------------------------------
// Binary Operators
expr
: expr '+' expr { $$ = new NodeBinaryOperator( *$1, '+', *$3 ); }
| expr '-' expr { $$ = new NodeBinaryOperator( *$1, '-', *$3 ); }
| expr '*' expr { $$ = new NodeBinaryOperator( *$1, '*', *$3 ); }
| expr '/' expr { $$ = new NodeBinaryOperator( *$1, '/', *$3 ); }
;
expr
: '(' expr ')' { $$ = $2; }
| TOKEN_ID
| TOKEN_LIT_INT
| TOKEN_LIT_REAL
| TOKEN_LIT_STR
;
%% //---- USER CODE ----------------------------------------------
void yyerror( YYLTYPE *llocp, void *_scanner, void *_result, const char *msg )
{
(void) _scanner; // Not used at present
(void) _result; // Not used at present
printfLoc( *llocp, "Error: %s\n", msg );
}
void printfLoc( YYLTYPE loc, char *fmt, ... )
{
fprintLoc( stdout, loc );
fputc( ' ', stdout );
va_list ap;
va_start( ap, fmt );
vprintf( fmt, ap );
va_end( ap );
}
void fprintLoc( FILE *fp, YYLTYPE loc )
{
if ( loc.first_line == loc.last_line ) {
if ( loc.first_column == loc.last_column ) {
fprintf( fp, "%d(%d)", loc.first_line, loc.first_column );
} else {
fprintf( fp, "%d(%d-%d)", loc.first_line, loc.first_column, loc.last_column );
}
} else {
fprintf( fp, "%d(%d)-%d(%d)",
loc.first_line, loc.first_column,
loc.last_line, loc.last_column );
}
}
//----------------------------------------------------------------