-
Notifications
You must be signed in to change notification settings - Fork 1
/
quadRuplesOutput.cpp
210 lines (198 loc) · 5.54 KB
/
quadRuplesOutput.cpp
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
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <stack>
#include <map>
#include <vector>
#include <iomanip>
#include "lexer.h"
#include "quadRuplesOutput.h"
using namespace std;
void quadRuplesOutput(map<string, string> &l_map, vector<quadRuple> &QT)
{
bool isBreak;
string str;
bool isNot, isMinus, isReturn; //if !, if -, if return
int tv, code_in_block, ifValue, whileValue; //temp value and count how many quadruple code in the block, the states of if and while
stack<string> id, op; //identifier and operator
tv = code_in_block = ifValue = whileValue = 0;
ifstream ifs;
ifs.open("./input/main.c", ifstream::in);
ofstream ofs;
ofs.open("quadruples.txt", ofstream::out);
while(getline(ifs, str))
{
isBreak = isNot = isMinus = isReturn = false;
while(!id.empty() || !op.empty())
id.pop(), op.pop(); //empty the stacks
if(ifValue == 0 && str.find("if") != string::npos) ifValue = 1;
if(whileValue == 0 && str.find("while") != string::npos) whileValue = 1;
stringstream ss (str);
while(getline(ss, str, ' '))
{
if(ss.fail()) break;
string::iterator it = str.begin();
while(*it == '\t')
str.erase(0, 1);
if(str == "") continue;
if(str == "else") ifValue = 2; // go into another if block
switch(lexer(l_map, str))
{
case 'K':
if(str == "int" || str == "char")
isBreak = true;
else if(l_map.find(str)->second == "Operator")
{
op.push(str);
if(str == "!") isNot = true;
}
else if(str == "{")
{
code_in_block = 0;
if(ifValue > 0) ifValue++; //into block of if
if(whileValue > 0) whileValue++; //into block of while
}
else if(str == ")" || str == "}" || str == "]" || str == ";")
resultOutput(str, isNot, isReturn, tv, code_in_block, ifValue, whileValue, id, op, QT);
else if(str == "return")
isReturn = true;
break;
case 'I':
case 'D':
{
id.push(str);
if(isNot) resultOutput("!", isNot, isReturn, tv, code_in_block, ifValue, whileValue, id, op, QT);
}
break;
}
if(isBreak) break;
}
}
int count = 0;
quadRuple Q;
ofs<<setw(4)<<"line"<<setw(8)<<"op"<<setw(8)<<"arg1"<<setw(8)<<"arg2"<<setw(8)<<"result"<<endl;
for(vector<quadRuple>::iterator it = QT.begin(); it != QT.end(); ++it)
{
Q = *it;
ofs<<setw(4)<<count++<<setw(8)<<Q.op<<setw(8)<<Q.arg1<<setw(8)<<Q.arg2<<setw(8)<<Q.result<<endl;
}
ifs.close();
ofs.close();
}
void resultOutput(string str, bool &isNot, bool &isReturn, int &tv, int &code_in_block, int &ifValue, int &whileValue, stack<string> &id, stack<string> &op, vector<quadRuple> &QT)
{
string arg1, arg2;
if(str == ";") //if the str is ";"
{
while(!id.empty()) //clean the identifier stack
{
if(op.empty())
break;
else if(op.top() != "=")
{
arg2 = id.top(), id.pop();
arg1 = id.top(), id.pop();
QT.push_back(quadRupleMaker(op.top(), arg1, arg2, "t" + int2str(++tv)));
op.pop();
if(!id.empty()) id.push(QT[QT.size()-1].result);
}
else
{
arg1 = id.top(), id.pop();
QT.push_back(quadRupleMaker(op.top(), arg1, "", id.top()));
op.pop(), id.pop();
}
code_in_block++;
}
if(isReturn) //special condition, return
{
if(!id.empty())
{
QT.push_back(quadRupleMaker("ret", id.top(), "", "t" + int2str(++tv)));
id.pop();
}
else
QT.push_back(quadRupleMaker("ret", QT[QT.size()-1].result, "", "t" + int2str(++tv)));
isReturn = false;
}
}
else if(str == ")")
{
if(ifValue || whileValue) //this may be showed in the if statement and while loop
{
if(op.empty())
{
QT.push_back(quadRupleMaker("=", id.top(), "", "t" + int2str(++tv)));
id.pop();
}
else
{
arg2 = id.top(), id.pop();
arg1 = id.top(), id.pop();
QT.push_back(quadRupleMaker(op.top(), arg1, arg2, "t" + int2str(++tv)));
op.pop();
}
}
}
else if(str == "}")
{
//discuss the different condition if if statement and while loop
if(ifValue == 2)
QT.insert(QT.end() - code_in_block, quadRupleMaker("jfalse", int2str(QT.size()+2), QT[QT.size()-code_in_block-1].result, ""));
else if(ifValue == 3)
QT.insert(QT.end() - code_in_block, quadRupleMaker("jmp", int2str(QT.size()+1), QT[QT.size()-code_in_block-1].result, ""));
else if(whileValue == 2)
{
QT.insert(QT.end() - code_in_block, quadRupleMaker("jfalse", int2str(QT.size()+2), QT[QT.size()-code_in_block-1].result, ""));
QT.push_back(quadRupleMaker("jmp", int2str(QT.size()-code_in_block-2), "", ""));
}
ifValue = whileValue = 0;
}
else if(str == "]") //array
{
arg2 = id.top(), id.pop();
arg1 = id.top(), id.pop();
QT.push_back(quadRupleMaker("[]=", arg1, arg2, "t" + int2str(++tv)));
id.push(QT[QT.size()-1].result);
code_in_block++;
}
else if(str == "!") //not
{
QT.push_back(quadRupleMaker(op.top(), id.top(), "", "t" + int2str(++tv)));
id.pop(), op.pop();
id.push(QT[QT.size()-1].result);
code_in_block++;
isNot = false;
}
/*
else if(str == "-")
{
Q.arg1 = id.top();
id.pop();
Q.op = op.top();
op.pop();
Q.arg2 = "";
Q.result = "t" + int2str(++tv);
id.push(Q.result);
QT.push_back(Q);
code_in_block++;
}
*/
}
//integer to string
string int2str(int i)
{
char s[10];
sprintf(s, "%d", i);
string ss(s);
return ss;
}
//make a quadRuple variable
quadRuple quadRupleMaker(string a, string b, string c, string d)
{
quadRuple Q;
Q.op = a, Q.arg1 = b, Q.arg2 = c, Q.result = d;
return Q;
}