1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI or Corporation for National Research Initiatives or
13 CNRI not be used in advertising or publicity pertaining to
14 distribution of the software without specific, written prior
17 While CWI is the initial source for this software, a modified version
18 is made available by the Corporation for National Research Initiatives
19 (CNRI) at the Internet address ftp://ftp.python.org.
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28 PERFORMANCE OF THIS SOFTWARE.
30 ******************************************************************/
32 /* Parser accelerator module */
34 /* 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. */
43 #include "pgenheaders.h"
49 /* Forward references */
50 static void fixdfa
Py_PROTO((grammar
*, dfa
*));
51 static void fixstate
Py_PROTO((grammar
*, state
*));
54 PyGrammar_AddAccelerators(g
)
60 fprintf(stderr
, "Adding parser accelerators ...\n");
63 for (i
= g
->g_ndfas
; --i
>= 0; d
++)
67 fprintf(stderr
, "Done.\n");
72 PyGrammar_RemoveAccelerators(g
)
79 for (i
= g
->g_ndfas
; --i
>= 0; d
++) {
83 for (j
= 0; j
< d
->d_nstates
; j
++, s
++) {
85 PyMem_DEL(s
->s_accel
);
99 for (j
= 0; j
< d
->d_nstates
; j
++, s
++)
111 int nl
= g
->g_ll
.ll_nlabels
;
113 accel
= PyMem_NEW(int, nl
);
114 for (k
= 0; k
< nl
; k
++)
117 for (k
= s
->s_narcs
; --k
>= 0; a
++) {
119 label
*l
= &g
->g_ll
.ll_label
[lbl
];
120 int type
= l
->lb_type
;
121 if (a
->a_arrow
>= (1 << 7)) {
122 printf("XXX too many states!\n");
125 if (ISNONTERMINAL(type
)) {
126 dfa
*d1
= PyGrammar_FindDFA(g
, type
);
128 if (type
- NT_OFFSET
>= (1 << 7)) {
129 printf("XXX too high nonterminal number!\n");
132 for (ibit
= 0; ibit
< g
->g_ll
.ll_nlabels
; ibit
++) {
133 if (testbit(d1
->d_first
, ibit
)) {
135 #define MPW_881_BUG /* Undefine if bug below is fixed */
138 /* In 881 mode MPW 3.1 has a code
139 generation bug which seems to
140 set the upper bits; fix this by
141 explicitly masking them off */
144 if (accel
[ibit
] != -1)
145 printf("XXX ambiguity!\n");
148 (a
->a_arrow
| (1 << 7) |
149 ((type
- NT_OFFSET
) << 8));
152 accel
[ibit
] = a
->a_arrow
| (1 << 7) |
153 ((type
- NT_OFFSET
) << 8);
158 else if (lbl
== EMPTY
)
160 else if (lbl
>= 0 && lbl
< nl
)
161 accel
[lbl
] = a
->a_arrow
;
163 while (nl
> 0 && accel
[nl
-1] == -1)
165 for (k
= 0; k
< nl
&& accel
[k
] == -1;)
169 s
->s_accel
= PyMem_NEW(int, nl
-k
);
170 if (s
->s_accel
== NULL
) {
171 fprintf(stderr
, "no mem to add parser accelerators\n");
176 for (i
= 0; k
< nl
; i
++, k
++)
177 s
->s_accel
[i
] = accel
[k
];