-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUntitled1.cpp
More file actions
279 lines (218 loc) · 6.39 KB
/
Copy pathUntitled1.cpp
File metadata and controls
279 lines (218 loc) · 6.39 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
269
270
271
272
273
274
275
276
277
278
// Tokenizing
// Phat Phan, La Salle University
// project 3 - CSC366
// Header file for input output functions
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
// Returns true if string is a number else false
bool isNumber(string s)
{
for (int i = 0; i < s.length(); i++)
if (isdigit(s[i]) == false)
return false;
return true;
}
// main function -
// where the execution of program begins
int main()
{
// streaming file
ifstream inFile;
//inFile point to the location of the file you want to read
// can be the path or just name of the file if it's in the same folder of the program
// path checked: C:\\Users\\phand\\Desktop\\cpp\\p3.txt
inFile.open("p3.txt");
//check if the file is readable or not
if (!inFile)
{
cerr << "Unable to open file datafile.txt";
exit(1); // call system to stop
}
//array to hold each line
string a[50];
//string to hold each line
// it's somehow has dynamic lenght ?!
string x;
//counter how many lines in the text file
int number_of_lines;
//read line by line
while (getline(inFile,x))
//read word by word (one space => next line)
//while (inFile >> x)
{
// save the line/word to array of that number of line
//a[number_of_words] = x;
a[number_of_lines] = x;
//count number of line
number_of_lines++;
//number_of_words++;
}//end white
//create a 2 dimensional string array
//number_of_lines of code as max, assuming each line has as most 10 words
//it's a waste, but didn't find better solutions at the moment
string LineWord[number_of_lines][20];
//loop through each line and put words in each line to the array
//this will populate squence of token into 2D array
for(int k=0;k<number_of_lines;k++)
{
//read each line
istringstream iss(a[k]);
//loop through each word in each line
for(int f=0; f<sizeof(a[k]);f++)
{
string word;
iss >> word;
//2 dimensional array hold value
// the first number is row, which is the number of line (not the line number in IML), second is colum, location of element in each row(words, tokens)
LineWord[k][f] = word;
}//end inner for
}//end outter for
//RL stands for row location
//CL stands for column location
//arrray to hold variable location row
int varlocationR[100];
//arrray to hold variable location second array
int varlocationC[100];
//counter of variable row/line location
int varRL=0;
//counter of literal integer colum location;
int varCL=0;
//array to hold literal integer location in 2darray
int numR[100];
int numC[100];
//counter
int numCountR=0;
int numCountC=0;
//array to hold line number of IML after "goto"
int gotoR[100];
int gotoC[100];
//counter
int gotoCountR=0;
int gotoCountC=0;
//loop through the 2D array to store the location of the symbol you want
for(int r=0;r<number_of_lines;r++)
{
for (int c=0; c<20;c++)
{
//after get . it must be a variable name
if(!LineWord[r][c].compare("get"))
{
varlocationR[varRL] = r;
varlocationC[varCL] = c+1 ;
varRL++;
varCL++;
}
//after if must be variable name
if(!LineWord[r][c].compare("if"))
{
varlocationR[varRL] = r;
varlocationC[varCL] = c+1 ;
varRL++;
varCL++;
}
//after let must be variable name
if(!LineWord[r][c].compare("let"))
{
varlocationR[varRL] = r;
varlocationC[varCL] = c+1 ;
varRL++;
varCL++;
}
//after "=, >, <, or !=. ", must be variable or literal integer
if(!LineWord[r][c].compare("=") || !LineWord[r][c].compare(">") || !LineWord[r][c].compare("<") ||!LineWord[r][c].compare("!="))
{
if(!isNumber(LineWord[r][c+1]))
{
varlocationR[varRL] = r;
varlocationC[varCL] = c+1 ;
varRL++;
varCL++;
}else
{
numR[numCountR] = r;
numC[numCountC] = c+1;
numCountR++;
numCountC++;
}
}
//after "+,-,*,/", must be variable or literal integer
if(!LineWord[r][c].compare("-") || !LineWord[r][c].compare("+") || !LineWord[r][c].compare("/") || !LineWord[r][c].compare("*") )
{
if(!isNumber(LineWord[r][c+1]))
{
varlocationR[varRL] = r;
varlocationC[varCL] = c+1 ;
varRL++;
varCL++;
}else
{
numR[numCountR] = r;
numC[numCountC] = c+1;
numCountR++;
numCountC++;
}
}
//after goto . it must be a IML line number
if(!LineWord[r][c].compare("goto"))
{
gotoR[gotoCountR] = r;
gotoC[gotoCountC] = c+1 ;
gotoCountR++;
gotoCountC++;
}
}//end inner for
} //end outer for
cout<< "I have " << varRL << " variable/s. Below are these location in 2D array";
cout << endl;
//the location of each variable, what line it on and what elecment is it
for (int i=0;i<varRL;i++)
{
cout << varlocationR[i];
cout << varlocationC[i];
cout << endl;
}
cout << endl;
cout<< "I have " << numCountR << " literal integer/s. Below are these location in 2D array";
cout << endl;
//the location of each variable, what line it on and what elecment is it
for (int i=0;i<numCountR;i++)
{
cout << numR[i];
cout << numC[i];
cout << endl;
}
cout << endl;
cout << "Below are line number in IML location in 2D array after 'goto' ";
cout << endl;
//the location of each variable, what line it on and what elecment is it
for (int i=0;i<gotoCountR;i++)
{
cout << gotoR[i];
cout << gotoC[i];
cout << endl;
}
cout << endl;
cout <<"to get the actual value of literal integer name of variable of all above we just need to plug the number in 2Darray. For example";
cout << endl;
cout<< "This is the value of second literal integer in the plain text: ";
cout<< LineWord[numR[1]][numC[1]];
cout << endl;
cout<< "its location in 2D array is: " << numR[1] << numC[1];
cout << endl;
cout << endl;
cout << "Below are line number in IML. Line number of IML is the first word on each code line so don't need to remember";
cout << endl;
//first word of each line is a line number in IML. No need to store
for(int i=0;i<number_of_lines;i++)
{
cout << LineWord[i][0];
cout << endl;
}
inFile.close();
return 0;
}