-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlr2.cc
70 lines (59 loc) · 1.62 KB
/
lr2.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
#include <string>
#include <iostream>
#include <vector>
#include <map>
#include <cstdlib>
#include <sstream>
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elements;
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
elements.push_back(item);
}
return elements;
}
int main() {
std::vector<std::string> rules;
std::map<std::string, std::string> actions;
std::string current_line;
// skip symbol lists
for (int i = 0; i < 2; ++i) {
int skips;
std::getline(std::cin, current_line);
skips = atoi(current_line.c_str());
while (skips-- > 0) {
std::getline(std::cin, current_line);
}
}
// skip start symbol
std::getline(std::cin, current_line);
// load rule vector
int n;
std::getline(std::cin, current_line);
n = atoi(current_line.c_str());
while (n-- > 0) {
std::getline(std::cin, current_line);
rules.push_back(current_line);
}
// load action map
int dfa_states;
std::getline(std::cin, current_line);
dfa_states = atoi(current_line.c_str());
std::getline(std::cin, current_line);
n = atoi(current_line.c_str());
while (n-- > 0) {
std::getline(std::cin, current_line);
std::vector<std::string> line_parts = split(current_line, ' ');
std::string key = line_parts[0] + " " + line_parts[1];
std::string value = line_parts[2] + " ";
value += (value == "shift ") ? line_parts[3] : rules[atoi(line_parts[3].c_str())];
actions[key] = value;
}
// run on input
while (std::getline(std::cin, current_line)) {
std::cout << ((actions.count(current_line)) ? actions[current_line] : "error") << std::endl;
}
return 0;
}