-
Notifications
You must be signed in to change notification settings - Fork 1
/
configlist.c
307 lines (288 loc) · 8.75 KB
/
configlist.c
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
/*+@@file@@----------------------------------------------------------------*//*!
\file configlist.c
\par Description
Routines to processing a configuration list and building a
state in the LEMON parser generator.
\par Status:
\par Project:
Lemon parser
\date Created on Sat Sep 1 18:35:49 2018
\date Modified on Sat Sep 1 18:35:49 2018
\author
\*//*-@@file@@----------------------------------------------------------------*/
#include "main.h"
#include "struct.h"
#include "table.h"
#include "set.h"
#include "plink.h"
static struct config *freelist = 0; /* List of free configurations */
static struct config *current = 0; /* Top of list of configurations */
static struct config **currentend = 0; /* Last on list of configs */
static struct config *basis = 0; /* Top of list of basis configs */
static struct config **basisend = 0; /* End of list of basis configs */
/*+@@fnc@@----------------------------------------------------------------*//*!
\brief newconfig
\details Return a pointer to a new configuration
\date Created on Sat Sep 1 18:36:25 2018
\date Modified on Sat Sep 1 18:36:25 2018
\*//*-@@fnc@@----------------------------------------------------------------*/
PRIVATE struct config *newconfig(void)
{
struct config *newcfg;
if (freelist == 0)
{
int i;
int amt = 3;
freelist = (struct config *)calloc(amt, sizeof(struct config));
if (freelist == 0)
{
fprintf(stderr, "Unable to allocate memory for a new configuration.");
exit(1);
}
for (i = 0; i < amt - 1; i++)
freelist[i].next = &freelist[i + 1];
freelist[amt - 1].next = 0;
}
newcfg = freelist;
freelist = freelist->next;
return newcfg;
}
/*+@@fnc@@----------------------------------------------------------------*//*!
\brief deleteconfig
\details The configuration "old" is no longer used
\date Created on Sat Sep 1 18:36:41 2018
\date Modified on Sat Sep 1 18:36:41 2018
\*//*-@@fnc@@----------------------------------------------------------------*/
PRIVATE void deleteconfig(struct config *old)
{
old->next = freelist;
freelist = old;
}
/*+@@fnc@@----------------------------------------------------------------*//*!
\brief Configlist_init
\details Initialized the configuration list builder
\date Created on Sat Sep 1 18:36:55 2018
\date Modified on Sat Sep 1 18:36:55 2018
\*//*-@@fnc@@----------------------------------------------------------------*/
void Configlist_init(void)
{
current = 0;
currentend = ¤t;
basis = 0;
basisend = &basis;
Configtable_init();
return;
}
/*+@@fnc@@----------------------------------------------------------------*//*!
\brief Configlist_reset
\details Initialized the configuration list builder
\date Created on Sat Sep 1 18:37:20 2018
\date Modified on Sat Sep 1 18:37:20 2018
\*//*-@@fnc@@----------------------------------------------------------------*/
void Configlist_reset(void)
{
current = 0;
currentend = ¤t;
basis = 0;
basisend = &basis;
Configtable_clear(0);
return;
}
/*+@@fnc@@----------------------------------------------------------------*//*!
\brief Configlist_add
\details Add another configuration to the configuration list
\param [in] rp The rule
\param [in] dot Index into the RHS of the rule where the dot goes
\date Created on Sat Sep 1 18:39:40 2018
\date Modified on Sat Sep 1 18:39:40 2018
\*//*-@@fnc@@----------------------------------------------------------------*/
struct config *Configlist_add(struct rule *rp, int dot)
{
struct config *cfp, model;
assert(currentend != 0);
model.rp = rp;
model.dot = dot;
cfp = Configtable_find(&model);
if (cfp == 0)
{
cfp = newconfig();
cfp->rp = rp;
cfp->dot = dot;
cfp->fws = SetNew();
cfp->stp = 0;
cfp->fplp = cfp->bplp = 0;
cfp->next = 0;
cfp->bp = 0;
*currentend = cfp;
currentend = &cfp->next;
Configtable_insert(cfp);
}
return cfp;
}
/*+@@fnc@@----------------------------------------------------------------*//*!
\brief Configlist_addbasis
\details Add a basis configuration to the configuration list
\date Created on Sat Sep 1 18:40:03 2018
\date Modified on Sat Sep 1 18:40:03 2018
\*//*-@@fnc@@----------------------------------------------------------------*/
struct config *Configlist_addbasis(struct rule *rp, int dot)
{
struct config *cfp, model;
assert(basisend != 0);
assert(currentend != 0);
model.rp = rp;
model.dot = dot;
cfp = Configtable_find(&model);
if (cfp == 0)
{
cfp = newconfig();
cfp->rp = rp;
cfp->dot = dot;
cfp->fws = SetNew();
cfp->stp = 0;
cfp->fplp = cfp->bplp = 0;
cfp->next = 0;
cfp->bp = 0;
*currentend = cfp;
currentend = &cfp->next;
*basisend = cfp;
basisend = &cfp->bp;
Configtable_insert(cfp);
}
return cfp;
}
/*+@@fnc@@----------------------------------------------------------------*//*!
\brief Configlist_closure
\details Compute the closure of the configuration list
\date Created on Sat Sep 1 18:41:00 2018
\date Modified on Sat Sep 1 18:41:00 2018
\*//*-@@fnc@@----------------------------------------------------------------*/
void Configlist_closure(struct lemon *lemp)
{
struct config *cfp, *newcfp;
struct rule *rp, *newrp;
struct symbol *sp, *xsp;
int i, dot;
assert(currentend != 0);
for (cfp = current; cfp; cfp = cfp->next)
{
rp = cfp->rp;
dot = cfp->dot;
if (dot >= rp->nrhs)
continue;
sp = rp->rhs[dot];
if (sp->type == NONTERMINAL)
{
if (sp->rule == 0 && sp != lemp->errsym)
{
ErrorMsg(lemp->filename, rp->line, "Nonterminal \"%s\" has no rules.", sp->name);
lemp->errorcnt++;
}
for (newrp = sp->rule; newrp; newrp = newrp->nextlhs)
{
newcfp = Configlist_add(newrp, 0);
for (i = dot + 1; i < rp->nrhs; i++)
{
xsp = rp->rhs[i];
if (xsp->type == TERMINAL)
{
SetAdd(newcfp->fws, xsp->index);
break;
}
else if (xsp->type == MULTITERMINAL)
{
int k;
for (k = 0; k < xsp->nsubsym; k++)
{
SetAdd(newcfp->fws, xsp->subsym[k]->index);
}
break;
}
else
{
SetUnion(newcfp->fws, xsp->firstset);
if (xsp->lambda == LEMON_FALSE)
break;
}
}
if (i == rp->nrhs)
Plink_add(&cfp->fplp, newcfp);
}
}
}
return;
}
/*+@@fnc@@----------------------------------------------------------------*//*!
\brief Configlist_sort
\details Sort the configuration list
\date Created on Sat Sep 1 18:41:45 2018
\date Modified on Sat Sep 1 18:41:45 2018
\*//*-@@fnc@@----------------------------------------------------------------*/
void Configlist_sort(void)
{
current = (struct config *)msort((char *)current, (char **)&(current->next), Configcmp);
currentend = 0;
return;
}
/*+@@fnc@@----------------------------------------------------------------*//*!
\brief Configlist_sortbasis
\details Sort the basis configuration list
\date Created on Sat Sep 1 18:42:04 2018
\date Modified on Sat Sep 1 18:42:04 2018
\*//*-@@fnc@@----------------------------------------------------------------*/
void Configlist_sortbasis(void)
{
basis = (struct config *)msort((char *)current, (char **)&(current->bp), Configcmp);
basisend = 0;
return;
}
/*+@@fnc@@----------------------------------------------------------------*//*!
\brief Configlist_return
\details Return a pointer to the head of the configuration list and
reset the list
\date Created on Sat Sep 1 18:42:23 2018
\date Modified on Sat Sep 1 18:42:23 2018
\*//*-@@fnc@@----------------------------------------------------------------*/
struct config *Configlist_return(void)
{
struct config *old;
old = current;
current = 0;
currentend = 0;
return old;
}
/*+@@fnc@@----------------------------------------------------------------*//*!
\brief Configlist_basis
\details Return a pointer to the head of the configuration list and
reset the list
\date Created on Sat Sep 1 18:42:48 2018
\date Modified on Sat Sep 1 18:42:48 2018
\*//*-@@fnc@@----------------------------------------------------------------*/
struct config *Configlist_basis(void)
{
struct config *old;
old = basis;
basis = 0;
basisend = 0;
return old;
}
/*+@@fnc@@----------------------------------------------------------------*//*!
\brief Configlist_eat
\details Free all elements of the given configuration list
\date Created on Sat Sep 1 18:43:13 2018
\date Modified on Sat Sep 1 18:43:13 2018
\*//*-@@fnc@@----------------------------------------------------------------*/
void Configlist_eat(struct config *cfp)
{
struct config *nextcfp;
for (; cfp; cfp = nextcfp)
{
nextcfp = cfp->next;
assert(cfp->fplp == 0);
assert(cfp->bplp == 0);
if (cfp->fws)
SetFree(cfp->fws);
deleteconfig(cfp);
}
return;
}