blob: e515833e1dda1d4924a3a94892d90a6c6cb6a6b4 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Parser accelerator module */
3
Guido van Rossum3f5da241990-12-20 15:06:42 +00004/* The parser as originally conceived had disappointing performance.
5 This module does some precomputation that speeds up the selection
6 of a DFA based upon a token, turning a search through an array
7 into a simple indexing operation. The parser now cannot work
8 without the accelerators installed. Note that the accelerators
9 are installed dynamically when the parser is initialized, they
10 are not part of the static data structure written on graminit.[ch]
11 by the parser generator. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012
Pablo Galindof2cf1e32019-04-13 17:05:14 +010013#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014#include "grammar.h"
Guido van Rossum1d5735e1994-08-30 08:27:36 +000015#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000016#include "token.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000017#include "parser.h"
18
19/* Forward references */
Inada Naoki09415ff2019-04-23 20:39:37 +090020static void fixdfa(grammar *, const dfa *);
Tim Petersdbd9ba62000-07-09 03:09:57 +000021static void fixstate(grammar *, state *);
Guido van Rossum3f5da241990-12-20 15:06:42 +000022
23void
Thomas Wouters23c9e002000-07-22 19:20:54 +000024PyGrammar_AddAccelerators(grammar *g)
Guido van Rossum3f5da241990-12-20 15:06:42 +000025{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 int i;
Inada Naoki09415ff2019-04-23 20:39:37 +090027 const dfa *d = g->g_dfa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 for (i = g->g_ndfas; --i >= 0; d++)
29 fixdfa(g, d);
30 g->g_accel = 1;
Guido van Rossum3f5da241990-12-20 15:06:42 +000031}
32
Guido van Rossumaee094c1997-08-02 03:02:27 +000033void
Thomas Wouters23c9e002000-07-22 19:20:54 +000034PyGrammar_RemoveAccelerators(grammar *g)
Guido van Rossumaee094c1997-08-02 03:02:27 +000035{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000036 int i;
37 g->g_accel = 0;
Inada Naoki09415ff2019-04-23 20:39:37 +090038 const dfa *d = g->g_dfa;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 for (i = g->g_ndfas; --i >= 0; d++) {
40 state *s;
41 int j;
42 s = d->d_state;
43 for (j = 0; j < d->d_nstates; j++, s++) {
44 if (s->s_accel)
45 PyObject_FREE(s->s_accel);
46 s->s_accel = NULL;
47 }
48 }
Guido van Rossumaee094c1997-08-02 03:02:27 +000049}
50
Guido van Rossum3f5da241990-12-20 15:06:42 +000051static void
Inada Naoki09415ff2019-04-23 20:39:37 +090052fixdfa(grammar *g, const dfa *d)
Guido van Rossum3f5da241990-12-20 15:06:42 +000053{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 state *s;
55 int j;
56 s = d->d_state;
57 for (j = 0; j < d->d_nstates; j++, s++)
58 fixstate(g, s);
Guido van Rossum3f5da241990-12-20 15:06:42 +000059}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060
61static void
Thomas Wouters23c9e002000-07-22 19:20:54 +000062fixstate(grammar *g, state *s)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063{
Inada Naoki09415ff2019-04-23 20:39:37 +090064 const arc *a;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 int k;
66 int *accel;
67 int nl = g->g_ll.ll_nlabels;
68 s->s_accept = 0;
69 accel = (int *) PyObject_MALLOC(nl * sizeof(int));
70 if (accel == NULL) {
71 fprintf(stderr, "no mem to build parser accelerators\n");
72 exit(1);
73 }
74 for (k = 0; k < nl; k++)
75 accel[k] = -1;
76 a = s->s_arc;
77 for (k = s->s_narcs; --k >= 0; a++) {
78 int lbl = a->a_lbl;
Inada Naoki09415ff2019-04-23 20:39:37 +090079 const label *l = &g->g_ll.ll_label[lbl];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 int type = l->lb_type;
81 if (a->a_arrow >= (1 << 7)) {
82 printf("XXX too many states!\n");
83 continue;
84 }
85 if (ISNONTERMINAL(type)) {
Inada Naoki09415ff2019-04-23 20:39:37 +090086 const dfa *d1 = PyGrammar_FindDFA(g, type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 int ibit;
88 if (type - NT_OFFSET >= (1 << 7)) {
89 printf("XXX too high nonterminal number!\n");
90 continue;
91 }
92 for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) {
93 if (testbit(d1->d_first, ibit)) {
94 if (accel[ibit] != -1)
95 printf("XXX ambiguity!\n");
96 accel[ibit] = a->a_arrow | (1 << 7) |
97 ((type - NT_OFFSET) << 8);
98 }
99 }
100 }
101 else if (lbl == EMPTY)
102 s->s_accept = 1;
103 else if (lbl >= 0 && lbl < nl)
104 accel[lbl] = a->a_arrow;
105 }
106 while (nl > 0 && accel[nl-1] == -1)
107 nl--;
108 for (k = 0; k < nl && accel[k] == -1;)
109 k++;
110 if (k < nl) {
111 int i;
112 s->s_accel = (int *) PyObject_MALLOC((nl-k) * sizeof(int));
113 if (s->s_accel == NULL) {
114 fprintf(stderr, "no mem to add parser accelerators\n");
115 exit(1);
116 }
117 s->s_lower = k;
118 s->s_upper = nl;
119 for (i = 0; k < nl; i++, k++)
120 s->s_accel[i] = accel[k];
121 }
122 PyObject_FREE(accel);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123}