-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLexAn.cpp
369 lines (352 loc) · 11.2 KB
/
LexAn.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "stdafx.h"
#include "LexAn.h"
#include "Div.h"
#include "FST.h"
#include "Graphs.h"
#define DIV '|'
#define SPACE ' '
#define PLUS '+'
#define MINUS '-'
#define STAR '*'
#define DIRSLASH '/'
#define EQUAL '='
using namespace std;
namespace Lex
{
LEX lexAnaliz(Log::LOG log, In::IN in)
{
LEX lex;
LT::LexTable lextable = LT::Create(LT_MAXSIZE);
IT::IdTable idtable = IT::Create(TI_MAXSIZE);
char** word = new char* [max_world];
for (int i = 0; i < max_world; i++)
word[i] = new char[size_world] {NULL};
do {
word = divideText(in.text, in.size);
} while (word == NULL);
//for (int i = 0; word[i] != NULL; i++)
//{
// /*if (word[4][i] == '\0')
// break;
// else*/
// cout << word[i] << endl;
//}
int indxLex = 0; // èíäåêñ ëåêñåìû
int indexID = 0; // èíäåêñ èäåíò.
int clit = 1; // ñ÷åò÷èê ëèòåðàëîâ
int line = 1; // íîìåð ñòðîêè
int position = 0; // íîìåð ïîçèöèè â èñõîäíîì ôàéëå
bool findMain = false;
int countMain = 0;
bool findDec = false;
bool findType = false;
int bufPos = 0;
char emptystr[] = ""; // ïóñòàÿ ñòðîêà
char* prefix = new char[10]{ "" }; // òåêóùèé ïðåôèêñ
char* bufprefix = new char[10]{ "" }; // áóôåð äëÿ ïðåôèêñà
char* oldprefix = new char[10]{ "" }; // ïðåäûäóùèé ïðåôèêñ
char* L = new char[2]{ "L" };
char* bufL = new char[TI_STR_MAXSIZE];
char* nameLiteral = new char[10]{ "" };
char* charclit = new char[10]{ "" }; // äëÿ ñòðîêîâîé çàïèñè ñ÷åò÷èêà
bool findFunc = false;
bool findParm = false;
IT::Entry entryIT;
for (int i = 0; word[i] != NULL; i++, indxLex++)
{
bool findSameID = false;
FST::FST fstDeclare(word[i], FST_DECLARE);
if (FST::execute(fstDeclare))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_DECLARE, LT_TI_NULLIDX, line);
LT::Add(lextable, entryLT);
findDec = true;
continue;
}
FST::FST fstTypeInteger(word[i], FST_INTEGER);
if (FST::execute(fstTypeInteger))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_INTEGER, LT_TI_NULLIDX, line);
LT::Add(lextable, entryLT);
findType = true;
entryIT.iddatatype = IT::INT;
continue;
}
FST::FST fstTypeString(word[i], FST_STRING);
if (FST::execute(fstTypeString))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_STRING, LT_TI_NULLIDX, line);
LT::Add(lextable, entryLT);
findType = true;
entryIT.iddatatype = IT::STR;
strcpy(entryIT.value.vstr.str, emptystr);
continue;
}
FST::FST fstFunction(word[i], FST_FUNCTION);
if (FST::execute(fstFunction))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_FUNCTION, LT_TI_NULLIDX, line);
LT::Add(lextable, entryLT);
entryIT.idtype = IT::F;
findFunc = true;
continue;
}
FST::FST fstReturn(word[i], FST_RETURN);
if (FST::execute(fstReturn))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_RETURN, LT_TI_NULLIDX, line);
LT::Add(lextable, entryLT);
continue;
}
FST::FST fstPrint(word[i], FST_PRINT);
if (FST::execute(fstPrint))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_PRINT, LT_TI_NULLIDX, line);
LT::Add(lextable, entryLT);
continue;
}
FST::FST fstMain(word[i], FST_MAIN);
if (FST::execute(fstMain))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_MAIN, LT_TI_NULLIDX, line);
LT::Add(lextable, entryLT);
strcpy(oldprefix, prefix);
strcpy(prefix, word[i]);
strcpy(entryIT.visibility, emptystr);
findMain = true;
countMain++;
continue;
}
FST::FST fstIdentif(word[i], FST_ID);
if (FST::execute(fstIdentif))
{
// ïîèñê òàêîãî æå
if (findFunc) // åñëè ôóíêöèÿ
{
int idx = IT::IsId(idtable, word[i]); // èùåì áåç ïðåôèêñà
if (idx != TI_NULLIDX) // åñëè òàêîé èäåíòèôèêàòîð óæå åñòü
{
LT::Entry entryLT = writeEntry(entryLT, LEX_ID, idx, line);
LT::Add(lextable, entryLT);
findFunc = false;
continue;
}
}
else
{
int idx = IT::IsId(idtable, word[i]); // èùåì áåç ïðåôèêñà, à ïîòîì ñ ïðåôèêñîì
if (idx == TI_NULLIDX && findDec == true && findType == false)
throw ERROR_THROW_IN(123, line, position);
if (idx != TI_NULLIDX) // åñëè òàêîé èäåíòèôèêàòîð óæå åñòü
{
if (findDec == true) //Ïðîâåðêà íà ïîâòîðíîå îáúÿâëåíèå
throw ERROR_THROW_IN(122, line, position);
LT::Entry entryLT = writeEntry(entryLT, LEX_ID, idx, line);
LT::Add(lextable, entryLT);
findFunc = false;
continue;
}
strcpy(bufprefix, prefix);
word[i] = strcat(bufprefix, word[i]);
idx = IT::IsId(idtable, word[i]);
if (idx != TI_NULLIDX) // åñëè òàêîé èäåíòèôèêàòîð óæå åñòü
{
if (findDec == true) //Ïðîâåðêà íà ïîâòîðíîå îáúÿâëåíèå
throw ERROR_THROW_IN(122, line, position);
LT::Entry entryLT = writeEntry(entryLT, LEX_ID, idx, line);
LT::Add(lextable, entryLT);
continue;
}
}
LT::Entry entryLT = writeEntry(entryLT, LEX_ID, indexID++, line);
LT::Add(lextable, entryLT);
findDec = false;
if (findParm) { // åñëè ïàðàìåòð
entryIT.idtype = IT::P;
idtable.table[bufPos].countOfPar++;
strcpy(entryIT.visibility, prefix);
}
else if (!findFunc) { // åñëè ïåðåìåííàÿ
entryIT.idtype = IT::V;
strcpy(entryIT.visibility, prefix);
if (entryIT.iddatatype == IT::INT)
entryIT.value.vint = TI_INT_DEFAULT;
if (entryIT.iddatatype == IT::STR) {
entryIT.value.vstr.len = 0;
memset(entryIT.value.vstr.str, TI_STR_DEFAULT, sizeof(char));
}
}
else { // åñëè ôóíêöèÿ
strcpy(oldprefix, prefix);
strcpy(prefix, word[i]);
strcpy(entryIT.visibility, emptystr);
}
entryIT.idxfirstLE = indxLex;
strcpy(entryIT.id, word[i]);
IT::Add(idtable, entryIT);
findFunc = false;
findType = false;
continue;
}
FST::FST fstLiteralInt(word[i], FST_INTLIT);
if (FST::execute(fstLiteralInt))
{
int value = atoi((char*)word[i]); // çíà÷åíèå ëèòåðàëà
// ïîèñê òàêîãî æå
for (int k = 0; k < idtable.size; k++)
{
if (idtable.table[k].value.vint == value && idtable.table[k].idtype == IT::L)
{
LT::Entry entryLT = writeEntry(entryLT, LEX_LITERAL, k, line);
LT::Add(lextable, entryLT);
findSameID = true;
break;
}
}
if (findSameID) // åñëè áûë íàéäåí òàêîé æå
continue;
LT::Entry entryLT = writeEntry(entryLT, LEX_LITERAL, indexID++, line);
LT::Add(lextable, entryLT);
entryIT.idtype = IT::L;
entryIT.iddatatype = IT::INT;
entryIT.value.vint = value;
entryIT.idxfirstLE = indxLex;
// ôîðìèðîâàíèå èìåíè ëèòåðàëà
_itoa_s(clit++, charclit, sizeof(char) * 10, 10); // ïðåîáðàçóåì çíà÷åíèå ñ÷åò÷èêà â ñòðîêó(charclit)
strcpy(bufL, L); // ïîìåùàåì â áóôåð "L"
word[i] = strcat(bufL, (char*)charclit); // ôîðìèðóåì èìÿ äëÿ ëèòåðàëà
strcpy(entryIT.id, word[i]);
IT::Add(idtable, entryIT);
continue;
}
FST::FST fstLiteralString(word[i], FST_STRLIT);
if (FST::execute(fstLiteralString))
{
// çàïèñü çíà÷åíèÿ
int length = strlen(word[i]);
for (int k = 0; k < length; k++) // ïåðåçàïèñü ìàññèâà, óáèðàåì êàâû÷êè
word[i][k] = word[i][k + 1];
word[i][length - 2] = 0;
// ïîèñê òàêîãî æå
for (int k = 0; k < idtable.size; k++)
{
if (!(strcmp(idtable.table[k].value.vstr.str, word[i])))
{
findSameID = true;
LT::Entry entryLT = writeEntry(entryLT, LEX_LITERAL, k, line);
LT::Add(lextable, entryLT);
break;
}
}
if (findSameID) // åñëè áûë íàéäåí òàêîé æå
continue;
LT::Entry entryLT = writeEntry(entryLT, LEX_LITERAL, indexID++, line);
LT::Add(lextable, entryLT);
strcpy(entryIT.value.vstr.str, word[i]); // çàïèñü çíà÷èíèÿ ñòðîêîâîãî ëèòåðàëà
entryIT.value.vstr.len = length - 2; // çàïèñü äëèíû ñòðîêîâîãî ëèòåðàëà
entryIT.idtype = IT::L;
entryIT.iddatatype = IT::STR;
entryIT.idxfirstLE = indxLex;
// ôîðìèðîâàíèå èìåíè ëèòåðàëà
_itoa_s(clit++, charclit, sizeof(char) * 10, 10); // ïðåîáðàçóåì çíà÷åíèå ñ÷åò÷èêà â ñòðîêó(charclit)
strcpy(bufL, L); // ïîìåùàåì â áóôåð "L"
nameLiteral = strcat(bufL, (char*)charclit); // ôîðìèðóåì èìÿ äëÿ ëèòåðàëà (L + charclit)
strcpy(entryIT.id, nameLiteral);
IT::Add(idtable, entryIT);
continue;
}
FST::FST fstOperator(word[i], FST_OPERATOR);
if (FST::execute(fstOperator))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_OPERATOR, indexID++, line);
switch (word[i][0])
{
case PLUS: case MINUS:
entryLT.priority = 2;
break;
case DIRSLASH: case STAR:
entryLT.priority = 3;
break;
}
LT::Add(lextable, entryLT);
strcpy(entryIT.id, word[i]);
entryIT.idxfirstLE = indxLex;
entryIT.idtype = IT::OP;
IT::Add(idtable, entryIT);
continue;
}
FST::FST fstSemicolon(word[i], FST_SEMICOLON);
if (FST::execute(fstSemicolon))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_SEMICOLON, LT_TI_NULLIDX, line);
LT::Add(lextable, entryLT);
continue;
}
FST::FST fstComma(word[i], FST_COMMA);
if (FST::execute(fstComma))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_COMMA, LT_TI_NULLIDX, line);
entryLT.priority = 1;
LT::Add(lextable, entryLT);
continue;
}
FST::FST fstLeftBrace(word[i], FST_LEFTBRACE);
if (FST::execute(fstLeftBrace))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_LEFTBRACE, LT_TI_NULLIDX, line);
LT::Add(lextable, entryLT);
continue;
}
FST::FST fstRightBrace(word[i], FST_BRACELET);
if (FST::execute(fstRightBrace))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_BRACELET, LT_TI_NULLIDX, line);
LT::Add(lextable, entryLT);
continue;
}
FST::FST fstLeftThesis(word[i], FST_LEFTTHESIS);
if (FST::execute(fstLeftThesis))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_LEFTTHESIS, LT_TI_NULLIDX, line);
entryLT.priority = 0;
LT::Add(lextable, entryLT);
if (idtable.table[indexID - 1].idtype == IT::F)
findParm = true;
bufPos = indexID - 1;
continue;
}
FST::FST fstRightThesis(word[i], FST_RIGHTTHESIS);
if (FST::execute(fstRightThesis))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_RIGHTTHESIS, LT_TI_NULLIDX, line);
entryLT.priority = 0;
if (findParm && word[i + 1][0] != LEX_LEFTBRACE && word[i + 2][0] != LEX_LEFTBRACE) // åñëè ïîñëå ôóíêöèè íåò {
strcpy(prefix, oldprefix); // âîçâðàùàåì ïðåäûäóùóþ îáë. âèäèìîñòè
findParm = false;
LT::Add(lextable, entryLT);
continue;
}
FST::FST fstEqual(word[i], FST_EQUAL);
if (FST::execute(fstEqual))
{
LT::Entry entryLT = writeEntry(entryLT, LEX_EQUAL, LT_TI_NULLIDX, line);
LT::Add(lextable, entryLT);
continue;
}
position += strlen(word[i]);
if (word[i][0] == DIV) {
line++;
position = 0;
indxLex--;
continue;
}
throw ERROR_THROW_IN(108, line, position);
}
if(!findMain) throw ERROR_THROW(2);
if (countMain > 1) throw ERROR_THROW(3);
cout << idtable.table[lextable.table[2].idxTI].countOfPar;
lex.idtable = idtable;
lex.lextable = lextable;
return lex;
}
}