-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPGOptimizeStates.cpp
167 lines (151 loc) · 4.33 KB
/
PGOptimizeStates.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
#ifdef LRSTAR
#include "ComGlobal.h"
#include "PGOptimizeStates.h"
int *pr_type;
int *hs_type;
int *old_state;
int *new_state;
int *reductions;
///////////////////////////////////////////////////////////////////////////////
// //
int PGOptimizeStates::OptimizeStates ()
{
accept_state = FIND_ACCEPT_STATE ();
MAKE_SHIFT_REDUCE_ACTIONS ();
ELIMINATE_CHAIN_REDUCTIONS ();
if (optn[PG_SHIFTREDUCE])
{
n_states = opt_states+1;
tt_states = opt_states+1;
ntt_states = opt_states+1;
}
else
{
tt_states = n_states;
ntt_states = n_states;
}
return (1);
}
///////////////////////////////////////////////////////////////////////////////
// //
// Find accept state (need this for PG_CLR_PARSER, works for the others too) //
// This is a generalized version. Could have searched state 1 for <eof>.
int PGOptimizeStates::FIND_ACCEPT_STATE ()
{
int ntt, tt;
int state = 0;
int symb = Tail [F_tail [F_prod[0]]];
if (symb < 0) // Nonterminal symbol?
{
for (ntt = ntt_start[state]; ntt < ntt_start[state+1]; ntt++)
{
if (ntt_symb[ntt] == -symb)
{
state = ntt_action[ntt];
break;
}
}
}
else // Terminal symbol!
{
for (tt = tt_start[state]; tt < tt_start[state+1]; tt++)
{
if (tt_symb[tt] == symb)
{
state = tt_action[tt];
break;
}
}
}
symb = Tail [F_tail [F_prod[0]] + 1]; // <eof> symbol
for (tt = tt_start[state]; tt < tt_start[state+1]; tt++)
{
if (tt_symb[tt] == symb)
{
state = tt_action[tt];
break;
}
}
return state;
}
///////////////////////////////////////////////////////////////////////////////
// //
void PGOptimizeStates::MAKE_SHIFT_REDUCE_ACTIONS ()
{
int state, i;
if (optn[PG_SHIFTREDUCE]) // Shift-reduce actions wanted?
{
for (state = 0; state < opt_states; state++)
{
for (i = tt_start [state]; i < tt_start [state+1]; i++)
{
if (tt_action[i] > opt_states)
{
tt_action[i] = -D_red [tt_action[i]];
}
}
for (i = ntt_start [state]; i < ntt_start [state+1]; i++)
{
if (ntt_action[i] >= opt_states)
{
ntt_action[i] = -D_red [ntt_action[i]];
}
}
if (optn[PG_NONDETER])
{
for (i = nd_start[state]; i < nd_start[state+1]; i++)
{
if (nd_action[i] >= opt_states)
nd_action[i] = opt_states + D_red [nd_action[i]];
}
}
}
}
}
///////////////////////////////////////////////////////////////////////////////
// //
void PGOptimizeStates::ELIMINATE_CHAIN_REDUCTIONS () // Eliminate Chain Shift-Reduce Actions.
{
int state, i, h, p, j, n_changed, total = 0;
if (optn [PG_OPTIMIZE])
{
for (state = 0; state < opt_states; state++)
{
Loop: n_changed = 0;
for (i = ntt_start[state]; i < ntt_start[state+1]; i++)
{
p = -ntt_action[i];
if (p > 0 && prod_len[p] == 1 && Pact_numb[p] == -1 && Node_numb[p] == -1)
{
h = head_sym[p];
for (j = ntt_start[state]; j < ntt_start[state+1]; j++)
{
if (ntt_symb[j] == h) break;
}
ntt_action[i] = ntt_action [j];
n_changed++;
total++;
}
}
if (n_changed > 0) goto Loop;
for (i = tt_start[state]; i < tt_start[state+1]; i++)
{
p = -tt_action[i];
if (p > 0 && prod_len[p] == 1 && Pact_numb[p] == -1 && Node_numb[p] == -1)
{
h = head_sym[p];
for (j = ntt_start[state]; j < ntt_start[state+1]; j++)
{
if (ntt_symb[j] == h) break;
}
tt_action[i] = ntt_action[j];
}
}
}
// if (optn[PG_VERBOSE] > 1)
// prt_log (" %8d nonterminal transitions were removed by optimization.\n", total);
}
}
// //
///////////////////////////////////////////////////////////////////////////////
#endif