Skip to content

Commit 71f78a9

Browse files
committed
Fix some bugs
1 parent 3818f20 commit 71f78a9

File tree

2 files changed

+67
-120
lines changed

2 files changed

+67
-120
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@
99
# Generated Executables
1010
*.out
1111

12-
.idea
12+
.idea
13+
.vscode

java_parser.y

Lines changed: 65 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <cstdio>
33
#include <iostream>
44
#include "semantic_actions_utils.h"
5+
#include <string.h>
56

67
using namespace semantic_actions_util;
78

@@ -18,9 +19,9 @@
1819
%token<id> IDENTIFIER
1920
%token INT_NUM
2021
%token FLOAT_NUM
21-
%token ARITH_OP
22-
%token BOOL_OP
23-
%token REL_OP
22+
%token<smallString> ARITH_OP
23+
%token <smallString>BOOL_OP
24+
%token <smallString>REL_OP
2425
%token IF
2526
%token ELSE
2627
%token WHILE
@@ -31,13 +32,25 @@
3132

3233
%type<varType> primitive_type
3334
%type<expressionType> expression;
35+
%type<booleanExpressionType> boolean_expression;
36+
37+
%code requires{
38+
#include <unordered_set>
39+
}
3440

3541
%union{
42+
char smallString[20];
3643
char id[30];
3744
int varType;
3845
struct ExpressionType{
3946
int varType;
47+
std::unordered_set<int> trueList;
48+
std::unordered_set<int> falseList;
4049
}expressionType;
50+
struct BooleanExpressionType{
51+
std::unordered_set<int> trueList;
52+
std::unordered_set<int> falseList;
53+
}booleanExpressionType;
4154
}
4255

4356
%%
@@ -101,141 +114,74 @@ assignment: IDENTIFIER '=' expression ';'{
101114
};
102115

103116
expression:
104-
<<<<<<< HEAD
105-
INT_NUM {$$.varType = VarType::INT_TYPE;}
106-
|FLOAT_NUM {$$.varType = VarType::FLOAT_TYPE;}
117+
INT_NUM {$$.varType = VarType::INT_TYPE; }
118+
|FLOAT_NUM {$$.varType = VarType::FLOAT_TYPE ; }
107119
|IDENTIFIER {
108-
if(checkIfVariableExists($1)) {
109-
$$.varType = varToVarIndexAndType[$1].second;
110-
} else {
111-
//TODO handle the error of variable used but not declared
112-
}
113-
}
114-
|expression ARITH_OP expression
115-
|'(' expression ')'{}
116-
=======
117-
INT_NUM {$$.sType = INT_TYPE; }
118-
|FLOAT_NUM {$$.sType = FLOAT_TYPE ; }
119-
|IDENTIFIER {
120-
121120
// check if the identifier already exist to load or not
122-
if(checkIfVariableExists($1))
123-
{
124-
$$.sType = varToVarIndexAndType[$1].second;
125-
if($$.sType == INT_TYPE )
126-
{
127-
//write iload + identifier
128-
appendToCode("iload " + to_string(varToVarIndexAndType[$1].first));
121+
if(checkIfVariableExists($1)) {
122+
$$.varType = varToVarIndexAndType[$1].second;
123+
if($$.varType == VarType::INT_TYPE ) {
124+
//write iload + identifier
125+
appendToCode("iload_" + to_string(varToVarIndexAndType[$1].first));
129126
}
130-
else
131-
//float
132-
{
133-
//write fload + identifier
134-
appendToCode("fload " + to_string(varToVarIndexAndType[$1].first));
127+
else {
128+
//float
129+
//write fload + identifier
130+
appendToCode("fload_" + to_string(varToVarIndexAndType[$1].first));
135131
}
136-
137-
138-
139132
}
140-
else //it's not declared at all
141-
142-
{
143-
string err = "identifier: "+$1+" isn't declared in this scope";
144-
yyerror(err.c_str());
145-
$$.sType = ERROR_T;
133+
else {//it's not declared at all
134+
string err = "identifier: "+string{$1}+" isn't declared in this scope";
135+
yyerror(err.c_str());
146136
}
147-
148-
149-
150137
}
151138
|expression ARITH_OP expression {
152-
153-
if ($1.sType == $3.sType )
154-
{
155-
if ($1.sType == INT_TYPE)
156-
157-
158-
appendToCode("i" + getOperationCode($2) );
159-
else //it's float
160-
161-
appendToCode("f" + getOperationCode($2) );
162-
}
163-
164-
165-
166-
167-
}
168-
|'(' expression ')' {$$.sType = $2.sType;}
169-
>>>>>>> feature/expression
139+
if ($1.varType == $3.varType ) {
140+
if ($1.varType == VarType::INT_TYPE)
141+
appendToCode("i" + getOperationCode($2));
142+
else //it's float
143+
appendToCode("f" + getOperationCode($2));
144+
}
145+
}
146+
|'(' expression ')' {$$.varType = $2.varType;}
170147
;
171148

172149
boolean_expression:
173-
TRUE
174-
{
175-
$$.trueList = new vector<int> ();
176-
$$.trueList->push_back(//code size );
177-
$$.falseList = new vector<int>();
150+
TRUE {
151+
// $$.trueList = new vector<int> ();
152+
$$.trueList.insert(static_cast<int>(outputCode.size()));
153+
// $$.falseList = new vector<int>();
178154
// write code goto line #
179-
appendToCode("goto ")
180-
181-
155+
appendToCode("goto ");
182156
}
183-
|FALSE
184-
{
185-
$$.trueList = new vector<int> ();
186-
$$.falseList= new vector<int>();
187-
$$.falseList->push_back(//code size);
157+
|FALSE {
158+
// $$.trueList = new vector<int> ();
159+
// $$.falseList= new vector<int>();
160+
$$.falseList.insert(static_cast<int>(outputCode.size()));
188161
// write code goto line #
189-
appendToCode("goto ")
190-
162+
appendToCode("goto ");
191163
}
192-
|expression BOOL_OP expression
193-
{
194-
if(!strcmp($2, "&&"))
195-
{
196-
197-
$$.trueList = $3.trueList;
198-
$$.falseList = merge($1.falseList,$3.falseList);
199-
}
200-
else if (!strcmp($2,"||"))
201-
{
202-
203-
$$.trueList = merge($1.trueList, $3.trueList);
204-
$$.falseList = $3.falseList;
205-
}
206-
207-
164+
|expression BOOL_OP expression {
165+
if(!strcmp($2, "&&")) {
166+
$$.trueList = $3.trueList;
167+
// $$.falseList = merge($1.falseList,$3.falseList);
168+
}
169+
else if (!strcmp($2,"||")) {
170+
// $$.trueList = merge($1.trueList, $3.trueList);
171+
$$.falseList = $3.falseList;
172+
}
208173
}
209-
|expression REL_OP expression
210-
{
211-
$$.trueList = new vector<int>();
212-
$$.trueList ->push_back (//code size);
213-
214-
$$.falseList = new vector<int>();
215-
$$.falseList->push_back(//code size+1);
216-
217-
218-
appendToCode(getOperationCode($2)+ " ");
219-
appendToCode("goto ");
220-
221-
174+
|expression REL_OP expression {
175+
// $$.trueList = new vector<int>();
176+
$$.trueList.insert(static_cast<int>(outputCode.size()));
177+
// $$.falseList = new vector<int>();
178+
$$.falseList.insert(static_cast<int>(outputCode.size()+1));
179+
appendToCode(getOperationCode($2)+ " ");
180+
appendToCode("goto ");
222181
}
223-
224-
182+
;
225183
%%
226184

227-
228-
string getOperationCode(string operational)
229-
{
230-
map<string, string>::iterator it ;
231-
it = mp.find(operational);
232-
233-
if(it == opList.end())
234-
return "";
235-
else
236-
return it->second;
237-
238-
}
239185
string genLabel()
240186
{
241187
return "L_"+to_string(labelsCount++);

0 commit comments

Comments
 (0)