-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtermProject.y
225 lines (217 loc) · 4.72 KB
/
termProject.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
%{
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
#include "y.tab.h"
using namespace std;
extern FILE *yyin;
extern int yylex();
extern int linenum;
extern int tabamount;
int openedifs=1;
void yyerror(string s);
struct nameStruct {
string name;
bool isInt;
bool isFlt;
bool isStr;
int lasttype;
nameStruct(string s, bool i, bool f, bool str, int lt){
name = s;
isInt = i;
isFlt = f;
isStr = str;
lasttype = lt;
}
};
vector<nameStruct> varArray;
string finalOutput="\t";
string expr="";
string declpart="";
void throwerror(int i){
if(i==1){ //Type mismatch
cout << "Type mismatch in line: " << linenum << endl;
exit(0);
}
if(i==2){ //Untyped variable
cout << "Untyped variable is used in line: " << linenum << endl;
exit(1);
}
if(i==3){ //Tab inconstincency
cout << "Tab inconstincency in line: " << linenum << endl;
exit(1);
}
}
vector <string> storage;
FILE *output;
%}
%union
{
char * str;
int inum;
float fnum;
}
%token QUOTE
%token <str> VAR ASSIGN INTRSV FLTRSV EQUAL
%type <inum> typelist selector isdefined operations
%left ASSIGN
%%
program:
statement
|
statement program
;
statement:
VAR EQUAL operations
{
int flag = -1;
for (int i = 0;i< varArray.size();i++)
if (varArray[i].name == string($1))
flag = i;
if(flag==-1){
struct nameStruct varname = nameStruct(string($1), false,false,false,0);
if($3 == 1)
varname.isInt = true;
if($3 == 2)
varname.isFlt = true;
if($3 == 3)
varname.isStr = true;
varname.lasttype = $3;
varArray.push_back(varname);
}
else {
if($3 == 1)
varArray[flag].isInt = true;
if($3 == 2)
varArray[flag].isFlt = true;
if($3 == 3)
varArray[flag].isStr = true;
varArray[flag].lasttype = $3;
}
string alt="";
if($3 == 1)
alt+=string($1)+"_int";
if($3 == 2)
alt+=string($1)+"_flt";
if($3 == 3)
alt+=string($1)+"_str";
if(expr!="")
finalOutput+=alt+" = " +expr+";\n";
else
finalOutput+=alt+" = " +storage[0]+";\n";
for(int i = 0;i<tabamount;i++)
finalOutput+="\t";
if(tabamount != openedifs)
throwerror(3);
expr="";
storage.clear();
}
;
operations:
selector {$$ = $1;}
|
operations ASSIGN operations {
if($1 != $3&&($1==3 || $3==3)){
throwerror(1);
}
if($1==2 || $3 == 2){
$$=2;
}
if(storage.size()==2)
expr+=string(storage[0])+ " "+ string($2) +" "+ string(storage[1]);
if(storage.size()==1)
expr+=" "+ string($2) +" "+ string(storage[0]);
storage.clear();
}
;
selector:
typelist {$$ = $1;}
|
isdefined {$$ = $1;}
;
typelist:
INTRSV {$$ = 1;storage.push_back($1);}
|
FLTRSV {$$ = 2;storage.push_back($1);}
|
QUOTE VAR QUOTE {$$ = 3;string alt = "\""+ string($2)+"\"";storage.push_back(alt);}
|
QUOTE INTRSV QUOTE {$$ = 3;string alt = "\""+ string($2)+"\"";storage.push_back(alt);}
|
QUOTE FLTRSV QUOTE {$$ = 3;string alt = "\""+ string($2)+"\"";storage.push_back(alt);}
;
isdefined:
VAR
{
int flag = -1;
for (int i = 0;i< varArray.size();i++)
if (varArray[i].name == string($1))
flag = i;
if(flag == -1)
throwerror(2);
else {
if(varArray[flag].lasttype==1 && varArray[flag].isInt){
$$=1;
storage.push_back(string($1)+"_int");
}
if(varArray[flag].lasttype==2 && varArray[flag].isFlt){
$$=2;
storage.push_back(string($1)+"_flt");
}
if(varArray[flag].lasttype==3 && varArray[flag].isStr){
$$=3;
storage.push_back(string($1)+"_str");
}
}
}
;
%%
void yyerror(string s){
cout<<"error at line: "<<linenum<<endl;
}
int yywrap(){
return 1;
}
int main(int argc, char *argv[])
{
yyin=fopen(argv[1],"r");
yyparse();
fclose(yyin);
vector<string> intarray;
vector<string> floatarray;
vector<string> strarray;
for (int i = 0;i< varArray.size();i++){
if(varArray[i].isStr == true)
strarray.push_back(varArray[i].name + "_str");
if(varArray[i].isInt == true)
intarray.push_back(varArray[i].name + "_int");
if(varArray[i].isFlt == true)
floatarray.push_back(varArray[i].name + "_flt");
}
cout << "void main()\n{\n";
if(intarray.size()>0){
cout <<"\tint ";
for (int i = 0; i < intarray.size()-1; i++){
cout << intarray[i] << ",";
}
cout << intarray[intarray.size()-1] << ";\n";
}
if(floatarray.size()>0){
cout <<"\tfloat ";
for (int i = 0; i < floatarray.size()-1; i++) {
cout << floatarray[i] << ",";
}
cout << floatarray[floatarray.size()-1] << ";\n";
}
if(strarray.size()>0){
cout <<"\tstring ";
for (int i = 0; i < strarray.size()-1; i++) {
cout << strarray[i] << ",";
}
cout << strarray[strarray.size()-1] << ";\n";
}
cout <<"\n"<<finalOutput.substr(0,finalOutput.length()-1);
cout << "}"<<endl;
return 0;
}