-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActionMap.cpp
310 lines (290 loc) · 12.1 KB
/
ActionMap.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
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
#include "ActionMap.h"
//-------------------------------------------------------
#include "bux/XException.h" // RUNTIME_ERROR()
#include <optional> // std::optional<>
#define GLR_ACTIONS_
namespace {
//
// Local Functions
//
#ifndef GLR_ACTIONS_
std::string toStr(const C_ReductionStep &srcRStep)
{
return srcRStep.first->inputName(srcRStep.second, "(.)");
}
std::string toMessage (
const std::string &prefix,
const C_ReductionStep &item1,
const C_ReductionStep &item2 )
{
return prefix + " Conflict :\n"
"\t" + toStr(item1) + "\n"
"\t" + toStr(item2);
}
#endif
std::optional<int> makeCompare(
const C_PriorityMap &priorMap,
T_LexID left,
T_LexID right )
{
const auto i1 = priorMap.find(left);
if (i1 == priorMap.end())
return {};
const auto i2 = priorMap.find(right);
if (i2 == priorMap.end())
return {};
int compare = int(i1->second.m_Weight) - int(i2->second.m_Weight);
if (!compare && i1->second.m_Assoc == i2->second.m_Assoc)
switch (i1->second.m_Assoc)
{
case ASSOC_NONE:
return {};
case LEFT2RIGHT:
compare = 1;
break;
case RIGHT2LEFT:
compare = -1;
break;
default:
RUNTIME_ERROR("Unknown associativity {}", int(i1->second.m_Assoc));
}
return compare;
}
} // namespace
//
// External Functions
//
C_ActionMap makeActionMap(const C_ParserInfo &parsed, const C_States &states, C_ActionShifts &loserShits)
{
// (1/2) Collect all
C_ActionMap actionMap;
for (auto &i: states)
{
auto &dstLex2Act = actionMap.emplace(i.m_id, parsed).first->second; // C_Lex2Action
for (auto &j: i.m_rstep2la)
{
const auto srcRStep = j.first;
if (srcRStep.first->m_Rval.size() == srcRStep.second)
// At end of production - Reduction or Acceptance
{
#ifdef GLR_ACTIONS_
for (auto k: j.second)
{
auto &dst = dstLex2Act[k];
if (!k && srcRStep.first->m_Lval == "@")
dst.m_hasAccept = true;
else
dst.m_reduces.insert(srcRStep.first);
}
#else
const auto &srcLA = j.second;
if (srcRStep.first->m_Lval == "@" && srcLA.size() == 1 && !*srcLA.begin())
// <@> reduced on EOF - End of parsing
{
const auto iDst = dstLex2Act.find(nullptr); // nullptr <-> EOF
if (iDst != dstLex2Act.end() &&
iDst->second.first != ACCEPT)
// Just in case: Simply overwrite the action
conflicts.insert(toMessage("EOF", iDst->second.second, srcRStep));
auto &t = dstLex2Act[nullptr];
t.first = ACCEPT;
t.second = srcRStep;
}
else for (auto k: srcLA)
{
C_ActionValue val{REDUCE, srcRStep}; // defaulted to reduce on each lookahead
const auto iDst = dstLex2Act.find(k);
if (iDst != dstLex2Act.end())
// Conflict iDst
{
bool conflict = true;
const char *prefix = nullptr;
if (iDst->second.first == SHIFT)
// Shift-Reduce Conflict -- Sovled wherever operator precedency applies
{
const auto kid = parsed.prodTerm2id(k);
const auto &srcRval = srcRStep.first->m_Rval;
if (srcRval.empty())
// Reduce empty production first
conflict = false;
else
// Check operator precedency
for (auto m = srcRval.crbegin(); m != srcRval.crend(); ++m)
{
const auto mid = parsed.prodTerm2id(*m);
int compare;
if (makeCompare(parsed.priorityMap(), mid, kid, compare))
{
if (compare < 0)
// Don't change it
val = iDst->second;
conflict = false;
break;
}
}
if (conflict)
{
prefix = "Shift-Reduce";
val = iDst->second; // Don't change it
}
}
else if (iDst->second.first == REDUCE)
// Reduce-Reduce Conflict
{
/* Either they are the same, or the longer the better
*/
if (iDst->second.second.first == srcRStep.first ||
iDst->second.second.first->m_Rval.size() < srcRStep.first->m_Rval.size())
conflict = false;
else if (iDst->second.second.first->m_Rval.size() > srcRStep.first->m_Rval.size())
{
conflict = false;
val.second = iDst->second.second;
}
else
{
prefix = "Reduce-Reduce";
//%%%%% Case Study
}
}
else
prefix = "Other(1/2)";
if (conflict)
conflicts.insert(toMessage(prefix, iDst->second.second, srcRStep));
}
dstLex2Act[k] = val;
} // for (auto &k: srcLA)
#endif // GLR_ACTIONS_
}
else if (auto const t = dynamic_cast<I_Terminal*>(srcRStep.first->m_Rval.at(srcRStep.second)))
// Shift next terminal - The sufficient condition to grow the dstLex2Act
{
#ifdef GLR_ACTIONS_
dstLex2Act[t].m_hasShift = true;
#else
C_ActionValue val(SHIFT, srcRStep); // defaulted to shift
auto iDst = dstLex2Act.find(t);
if (iDst != dstLex2Act.end())
// Conflict iDst
{
bool conflict = true;
const char *prefix = nullptr;
const auto tid = parsed.prodTerm2id(t);
if (iDst->second.first == REDUCE)
// Shift-Reduce Conflict (Again) - Prefer SHIFT unless operator precedency speaks.
{
auto const prod = iDst->second.second.first;
const auto &seq = prod->m_Rval;
for (auto m = seq.end(); m != seq.begin();)
{
const auto mid = parsed.prodTerm2id(*--m);
int compare;
if (makeCompare(parsed.priorityMap(), mid, tid, compare))
// Conflict sovled wherever operator precedency applies
{
if (compare > 0)
val = iDst->second;
break;
}
}
conflict = false;
}
else if (iDst->second.first == SHIFT)
// Shift-Shift Conflict -- %%%%% Why ? Defaulted ?
{
if (iDst->second.second == srcRStep)
conflict = false;
else
{
auto &fstep = iDst->second.second;
auto &rval = srcRStep.first->m_Rval;
auto &fval = fstep.first->m_Rval;
const size_t n = rval.size() - srcRStep.second;
if (n == fval.size() - fstep.second)
{
for (size_t i = 0; i < n; ++i)
{
const auto rid = parsed.prodTerm2id(rval[srcRStep.second+i]);
const auto fid = parsed.prodTerm2id(fval[fstep.second+i]);
if (rid != fid)
goto conflictStill;
}
conflict = false;
if (fval.size() > rval.size())
// Prefer longer production
val.second = fstep;
conflictStill:;
}
}
if (conflict)
prefix = "Shift-Shift";
}
else
prefix = "Other(2/2)";
if (conflict)
conflicts.insert(toMessage(prefix, iDst->second.second, srcRStep));
}
dstLex2Act[t] = val;
#endif // GLR_ACTIONS_
}
} // for (auto &j: i.m_rstep2la)
} // for (auto &i: states)
// (2/2) Minimize conflicts
for (auto &i: actionMap)
for (auto &j: i.second)
{
if (j.second.m_hasShift && !j.second.m_reduces.empty())
// Minimize shift-reduce conflicts by operator precedency
{
const auto jid = parsed.prodTerm2id(j.first);
for (auto k = j.second.m_reduces.begin(); k != j.second.m_reduces.end();)
{
const auto &srcRval = (**k).m_Rval;
for (auto m = srcRval.crbegin(); m != srcRval.crend(); ++m)
if (auto compare = makeCompare(parsed.priorityMap(), parsed.prodTerm2id(*m), jid))
{
if (*compare < 0)
// Shift wins
{
k = j.second.m_reduces.erase(k);
goto NextK;
}
else
// Reduce wins
{
j.second.m_hasShift = false;
loserShits.emplace_back(i.first, j.first);
goto DoneShiftReduce;
}
}
++k;
NextK:;
}
DoneShiftReduce:;
}
}
return actionMap;
}
//
// Implement Classes
//
bool C_Actions::operator<(const C_Actions &other) const
{
if (m_hasShift != other.m_hasShift)
return m_hasShift < other.m_hasShift;
if (m_hasAccept != other.m_hasAccept)
return m_hasAccept < other.m_hasAccept;
return m_reduces < other.m_reduces;
}
bool C_Actions::canUseLR1() const
{
switch (m_reduces.size())
{
case 0:
return m_hasShift? !m_hasAccept: m_hasAccept;
case 1:
return !m_hasShift && !m_hasAccept;
default:
return false;
}
}