-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexer.cpp
More file actions
308 lines (293 loc) · 11.3 KB
/
Lexer.cpp
File metadata and controls
308 lines (293 loc) · 11.3 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//
// Created by duni on 17/12/2019.
//
#include <iostream>
#include <fstream>
#include <regex>
#include "Lexer.h"
// The function returns true if a comma exists in the line
static bool doesCommaExist(string line) {
regex parenthesis("(.*,.*)");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if a comma exists between quotation marks in the line
static bool doesCommaExistBetweenQuotationMarks(string line) {
regex parenthesis("(.*\".*,.*\".*)");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if "=" exists in the line
static bool doesEqualExist(string line) {
regex parenthesis("(.*\\=.*)");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if "Var" exists with an arrow
static bool doesVarExistAndArrow(string line) {
regex parenthesis("(var.*(->|<-).*)");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if "Var" exists with right arrow
static bool doesVarExistAndArrowRight(string line) {
regex parenthesis("(var.*->.*)");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if "Var" exists with "="
static bool doesVarExistAndEqual(string line) {
regex parenthesis("(var.*\\=.*)");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if parenthesis exist in the line
static bool doesParenthesisExist(string line) {
regex parenthesis("((\\().*(\\)))");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if "While" or "If" exists with space
static bool doesIfOrWhileExistAndSpace(string line) {
regex parenthesis("(while|if)( )(?!\\().*");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if "<=" exists in the line
static bool doesESExist(string line) {
regex parenthesis("(\\<\\=)");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if ">=" exists in the line
static bool doesEGExist(string line) {
regex parenthesis("(\\>\\=)");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if "==" exists in the line
static bool doesEEExist(string line) {
regex parenthesis("(\\=\\=)");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if "!=" exists in the line
static bool doesNEExist(string line) {
regex parenthesis("(\\!\\=)");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if "While" or "If" exists with parenthesis
static bool doesIfOrWhileExistAndPar(string line) {
regex parenthesis("(while|if)( )?(\\().*");
int count = distance(sregex_iterator(line.begin(), line.end(), parenthesis), sregex_iterator());
return count != 0;
}
// The function returns true if the specified delimiter exists in the line
static bool doesDelimExist(string line, char delim) {
char currChar;
int i;
for (i = 0; i < line.length(); i++) {
currChar = line[i];
if (currChar == delim) {
return true;
}
}
return false;
}
// The function of the Lexer that do lexical Analysis of the text file
vector<string> Lexer::lexicalAnalysis(string fileName) {
int i;
vector<string> strArray;
string line;
char *wordToPushPartOne;
char *wordToPushPartTwo;
char *wordToPushPartThree;
char *wordToPushPartFour;
bool isPar = false;
bool isDDigit = false;
bool isArrowLeft = false;
string delim;
ifstream in_file{fileName, ios::in};
if (!in_file.is_open()) {
throw "Cannot open file";
}
// reads line by line from the file
while (getline(in_file, line)) {
// creates char* from the line string
char lineStrTok[line.length() + 1];
//copy the string to the char*
strcpy(lineStrTok, line.c_str());
// checks if the line contains "while" of "if" with space or parenthesis
if (doesIfOrWhileExistAndSpace(line) || doesIfOrWhileExistAndPar(line)) {
// checks if "While" or "If" exists with space
if (doesIfOrWhileExistAndSpace(line)) {
// push "while" or "if" to the vector
strArray.emplace_back(strtok(lineStrTok, " "));
} else {
// remove spaces from the line
string::iterator end_pos = remove(line.begin(), line.end(), ' ');
line.erase(end_pos, line.end());
// remove tabs from the line
end_pos = remove(line.begin(), line.end(), '\t');
line.erase(end_pos, line.end());
// initialize the char*
lineStrTok[line.length() + 1];
strcpy(lineStrTok, line.c_str());
isPar = true;
// push "(" to the vector
strArray.emplace_back(strtok(lineStrTok, "("));
}
// checks the possible conditions exists in "While" or "If" statement
if (doesESExist(line)) {
wordToPushPartOne = strtok(nullptr, "<=");
delim = "<=";
// The delimiter has 2 chars
isDDigit = true;
} else if (doesEGExist(line)) {
wordToPushPartOne = strtok(nullptr, ">=");
delim = ">=";
// The delimiter has 2 chars
isDDigit = true;
} else if (doesEEExist(line)) {
wordToPushPartOne = strtok(nullptr, "==");
delim = "==";
// The delimiter has 2 chars
isDDigit = true;
} else if (doesNEExist(line)) {
wordToPushPartOne = strtok(nullptr, "!=");
delim = "!=";
// The delimiter has 2 chars
isDDigit = true;
} else if (doesDelimExist(line, '>')) {
wordToPushPartOne = strtok(nullptr, ">");
delim = ">";
// The delimiter has 1 chars
isDDigit = false;
} else if (doesDelimExist(line, '<')) {
wordToPushPartOne = strtok(nullptr, "<");
delim = "<";
// The delimiter has 1 chars
isDDigit = false;
}
// push the left expression to the vector
strArray.emplace_back(wordToPushPartOne);
// push the delimiter to the vector
strArray.emplace_back(delim);
// remove "{" from the left expression
wordToPushPartTwo = strtok(nullptr, "{");
// checks if the right expression has parenthesis
if (isPar) {
// checks if the delimiter has 2 chars
if (isDDigit) {
// remove the extra char of the delimiter from the right expression
for (i = 1; i < strlen(wordToPushPartTwo); i++) {
wordToPushPartTwo[i - 1] = wordToPushPartTwo[i];
}
// remove ")" from the end of the right expression
wordToPushPartTwo[strlen(wordToPushPartTwo) - 2] = '\0';
} else {
// remove ")" from the end of the right expression
wordToPushPartTwo[strlen(wordToPushPartTwo) - 1] = '\0';
}
} else {
// remove spaces from the right expression
*remove(wordToPushPartTwo, wordToPushPartTwo + strlen(wordToPushPartTwo), ' ') = 0;
// checks if the delimiter has 2 chars
if (isDDigit) {
// remove the extra char of the delimiter from the right expression
for (i = 1; i < strlen(wordToPushPartTwo); i++) {
wordToPushPartTwo[i - 1] = wordToPushPartTwo[i];
}
wordToPushPartTwo[strlen(wordToPushPartTwo) - 1] = '\0';
}
}
// push the right expression the the vector
strArray.emplace_back(wordToPushPartTwo);
isPar = false;
strArray.emplace_back("{");
// checks if "var" exists with "="
} else if (doesVarExistAndEqual(line)) {
// splits with space
wordToPushPartOne = strtok(lineStrTok, " ");
// push "var" to the vector
strArray.emplace_back(wordToPushPartOne);
// split with "="
wordToPushPartTwo = strtok(nullptr, "=");
// push the variable to the vector
strArray.emplace_back(wordToPushPartTwo);
strArray.emplace_back("=");
// split with "\n"
wordToPushPartThree = strtok(nullptr, "\n");
// push the right expression to the vector
strArray.emplace_back(wordToPushPartThree);
// checks if "var" exists with an arrow
} else if (doesVarExistAndArrow(line)) {
// splits with space
wordToPushPartOne = strtok(lineStrTok, " ");
strArray.emplace_back(wordToPushPartOne);
// checks if "var" exists with "->"
if (doesVarExistAndArrowRight(line)) {
wordToPushPartTwo = strtok(nullptr, "->");
} else {
wordToPushPartTwo = strtok(nullptr, "<-");
isArrowLeft = true;
}
// push the variable name to the vector
strArray.emplace_back(wordToPushPartTwo);
if (isArrowLeft) {
strArray.emplace_back("<-");
} else {
strArray.emplace_back("->");
}
isArrowLeft = false;
// splits with "("
wordToPushPartThree = strtok(nullptr, "(");
// remove the extra char of the arrow from "sim"
for (i = 1; i < strlen(wordToPushPartThree); i++) {
wordToPushPartThree[i - 1] = wordToPushPartThree[i];
}
wordToPushPartThree[strlen(wordToPushPartThree) - 1] = '\0';
// push "sim" to the vector
strArray.emplace_back(wordToPushPartThree);
// gets the path in the sim
wordToPushPartFour = strtok(nullptr, "");
// remove the ")"
wordToPushPartFour[strlen(wordToPushPartFour) - 1] = '\0';
// push the path to the vector
strArray.emplace_back(wordToPushPartFour);
// checks if "=" exists and quotation marks does not exist
} else if ((doesEqualExist(line)) && (!doesDelimExist(line, '"'))) {
wordToPushPartOne = strtok(lineStrTok, "=");
// push left expression to the vector
strArray.emplace_back(wordToPushPartOne);
strArray.emplace_back("=");
wordToPushPartTwo = strtok(nullptr, "\n");
// push the right expression to the vector
strArray.emplace_back(wordToPushPartTwo);
// checks if parenthesis exist
} else if (doesParenthesisExist(line)) {
wordToPushPartOne = strtok(lineStrTok, "(");
strArray.emplace_back(wordToPushPartOne);
// checks if comma exists and it is not between quotation marks
if (doesCommaExist(line) && (!doesCommaExistBetweenQuotationMarks(line))) {
// removes ")"
wordToPushPartTwo[strlen(wordToPushPartTwo) - 1] = '\0';
wordToPushPartTwo = strtok(nullptr, ",");
// splits with comma
while (wordToPushPartTwo) {
strArray.emplace_back(wordToPushPartTwo);
wordToPushPartTwo = strtok(nullptr, ",");
}
} else {
wordToPushPartTwo = strtok(nullptr, "");
// removes ")"
wordToPushPartTwo[strlen(wordToPushPartTwo) - 1] = '\0';
strArray.emplace_back(wordToPushPartTwo);
}
} else {
strArray.emplace_back(line);
}
}
return strArray;
}