Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Parser accelerator module */ |
| 3 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 4 | /* 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 13 | #include "pgenheaders.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | #include "grammar.h" |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 15 | #include "node.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 16 | #include "token.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 17 | #include "parser.h" |
| 18 | |
| 19 | /* Forward references */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 20 | static void fixdfa(grammar *, dfa *); |
| 21 | static void fixstate(grammar *, state *); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 22 | |
| 23 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 24 | PyGrammar_AddAccelerators(grammar *g) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 25 | { |
| 26 | dfa *d; |
| 27 | int i; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 28 | d = g->g_dfa; |
| 29 | for (i = g->g_ndfas; --i >= 0; d++) |
| 30 | fixdfa(g, d); |
| 31 | g->g_accel = 1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | } |
| 33 | |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 34 | void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 35 | PyGrammar_RemoveAccelerators(grammar *g) |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 36 | { |
| 37 | dfa *d; |
| 38 | int i; |
| 39 | g->g_accel = 0; |
| 40 | d = g->g_dfa; |
| 41 | for (i = g->g_ndfas; --i >= 0; d++) { |
| 42 | state *s; |
| 43 | int j; |
| 44 | s = d->d_state; |
| 45 | for (j = 0; j < d->d_nstates; j++, s++) { |
| 46 | if (s->s_accel) |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 47 | PyObject_FREE(s->s_accel); |
Guido van Rossum | aee094c | 1997-08-02 03:02:27 +0000 | [diff] [blame] | 48 | s->s_accel = NULL; |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 53 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 54 | fixdfa(grammar *g, dfa *d) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 55 | { |
| 56 | state *s; |
| 57 | int j; |
| 58 | s = d->d_state; |
| 59 | for (j = 0; j < d->d_nstates; j++, s++) |
Guido van Rossum | 9abc539 | 1992-03-27 17:24:37 +0000 | [diff] [blame] | 60 | fixstate(g, s); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 61 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | |
| 63 | static void |
Thomas Wouters | 23c9e00 | 2000-07-22 19:20:54 +0000 | [diff] [blame] | 64 | fixstate(grammar *g, state *s) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | { |
| 66 | arc *a; |
| 67 | int k; |
| 68 | int *accel; |
| 69 | int nl = g->g_ll.ll_nlabels; |
| 70 | s->s_accept = 0; |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 71 | accel = (int *) PyObject_MALLOC(nl * sizeof(int)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | for (k = 0; k < nl; k++) |
| 73 | accel[k] = -1; |
| 74 | a = s->s_arc; |
| 75 | for (k = s->s_narcs; --k >= 0; a++) { |
| 76 | int lbl = a->a_lbl; |
| 77 | label *l = &g->g_ll.ll_label[lbl]; |
| 78 | int type = l->lb_type; |
| 79 | if (a->a_arrow >= (1 << 7)) { |
| 80 | printf("XXX too many states!\n"); |
| 81 | continue; |
| 82 | } |
| 83 | if (ISNONTERMINAL(type)) { |
Guido van Rossum | 86bea46 | 1997-04-29 21:03:06 +0000 | [diff] [blame] | 84 | dfa *d1 = PyGrammar_FindDFA(g, type); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | int ibit; |
| 86 | if (type - NT_OFFSET >= (1 << 7)) { |
| 87 | printf("XXX too high nonterminal number!\n"); |
| 88 | continue; |
| 89 | } |
| 90 | for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) { |
| 91 | if (testbit(d1->d_first, ibit)) { |
| 92 | if (accel[ibit] != -1) |
| 93 | printf("XXX ambiguity!\n"); |
| 94 | accel[ibit] = a->a_arrow | (1 << 7) | |
| 95 | ((type - NT_OFFSET) << 8); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | else if (lbl == EMPTY) |
| 100 | s->s_accept = 1; |
| 101 | else if (lbl >= 0 && lbl < nl) |
| 102 | accel[lbl] = a->a_arrow; |
| 103 | } |
| 104 | while (nl > 0 && accel[nl-1] == -1) |
| 105 | nl--; |
| 106 | for (k = 0; k < nl && accel[k] == -1;) |
| 107 | k++; |
| 108 | if (k < nl) { |
| 109 | int i; |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 110 | s->s_accel = (int *) PyObject_MALLOC((nl-k) * sizeof(int)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | if (s->s_accel == NULL) { |
| 112 | fprintf(stderr, "no mem to add parser accelerators\n"); |
| 113 | exit(1); |
| 114 | } |
| 115 | s->s_lower = k; |
| 116 | s->s_upper = nl; |
| 117 | for (i = 0; k < nl; i++, k++) |
| 118 | s->s_accel[i] = accel[k]; |
| 119 | } |
Andrew MacIntyre | 80d4e2a | 2002-08-04 06:28:21 +0000 | [diff] [blame] | 120 | PyObject_FREE(accel); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | } |