-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
685 lines (580 loc) · 19.3 KB
/
main.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#include <regex>
#include <map>
#include <vector>
#include <stack>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
//Ñïèñîê ëåêñåì
enum Lexem {
lex_error = -1,
lex_begin = 1,
lex_var,
lex_type,
lex_id,
lex_const,
lex_equal,
lex_minus,
lex_plus,
lex_multiply,
lex_div,
lex_lbracket,
lex_rbracket,
lex_comma,
lex_end,
lex_dot,
lex_colon,
lex_semicol,
};
#define CODE_FILEPATH "code.txt"
#define LEX_FILEPATH "lex.txt"
#define PFIX_BUFF_FILEPATH "pfix_buff.txt"
#define PFIX_FILEPATH "pfix.txt"
#define MNEMOCODE_FILEPATH "mnemocode.txt"
ifstream lexFile{ LEX_FILEPATH };
ofstream buffPfixFile{ PFIX_BUFF_FILEPATH };
int currentId;
size_t currentString;
string currentLexeme;
int Z();
//Ïîäâûðàæåíèå
int W();
//Âûðàæåíèå
int E();
//Ïðèñâàèâàíèå (+ ïðèñâàèâàíèå)
int A();
//Êîä ïðîãðàììû
int F();
//Áëîê êîäà (begin F end.)
int D();
//Îáúÿâëåíèå òèïà è ïðîãðàììà
int T();
//Ñïèñîê ïåðåìåííûõ èëè îáúÿâëåíèå òèïà
int X();
//Îáúÿâëåíèå ïåðåìåííûõ
int S();
//Ëåñêè÷åñêèé àíàëèç
int LexicalAnalysis()
{
bool isErrorExists{ false };
regex regexw(R"([a-z_A-Z_]+)"); //Ñëîâî
regex regexd("([0-9]+)"); //×èñëî
regex regexi(R"(^[a-z_A-Z_]+[a-z_A-Z_0-9]*)"); //Èäåíòèôèêàòîð
//Ïîòîê ÷òåíèÿ èñõîäíîãî ôàéëà ñ êîäîì
ifstream srcFile{ CODE_FILEPATH };
//Ïîòîê çàïèñè ôàéëà ñ ëåêñè÷åñêèì àíàëèçîì
ofstream destFile{ LEX_FILEPATH };
if (!srcFile)
{
cout << "[ERROR]\t\t" << "File error : Unable to open source file.Expected path : " << CODE_FILEPATH << endl;
return 1;
}
else
{
cout << "[DEBUG]\t\t" << "The original file was successfully opened" << endl;
}
//Íàêàïëèâàþùàÿ ñòðîêà áóôåð
string strBuff;
//Òåêóùèé ñèìâîë
char sym;
//Èäåíòèôèêàòîð ëåêñåìû
int id;
//Òåêóùàÿ ñòðîêà
size_t currentString = 1;
while (!srcFile.eof())
{
id = lex_error;
strBuff = "";
sym = srcFile.get();
if (sym == ' ' || sym == '\n' || sym == '\t\t' || sym == EOF)
{
if (sym == '\n') currentString++;
continue;
}
if (isalpha(sym) || isdigit(sym))
{
strBuff += sym;
while (isalpha(srcFile.peek()) || isdigit(srcFile.peek()))
{
sym = srcFile.get();
strBuff += sym;
}
if (regex_match(strBuff, regexd)) id = lex_const; // Åñëè ñëîâî ÿâëÿåòñÿ êîíñòàíòîé
else if (regex_match(strBuff, regexi)) { // Ñðàâíåíèå ñëîâà ñ êëþ÷åâûìè ïî ðåãóëÿðíûì âûðàæåíèÿì
if (strBuff == "Begin") id = lex_begin;
else if (strBuff == "Var") id = lex_var;
else if (strBuff == "Integer") id = lex_type;
else if (strBuff == "End") id = lex_end;
else id = lex_id; // Èíà÷å ñëîâî ÿâÿåòñÿ èäåíòèôèêàòîðîì
}
else id = lex_error; // Îøèáêà
}
else {
strBuff += sym;
switch (strBuff[0]) // Åñëè âñòðåòèëñÿ îïåðàòîð
{
case '=':
id = lex_equal;
break;
case '-':
id = lex_minus;
break;
case '+':
id = lex_plus;
break;
case '*':
id = lex_multiply;
break;
case '/':
id = lex_div;
break;
case '(':
id = lex_lbracket;
break;
case ')':
id = lex_rbracket;
break;
case ',':
id = lex_comma;
break;
case '.':
id = lex_dot;
break;
case ';':
id = lex_semicol;
break;
case ':':
id = lex_colon;
break;
default:
id = lex_error;
break;
}
}
if (id == lex_error)
{
cout << "Lexical error: " << '\'' << strBuff << "\' " << " is undefined " << '\n';
isErrorExists = true;
}
destFile << strBuff << " " << id << " " << currentString << '\n';
}
cout << "[DEBUG]\t\t" << "The lexical file was successfully written" << endl;
srcFile.close();
cout << "[DEBUG]\t\t" << "The original file was closed" << endl;
destFile.close();
cout << "[DEBUG]\t\t" << "The lexical file was closed" << endl;
if (isErrorExists)
return 1;
return 0;
}
//ÍÅÒÅÐÌÈÍÀËÛ
//Âñïîìîãàòåëüíûé íåòåðìèíàë
int Z()
{
if (currentId == lex_semicol || currentId == lex_rbracket)
{
return 0;
}
else if (currentId == lex_plus || currentId == lex_minus || currentId == lex_multiply || currentId == lex_div)
{
buffPfixFile << currentLexeme << ' ' << currentId << ' ' << currentString << '\n';
lexFile >> currentLexeme >> currentId >> currentString;
return W();
}
else
{
cout << "[ERROR]\t\t" << "Syntax error: expected \';\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
}
}
//Âûðàæåíèå
int E()
{
if (currentId == lex_minus)
{
buffPfixFile << '~' << ' ' << currentId << ' ' << currentString << '\n';
lexFile >> currentLexeme >> currentId >> currentString;
}
return W();
}
//Ïîäâûðàæåíèå
int W()
{
if (currentId == lex_lbracket)
{
buffPfixFile << currentLexeme << ' ' << currentId << ' ' << currentString << '\n';
lexFile >> currentLexeme >> currentId >> currentString;
if (E() != 0)
{
return 1;
}
if (currentId == lex_rbracket)
{
buffPfixFile << currentLexeme << ' ' << currentId << ' ' << currentString << '\n';
lexFile >> currentLexeme >> currentId >> currentString;
return Z();
}
else {
cout << "[ERROR]\t\t" << "Syntax error: expected \')\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
}
}
else if (currentId == lex_id || currentId == lex_const)
{
buffPfixFile << currentLexeme << ' ' << currentId << ' ' << currentString << '\n';
lexFile >> currentLexeme >> currentId >> currentString;
return Z();
}
else {
cout << "[ERROR]\t\t" << "Syntax error: expected \'operator\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
}
}
//Ïðèñâàèâàíèå (+ ïðèñâàèâàíèå)
int A()
{
if (currentId != lex_id) {
cout << "[ERROR]\t\t" << "Syntax error: expected \'identificator\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
}
buffPfixFile << currentLexeme << ' ' << currentId << ' ' << currentString << '\n';
lexFile >> currentLexeme >> currentId >> currentString;
if (currentId != lex_equal)
{
cout << "[ERROR]\t\t" << "Syntax error: expected \'=\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
}
buffPfixFile << currentLexeme << ' ' << currentId << ' ' << currentString << '\n';
lexFile >> currentLexeme >> currentId >> currentString;
return E();
}
//Êîä ïðîãðàììû
int F()
{
lexFile >> currentLexeme >> currentId >> currentString;
while (currentId != lex_end && !lexFile.eof())
{
if (A() != 0)
{
return 1;
}
buffPfixFile << currentLexeme << ' ' << currentId << ' ' << currentString << '\n';
lexFile >> currentLexeme >> currentId >> currentString;
}
return 0;
}
//Áëîê êîäà (begin F end.)
int D()
{
lexFile >> currentLexeme >> currentId >> currentString;
if (currentId != lex_begin) {
cout << "[ERROR]\t\t" << "Syntax error: expected \'Begin\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
}
if (F() != 0) {
return 1;
}
if (currentId != lex_end) {
cout << "[ERROR]\t\t" << "Syntax error: expected \'End\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
}
lexFile >> currentLexeme >> currentId >> currentString;
if (currentId != lex_dot) {
cout << "[ERROR]\t\t" << "Syntax error: expected \'.\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
}
return 0;
}
//Îáúÿâëåíèå òèïà è ïðîãðàììà
int T()
{
lexFile >> currentLexeme >> currentId >> currentString;
if (currentId != lex_type) {
cout << "[ERROR]\t\t" << "Syntax error: expected \'Integer\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
}
lexFile >> currentLexeme >> currentId >> currentString;
buffPfixFile << currentLexeme << ' ' << currentId << ' ' << currentString << '\n';
if (currentId != lex_semicol) {
cout << "[ERROR]\t\t" << "Syntax error: expected \';\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
}
return D();
}
//Ñïèñîê ïåðåìåííûõ èëè îáúÿâëåíèå òèïà
int X()
{
lexFile >> currentLexeme >> currentId >> currentString;
if (currentId == lex_comma)
return S();
else if (currentId == lex_colon)
return T();
else
{
cout << "[ERROR]\t\t" << "Syntax error: expected \':\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
}
}
//Îáúÿâëåíèå ïåðåìåííûõ
int S()
{
lexFile >> currentLexeme >> currentId >> currentString;
if (currentId == lex_id)
{
buffPfixFile << currentLexeme << ' ' << currentId << ' ' << currentString << '\n';
return X();
}
else
{
cout << "[ERROR]\t\t" << "Syntax error: expected \'identificator\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
}
}
//Ñèíòàêñè÷åñêèé àíàëèç
int SyntaxAnalysis()
{
//Îòêðûòèå ïîòîêà äëÿ ÷òåíèÿ ôàéëà ëåêñè÷åñêîãî àíàëèçà
if (!lexFile)
{
cout << "[ERROR]\t\t" << "File error: Unable to open source file. Expected path: " << LEX_FILEPATH << endl;
return 1;
}
else
{
cout << "[DEBUG]\t\t" << "The lexical file was successfully opened" << endl;
}
//Îòêðûòèå ïîòîêà äëÿ ÷òåíèÿ ôàéëà ëåêñè÷åñêîãî àíàëèçà
if (!buffPfixFile)
{
cout << "[ERROR]\t\t" << "File error: Unable to open source file. Expected path: " << PFIX_BUFF_FILEPATH << endl;
return 1;
}
else
{
cout << "[DEBUG]\t\t" << "The buffer postfix file was successfully opened" << endl;
}
lexFile >> currentLexeme >> currentId >> currentString;
if (currentId == lex_var)
{
return S();
}
else
{
cout << "[ERROR]\t\t" << "Syntax error: expected \'Var\', but " << currentLexeme << " encountered at line " << currentString << endl;
return 1;
};
}
int MakeRPN()
{
ifstream buffPfixFile{ PFIX_BUFF_FILEPATH };
ofstream pfixFile{ PFIX_FILEPATH }; // Ôàéë äëÿ çàïèñè ðåçóëüòàòà
if (!buffPfixFile)
{
cout << "[ERROR]\t\t" << "File error: Unable to open source file. Expected path: " << PFIX_BUFF_FILEPATH << endl;
return 1;
}
else
{
cout << "[DEBUG]\t\t" << "The buffer postfix file was successfully opened" << endl;
}
if (!pfixFile)
{
cout << "[ERROR]\t\t" << "File error: Unable to open output file. Expected path: " << PFIX_FILEPATH << endl;
return 1;
}
else
{
cout << "[DEBUG]\t\t" << "The postfix file was successfully opened" << endl;
}
map<string, int> varMap;
stack<string> opStack;
bool isAssignment = false; // Ôëàã, óêàçûâàþùèé, íàõîäèòñÿ ëè ëåâàÿ ÷àñòü âûðàæåíèÿ äî çíàêà "="
do
{
buffPfixFile >> currentLexeme >> currentId >> currentString;
if (currentId == lex_id)
{
varMap.insert(std::make_pair(currentLexeme, 1));
}
} while (currentId != lex_semicol && !buffPfixFile.eof());
while (buffPfixFile >> currentLexeme >> currentId >> currentString)
{
if (currentId == lex_id) // Åñëè ëåêñåìà ÿâëÿåòñÿ èäåíòèôèêàòîðîì
{
if (isAssignment) {
// Åñëè ïåðåìåííàÿ íàõîäèòñÿ â ëåâîé ÷àñòè âûðàæåíèÿ, èíèöèàëèçèðóåì å¸
varMap[currentLexeme] = 2;
}
else {
// Ïðîâåðÿåì, áûëà ëè îáúÿâëåíà ïåðåìåííàÿ
if (varMap.find(currentLexeme) == varMap.end() || varMap[currentLexeme] == 0) {
cout << "[ERROR]\t\t" << "Error: Variable '" << currentLexeme << "' is not declared or initialized at line " << currentString <<"." << endl;
return 1;
}
}
pfixFile << currentLexeme << " ";
}
else if (currentId == lex_const) // Åñëè ëåêñåìà ÿâëÿåòñÿ êîíñòàíòîé
{
pfixFile << currentLexeme << " ";
}
else if (currentId == lex_equal) // Åñëè ýòî çíàê ïðèñâàèâàíèÿ
{
isAssignment = true; // Ëåâàÿ ÷àñòü âûðàæåíèÿ äî çíàêà "="
opStack.push(currentLexeme);
}
else if (currentId == lex_plus || currentId == lex_minus || currentId == lex_multiply || currentId == lex_div) // Åñëè ýòî îïåðàòîðû
{
while (!opStack.empty() && ((currentId == lex_plus || currentId == lex_minus) &&
(opStack.top() == "*" || opStack.top() == "/" || opStack.top() == "-")) ||
((currentId == lex_multiply || currentId == lex_div) &&
(opStack.top() == "*" || opStack.top() == "/")))
{
pfixFile << opStack.top() << " ";
opStack.pop();
}
opStack.push(currentLexeme);
}
else if (currentId == lex_lbracket) // Åñëè îòêðûâàþùàÿ ñêîáêà
{
opStack.push(currentLexeme);
}
else if (currentId == lex_rbracket) // Åñëè çàêðûâàþùàÿ ñêîáêà
{
while (!opStack.empty() && opStack.top() != "(") {
pfixFile << opStack.top() << " ";
opStack.pop();
}
opStack.pop(); // Óáèðàåì îòêðûâàþùóþ ñêîáêó
}
else if (currentId == lex_semicol) // Êîíåö âûðàæåíèÿ
{
while (!opStack.empty()) {
pfixFile << opStack.top() << " ";
opStack.pop();
}
pfixFile << endl;
isAssignment = false; // Îáíóëÿåì ôëàã ïðèñâàèâàíèÿ äëÿ ñëåäóþùåãî âûðàæåíèÿ
}
}
buffPfixFile.close();
cout << "[DEBUG]\t\t" << "The buffer postfix file was successfully closed" << endl;
pfixFile.close();
cout << "[DEBUG]\t\t" << "The postfix file was successfully closed" << endl;
return 0;
}
int GenerateMnemocode()
{
regex regexd("([0-9]+)");
regex regexi(R"(^[a-z_A-Z_]+[a-z_A-Z_0-9]*)");
ifstream pfixFile{ PFIX_FILEPATH };
if (!pfixFile)
{
cout << "[ERROR]\t\t" << "Unable to open postfix file. Expected path: " << PFIX_FILEPATH << endl;
return 1;
}
else
{
cout << "[DEBUG]\t\t" << "The postfix file was successfully opened" << endl;
}
ofstream mnemocodeFile{ MNEMOCODE_FILEPATH };
if (!mnemocodeFile)
{
cout << "[ERROR]\t\t" << "Unable to open mnemocode file. Expected path: " << MNEMOCODE_FILEPATH << endl;
return 1;
}
else
{
cout << "[DEBUG]\t\t" << "The mnemocode file was successfully opened" << endl;
}
vector<string> pfixLine;
string strBuff = "";
string varBuff;
while (!pfixFile.eof())
{
pfixFile >> varBuff;
pfixFile >> strBuff;
while (strBuff != "=" && !pfixFile.eof())
{
if (regex_match(strBuff, regexi))
{
mnemocodeFile << "LOAD " << strBuff << "\n";
}
else if (regex_match(strBuff, regexd))
{
mnemocodeFile << "LIT " << strBuff << "\n";
}
else {
switch (strBuff[0])
{
case '+':
mnemocodeFile << "ADD\n";
break;
case '-':
mnemocodeFile << "SUB\n";
break;
case '/':
mnemocodeFile << "DIV\n";
break;
case '*':
mnemocodeFile << "MUL\n";
break;
case '~':
mnemocodeFile << "NOT\n";
break;
default:
cout << "[ERROR]\t\t" << "Lexical error: unexpected symbol: '" << strBuff << "'" << endl;
cout << "[DEBUG]\t\t" << "The postfix file was successfully closed" << endl;
cout << "[DEBUG]\t\t" << "The mnemocode file was successfully closed" << endl;
pfixFile.close();
mnemocodeFile.close();
return 1;
}
}
pfixFile >> strBuff;
}
if (strBuff[0] == '=' && !pfixFile.eof())
{
mnemocodeFile << "STO " << varBuff << "\n";
}
else
{
if (pfixFile.eof()) {
pfixFile.close();
mnemocodeFile.close();
return 0;
}
cout << "[ERROR]\t\t" << "Lexical error: unexpected symbol: '" << strBuff << "'" << endl;
cout << "[DEBUG]\t\t" << "The postfix file was successfully closed" << endl;
cout << "[DEBUG]\t\t" << "The mnemocode file was successfully closed" << endl;
pfixFile.close();
mnemocodeFile.close();
return 1;
}
}
}
int main()
{
if (LexicalAnalysis()) return 1;
else
cout << "[RESULT]\t" << "Lexical analysis was successful" << endl;
if (SyntaxAnalysis()) return 1;
else
cout << "[RESULT]\t" << "Syntax analysis was successful" << endl;
buffPfixFile.close();
cout << "[DEBUG]\t\t" << "The buffer postfix file was successfully closed" << endl;
lexFile.close();
cout << "[DEBUG]\t\t" << "The lexical file was successfully closed" << endl;
if (MakeRPN()) return 1;
else
cout << "[RESULT]\t" << "The generation of reverse Polish notation was successful" << endl;
if (GenerateMnemocode()) return 1;
else
{
cout << "[RESULT]\t" << "The generation of mnemocode was successful" << endl;
}
cout << "\n\n\t\t\t" << "ALL OPERATIONS WAS SUCCESSFUL" << endl;
return 0;
}