forked from lsegal/my_toy_compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
169 lines (137 loc) · 5.73 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
%{
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "node.h"
NBlock *programBlock;
extern int line;
int yyerror(const char *str)
{
printf("Error: %s at %d line\n", str, line);
}
extern int yylex();
%}
%union
{
Node *node;
NBlock *block;
NExpression *expr;
NStatement *stmt;
NIdentifier *ident;
NVariableDeclaration *var_decl;
NGlobalVariableDeclaration *global_var_decl;
std::vector<NVariableDeclaration*> *varvec;
std::vector<NExpression*> *exprvec;
std::string *string;
int token;
}
%token <string> TIDENTIFIER TINTEGER TDOUBLE TCHARACTER TINT TDBL TCHAR TVOID TSTR
%token <token> TCEQ TCNE TCLT TCLE TCGT TCGE TEQUAL
%token <token> TLPAREN TRPAREN TCOMMA TDOT TCOLON TSEMICOLON TSHARP TTILDE TLBRACKET TRBRACKET TLBRACE TRBRACE
%token <token> TPLUS TMINUS TMUL TDIV
%token <token> TRETURN TWHILE TEXTERN TENDWHILE TFOR TENDFOR
%token <token> TIF TELSE TENDIF
%token <token> TINC_INDENT TDEC_INDENT TCURR_INDENT TINDENT_ERR TBLANK
%type <ident> ident type
%type <expr> numeric expr
%type <varvec> func_decl_args
%type <exprvec> call_args
%type <block> program global_stmts local_stmts block
%type <stmt> local_stmt global_stmt var_decl global_var_decl func_decl extern_decl condition cycle array_decl
%type <token> comparison
%left TPLUS TMINUS
%left TMUL TDIV
%error-verbose
%start program
%%
program : global_stmts { programBlock = $1; }
;
global_stmts : global_stmt { $$ = new NBlock(); $$->statements.push_back($<stmt>1); }
| global_stmts global_stmt { $$->statements.push_back($<stmt>2); }
;
local_stmts : local_stmt { $$ = new NBlock(); $$->statements.push_back($<stmt>1); }
| local_stmts local_stmt { $$->statements.push_back($<stmt>2); }
;
global_stmt : func_decl
| global_var_decl
| extern_decl
| expr { $$ = new NExpressionStatement(*$1); }
;
local_stmt : var_decl
| array_decl
| cycle
| condition
| expr { $$ = new NExpressionStatement(*$1); }
| TRETURN expr { $$ = new NReturnStatement(*$2); }
;
block : TINC_INDENT local_stmts TDEC_INDENT { $$ = $2; }
| TINC_INDENT TBLANK TDEC_INDENT { $$ = new NBlock(); }
;
func_decl : type ident TLPAREN func_decl_args TRPAREN TCOLON block
{ $$ = new NFunctionDeclaration(*$1, *$2, *$4, *$7); delete $4; }
;
extern_decl : TEXTERN type ident TLPAREN func_decl_args TRPAREN
{ $$ = new NExternDeclaration(*$2, *$3, *$5); delete $5; }
;
global_var_decl : type ident { $$ = new NGlobalVariableDeclaration(*$1, *$2); }
;
var_decl : type ident { $$ = new NVariableDeclaration(*$1, *$2); }
| type ident TEQUAL expr { $$ = new NVariableDeclaration(*$1, *$2, $4); }
| type ident TEQUAL error { $$ = new NVariableDeclaration(*$1, *$2); }
;
array_decl : type ident TLBRACKET TINTEGER TRBRACKET { $$ = new NArrayDeclaration(*$1, *$2, atol($4->c_str())); }
| type ident TLBRACKET TRBRACKET TEQUAL TSTR { $$ = new NArrayDeclaration(*(new NIdentifier(*$1)), *$2, *$6);
delete $1;}
| type ident TLBRACKET TRBRACKET
TEQUAL TLBRACE call_args TRBRACE { $$ = new NArrayDeclaration(*$1, *$2, *$7); }
;
func_decl_args : /*blank*/ { $$ = new VariableList(); }
| var_decl { $$ = new VariableList(); $$->push_back($<var_decl>1); }
| func_decl_args TCOMMA var_decl { $1->push_back($<var_decl>3); }
;
ident : TIDENTIFIER { $$ = new NIdentifier(*$1); delete $1; }
;
numeric : TINTEGER { $$ = new NInteger(atol($1->c_str())); delete $1; }
| TDOUBLE { $$ = new NDouble(atof($1->c_str())); delete $1; }
| TMINUS TINTEGER { $$ = new NInteger(-atol($2->c_str())); delete $2; }
| TMINUS TDOUBLE { $$ = new NDouble(-atof($2->c_str())); delete $2; }
| TCHARACTER { $$ = new NChar($1->front()); delete $1; }
;
expr : ident TEQUAL expr { $$ = new NAssignment(*$<ident>1, *$3); }
| ident TLPAREN call_args TRPAREN { $$ = new NMethodCall(*$1, *$3); delete $3; }
| ident { $<ident>$ = $1; }
| numeric
| expr TMUL expr { $$ = new NBinaryOperator(*$1, $2, *$3); }
| expr TDIV expr { $$ = new NBinaryOperator(*$1, $2, *$3); }
| expr TPLUS expr { $$ = new NBinaryOperator(*$1, $2, *$3); }
| expr TPLUS error { $$ = $1; }
| expr TMINUS expr { $$ = new NBinaryOperator(*$1, $2, *$3); }
| expr comparison expr { $$ = new NBinaryOperator(*$1, $2, *$3); }
| TLPAREN expr TRPAREN { $$ = $2; }
| TLPAREN type TRPAREN TLPAREN expr TRPAREN { $$ = new NCastExpression(*$2, *$5); }
| ident TLBRACKET expr TRBRACKET { $$ = new NAccessToArray(*$1, *$3, NULL); }
| ident TLBRACKET expr TRBRACKET TEQUAL expr { $$ = new NAccessToArray(*$1, *$3, $6); }
;
call_args : /*blank*/ { $$ = new ExpressionList(); }
| expr { $$ = new ExpressionList(); $$->push_back($1); }
| call_args TCOMMA expr { $1->push_back($3); }
;
comparison : TCEQ
| TCNE
| TCLT
| TCLE
| TCGT
| TCGE
;
cycle : TWHILE TLPAREN expr TRPAREN block TENDWHILE { $$ = new NWhileStatement(*$3, *$5); }
| TFOR TLPAREN expr TSEMICOLON expr TSEMICOLON expr TRPAREN block TENDFOR { $$ = new NForStatement(*$3, *$5, *$7, *$9); }
;
condition : TIF TLPAREN expr TRPAREN block TELSE block TENDIF { $$ = new NConditionStatement(*$3, *$5, *$7); }
;
type : TINT { $$ = new NIdentifier(*$1); delete $1; }
| TDBL { $$ = new NIdentifier(*$1); delete $1; }
| TCHAR { $$ = new NIdentifier(*$1); delete $1; }
| TVOID { $$ = new NIdentifier(*$1); delete $1; }
| type TMUL { $1->name += '*'; }
;
%%