-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
269 lines (243 loc) · 5.54 KB
/
Copy pathparser.y
File metadata and controls
269 lines (243 loc) · 5.54 KB
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
%require "3.8"
%language "c++"
%define api.value.type variant
%define api.token.constructor
%define api.token.raw
%code requires {
#include "Ast.h"
#include <memory>
#include <string>
#include <vector>
#include <iostream>
class LexerWrapper;
}
%parse-param { LexerWrapper& lexer }
%lex-param { LexerWrapper& lexer }
%code {
#include "LexerWrapper.h"
extern std::vector<std::shared_ptr<AstNodeBase>> ast;
yy::parser::symbol_type yylex(LexerWrapper& lexer)
{
return lexer.yylex();
}
void yy::parser::error(const std::string& msg)
{
std::cerr << "Parse error: " << msg << std::endl;
}
}
%define api.token.prefix {PARSER_TOK_}
%token <int> INTEGER
%token <double> FLOAT
%token <std::string> STRING
%token <std::string> IDENTIFIER
%token
PLUS "+"
MINUS "-"
STAR "*"
SLASH "/"
PERCENT "%"
EQUAL "="
LPAREN "("
RPAREN ")"
LBRACE "{"
RBRACE "}"
SEMICOLON ";"
EQEQ "=="
NEQ "!="
LT "<"
LTE "<="
GT ">"
GTE ">="
;
%token TRUE_LIT "true"
%token FALSE_LIT "false"
%token IF_KW "if"
%token ELSE_KW "else"
%token WHILE_KW "while"
%token DO_KW "do"
%token <std::string> PREDEF_FUNCTION
%type <std::shared_ptr<AstNodeBase>> statement
%type <std::shared_ptr<AstNodeBase>> block
%type <std::shared_ptr<ExprNode>> expr
%type <std::shared_ptr<VarDeclaration>> varDecl
%type <std::vector<std::shared_ptr<ExprNode>>> exprList
%type <std::vector<std::shared_ptr<AstNodeBase>>> statementList
%nonassoc NO_ELSE
%nonassoc "else"
%right EQUAL
%left EQEQ NEQ
%left LT LTE GT GTE
%left PLUS MINUS
%left STAR SLASH PERCENT
%start program
%%
program:
/* empty */
| program statement
{
ast.push_back($2);
}
;
statement:
varDecl ";"
{
$$ = $1;
}
| expr ";"
{
$$ = $1;
}
| block
{
$$ = $1;
}
| "if" "(" expr ")" statement %prec NO_ELSE
{
$$ = std::make_shared<IfNode>($3, $5, nullptr);
}
| "if" "(" expr ")" statement "else" statement
{
$$ = std::make_shared<IfNode>($3, $5, $7);
}
| "while" "(" expr ")" statement
{
$$ = std::make_shared<WhileNode>($3, $5);
}
| "do" statement "while" "(" expr ")" ";"
{
$$ = std::make_shared<DoWhileNode>($5, $2);
}
;
statementList:
/* empty */
{
$$ = std::vector<std::shared_ptr<AstNodeBase>>{};
}
| statementList statement
{
$1.push_back($2);
$$ = $1;
}
;
block:
"{" statementList "}"
{
$$ = std::make_shared<ScopeBlockNode>($2);
}
;
exprList:
expr
{
$$ = std::vector<std::shared_ptr<ExprNode>>{ $1 };
}
| exprList expr
{
$1.push_back($2);
$$ = $1;
}
;
varDecl:
IDENTIFIER IDENTIFIER
{
if ($1 == "int")
$$ = std::make_shared<VarDeclaration>(ExprType(ExprTypeBase::INT), $2);
else if ($1 == "float")
$$ = std::make_shared<VarDeclaration>(ExprType(ExprTypeBase::FLOAT), $2);
else if ($1 == "string")
$$ = std::make_shared<VarDeclaration>(ExprType(ExprTypeBase::STRING), $2);
else if ($1 == "bool")
$$ = std::make_shared<VarDeclaration>(ExprType(ExprTypeBase::BOOL), $2);
else
$$ = std::make_shared<VarDeclaration>(ExprType($1), $2);
}
;
expr:
INTEGER
{
$$ = std::make_shared<Integer>($1);
}
| FLOAT
{
$$ = std::make_shared<Float>($1);
}
| STRING
{
$$ = std::make_shared<String>($1);
}
| IDENTIFIER
{
$$ = std::make_shared<VarAccessorNode>($1);
}
| PREDEF_FUNCTION "(" exprList ")"
{
if ($1 == "print")
$$ = std::make_shared<PrintFunctionNode>($3);
else if ($1 == "read")
$$ = std::make_shared<ReadFunctionNode>($3);
}
| PREDEF_FUNCTION "(" ")"
{
if ($1 == "rand")
$$ = std::make_shared<RandFunctionNode>();
}
| "(" expr ")"
{
$$ = $2;
}
| expr "=" expr %prec "="
{
$$ = std::make_shared<AssignationNode>($1, $3);
}
| expr "+" expr %prec "+"
{
$$ = std::make_shared<BinaryOperatorNode>(BinaryOperation::ADD, $1, $3);
}
| expr "-" expr %prec "-"
{
$$ = std::make_shared<BinaryOperatorNode>(BinaryOperation::SUBSTRACT, $1, $3);
}
| expr "*" expr %prec "*"
{
$$ = std::make_shared<BinaryOperatorNode>(BinaryOperation::MULTIPLY, $1, $3);
}
| expr "/" expr %prec "/"
{
$$ = std::make_shared<BinaryOperatorNode>(BinaryOperation::DIVIDE, $1, $3);
}
| expr "%" expr %prec "%"
{
$$ = std::make_shared<BinaryOperatorNode>(BinaryOperation::MODULO, $1, $3);
}
| TRUE_LIT
{
$$ = std::make_shared<Bool>(true);
}
| FALSE_LIT
{
$$ = std::make_shared<Bool>(false);
}
| expr "==" expr
{
$$ = std::make_shared<ComparisonNode>(ComparisonOperation::EQ, $1, $3);
}
| expr "!=" expr
{
$$ = std::make_shared<ComparisonNode>(ComparisonOperation::NEQ, $1, $3);
}
| expr "<" expr
{
$$ = std::make_shared<ComparisonNode>(ComparisonOperation::LT, $1, $3);
}
| expr "<=" expr
{
$$ = std::make_shared<ComparisonNode>(ComparisonOperation::LTE, $1, $3);
}
| expr ">" expr
{
$$ = std::make_shared<ComparisonNode>(ComparisonOperation::GT, $1, $3);
}
| expr ">=" expr
{
$$ = std::make_shared<ComparisonNode>(ComparisonOperation::GTE, $1, $3);
}
;