You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.3 KiB

36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
  1. /* Parser accelerator module */
  2. /* The parser as originally conceived had disappointing performance.
  3. This module does some precomputation that speeds up the selection
  4. of a DFA based upon a token, turning a search through an array
  5. into a simple indexing operation. The parser now cannot work
  6. without the accelerators installed. Note that the accelerators
  7. are installed dynamically when the parser is initialized, they
  8. are not part of the static data structure written on graminit.[ch]
  9. by the parser generator. */
  10. #include "Python.h"
  11. #include "grammar.h"
  12. #include "node.h"
  13. #include "token.h"
  14. #include "parser.h"
  15. /* Forward references */
  16. static void fixdfa(grammar *, const dfa *);
  17. static void fixstate(grammar *, state *);
  18. void
  19. PyGrammar_AddAccelerators(grammar *g)
  20. {
  21. int i;
  22. const dfa *d = g->g_dfa;
  23. for (i = g->g_ndfas; --i >= 0; d++)
  24. fixdfa(g, d);
  25. g->g_accel = 1;
  26. }
  27. void
  28. PyGrammar_RemoveAccelerators(grammar *g)
  29. {
  30. int i;
  31. g->g_accel = 0;
  32. const dfa *d = g->g_dfa;
  33. for (i = g->g_ndfas; --i >= 0; d++) {
  34. state *s;
  35. int j;
  36. s = d->d_state;
  37. for (j = 0; j < d->d_nstates; j++, s++) {
  38. if (s->s_accel)
  39. PyObject_FREE(s->s_accel);
  40. s->s_accel = NULL;
  41. }
  42. }
  43. }
  44. static void
  45. fixdfa(grammar *g, const dfa *d)
  46. {
  47. state *s;
  48. int j;
  49. s = d->d_state;
  50. for (j = 0; j < d->d_nstates; j++, s++)
  51. fixstate(g, s);
  52. }
  53. static void
  54. fixstate(grammar *g, state *s)
  55. {
  56. const arc *a;
  57. int k;
  58. int *accel;
  59. int nl = g->g_ll.ll_nlabels;
  60. s->s_accept = 0;
  61. accel = (int *) PyObject_MALLOC(nl * sizeof(int));
  62. if (accel == NULL) {
  63. fprintf(stderr, "no mem to build parser accelerators\n");
  64. exit(1);
  65. }
  66. for (k = 0; k < nl; k++)
  67. accel[k] = -1;
  68. a = s->s_arc;
  69. for (k = s->s_narcs; --k >= 0; a++) {
  70. int lbl = a->a_lbl;
  71. const label *l = &g->g_ll.ll_label[lbl];
  72. int type = l->lb_type;
  73. if (a->a_arrow >= (1 << 7)) {
  74. printf("XXX too many states!\n");
  75. continue;
  76. }
  77. if (ISNONTERMINAL(type)) {
  78. const dfa *d1 = PyGrammar_FindDFA(g, type);
  79. int ibit;
  80. if (type - NT_OFFSET >= (1 << 7)) {
  81. printf("XXX too high nonterminal number!\n");
  82. continue;
  83. }
  84. for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) {
  85. if (testbit(d1->d_first, ibit)) {
  86. if (accel[ibit] != -1)
  87. printf("XXX ambiguity!\n");
  88. accel[ibit] = a->a_arrow | (1 << 7) |
  89. ((type - NT_OFFSET) << 8);
  90. }
  91. }
  92. }
  93. else if (lbl == EMPTY)
  94. s->s_accept = 1;
  95. else if (lbl >= 0 && lbl < nl)
  96. accel[lbl] = a->a_arrow;
  97. }
  98. while (nl > 0 && accel[nl-1] == -1)
  99. nl--;
  100. for (k = 0; k < nl && accel[k] == -1;)
  101. k++;
  102. if (k < nl) {
  103. int i;
  104. s->s_accel = (int *) PyObject_MALLOC((nl-k) * sizeof(int));
  105. if (s->s_accel == NULL) {
  106. fprintf(stderr, "no mem to add parser accelerators\n");
  107. exit(1);
  108. }
  109. s->s_lower = k;
  110. s->s_upper = nl;
  111. for (i = 0; k < nl; i++, k++)
  112. s->s_accel[i] = accel[k];
  113. }
  114. PyObject_FREE(accel);
  115. }