-
Notifications
You must be signed in to change notification settings - Fork 0
/
project2.cc
751 lines (608 loc) · 19.9 KB
/
project2.cc
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
* Copyright (C) Mohsen Zohrevandi, 2017
* Rida Bazzi 2019
* Do not share this file with anyone
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include "lexer.h"
#include "project2.h"
using namespace std;
vector<string> symbols;
unordered_map<string, int> symbolmap;
vector<rule*> rules;
unordered_set<int> nonterminals;
unordered_set<int> terminals;
int sym_index;
LexicalAnalyzer lexer;
Token expect(TokenType expected_type)
{
Token t = lexer.GetToken();
if (t.token_type != expected_type)
syntax_error(__LINE__);
return t;
}
void syntax_error(int lineno)
{
cout << "SYNTAX ERROR !!!\n";
//printf("called from line number: %d\n", lineno);
exit(1);
}
void parseIDList(rule* r) {
Token t = expect(ID);
if (symbolmap.count(t.lexeme) == 0) {
// add symbol to universe container
symbols.push_back(t.lexeme);
// update symbol table with the symbol's index in the universe container,
// indicating that the symbol has been found
symbolmap[t.lexeme] = sym_index;
// add symbol to terminals set by default
terminals.insert(sym_index);
sym_index += 1;
}
r->RHS.push_back(symbolmap[t.lexeme]);
t = lexer.peek(1);
if (t.token_type == ID)
parseIDList(r);
else if (t.token_type != STAR)
syntax_error(__LINE__);
}
void parseRHS(rule* r) {
Token t = lexer.peek(1);
if (t.token_type == ID)
parseIDList(r);
else if (t.token_type == STAR)
r->RHS.push_back(0);
else
syntax_error(__LINE__);
}
void parseRule() {
rule* r = new rule;
Token t = expect(ID);
if (symbolmap.count(t.lexeme) == 0) {
// add symbol to universe container
symbols.push_back(t.lexeme);
// update symbol table with the symbol's index in the universe container,
// indicating that the symbol has been found
symbolmap[t.lexeme] = sym_index;
// add symbol to nonterminals set
nonterminals.insert(sym_index);
sym_index += 1;
} else if (nonterminals.find(symbolmap[t.lexeme]) == nonterminals.end()) {
// if the current symbol is known, but is not in the nonterminals set,
// add the symbol to the nonterminals set
nonterminals.insert(symbolmap[t.lexeme]);
// remove the symbol from the terminals set
terminals.erase(symbolmap[t.lexeme]);
}
r->LHS = symbolmap[t.lexeme];
expect(ARROW);
parseRHS(r);
expect(STAR);
rules.push_back(r);
}
void parseRuleList() {
parseRule();
Token t = lexer.peek(1);
if (t.token_type == ID) {
parseRuleList();
} else if (t.token_type != HASH)
syntax_error(__LINE__);
}
void parseGrammar() {
parseRuleList();
expect(HASH);
expect(END_OF_FILE);
}
// read grammar
void ReadGrammar()
{
symbols.push_back("#");
symbols.push_back("$");
sym_index = symbols.size();
parseGrammar();
}
// Task 1
void printTerminalsAndNoneTerminals()
{
vector<int> terms;
vector<int> nonterms;
for (int i = 2; i < symbols.size(); i++) {
if(nonterminals.find(symbolmap[symbols[i]]) == nonterminals.end())
terms.push_back(symbolmap[symbols[i]]);
else
nonterms.push_back(symbolmap[symbols[i]]);
}
for (int i = 0; i < terms.size(); i++)
cout << symbols[terms[i]] << " ";
for (int i = 0; i < nonterms.size(); i++)
cout << symbols[nonterms[i]] << " ";
}
void calcGeneratingSymbols(vector<rule*>* result) {
// initialization
int genArray[symbols.size()] = {0};
genArray[0] = 1;
for (auto it = terminals.begin(); it != terminals.end(); it++) {
genArray[*it] = 1;
}
// begin iteration
bool changed = true;
while(changed) {
changed = false;
// iterate through every rule
for (int i = 0; i < rules.size(); i++) {
bool generates = true;
// iterate through every symbol of the current rule
for (auto it = rules[i]->RHS.begin(); it != rules[i]->RHS.end(); it++) {
if (genArray[*it] == 0) {
generates = false;
break;
}
}
// if the current rule generates,
// denote it as having said attribute in genArray
if (generates && genArray[rules[i]->LHS] != 1) {
genArray[rules[i]->LHS] = 1;
changed = true;
}
}
}
// remove rules with non-generating symbols
// iterate through every rule
for (int i = 0; i < rules.size(); i++) {
bool generates = true;
// iterate through every symbol of the current rule and check if even a
// single symbol within the current rule doesn't generate
for (auto it = rules[i]->RHS.begin(); it != rules[i]->RHS.end(); it++) {
// if a symbol in the current rule doesn't generate,
// don't add the rule to the result vector
if (genArray[*it] == 0) {
generates = false;
break;
}
}
// if the current rule generates,
// add it to the resulting vector of rules
if (generates) {
result->push_back(rules[i]);
}
}
}
void calcReachableSymbols(vector<rule*>* result) {
// initialize
int reachArr[symbols.size()] = {0};
if (result->size() != 0) {
reachArr[(*result)[0]->LHS] = 1;
// iterate through each rule, adding all
// symbols of a given rule if the LHS is reachable
for (int i = 0; i < result->size(); i++) {
if(reachArr[(*result)[i]->LHS] == 1) {
for (auto it = (*result)[i]->RHS.begin(); it != (*result)[i]->RHS.end(); it++) {
reachArr[*it] = 1;
}
}
}
// create vector for all reachable rules
vector<rule*>* newResult = new vector<rule*>;
for (auto it = result->begin(); it != result->end(); it++) {
if (reachArr[(*it)->LHS] == 1) {
newResult->push_back(*it);
}
}
result->clear();
result->insert(result->end(), newResult->begin(), newResult->end());
}
}
// Task 2 test
void RemoveUselessSymbols() {
vector<rule*>* result = new vector<rule*>;
calcGeneratingSymbols(result);
calcReachableSymbols(result);
if ((result->size() != 0) && ((*result)[0]->LHS == rules[0]->LHS)) {
for (int i = 0; i < result->size(); i++) {
cout << symbols[(*result)[i]->LHS] << " ->";
for (auto it = (*result)[i]->RHS.begin(); it != (*result)[i]->RHS.end(); it++)
cout << " " << symbols[*it];
cout << endl;
}
}
}
// this function performs a + b, for a and b are both sets
// if mode == 1: add (a - {epsilon}) to b
// if mode == 0: add a to b
bool addSet (unordered_set<int>* a, unordered_set<int>* b, int mode) {
bool changed = false;
int origSize = b->size();
for (auto it = a->begin(); it != a->end(); it++) {
if (mode && (*it == 0))
continue;
else
b->insert(*it);
}
if (b->size() > origSize)
changed = true;
return changed;
}
// this function prints the contents of FIRST and FOLLOW sets
// mode == 1: print FIRST sets
// mode == 0: print FOLLOW sets
void printSets (std::unordered_map<int, std::unordered_set<int>>* s, int mode) {
vector<int> terms;
vector<int> nonterms;
string setName;
if (mode)
setName = "FIRST";
else
setName = "FOLLOW";
for (int i = 2; i < symbols.size(); i++) {
if(nonterminals.find(symbolmap[symbols[i]]) == nonterminals.end())
terms.push_back(symbolmap[symbols[i]]);
else
nonterms.push_back(symbolmap[symbols[i]]);
}
for (int i = 0; i < nonterms.size(); i++) {
cout << setName << "(" << symbols[nonterms[i]] << ") = { ";
bool firstFound = false;
for (int j = 0; j < symbols.size(); j++) {
if ((*s)[nonterms[i]].find(j) != (*s)[nonterms[i]].end())
if (!firstFound) {
cout << symbols[j];
firstFound = true;
} else
cout << ", " << symbols[j];
}
cout << " }" << endl;
}
}
// Task 3
unordered_map<int, unordered_set<int>>* CalculateFirstSets() {
unordered_map<int, unordered_set<int>>* FIRST = new unordered_map<int, unordered_set<int>>;
// apply rule I and II by
// initializing FIRST[x] = x for all terminals and epsilon
(*FIRST)[0].insert(0);
for (auto it = terminals.begin(); it != terminals.end(); it++) {
(*FIRST)[*it].insert(*it);
}
// begin iterating through each rule, applying rules III, IV, and V
// in successive order until there are no further changes made to FIRST
bool changed = true;
while (changed) {
changed = false;
for (auto it = rules.begin(); it != rules.end(); it++) {
// apply rule III
changed = changed || addSet(&(*FIRST)[(*it)->RHS.at(0)], &(*FIRST)[(*it)->LHS], 1);
// apply rule IV
bool fiveApplies = false;
for (int i = 0; i < (*it)->RHS.size(); i++) {
// check if epsilon is in FIRST[current symbol]
if (((*FIRST).count((*it)->RHS[i]) != 0) && ((*FIRST)[(*it)->RHS[i]].find(0) != (*FIRST)[(*it)->RHS[i]].end())) {
// if it is, then add the next symbol's FIRST set - {epsilon}
// to FIRST[LHS]
int next = i + 1;
if (next < (*it)->RHS.size())
changed = changed || addSet(&(*FIRST)[(*it)->RHS[next]], &(*FIRST)[(*it)->LHS], 1);
else if (next == (*it)->RHS.size())
fiveApplies = true;
} else
break;
}
// apply rule V
if (fiveApplies)
changed = changed || addSet(&(*FIRST)[0], &(*FIRST)[(*it)->LHS], 0);
}
}
return FIRST;
}
void printRule(rule* r) {
cout << symbols[r->LHS] << " ->";
for (auto it = r->RHS.begin(); it != r->RHS.end(); it++) {
cout << " " << symbols[*it];
}
cout << endl;
}
// Task 4
unordered_map<int, unordered_set<int>>* CalculateFollowSets(unordered_map<int, unordered_set<int>>* FIRST) {
unordered_map<int, unordered_set<int>>* FOLLOW = new unordered_map<int, unordered_set<int>>;
// start by applying rule I to add $ to FOLLOW(S)
int LHSindex = rules[0]->LHS;
(*FOLLOW)[LHSindex].insert(1);
// first pass, apply rules IV and V to all grammar rules
for (auto it = rules.begin(); it != rules.end(); it++) {
int RHSsize = (*it)->RHS.size();
int nextIndex = 0;
// apply rule IV to all nonterminals in a given rule
for (int i = 0; i < RHSsize; i++) {
// get current symbol index
int currentSym = (*it)->RHS[i];
// check if current symbol is a nonterminal
// if a subsequent symbol exists, add it's first set (minus epsilon)
// to the current symbol's FOLLOW set.
if ((nonterminals.find(currentSym) != nonterminals.end()) && (i + 1 < RHSsize)) {
nextIndex = (*it)->RHS[i + 1];
addSet(&(*FIRST)[nextIndex], &(*FOLLOW)[currentSym], 1);
}
}
// apply rule V to all nonterminals in a given rule
if (RHSsize > 2) {
for (int i = 0; i < RHSsize; i++) {
// get current symbol index
int currentSym = (*it)->RHS[i];
// check if current symbol is a nonterminal
if (nonterminals.find(currentSym) != nonterminals.end()) {
// iterate through subsequent symbols,
// adding FIRST(subsequent) - {epsilon} to the current nonterminal's FOLLOW set
// if the symbols between the currentSym and subsequent symbol all contain epsilon
int subseqSym = 0;
for (int j = i + 1; j < RHSsize; j++) {
if (j < RHSsize) {
subseqSym = (*it)->RHS[j];
// check if subsequent symbol's FIRST set contains epsilon
if((*FIRST)[subseqSym].find(0) != (*FIRST)[subseqSym].end()) {
addSet(&(*FIRST)[subseqSym], &(*FOLLOW)[currentSym], 1);
} else {
addSet(&(*FIRST)[subseqSym], &(*FOLLOW)[currentSym], 1);
break;
}
} else
break;
}
}
}
}
}
// apply rules II and III to all grammar rules until there are no changes
bool changed = true;
while (changed) {
changed = false;
for (auto it = rules.begin(); it != rules.end(); it++) {
int currentRule = (*it)->LHS;
int RHSsize = (*it)->RHS.size();
// apply rule II
if (RHSsize > 0) {
int lastSym = (*it)->RHS[RHSsize - 1];
if (nonterminals.find(lastSym) != nonterminals.end())
changed = changed || addSet(&(*FOLLOW)[currentRule], &(*FOLLOW)[lastSym], 0);
// apply rule III
if ((*FIRST)[lastSym].find(0) != (*FIRST)[lastSym].end())
for (int i = RHSsize - 2; i >= 0; i--) {
int currentSym = (*it)->RHS[i];
// check if the current symbol is a nonterminal
// if it's not, continue on to the next rule
if (nonterminals.find(currentSym) != nonterminals.end()) {
// starting from the last rule, check if the FIRST set of the
// currently observed symbol contains epsilon.
if((*FIRST)[currentSym].find(0) != (*FIRST)[(*it)->RHS[i]].end()) {
changed = changed || addSet(&(*FOLLOW)[currentRule], &(*FOLLOW)[currentSym], 0);
} else {
changed = changed || addSet(&(*FOLLOW)[currentRule], &(*FOLLOW)[currentSym], 0);
break;
}
} else
break;
}
}
}
}
return FOLLOW;
}
// this function performs "a intersect b", (for a,b are sets) and
// returns true if the result is the empty set, and returns false if the
// result is not the empty set
bool intersectSets(unordered_set<int>* a, unordered_set<int>* b) {
unordered_set<int>* larger = a;
unordered_set<int>* smaller = b;
bool isNil = true;
// if the b set is larger, specify it as such
if(b->size() > a->size()) {
larger = b;
smaller = a;
}
for (auto it = larger->begin(); it != larger->end(); it++) {
if (smaller->find(*it) != smaller->end()) {
isNil = false;
break;
}
}
return isNil;
}
// this function performs "a intersect b intersect ... intersect k", (for a...k are sets) and
// returns true if the result is the empty set, and returns false if the
// result is not the empty set
bool intersectSets(vector<int>* setSymbols) {
unordered_map<int, int> seenSyms;
bool isNil = true;
for (int i = 0; i < setSymbols->size(); i++) {
if (seenSyms.count((*setSymbols)[i]) == 0)
seenSyms[(*setSymbols)[i]] = 1;
else {
isNil = false;
break;
}
}
return isNil;
}
// Task 5
void CheckIfGrammarHasPredictiveParser() {
vector<rule*>* vec = new vector<rule*>;
bool hasPredParser = true;
// check if any of the rules are useless
// to determine if the grammar has a predictive parser
calcGeneratingSymbols(vec);
calcReachableSymbols(vec);
if(vec->size() != rules.size())
hasPredParser = false;
else {
// if none of the rules are useless,
// check the formal rules of predictive parsing
unordered_map<int, unordered_set<int>>* firstSets = CalculateFirstSets();
unordered_map<int, unordered_set<int>>* followSets = CalculateFollowSets(firstSets);
multimap<int, rule*> ruleSets;
for (int i = 0; i < rules.size(); i++)
ruleSets.insert(pair<int, rule*>(rules[i]->LHS, rules[i]));
// iterate through each non-terminal's rules and determine if the following conditions are met:
// condition 1: FIRST(a) intersect FIRST(b) == null set (for a and b are the RHS of a given NT's rules), and
// condition 2: if a nonterminal A derives to epsilon, check if
// FIRST(A) intersect FOLLOW(A)
for (auto nt = nonterminals.begin(); nt != nonterminals.end(); nt++) {
// check condition 2
if ((*firstSets)[*nt].find(0) != (*firstSets)[*nt].end()) {
if (!(intersectSets(&(*firstSets)[*nt], &(*followSets)[*nt]))) {
hasPredParser = false;
break;
}
}
// check condition 1
if (ruleSets.count(*nt) > 1) {
// iterator pair of the first and last rule of a given nonterminal
pair<multimap<int, rule*>::iterator, multimap<int, rule*>::iterator> range;
range = ruleSets.equal_range(*nt);
// vector used to hold all symbols of a
// given non-terminal's FIRST(RHS) (for every RHS of the given non-terminal)
vector<int> seenSyms;
// a map where: sizeTbl[rule-size] == rules with a "rule-size" number of symbols
multimap<int, rule*> sizeTbl;
// iterate through all of the current nonterminal's rules
for (auto ntRule = range.first; ntRule != range.second; ntRule++) {
bool uniqueRule = false;
// check if a rule with the same sized RHS has been seen before
// if it hasn't, continue
int currentRHSsize = (*ntRule).second->RHS.size();
rule* currentRule = (*ntRule).second;
if (sizeTbl.count(currentRHSsize) == 0) {
sizeTbl.insert(pair<int, rule*>(currentRHSsize, currentRule)); // come back
uniqueRule = true;
// else, check each rule to see if the current rule "rule" has been seen before, symbol by symbol.
} else {
// get pair of iterators that indicate the begin and end of the rules that correspond
// to a given rule's RHS size
pair<multimap<int, rule*>::iterator, multimap<int, rule*>::iterator> sizeRange;
sizeRange = sizeTbl.equal_range((*ntRule).second->RHS.size());
// iterate through each rule
for (auto curr = sizeRange.first; curr != sizeRange.second; curr++) {
// iterate through all symbols of the current rule,
// and compare them to those of all other rules of the same RHS size.
// if a discrepancy is found, then the rule is considered unique
uniqueRule = false;
//for (int i = 0; i < (*curr).second->RHS.size(); i++)
for (int i = 0; i < currentRHSsize; i++)
if ((*curr).second->RHS[i] != (*ntRule).second->RHS[i]) {
uniqueRule = true;
break;
}
if (!uniqueRule)
break;
}
}
if (uniqueRule) {
// add currently observed unique rule to the list of unique rules
sizeTbl.insert(pair<int, rule*>((*ntRule).second->RHS.size(), (*ntRule).second));
// calculate the FIRST sets of all the rules, and add all the elements to a vector by
// iterating through each symbol of a given rule, adding their FIRST sets until a symbol whose
// FIRST set does not contain epsilon is found
unordered_set<int> firstRHS;
for (auto symbol = (*ntRule).second->RHS.begin(); symbol != (*ntRule).second->RHS.end(); symbol++) {
addSet(&(*firstSets)[*symbol], &firstRHS, 0);
if ((*firstSets)[*symbol].find(0) == (*firstSets)[*symbol].end())
break;
}
copy(firstRHS.begin(), firstRHS.end(), back_inserter(seenSyms));
}
}
if(!(intersectSets(&seenSyms))) {
hasPredParser = false;
break;
}
}
if (!hasPredParser)
break;
}
}
if (hasPredParser)
cout << "YES" << endl;
else
cout << "NO" << endl;
}
int main (int argc, char* argv[]) {
int task;
if (argc < 2)
{
cout << "Error: missing argument\n";
return 1;
}
/*
Note that by convention argv[0] is the name of your executable,
and the first argument to your program is stored in argv[1]
*/
task = atoi(argv[1]);
ReadGrammar(); // Reads the input grammar from standard input
// and represent it internally in data structures
// ad described in project 2 presentation file
switch (task) {
case 1: printTerminalsAndNoneTerminals();
break;
case 2: RemoveUselessSymbols();
break;
case 3:
{
unordered_map<int, unordered_set<int>>* firstSets = CalculateFirstSets();
printSets(firstSets, 1);
break;
}
case 4:
{
unordered_map<int, unordered_set<int>>* firstSets = CalculateFirstSets();
unordered_map<int, unordered_set<int>>* followSets = CalculateFollowSets(firstSets);
printSets(followSets, 0);
break;
}
case 5: CheckIfGrammarHasPredictiveParser();
break;
// test set addition function
case 6:
{
unordered_set<int> presetB = {0};
unordered_set<int> presetA = {3, 4};
unordered_set<int> a = presetA;
unordered_set<int> b = presetB;
cout << "set A: ";
for (auto it = a.begin(); it != a.end(); it++)
cout << *it << ", ";
cout << endl << "set B: ";
for (auto it = b.begin(); it != b.end(); it++)
cout << *it << ", ";
cout << endl << endl;
cout << "add A to B:" << endl;
if (addSet(&a, &b, 0)) {
cout << "a change occured." << endl
<< "set B now contains:";
for (auto it = b.begin(); it != b.end(); it++)
cout << " " << *it;
cout << endl;
} else
cout << "no change." << endl;
cout << endl;
a = presetA;
b = presetB;
cout << "add A - {epsilon} to B" << endl;
if (addSet(&a, &b, 1)) {
cout << "a change occured." << endl
<< "set B now contains:";
for (auto it = b.begin(); it != b.end(); it++)
cout << " " << *it;
cout << endl;
} else
cout << "no change." << endl;
cout << endl;
break;
}
default:
cout << "Error: unrecognized task number " << task << "\n";
break;
}
return 0;
}