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
|
#include "list.h"
#include "ht.h"
#include "slr_table.h"
struct rule_s{
NUM nt_num;
NUM rule_num;
};
struct item_s {
rule r;
NUM pos;
};
struct item_set_s {
/* a list of items */
List *nonkernels;
/* a list of nonterminal symbols: a kernel item for a nonterminal
consists of all rules for that nonterminal, so we only need to
store the nonterminal, which are represented as NUM's. */
List *kernels;
};
typedef enum SLR_ACTION_e {
SHIFT,
REDUCE,
ACCEPT,
REJECT
} SLR_ACTION;
struct action_s {
SLR_ACTION type;
NUM arg1;
NUM arg2;
};
struct slr_table_s {
/* a list of states/rows
Each row contains a hash table mapping non-terminals, terminals,
and predicate terminals to an action set. */
List *rows;
/* a list of item sets; these are the states */
List *states;
};
BOOL
closure(item_set *is, Grammar *g)
{
NUM nonlen = list_length(is->nonkernels);
for (NUM i = 0; i < nonlen; i++) {
item it = *((item *)list_nth(is->nonkernels, i));
Rule_group *rg = grammar_rule(g, it.r.nt_num);
List *tnts = rg_nth(rg, it.r.rule_num);
if (list_length(tnts) == 0) continue;
NUM *temp = MYALLOC(NUM, 1);
*temp = *((NUM *) list_nth(tnts, it.pos));
if (add_to_list(is->kernels, temp)) {
free(temp);
fleprintf("Fail to add to list at i = %ld\n", i);
return 1;
}
}
return 0;
}
|