-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParse_table.cpp
executable file
·117 lines (106 loc) · 2.8 KB
/
Parse_table.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
#include "Parse_table.h"
#include "recource.h"
map<string, map<string, vector<string>>> parse_table;
bool isterminal(string str) // check if a string is a terminal or not.
{
std::vector<string>::iterator it;
it = std::find(terminals.begin(), terminals.end(), str);
if (it != terminals.end())
{
return true;
}
else
{
return false;
}
}
int get_nonTreminalindex(string str) // get the index of a certain non terminal by its name.
{
for (int i = 0; i < all_nonTerminals.size(); i++)
{
if (all_nonTerminals.at(i).name == str)
{
return i;
}
}
return -1;
}
bool is_this_production(string production_first, string terminal)
{
bool ans = false;
if (production_first.size() == 0)
return false;
if (isterminal(production_first))
{
if (production_first == terminal)
return true;
else
return false;
}
else
{
int index = get_nonTreminalindex(production_first);
for (auto each_production : all_nonTerminals[index].productions)
{
ans |= is_this_production(each_production[0], terminal);
}
}
return ans;
}
bool has_epsilon(string non_terminal_name)
{
int index = get_nonTreminalindex(non_terminal_name);
for (auto production : all_nonTerminals[index].productions)
{
if (production.back() == "^")
{
return true;
}
}
return false;
}
void filltable()
{
for (auto non_T : all_nonTerminals)
{
string nonT_name = non_T.name;
vector<vector<string>> productions = non_T.productions;
for (auto terminal : first[nonT_name])
{
if (terminal == "^")
continue;
if (productions.size() == 1)
{
parse_table[nonT_name][terminal] = productions.back();
}
else
{
for (auto production : productions)
{
if (is_this_production(production[0], terminal))
{
parse_table[nonT_name][terminal] = production;
}
}
}
}
//add synch for error panic recovery mode
if (has_epsilon(nonT_name))
{
for (auto terminal : follow[nonT_name])
{
if (parse_table[nonT_name].count(terminal) == 0)
parse_table[nonT_name][terminal] = {"^"};
}
}
else
{
for (auto terminal : follow[nonT_name])
{
if (parse_table[nonT_name].count(terminal) == 0)
parse_table[nonT_name][terminal] = {"synch"};
}
}
//check if non terminal has an epsilon
}
}