-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjava_parser.y
281 lines (250 loc) · 8.44 KB
/
java_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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
%{
#include <cstdio>
#include <iostream>
#include <string.h>
#include <fstream>
#include "semantic_actions_utils.h"
using namespace semantic_actions_util;
extern int yylex();
extern int yyparse();
extern FILE *yyin;
extern int line_num;
void yyerror(const char *s);
%}
%start method_body
%error-verbose
%token<id> IDENTIFIER
%token<integer> INT_NUM
%token<floatType> FLOAT_NUM
%token<smallString> ARITH_OP
%token <smallString>BOOL_OP
%token <smallString>REL_OP
%token IF
%token ELSE
%token WHILE
%token TRUE
%token FALSE
%token INT
%token FLOAT
%type<varType> primitive_type
%type<expressionType> expression
%type<markerMType> marker_m
%type<markerNType> marker_n
%type<statementType> statement statement_list while if
%type<booleanExpressionType> boolean_expression
%code requires{
#include <unordered_set>
}
%union{
char id[30];
char smallString[20];
int varType;
int integer;
float floatType;
struct ExpressionType{
int varType;
}expressionType;
struct MarkerMType{
int nextInstructionIndex;
}markerMType;
struct MarkerNType{
std::unordered_set<int> *nextList;
}markerNType;
struct StatementType{
std::unordered_set<int> *nextList;
}statementType;
struct BooleanExpressionType{
std::unordered_set<int> *trueList;
std::unordered_set<int> *falseList;
}booleanExpressionType;
}
%%
method_body:
%empty
|{generateHeader();}statement_list marker_m{
backpatch(*($2.nextList), $3.nextInstructionIndex);
writeBytecode();
generateFooter();}
;
statement_list:
statement {*($$.nextList) = *($1.nextList);}
|statement_list marker_m statement
{
backpatch(*($1.nextList) ,$2.nextInstructionIndex);
*($$.nextList) = *($3.nextList);
}
;
statement:
declaration {$$.nextList = new unordered_set<int>();}
|if {*($$.nextList)=*($1.nextList);}
|while {
*($$.nextList)=*($1.nextList);
}
|assignment {$$.nextList = new unordered_set<int>();}
;
marker_m:
%empty{
// Save the index of the next instruction index in the marker
$$.nextInstructionIndex = nextInstructionIndex;
}
;
marker_n:
%empty{
// Save the index of the next instruction index in the marker
$$.nextList = new unordered_set<int>();
*($$.nextList) = makeList(nextInstructionIndex);
appendToCode("goto _");
}
;
declaration: primitive_type IDENTIFIER ';' {
if(checkIfVariableExists($2)) {
yyerror(string{"variable "+string{$2}+" is already declared"}.data());
} else {
defineVariable($2, $1);
}
};
primitive_type:
INT { $$ = VarType::INT_TYPE; }
|FLOAT {$$ = VarType::FLOAT_TYPE; }
;
if:
IF '(' boolean_expression ')'
marker_m
'{' statement_list '}'
marker_n
ELSE '{' marker_m statement_list '}' {
backpatch(*($3.trueList), $5.nextInstructionIndex);
backpatch(*($3.falseList), $12.nextInstructionIndex);
std::unordered_set<int> temp = mergeLists( *($7.nextList), *($9.nextList));
$$.nextList = new unordered_set<int>();
*($$.nextList) = mergeLists(temp, *($13.nextList));
}
;
while:
WHILE marker_m '(' boolean_expression ')'
'{' marker_m statement_list '}' {
backpatch(*($8.nextList), $2.nextInstructionIndex);
backpatch(*($4.trueList), $7.nextInstructionIndex);
$$.nextList = new unordered_set<int>();
*($$.nextList) = *($4.falseList);
appendToCode("goto Label_" + to_string($2.nextInstructionIndex));
}
;
assignment: IDENTIFIER '=' expression ';'{
if(checkIfVariableExists($1)) {
//Check if the two sides have the same type
if(varToVarIndexAndType[$1].second == $3.varType) {
if(varToVarIndexAndType[$1].second == VarType::INT_TYPE) {
appendToCode("istore "+to_string(varToVarIndexAndType[$1].first));
} else {//Only int and float are supported
appendToCode("fstore "+to_string(varToVarIndexAndType[$1].first));
}
} else { // case when the two types aren't the same
//TODO Cast the two variables
}
} else {
yyerror(string{"variable "+string{$1}+" not declared"}.data());
}
};
expression:
INT_NUM {
$$.varType = VarType::INT_TYPE;
appendToCode("ldc "+ to_string($1));
}
|FLOAT_NUM {
$$.varType = VarType::FLOAT_TYPE ;
appendToCode("ldc "+ to_string($1)); }
|IDENTIFIER {
// check if the identifier already exist to load or not
if(checkIfVariableExists($1)) {
$$.varType = varToVarIndexAndType[$1].second;
if($$.varType == VarType::INT_TYPE ) {
//write iload + identifier
appendToCode("iload " + to_string(varToVarIndexAndType[$1].first));
}
else {
//float
//write fload + identifier
appendToCode("fload " + to_string(varToVarIndexAndType[$1].first));
}
}
else {//it's not declared at all
string err = "identifier: "+string{$1}+" isn't declared in this scope";
yyerror(err.c_str());
}
}
|expression ARITH_OP expression {
if ($1.varType == $3.varType ) {
if ($1.varType == VarType::INT_TYPE) {
appendToCode("i" + getOperationCode($2));
}
else{ //it's float
appendToCode("f" + getOperationCode($2));
}
}
}
|'(' expression ')' {$$.varType = $2.varType;}
;
boolean_expression:
TRUE {
$$.trueList = new unordered_set<int> ();
$$.trueList->insert(static_cast<int>(outputCode.size()));
$$.falseList = new unordered_set<int>();
// write code goto line #
appendToCode("goto _");
}
|FALSE {
$$.trueList = new unordered_set<int> ();
$$.falseList= new unordered_set<int>();
$$.falseList->insert(static_cast<int>(outputCode.size()));
// write code goto line #
appendToCode("goto _");
}
|boolean_expression BOOL_OP marker_m boolean_expression {
if(!strcmp($2, "&&")) {
backpatch(*($1.trueList), $3.nextInstructionIndex);
*($$.trueList) = *($4.trueList);
*($$.falseList) = mergeLists(*($1.falseList), *($4.falseList));
}
else if (!strcmp($2,"||")) {
backpatch(*($1.falseList), $3.nextInstructionIndex);
*($$.trueList) = mergeLists(*($1.trueList), *($4.trueList));
*($$.falseList) = *($4.falseList);
}
}
|expression REL_OP expression {
$$.trueList = new unordered_set<int>();
($$.trueList)->insert(static_cast<int>(outputCode.size()));
$$.falseList = new unordered_set<int>();
($$.falseList)->insert(static_cast<int>(outputCode.size()+1));
appendToCode(getOperationCode($2)+ " _");
appendToCode("goto _");
}
;
%%
string genLabel()
{
return "L_"+to_string(labelsCount++);
}
int main(int argc, char** argv) {
// Open a file handle to a particular file:
if (argc == 1){
std::cout << "No file's specified to open ! Please provide a valid file name" << std::endl;
return -1;
}
FILE *myfile = fopen(argv[1], "r");
// Make sure it is valid:
if (!myfile) {
std::cout << "I can't open file!" << std::endl;
return -1;
}
// Set Flex to read from it instead of defaulting to STDIN:
yyin = myfile;
// Parse through the input:
yyparse();
std::cout<<"Yay ! Parser completed working successfully."<<std::endl;
}
void yyerror(const char *s) {
std::cout << "Ouch, I found a parse error on line "<< line_num <<" ! Error Message: " << s << std::endl;
std::exit(-1);
}