-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptable_reader.cpp
122 lines (115 loc) · 3.57 KB
/
ptable_reader.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
#include "ptable_reader.h"
#include "exceptions.h"
#include <iostream>
#include <string>
ParsingTableReader::ParsingTableReader(std::string actionFileName, std::string gotoFileName) {
std::ifstream actionFile(actionFileName);
if (!actionFile.is_open()) {
throw FileNotFoundException(actionFileName);
}
std::ifstream gotoFile(gotoFileName);
if (!gotoFile.is_open()) {
throw FileNotFoundException(gotoFileName);
}
std::string line, terminal="";
actionFile >> line;
for(int i = 0; i < line.length(); i++) {
if(line[i] != ',') {
terminal += line[i];
}
if (line[i] == ',') {
if(terminal.length() > 0) {
terminals.push_back(terminal);
terminal = "";
}
} else if (i < line.length() - 1 && line[i] == '"' && line[i + 1] == ','){
terminals.push_back(",");
terminal = "";
i += 2;
}
}
if (terminal.length() > 0) {
terminals.push_back(terminal);
}
std::string nonTerminal = "";
gotoFile >> line;
for(int i = 0; i < line.length(); i++) {
if(line[i] != ',') {
nonTerminal += line[i];
}
if (line[i] == ',') {
if(nonTerminal.length() > 0) {
nonTerminals.push_back(nonTerminal);
nonTerminal = "";
}
} else if (i < line.length() - 1 && line[i] == '"' && line[i + 1] == ','){
nonTerminals.push_back(",");
nonTerminal = "";
i += 2;
}
}
if (nonTerminal.length() > 0) {
nonTerminals.push_back(nonTerminal);
}
// print all terminals
// cout << "Terminals: ";
// for(int i = 0; i < terminals.size(); i++) {
// cout << terminals[i] << " ";
// }
// cout << endl;
// // print all non terminals
// cout << "Non-Terminals: ";
// for(int i = 0; i < nonTerminals.size(); i++) {
// cout << nonTerminals[i] << " ";
// }
// cout << endl;
while (actionFile >> line) {
std::string action = "";
std::vector<std::string> actions;
for(int i = 0; i < line.length(); i++) {
if(line[i] != ',') {
action += line[i];
}
if (line[i] == ',') {
actions.push_back(action);
action = "";
}
}
actions.push_back(action);
std::string state = actions[0];
for (int i = 1; i < actions.size(); i++) {
std::string action = actions[i];
if (action.length() == 0) {
continue;
}
// actionTable[ { state, terminals[i] } ] = action;
actionTable[make_pair(state, terminals[i])] = action;
}
}
while (gotoFile >> line) {
std::string _goto = "";
std::string state = "";
std::vector<std::string> gotos;
for(int i = 0; i < line.length(); i++) {
if(line[i] != ',') {
_goto += line[i];
}
if (line[i] == ',') {
gotos.push_back(_goto);
_goto = "";
}
}
gotos.push_back(_goto);
state = gotos[0];
for (int i = 1; i < gotos.size(); i++) {
std::string _gotoInside = gotos[i];
if (_gotoInside.length() == 0) {
continue;
}
// gotoTable[{state, terminals[i]}] = _gotoInside;
gotoTable[make_pair(state, nonTerminals[i])] = _gotoInside;
}
}
gotoFile.close();
actionFile.close();
}