blob: 33f91b66727bc2f85792dc7145154b3ce155c1e2 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Parser accelerator module */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027/* The parser as originally conceived had disappointing performance.
28 This module does some precomputation that speeds up the selection
29 of a DFA based upon a token, turning a search through an array
30 into a simple indexing operation. The parser now cannot work
31 without the accelerators installed. Note that the accelerators
32 are installed dynamically when the parser is initialized, they
33 are not part of the static data structure written on graminit.[ch]
34 by the parser generator. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035
Guido van Rossum3f5da241990-12-20 15:06:42 +000036#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037#include "grammar.h"
38#include "token.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000039#include "parser.h"
40
41/* Forward references */
42static void fixdfa PROTO((grammar *, dfa *));
43static void fixstate PROTO((grammar *, dfa *, state *));
44
45void
46addaccelerators(g)
47 grammar *g;
48{
49 dfa *d;
50 int i;
51#ifdef DEBUG
52 printf("Adding parser accellerators ...\n");
53#endif
54 d = g->g_dfa;
55 for (i = g->g_ndfas; --i >= 0; d++)
56 fixdfa(g, d);
57 g->g_accel = 1;
58#ifdef DEBUG
59 printf("Done.\n");
60#endif
61}
62
63static void
64fixdfa(g, d)
65 grammar *g;
66 dfa *d;
67{
68 state *s;
69 int j;
70 s = d->d_state;
71 for (j = 0; j < d->d_nstates; j++, s++)
72 fixstate(g, d, s);
73}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000074
75static void
76fixstate(g, d, s)
77 grammar *g;
78 dfa *d;
79 state *s;
80{
81 arc *a;
82 int k;
83 int *accel;
84 int nl = g->g_ll.ll_nlabels;
85 s->s_accept = 0;
86 accel = NEW(int, nl);
87 for (k = 0; k < nl; k++)
88 accel[k] = -1;
89 a = s->s_arc;
90 for (k = s->s_narcs; --k >= 0; a++) {
91 int lbl = a->a_lbl;
92 label *l = &g->g_ll.ll_label[lbl];
93 int type = l->lb_type;
94 if (a->a_arrow >= (1 << 7)) {
95 printf("XXX too many states!\n");
96 continue;
97 }
98 if (ISNONTERMINAL(type)) {
99 dfa *d1 = finddfa(g, type);
100 int ibit;
101 if (type - NT_OFFSET >= (1 << 7)) {
102 printf("XXX too high nonterminal number!\n");
103 continue;
104 }
105 for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) {
106 if (testbit(d1->d_first, ibit)) {
107 if (accel[ibit] != -1)
108 printf("XXX ambiguity!\n");
109 accel[ibit] = a->a_arrow | (1 << 7) |
110 ((type - NT_OFFSET) << 8);
111 }
112 }
113 }
114 else if (lbl == EMPTY)
115 s->s_accept = 1;
116 else if (lbl >= 0 && lbl < nl)
117 accel[lbl] = a->a_arrow;
118 }
119 while (nl > 0 && accel[nl-1] == -1)
120 nl--;
121 for (k = 0; k < nl && accel[k] == -1;)
122 k++;
123 if (k < nl) {
124 int i;
125 s->s_accel = NEW(int, nl-k);
126 if (s->s_accel == NULL) {
127 fprintf(stderr, "no mem to add parser accelerators\n");
128 exit(1);
129 }
130 s->s_lower = k;
131 s->s_upper = nl;
132 for (i = 0; k < nl; i++, k++)
133 s->s_accel[i] = accel[k];
134 }
135 DEL(accel);
136}