-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
123 lines (104 loc) · 4.89 KB
/
main.cpp
File metadata and controls
123 lines (104 loc) · 4.89 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
//
// Created by Dell on 03/12/2023.
//
#include <iostream>
#include <chrono>
#include "lexical_analyzer/NFA.cpp"
#include "lexical_analyzer/RegexParser.cpp"
#include "lexical_analyzer/State.cpp"
#include "lexical_analyzer/PriorityTable.cpp"
#include "lexical_analyzer/DFA.cpp"
#include "lexical_analyzer/NfaToDfaConverter.cpp"
#include "lexical_analyzer/LexicalParser.cpp"
#include "lexical_analyzer/DFAMinimization.cpp"
#include "lexical_analyzer/TransitionTableWriter.cpp"
#include "lexical_analyzer/ThomsonConstructor.cpp"
#include "lexical_analyzer/Utility.cpp"
#include "syntax_analyzer/CFGReader.h"
#include "syntax_analyzer/LeftRecursionEliminator.h"
#include "syntax_analyzer/LeftFactorer.h"
#include "syntax_analyzer/ParsingTable.h"
#include "syntax_analyzer/FollowSet.h"
#include "syntax_analyzer/FirstSet.h"
#include "syntax_analyzer/SyntaxParser.h"
#include "syntax_analyzer/OUtil.h"
using namespace std;
string getFileName(const string& filePath) {
return filePath.substr(filePath.find_last_of("/\\") + 1);
}
int main(int argc, char* argv[]) {
// arguments are as follows:
// 1. Lexical Rules FilePath
// 2. CFG Rules FilePath
// 3. paths of any number of input source programs
if (argc < 3) {
cerr << "Expected 4 arguments, but got " << argc << std::endl;
exit(1); // Return an error
}
string rulesFilePath = argv[1];
string rulesFileName = getFileName(rulesFilePath);
auto start = chrono::high_resolution_clock::now();
RegexParser regexParser;
NFA* nfa = regexParser.parseREs(rulesFilePath);
vector<DFA*> dfa = NfaToDfaConverter::convertNFAToDFA(nfa->startState);
std::set<DFA*> minimizedDfa = DFAMinimization::minimization(dfa);
auto end = std::chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(end - start);
cout << "No of NFA states: " << nfa->getSize() << endl;
cout << "No of DFA states: " << dfa.size() << endl;
cout << "No of minimized DFA states: " << minimizedDfa.size() << endl;
cout << "Execution time of grammar parsing: " << duration.count() << " milliseconds" << endl;
TransitionTableWriter::writeTableInTabularForm(minimizedDfa, "../lexical_analyzer/output", rulesFileName);
DFA* startDFA = DFAMinimization::getStartState(minimizedDfa);
cout << "===================================================\n";
cout << "\nLexical Analyzer Output:\n";
for (int i = 3; i < argc; i++) {
std::cout << "Token Classes of " << argv[i] << ":\n\n";
LexicalParser parser(*startDFA, argv[i]);
while (!parser.isClosedFile()) {
string tokenClass = parser.getNextToken();
cout << "Token: ";
if (tokenClass == "$")
cout << "Input File EOF has been reached!!" << std::endl;
else
cout << tokenClass << std::endl;
}
cout << "\n===================================================================\n";
LexicalParser parserFW(*startDFA, argv[i]);
parserFW.writeAllTokens("../lexical_analyzer/output/" + getFileName(argv[i]) + "_tokens.txt");
}
/// Parser Part
CFGReader reader = CFGReader(argv[2]);
map<string, vector<vector<string>>> rules = reader.parseRules();
LeftRecursionEliminator lREliminator = LeftRecursionEliminator(rules);
rules = lREliminator.removeLeftRecursion();
string startSymbol = reader.getStartSymbol();
cout << "\n\nRules After Left Recursion Elimination: \n";
OUtil::printCFG(startSymbol, rules);
rules = LeftFactorer::leftFactor(rules);
cout << "\n\nRules After Left Factoring: \n";
OUtil::printCFG(startSymbol, rules);
cout << "\n===================================================================\n";
// Compute FIRST sets
map<string, set<string>> firstSet = FirstSet::firstSet(rules);
OUtil::printSet(firstSet, "FIRST");
cout << "\n===================================================================\n";
// Compute FOLLOW sets
//string startSymbol ="E";
map<string, set<string>> followSet = FollowSet::followSet(rules, firstSet, startSymbol);
OUtil::printSet(followSet, "FOLLOW");
cout << "\n===================================================================\n";
// Print Parsing table
map<string, map<string, vector<string>>> parsingTable = ParsingTable::getParsingTable(rules, firstSet, followSet);
OUtil::printTable(parsingTable);
for (int i = 3; i < argc; i++) {
cout << "Parser Output of " << argv[i] << ":\n\n";
LexicalParser lexicalParser(*startDFA, argv[i]);
SyntaxParser::init(lexicalParser, parsingTable, startSymbol);
auto pair = SyntaxParser::parseProgram();
OUtil::writeDerivations(pair.first, "../syntax_analyzer/output", getFileName(argv[i]));
OUtil::printOutput(pair.second);
cout << "\n===================================================================\n";
}
return 0;
}