1 | n/a | |
---|
2 | n/a | /* Parser accelerator module */ |
---|
3 | n/a | |
---|
4 | n/a | /* The parser as originally conceived had disappointing performance. |
---|
5 | n/a | This module does some precomputation that speeds up the selection |
---|
6 | n/a | of a DFA based upon a token, turning a search through an array |
---|
7 | n/a | into a simple indexing operation. The parser now cannot work |
---|
8 | n/a | without the accelerators installed. Note that the accelerators |
---|
9 | n/a | are installed dynamically when the parser is initialized, they |
---|
10 | n/a | are not part of the static data structure written on graminit.[ch] |
---|
11 | n/a | by the parser generator. */ |
---|
12 | n/a | |
---|
13 | n/a | #include "pgenheaders.h" |
---|
14 | n/a | #include "grammar.h" |
---|
15 | n/a | #include "node.h" |
---|
16 | n/a | #include "token.h" |
---|
17 | n/a | #include "parser.h" |
---|
18 | n/a | |
---|
19 | n/a | /* Forward references */ |
---|
20 | n/a | static void fixdfa(grammar *, dfa *); |
---|
21 | n/a | static void fixstate(grammar *, state *); |
---|
22 | n/a | |
---|
23 | n/a | void |
---|
24 | n/a | PyGrammar_AddAccelerators(grammar *g) |
---|
25 | n/a | { |
---|
26 | n/a | dfa *d; |
---|
27 | n/a | int i; |
---|
28 | n/a | d = g->g_dfa; |
---|
29 | n/a | for (i = g->g_ndfas; --i >= 0; d++) |
---|
30 | n/a | fixdfa(g, d); |
---|
31 | n/a | g->g_accel = 1; |
---|
32 | n/a | } |
---|
33 | n/a | |
---|
34 | n/a | void |
---|
35 | n/a | PyGrammar_RemoveAccelerators(grammar *g) |
---|
36 | n/a | { |
---|
37 | n/a | dfa *d; |
---|
38 | n/a | int i; |
---|
39 | n/a | g->g_accel = 0; |
---|
40 | n/a | d = g->g_dfa; |
---|
41 | n/a | for (i = g->g_ndfas; --i >= 0; d++) { |
---|
42 | n/a | state *s; |
---|
43 | n/a | int j; |
---|
44 | n/a | s = d->d_state; |
---|
45 | n/a | for (j = 0; j < d->d_nstates; j++, s++) { |
---|
46 | n/a | if (s->s_accel) |
---|
47 | n/a | PyObject_FREE(s->s_accel); |
---|
48 | n/a | s->s_accel = NULL; |
---|
49 | n/a | } |
---|
50 | n/a | } |
---|
51 | n/a | } |
---|
52 | n/a | |
---|
53 | n/a | static void |
---|
54 | n/a | fixdfa(grammar *g, dfa *d) |
---|
55 | n/a | { |
---|
56 | n/a | state *s; |
---|
57 | n/a | int j; |
---|
58 | n/a | s = d->d_state; |
---|
59 | n/a | for (j = 0; j < d->d_nstates; j++, s++) |
---|
60 | n/a | fixstate(g, s); |
---|
61 | n/a | } |
---|
62 | n/a | |
---|
63 | n/a | static void |
---|
64 | n/a | fixstate(grammar *g, state *s) |
---|
65 | n/a | { |
---|
66 | n/a | arc *a; |
---|
67 | n/a | int k; |
---|
68 | n/a | int *accel; |
---|
69 | n/a | int nl = g->g_ll.ll_nlabels; |
---|
70 | n/a | s->s_accept = 0; |
---|
71 | n/a | accel = (int *) PyObject_MALLOC(nl * sizeof(int)); |
---|
72 | n/a | if (accel == NULL) { |
---|
73 | n/a | fprintf(stderr, "no mem to build parser accelerators\n"); |
---|
74 | n/a | exit(1); |
---|
75 | n/a | } |
---|
76 | n/a | for (k = 0; k < nl; k++) |
---|
77 | n/a | accel[k] = -1; |
---|
78 | n/a | a = s->s_arc; |
---|
79 | n/a | for (k = s->s_narcs; --k >= 0; a++) { |
---|
80 | n/a | int lbl = a->a_lbl; |
---|
81 | n/a | label *l = &g->g_ll.ll_label[lbl]; |
---|
82 | n/a | int type = l->lb_type; |
---|
83 | n/a | if (a->a_arrow >= (1 << 7)) { |
---|
84 | n/a | printf("XXX too many states!\n"); |
---|
85 | n/a | continue; |
---|
86 | n/a | } |
---|
87 | n/a | if (ISNONTERMINAL(type)) { |
---|
88 | n/a | dfa *d1 = PyGrammar_FindDFA(g, type); |
---|
89 | n/a | int ibit; |
---|
90 | n/a | if (type - NT_OFFSET >= (1 << 7)) { |
---|
91 | n/a | printf("XXX too high nonterminal number!\n"); |
---|
92 | n/a | continue; |
---|
93 | n/a | } |
---|
94 | n/a | for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) { |
---|
95 | n/a | if (testbit(d1->d_first, ibit)) { |
---|
96 | n/a | if (accel[ibit] != -1) |
---|
97 | n/a | printf("XXX ambiguity!\n"); |
---|
98 | n/a | accel[ibit] = a->a_arrow | (1 << 7) | |
---|
99 | n/a | ((type - NT_OFFSET) << 8); |
---|
100 | n/a | } |
---|
101 | n/a | } |
---|
102 | n/a | } |
---|
103 | n/a | else if (lbl == EMPTY) |
---|
104 | n/a | s->s_accept = 1; |
---|
105 | n/a | else if (lbl >= 0 && lbl < nl) |
---|
106 | n/a | accel[lbl] = a->a_arrow; |
---|
107 | n/a | } |
---|
108 | n/a | while (nl > 0 && accel[nl-1] == -1) |
---|
109 | n/a | nl--; |
---|
110 | n/a | for (k = 0; k < nl && accel[k] == -1;) |
---|
111 | n/a | k++; |
---|
112 | n/a | if (k < nl) { |
---|
113 | n/a | int i; |
---|
114 | n/a | s->s_accel = (int *) PyObject_MALLOC((nl-k) * sizeof(int)); |
---|
115 | n/a | if (s->s_accel == NULL) { |
---|
116 | n/a | fprintf(stderr, "no mem to add parser accelerators\n"); |
---|
117 | n/a | exit(1); |
---|
118 | n/a | } |
---|
119 | n/a | s->s_lower = k; |
---|
120 | n/a | s->s_upper = nl; |
---|
121 | n/a | for (i = 0; k < nl; i++, k++) |
---|
122 | n/a | s->s_accel[i] = accel[k]; |
---|
123 | n/a | } |
---|
124 | n/a | PyObject_FREE(accel); |
---|
125 | n/a | } |
---|