1 | n/a | |
---|
2 | n/a | /* Computation of FIRST stets */ |
---|
3 | n/a | |
---|
4 | n/a | #include "pgenheaders.h" |
---|
5 | n/a | #include "grammar.h" |
---|
6 | n/a | #include "token.h" |
---|
7 | n/a | |
---|
8 | n/a | extern int Py_DebugFlag; |
---|
9 | n/a | |
---|
10 | n/a | /* Forward */ |
---|
11 | n/a | static void calcfirstset(grammar *, dfa *); |
---|
12 | n/a | |
---|
13 | n/a | void |
---|
14 | n/a | addfirstsets(grammar *g) |
---|
15 | n/a | { |
---|
16 | n/a | int i; |
---|
17 | n/a | dfa *d; |
---|
18 | n/a | |
---|
19 | n/a | if (Py_DebugFlag) |
---|
20 | n/a | printf("Adding FIRST sets ...\n"); |
---|
21 | n/a | for (i = 0; i < g->g_ndfas; i++) { |
---|
22 | n/a | d = &g->g_dfa[i]; |
---|
23 | n/a | if (d->d_first == NULL) |
---|
24 | n/a | calcfirstset(g, d); |
---|
25 | n/a | } |
---|
26 | n/a | } |
---|
27 | n/a | |
---|
28 | n/a | static void |
---|
29 | n/a | calcfirstset(grammar *g, dfa *d) |
---|
30 | n/a | { |
---|
31 | n/a | int i, j; |
---|
32 | n/a | state *s; |
---|
33 | n/a | arc *a; |
---|
34 | n/a | int nsyms; |
---|
35 | n/a | int *sym; |
---|
36 | n/a | int nbits; |
---|
37 | n/a | static bitset dummy; |
---|
38 | n/a | bitset result; |
---|
39 | n/a | int type; |
---|
40 | n/a | dfa *d1; |
---|
41 | n/a | label *l0; |
---|
42 | n/a | |
---|
43 | n/a | if (Py_DebugFlag) |
---|
44 | n/a | printf("Calculate FIRST set for '%s'\n", d->d_name); |
---|
45 | n/a | |
---|
46 | n/a | if (dummy == NULL) |
---|
47 | n/a | dummy = newbitset(1); |
---|
48 | n/a | if (d->d_first == dummy) { |
---|
49 | n/a | fprintf(stderr, "Left-recursion for '%s'\n", d->d_name); |
---|
50 | n/a | return; |
---|
51 | n/a | } |
---|
52 | n/a | if (d->d_first != NULL) { |
---|
53 | n/a | fprintf(stderr, "Re-calculating FIRST set for '%s' ???\n", |
---|
54 | n/a | d->d_name); |
---|
55 | n/a | } |
---|
56 | n/a | d->d_first = dummy; |
---|
57 | n/a | |
---|
58 | n/a | l0 = g->g_ll.ll_label; |
---|
59 | n/a | nbits = g->g_ll.ll_nlabels; |
---|
60 | n/a | result = newbitset(nbits); |
---|
61 | n/a | |
---|
62 | n/a | sym = (int *)PyObject_MALLOC(sizeof(int)); |
---|
63 | n/a | if (sym == NULL) |
---|
64 | n/a | Py_FatalError("no mem for new sym in calcfirstset"); |
---|
65 | n/a | nsyms = 1; |
---|
66 | n/a | sym[0] = findlabel(&g->g_ll, d->d_type, (char *)NULL); |
---|
67 | n/a | |
---|
68 | n/a | s = &d->d_state[d->d_initial]; |
---|
69 | n/a | for (i = 0; i < s->s_narcs; i++) { |
---|
70 | n/a | a = &s->s_arc[i]; |
---|
71 | n/a | for (j = 0; j < nsyms; j++) { |
---|
72 | n/a | if (sym[j] == a->a_lbl) |
---|
73 | n/a | break; |
---|
74 | n/a | } |
---|
75 | n/a | if (j >= nsyms) { /* New label */ |
---|
76 | n/a | sym = (int *)PyObject_REALLOC(sym, |
---|
77 | n/a | sizeof(int) * (nsyms + 1)); |
---|
78 | n/a | if (sym == NULL) |
---|
79 | n/a | Py_FatalError( |
---|
80 | n/a | "no mem to resize sym in calcfirstset"); |
---|
81 | n/a | sym[nsyms++] = a->a_lbl; |
---|
82 | n/a | type = l0[a->a_lbl].lb_type; |
---|
83 | n/a | if (ISNONTERMINAL(type)) { |
---|
84 | n/a | d1 = PyGrammar_FindDFA(g, type); |
---|
85 | n/a | if (d1->d_first == dummy) { |
---|
86 | n/a | fprintf(stderr, |
---|
87 | n/a | "Left-recursion below '%s'\n", |
---|
88 | n/a | d->d_name); |
---|
89 | n/a | } |
---|
90 | n/a | else { |
---|
91 | n/a | if (d1->d_first == NULL) |
---|
92 | n/a | calcfirstset(g, d1); |
---|
93 | n/a | mergebitset(result, |
---|
94 | n/a | d1->d_first, nbits); |
---|
95 | n/a | } |
---|
96 | n/a | } |
---|
97 | n/a | else if (ISTERMINAL(type)) { |
---|
98 | n/a | addbit(result, a->a_lbl); |
---|
99 | n/a | } |
---|
100 | n/a | } |
---|
101 | n/a | } |
---|
102 | n/a | d->d_first = result; |
---|
103 | n/a | if (Py_DebugFlag) { |
---|
104 | n/a | printf("FIRST set for '%s': {", d->d_name); |
---|
105 | n/a | for (i = 0; i < nbits; i++) { |
---|
106 | n/a | if (testbit(result, i)) |
---|
107 | n/a | printf(" %s", PyGrammar_LabelRepr(&l0[i])); |
---|
108 | n/a | } |
---|
109 | n/a | printf(" }\n"); |
---|
110 | n/a | } |
---|
111 | n/a | |
---|
112 | n/a | PyObject_FREE(sym); |
---|
113 | n/a | } |
---|