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.

125 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 "pgenheaders.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 *, dfa *);
  17. static void fixstate(grammar *, state *);
  18. void
  19. PyGrammar_AddAccelerators(grammar *g)
  20. {
  21. dfa *d;
  22. int i;
  23. d = g->g_dfa;
  24. for (i = g->g_ndfas; --i >= 0; d++)
  25. fixdfa(g, d);
  26. g->g_accel = 1;
  27. }
  28. void
  29. PyGrammar_RemoveAccelerators(grammar *g)
  30. {
  31. dfa *d;
  32. int i;
  33. g->g_accel = 0;
  34. d = g->g_dfa;
  35. for (i = g->g_ndfas; --i >= 0; d++) {
  36. state *s;
  37. int j;
  38. s = d->d_state;
  39. for (j = 0; j < d->d_nstates; j++, s++) {
  40. if (s->s_accel)
  41. PyObject_FREE(s->s_accel);
  42. s->s_accel = NULL;
  43. }
  44. }
  45. }
  46. static void
  47. fixdfa(grammar *g, dfa *d)
  48. {
  49. state *s;
  50. int j;
  51. s = d->d_state;
  52. for (j = 0; j < d->d_nstates; j++, s++)
  53. fixstate(g, s);
  54. }
  55. static void
  56. fixstate(grammar *g, state *s)
  57. {
  58. arc *a;
  59. int k;
  60. int *accel;
  61. int nl = g->g_ll.ll_nlabels;
  62. s->s_accept = 0;
  63. accel = (int *) PyObject_MALLOC(nl * sizeof(int));
  64. if (accel == NULL) {
  65. fprintf(stderr, "no mem to build parser accelerators\n");
  66. exit(1);
  67. }
  68. for (k = 0; k < nl; k++)
  69. accel[k] = -1;
  70. a = s->s_arc;
  71. for (k = s->s_narcs; --k >= 0; a++) {
  72. int lbl = a->a_lbl;
  73. label *l = &g->g_ll.ll_label[lbl];
  74. int type = l->lb_type;
  75. if (a->a_arrow >= (1 << 7)) {
  76. printf("XXX too many states!\n");
  77. continue;
  78. }
  79. if (ISNONTERMINAL(type)) {
  80. dfa *d1 = PyGrammar_FindDFA(g, type);
  81. int ibit;
  82. if (type - NT_OFFSET >= (1 << 7)) {
  83. printf("XXX too high nonterminal number!\n");
  84. continue;
  85. }
  86. for (ibit = 0; ibit < g->g_ll.ll_nlabels; ibit++) {
  87. if (testbit(d1->d_first, ibit)) {
  88. if (accel[ibit] != -1)
  89. printf("XXX ambiguity!\n");
  90. accel[ibit] = a->a_arrow | (1 << 7) |
  91. ((type - NT_OFFSET) << 8);
  92. }
  93. }
  94. }
  95. else if (lbl == EMPTY)
  96. s->s_accept = 1;
  97. else if (lbl >= 0 && lbl < nl)
  98. accel[lbl] = a->a_arrow;
  99. }
  100. while (nl > 0 && accel[nl-1] == -1)
  101. nl--;
  102. for (k = 0; k < nl && accel[k] == -1;)
  103. k++;
  104. if (k < nl) {
  105. int i;
  106. s->s_accel = (int *) PyObject_MALLOC((nl-k) * sizeof(int));
  107. if (s->s_accel == NULL) {
  108. fprintf(stderr, "no mem to add parser accelerators\n");
  109. exit(1);
  110. }
  111. s->s_lower = k;
  112. s->s_upper = nl;
  113. for (i = 0; k < nl; i++, k++)
  114. s->s_accel[i] = accel[k];
  115. }
  116. PyObject_FREE(accel);
  117. }