blob: 9417b76bd1a59186f090759c2e3a12196f1381f6 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumb9f8d6e1995-01-04 19:08:09 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Parser accelerator module */
33
Guido van Rossum3f5da241990-12-20 15:06:42 +000034/* The parser as originally conceived had disappointing performance.
35 This module does some precomputation that speeds up the selection
36 of a DFA based upon a token, turning a search through an array
37 into a simple indexing operation. The parser now cannot work
38 without the accelerators installed. Note that the accelerators
39 are installed dynamically when the parser is initialized, they
40 are not part of the static data structure written on graminit.[ch]
41 by the parser generator. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000042
Guido van Rossum3f5da241990-12-20 15:06:42 +000043#include "pgenheaders.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044#include "grammar.h"
Guido van Rossum1d5735e1994-08-30 08:27:36 +000045#include "node.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000046#include "token.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000047#include "parser.h"
48
49/* Forward references */
Guido van Rossum86bea461997-04-29 21:03:06 +000050static void fixdfa Py_PROTO((grammar *, dfa *));
51static void fixstate Py_PROTO((grammar *, state *));
Guido van Rossum3f5da241990-12-20 15:06:42 +000052
53void
Guido van Rossum86bea461997-04-29 21:03:06 +000054PyGrammar_AddAccelerators(g)
Guido van Rossum3f5da241990-12-20 15:06:42 +000055 grammar *g;
56{
57 dfa *d;
58 int i;
Guido van Rossum408027e1996-12-30 16:17:54 +000059#ifdef Py_DEBUG
Guido van Rossum888d2051992-09-03 20:45:24 +000060 fprintf(stderr, "Adding parser accelerators ...\n");
Guido van Rossum3f5da241990-12-20 15:06:42 +000061#endif
62 d = g->g_dfa;
63 for (i = g->g_ndfas; --i >= 0; d++)
64 fixdfa(g, d);
65 g->g_accel = 1;
Guido van Rossum408027e1996-12-30 16:17:54 +000066#ifdef Py_DEBUG
Guido van Rossum888d2051992-09-03 20:45:24 +000067 fprintf(stderr, "Done.\n");
Guido van Rossum3f5da241990-12-20 15:06:42 +000068#endif
69}
70
71static void
72fixdfa(g, d)
73 grammar *g;
74 dfa *d;
75{
76 state *s;
77 int j;
78 s = d->d_state;
79 for (j = 0; j < d->d_nstates; j++, s++)
Guido van Rossum9abc5391992-03-27 17:24:37 +000080 fixstate(g, s);
Guido van Rossum3f5da241990-12-20 15:06:42 +000081}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000082
83static void
Guido van Rossum9abc5391992-03-27 17:24:37 +000084fixstate(g, s)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085 grammar *g;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000086 state *s;
87{
88 arc *a;
89 int k;
90 int *accel;
91 int nl = g->g_ll.ll_nlabels;
92 s->s_accept = 0;
Guido van Rossum86bea461997-04-29 21:03:06 +000093 accel = PyMem_NEW(int, nl);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000094 for (k = 0; k < nl; k++)
95 accel[k] = -1;
96 a = s->s_arc;
97 for (k = s->s_narcs; --k >= 0; a++) {
98 int lbl = a->a_lbl;
99 label *l = &g->g_ll.ll_label[lbl];
100 int type = l->lb_type;
101 if (a->a_arrow >= (1 << 7)) {
102 printf("XXX too many states!\n");
103 continue;
104 }
105 if (ISNONTERMINAL(type)) {
Guido van Rossum86bea461997-04-29 21:03:06 +0000106 dfa *d1 = PyGrammar_FindDFA(g, type);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107 int ibit;
108 if (type - NT_OFFSET >= (1 << 7)) {
109 printf("XXX too high nonterminal number!\n");
110 continue;
111 }
112 for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) {
113 if (testbit(d1->d_first, ibit)) {
Guido van Rossum7d8b5091991-09-10 14:53:39 +0000114#ifdef applec
Guido van Rossum1d5735e1994-08-30 08:27:36 +0000115#define MPW_881_BUG /* Undefine if bug below is fixed */
Guido van Rossum7d8b5091991-09-10 14:53:39 +0000116#endif
117#ifdef MPW_881_BUG
118 /* In 881 mode MPW 3.1 has a code
119 generation bug which seems to
120 set the upper bits; fix this by
121 explicitly masking them off */
122 int temp;
123#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124 if (accel[ibit] != -1)
125 printf("XXX ambiguity!\n");
Guido van Rossum7d8b5091991-09-10 14:53:39 +0000126#ifdef MPW_881_BUG
127 temp = 0xFFFF &
128 (a->a_arrow | (1 << 7) |
129 ((type - NT_OFFSET) << 8));
130 accel[ibit] = temp;
131#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132 accel[ibit] = a->a_arrow | (1 << 7) |
133 ((type - NT_OFFSET) << 8);
Guido van Rossum7d8b5091991-09-10 14:53:39 +0000134#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135 }
136 }
137 }
138 else if (lbl == EMPTY)
139 s->s_accept = 1;
140 else if (lbl >= 0 && lbl < nl)
141 accel[lbl] = a->a_arrow;
142 }
143 while (nl > 0 && accel[nl-1] == -1)
144 nl--;
145 for (k = 0; k < nl && accel[k] == -1;)
146 k++;
147 if (k < nl) {
148 int i;
Guido van Rossum86bea461997-04-29 21:03:06 +0000149 s->s_accel = PyMem_NEW(int, nl-k);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 if (s->s_accel == NULL) {
151 fprintf(stderr, "no mem to add parser accelerators\n");
152 exit(1);
153 }
154 s->s_lower = k;
155 s->s_upper = nl;
156 for (i = 0; k < nl; i++, k++)
157 s->s_accel[i] = accel[k];
158 }
Guido van Rossum86bea461997-04-29 21:03:06 +0000159 PyMem_DEL(accel);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000160}