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.

447 lines
12 KiB

36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
36 years ago
  1. /* Parser implementation */
  2. /* For a description, see the comments at end of this file */
  3. /* XXX To do: error recovery */
  4. #include "Python.h"
  5. #include "pgenheaders.h"
  6. #include "token.h"
  7. #include "grammar.h"
  8. #include "node.h"
  9. #include "parser.h"
  10. #include "errcode.h"
  11. #ifdef Py_DEBUG
  12. extern int Py_DebugFlag;
  13. #define D(x) if (!Py_DebugFlag); else x
  14. #else
  15. #define D(x)
  16. #endif
  17. /* STACK DATA TYPE */
  18. static void s_reset(stack *);
  19. static void
  20. s_reset(stack *s)
  21. {
  22. s->s_top = &s->s_base[MAXSTACK];
  23. }
  24. #define s_empty(s) ((s)->s_top == &(s)->s_base[MAXSTACK])
  25. static int
  26. s_push(stack *s, dfa *d, node *parent)
  27. {
  28. stackentry *top;
  29. if (s->s_top == s->s_base) {
  30. fprintf(stderr, "s_push: parser stack overflow\n");
  31. return E_NOMEM;
  32. }
  33. top = --s->s_top;
  34. top->s_dfa = d;
  35. top->s_parent = parent;
  36. top->s_state = 0;
  37. return 0;
  38. }
  39. #ifdef Py_DEBUG
  40. static void
  41. s_pop(stack *s)
  42. {
  43. if (s_empty(s))
  44. Py_FatalError("s_pop: parser stack underflow -- FATAL");
  45. s->s_top++;
  46. }
  47. #else /* !Py_DEBUG */
  48. #define s_pop(s) (s)->s_top++
  49. #endif
  50. /* PARSER CREATION */
  51. parser_state *
  52. PyParser_New(grammar *g, int start)
  53. {
  54. parser_state *ps;
  55. if (!g->g_accel)
  56. PyGrammar_AddAccelerators(g);
  57. ps = (parser_state *)PyMem_MALLOC(sizeof(parser_state));
  58. if (ps == NULL)
  59. return NULL;
  60. ps->p_grammar = g;
  61. #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
  62. ps->p_flags = 0;
  63. #endif
  64. ps->p_tree = PyNode_New(start);
  65. if (ps->p_tree == NULL) {
  66. PyMem_FREE(ps);
  67. return NULL;
  68. }
  69. s_reset(&ps->p_stack);
  70. (void) s_push(&ps->p_stack, PyGrammar_FindDFA(g, start), ps->p_tree);
  71. return ps;
  72. }
  73. void
  74. PyParser_Delete(parser_state *ps)
  75. {
  76. /* NB If you want to save the parse tree,
  77. you must set p_tree to NULL before calling delparser! */
  78. PyNode_Free(ps->p_tree);
  79. PyMem_FREE(ps);
  80. }
  81. /* PARSER STACK OPERATIONS */
  82. static int
  83. shift(stack *s, int type, char *str, int newstate, int lineno, int col_offset)
  84. {
  85. int err;
  86. assert(!s_empty(s));
  87. err = PyNode_AddChild(s->s_top->s_parent, type, str, lineno, col_offset);
  88. if (err)
  89. return err;
  90. s->s_top->s_state = newstate;
  91. return 0;
  92. }
  93. static int
  94. push(stack *s, int type, dfa *d, int newstate, int lineno, int col_offset)
  95. {
  96. int err;
  97. node *n;
  98. n = s->s_top->s_parent;
  99. assert(!s_empty(s));
  100. err = PyNode_AddChild(n, type, (char *)NULL, lineno, col_offset);
  101. if (err)
  102. return err;
  103. s->s_top->s_state = newstate;
  104. return s_push(s, d, CHILD(n, NCH(n)-1));
  105. }
  106. /* PARSER PROPER */
  107. static int
  108. classify(parser_state *ps, int type, const char *str)
  109. {
  110. grammar *g = ps->p_grammar;
  111. int n = g->g_ll.ll_nlabels;
  112. if (type == NAME) {
  113. label *l = g->g_ll.ll_label;
  114. int i;
  115. for (i = n; i > 0; i--, l++) {
  116. if (l->lb_type != NAME || l->lb_str == NULL ||
  117. l->lb_str[0] != str[0] ||
  118. strcmp(l->lb_str, str) != 0)
  119. continue;
  120. #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
  121. #if 0
  122. /* Leaving this in as an example */
  123. if (!(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) {
  124. if (str[0] == 'w' && strcmp(str, "with") == 0)
  125. break; /* not a keyword yet */
  126. else if (str[0] == 'a' && strcmp(str, "as") == 0)
  127. break; /* not a keyword yet */
  128. }
  129. #endif
  130. #endif
  131. D(printf("It's a keyword\n"));
  132. return n - i;
  133. }
  134. }
  135. {
  136. label *l = g->g_ll.ll_label;
  137. int i;
  138. for (i = n; i > 0; i--, l++) {
  139. if (l->lb_type == type && l->lb_str == NULL) {
  140. D(printf("It's a token we know\n"));
  141. return n - i;
  142. }
  143. }
  144. }
  145. D(printf("Illegal token\n"));
  146. return -1;
  147. }
  148. #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
  149. #if 0
  150. /* Leaving this in as an example */
  151. static void
  152. future_hack(parser_state *ps)
  153. {
  154. node *n = ps->p_stack.s_top->s_parent;
  155. node *ch, *cch;
  156. int i;
  157. /* from __future__ import ..., must have at least 4 children */
  158. n = CHILD(n, 0);
  159. if (NCH(n) < 4)
  160. return;
  161. ch = CHILD(n, 0);
  162. if (STR(ch) == NULL || strcmp(STR(ch), "from") != 0)
  163. return;
  164. ch = CHILD(n, 1);
  165. if (NCH(ch) == 1 && STR(CHILD(ch, 0)) &&
  166. strcmp(STR(CHILD(ch, 0)), "__future__") != 0)
  167. return;
  168. ch = CHILD(n, 3);
  169. /* ch can be a star, a parenthesis or import_as_names */
  170. if (TYPE(ch) == STAR)
  171. return;
  172. if (TYPE(ch) == LPAR)
  173. ch = CHILD(n, 4);
  174. for (i = 0; i < NCH(ch); i += 2) {
  175. cch = CHILD(ch, i);
  176. if (NCH(cch) >= 1 && TYPE(CHILD(cch, 0)) == NAME) {
  177. char *str_ch = STR(CHILD(cch, 0));
  178. if (strcmp(str_ch, FUTURE_WITH_STATEMENT) == 0) {
  179. ps->p_flags |= CO_FUTURE_WITH_STATEMENT;
  180. } else if (strcmp(str_ch, FUTURE_PRINT_FUNCTION) == 0) {
  181. ps->p_flags |= CO_FUTURE_PRINT_FUNCTION;
  182. } else if (strcmp(str_ch, FUTURE_UNICODE_LITERALS) == 0) {
  183. ps->p_flags |= CO_FUTURE_UNICODE_LITERALS;
  184. }
  185. }
  186. }
  187. }
  188. #endif
  189. #endif /* future keyword */
  190. int
  191. PyParser_AddToken(parser_state *ps, int type, char *str,
  192. int lineno, int col_offset, int *expected_ret)
  193. {
  194. int ilabel;
  195. int err;
  196. D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str));
  197. /* Find out which label this token is */
  198. ilabel = classify(ps, type, str);
  199. if (ilabel < 0)
  200. return E_SYNTAX;
  201. /* Loop until the token is shifted or an error occurred */
  202. for (;;) {
  203. /* Fetch the current dfa and state */
  204. dfa *d = ps->p_stack.s_top->s_dfa;
  205. state *s = &d->d_state[ps->p_stack.s_top->s_state];
  206. D(printf(" DFA '%s', state %d:",
  207. d->d_name, ps->p_stack.s_top->s_state));
  208. /* Check accelerator */
  209. if (s->s_lower <= ilabel && ilabel < s->s_upper) {
  210. int x = s->s_accel[ilabel - s->s_lower];
  211. if (x != -1) {
  212. if (x & (1<<7)) {
  213. /* Push non-terminal */
  214. int nt = (x >> 8) + NT_OFFSET;
  215. int arrow = x & ((1<<7)-1);
  216. dfa *d1 = PyGrammar_FindDFA(
  217. ps->p_grammar, nt);
  218. if ((err = push(&ps->p_stack, nt, d1,
  219. arrow, lineno, col_offset)) > 0) {
  220. D(printf(" MemError: push\n"));
  221. return err;
  222. }
  223. D(printf(" Push ...\n"));
  224. continue;
  225. }
  226. /* Shift the token */
  227. if ((err = shift(&ps->p_stack, type, str,
  228. x, lineno, col_offset)) > 0) {
  229. D(printf(" MemError: shift.\n"));
  230. return err;
  231. }
  232. D(printf(" Shift.\n"));
  233. /* Pop while we are in an accept-only state */
  234. while (s = &d->d_state
  235. [ps->p_stack.s_top->s_state],
  236. s->s_accept && s->s_narcs == 1) {
  237. D(printf(" DFA '%s', state %d: "
  238. "Direct pop.\n",
  239. d->d_name,
  240. ps->p_stack.s_top->s_state));
  241. #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
  242. #if 0
  243. if (d->d_name[0] == 'i' &&
  244. strcmp(d->d_name,
  245. "import_stmt") == 0)
  246. future_hack(ps);
  247. #endif
  248. #endif
  249. s_pop(&ps->p_stack);
  250. if (s_empty(&ps->p_stack)) {
  251. D(printf(" ACCEPT.\n"));
  252. return E_DONE;
  253. }
  254. d = ps->p_stack.s_top->s_dfa;
  255. }
  256. return E_OK;
  257. }
  258. }
  259. if (s->s_accept) {
  260. #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
  261. #if 0
  262. if (d->d_name[0] == 'i' &&
  263. strcmp(d->d_name, "import_stmt") == 0)
  264. future_hack(ps);
  265. #endif
  266. #endif
  267. /* Pop this dfa and try again */
  268. s_pop(&ps->p_stack);
  269. D(printf(" Pop ...\n"));
  270. if (s_empty(&ps->p_stack)) {
  271. D(printf(" Error: bottom of stack.\n"));
  272. return E_SYNTAX;
  273. }
  274. continue;
  275. }
  276. /* Stuck, report syntax error */
  277. D(printf(" Error.\n"));
  278. if (expected_ret) {
  279. if (s->s_lower == s->s_upper - 1) {
  280. /* Only one possible expected token */
  281. *expected_ret = ps->p_grammar->
  282. g_ll.ll_label[s->s_lower].lb_type;
  283. }
  284. else
  285. *expected_ret = -1;
  286. }
  287. return E_SYNTAX;
  288. }
  289. }
  290. #ifdef Py_DEBUG
  291. /* DEBUG OUTPUT */
  292. void
  293. dumptree(grammar *g, node *n)
  294. {
  295. int i;
  296. if (n == NULL)
  297. printf("NIL");
  298. else {
  299. label l;
  300. l.lb_type = TYPE(n);
  301. l.lb_str = STR(n);
  302. printf("%s", PyGrammar_LabelRepr(&l));
  303. if (ISNONTERMINAL(TYPE(n))) {
  304. printf("(");
  305. for (i = 0; i < NCH(n); i++) {
  306. if (i > 0)
  307. printf(",");
  308. dumptree(g, CHILD(n, i));
  309. }
  310. printf(")");
  311. }
  312. }
  313. }
  314. void
  315. showtree(grammar *g, node *n)
  316. {
  317. int i;
  318. if (n == NULL)
  319. return;
  320. if (ISNONTERMINAL(TYPE(n))) {
  321. for (i = 0; i < NCH(n); i++)
  322. showtree(g, CHILD(n, i));
  323. }
  324. else if (ISTERMINAL(TYPE(n))) {
  325. printf("%s", _PyParser_TokenNames[TYPE(n)]);
  326. if (TYPE(n) == NUMBER || TYPE(n) == NAME)
  327. printf("(%s)", STR(n));
  328. printf(" ");
  329. }
  330. else
  331. printf("? ");
  332. }
  333. void
  334. printtree(parser_state *ps)
  335. {
  336. if (Py_DebugFlag) {
  337. printf("Parse tree:\n");
  338. dumptree(ps->p_grammar, ps->p_tree);
  339. printf("\n");
  340. printf("Tokens:\n");
  341. showtree(ps->p_grammar, ps->p_tree);
  342. printf("\n");
  343. }
  344. printf("Listing:\n");
  345. PyNode_ListTree(ps->p_tree);
  346. printf("\n");
  347. }
  348. #endif /* Py_DEBUG */
  349. /*
  350. Description
  351. -----------
  352. The parser's interface is different than usual: the function addtoken()
  353. must be called for each token in the input. This makes it possible to
  354. turn it into an incremental parsing system later. The parsing system
  355. constructs a parse tree as it goes.
  356. A parsing rule is represented as a Deterministic Finite-state Automaton
  357. (DFA). A node in a DFA represents a state of the parser; an arc represents
  358. a transition. Transitions are either labeled with terminal symbols or
  359. with non-terminals. When the parser decides to follow an arc labeled
  360. with a non-terminal, it is invoked recursively with the DFA representing
  361. the parsing rule for that as its initial state; when that DFA accepts,
  362. the parser that invoked it continues. The parse tree constructed by the
  363. recursively called parser is inserted as a child in the current parse tree.
  364. The DFA's can be constructed automatically from a more conventional
  365. language description. An extended LL(1) grammar (ELL(1)) is suitable.
  366. Certain restrictions make the parser's life easier: rules that can produce
  367. the empty string should be outlawed (there are other ways to put loops
  368. or optional parts in the language). To avoid the need to construct
  369. FIRST sets, we can require that all but the last alternative of a rule
  370. (really: arc going out of a DFA's state) must begin with a terminal
  371. symbol.
  372. As an example, consider this grammar:
  373. expr: term (OP term)*
  374. term: CONSTANT | '(' expr ')'
  375. The DFA corresponding to the rule for expr is:
  376. ------->.---term-->.------->
  377. ^ |
  378. | |
  379. \----OP----/
  380. The parse tree generated for the input a+b is:
  381. (expr: (term: (NAME: a)), (OP: +), (term: (NAME: b)))
  382. */