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.

448 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(register stack *s, dfa *d, node *parent)
  27. {
  28. register 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(register 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(register 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(register stack *s, int type, dfa *d, int newstate, int lineno, int col_offset)
  95. {
  96. int err;
  97. register 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, char *str)
  109. {
  110. grammar *g = ps->p_grammar;
  111. register int n = g->g_ll.ll_nlabels;
  112. if (type == NAME) {
  113. register char *s = str;
  114. register label *l = g->g_ll.ll_label;
  115. register int i;
  116. for (i = n; i > 0; i--, l++) {
  117. if (l->lb_type != NAME || l->lb_str == NULL ||
  118. l->lb_str[0] != s[0] ||
  119. strcmp(l->lb_str, s) != 0)
  120. continue;
  121. #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
  122. #if 0
  123. /* Leaving this in as an example */
  124. if (!(ps->p_flags & CO_FUTURE_WITH_STATEMENT)) {
  125. if (s[0] == 'w' && strcmp(s, "with") == 0)
  126. break; /* not a keyword yet */
  127. else if (s[0] == 'a' && strcmp(s, "as") == 0)
  128. break; /* not a keyword yet */
  129. }
  130. #endif
  131. #endif
  132. D(printf("It's a keyword\n"));
  133. return n - i;
  134. }
  135. }
  136. {
  137. register label *l = g->g_ll.ll_label;
  138. register int i;
  139. for (i = n; i > 0; i--, l++) {
  140. if (l->lb_type == type && l->lb_str == NULL) {
  141. D(printf("It's a token we know\n"));
  142. return n - i;
  143. }
  144. }
  145. }
  146. D(printf("Illegal token\n"));
  147. return -1;
  148. }
  149. #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
  150. #if 0
  151. /* Leaving this in as an example */
  152. static void
  153. future_hack(parser_state *ps)
  154. {
  155. node *n = ps->p_stack.s_top->s_parent;
  156. node *ch, *cch;
  157. int i;
  158. /* from __future__ import ..., must have at least 4 children */
  159. n = CHILD(n, 0);
  160. if (NCH(n) < 4)
  161. return;
  162. ch = CHILD(n, 0);
  163. if (STR(ch) == NULL || strcmp(STR(ch), "from") != 0)
  164. return;
  165. ch = CHILD(n, 1);
  166. if (NCH(ch) == 1 && STR(CHILD(ch, 0)) &&
  167. strcmp(STR(CHILD(ch, 0)), "__future__") != 0)
  168. return;
  169. ch = CHILD(n, 3);
  170. /* ch can be a star, a parenthesis or import_as_names */
  171. if (TYPE(ch) == STAR)
  172. return;
  173. if (TYPE(ch) == LPAR)
  174. ch = CHILD(n, 4);
  175. for (i = 0; i < NCH(ch); i += 2) {
  176. cch = CHILD(ch, i);
  177. if (NCH(cch) >= 1 && TYPE(CHILD(cch, 0)) == NAME) {
  178. char *str_ch = STR(CHILD(cch, 0));
  179. if (strcmp(str_ch, FUTURE_WITH_STATEMENT) == 0) {
  180. ps->p_flags |= CO_FUTURE_WITH_STATEMENT;
  181. } else if (strcmp(str_ch, FUTURE_PRINT_FUNCTION) == 0) {
  182. ps->p_flags |= CO_FUTURE_PRINT_FUNCTION;
  183. } else if (strcmp(str_ch, FUTURE_UNICODE_LITERALS) == 0) {
  184. ps->p_flags |= CO_FUTURE_UNICODE_LITERALS;
  185. }
  186. }
  187. }
  188. }
  189. #endif
  190. #endif /* future keyword */
  191. int
  192. PyParser_AddToken(register parser_state *ps, register int type, char *str,
  193. int lineno, int col_offset, int *expected_ret)
  194. {
  195. register int ilabel;
  196. int err;
  197. D(printf("Token %s/'%s' ... ", _PyParser_TokenNames[type], str));
  198. /* Find out which label this token is */
  199. ilabel = classify(ps, type, str);
  200. if (ilabel < 0)
  201. return E_SYNTAX;
  202. /* Loop until the token is shifted or an error occurred */
  203. for (;;) {
  204. /* Fetch the current dfa and state */
  205. register dfa *d = ps->p_stack.s_top->s_dfa;
  206. register state *s = &d->d_state[ps->p_stack.s_top->s_state];
  207. D(printf(" DFA '%s', state %d:",
  208. d->d_name, ps->p_stack.s_top->s_state));
  209. /* Check accelerator */
  210. if (s->s_lower <= ilabel && ilabel < s->s_upper) {
  211. register int x = s->s_accel[ilabel - s->s_lower];
  212. if (x != -1) {
  213. if (x & (1<<7)) {
  214. /* Push non-terminal */
  215. int nt = (x >> 8) + NT_OFFSET;
  216. int arrow = x & ((1<<7)-1);
  217. dfa *d1 = PyGrammar_FindDFA(
  218. ps->p_grammar, nt);
  219. if ((err = push(&ps->p_stack, nt, d1,
  220. arrow, lineno, col_offset)) > 0) {
  221. D(printf(" MemError: push\n"));
  222. return err;
  223. }
  224. D(printf(" Push ...\n"));
  225. continue;
  226. }
  227. /* Shift the token */
  228. if ((err = shift(&ps->p_stack, type, str,
  229. x, lineno, col_offset)) > 0) {
  230. D(printf(" MemError: shift.\n"));
  231. return err;
  232. }
  233. D(printf(" Shift.\n"));
  234. /* Pop while we are in an accept-only state */
  235. while (s = &d->d_state
  236. [ps->p_stack.s_top->s_state],
  237. s->s_accept && s->s_narcs == 1) {
  238. D(printf(" DFA '%s', state %d: "
  239. "Direct pop.\n",
  240. d->d_name,
  241. ps->p_stack.s_top->s_state));
  242. #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
  243. #if 0
  244. if (d->d_name[0] == 'i' &&
  245. strcmp(d->d_name,
  246. "import_stmt") == 0)
  247. future_hack(ps);
  248. #endif
  249. #endif
  250. s_pop(&ps->p_stack);
  251. if (s_empty(&ps->p_stack)) {
  252. D(printf(" ACCEPT.\n"));
  253. return E_DONE;
  254. }
  255. d = ps->p_stack.s_top->s_dfa;
  256. }
  257. return E_OK;
  258. }
  259. }
  260. if (s->s_accept) {
  261. #ifdef PY_PARSER_REQUIRES_FUTURE_KEYWORD
  262. #if 0
  263. if (d->d_name[0] == 'i' &&
  264. strcmp(d->d_name, "import_stmt") == 0)
  265. future_hack(ps);
  266. #endif
  267. #endif
  268. /* Pop this dfa and try again */
  269. s_pop(&ps->p_stack);
  270. D(printf(" Pop ...\n"));
  271. if (s_empty(&ps->p_stack)) {
  272. D(printf(" Error: bottom of stack.\n"));
  273. return E_SYNTAX;
  274. }
  275. continue;
  276. }
  277. /* Stuck, report syntax error */
  278. D(printf(" Error.\n"));
  279. if (expected_ret) {
  280. if (s->s_lower == s->s_upper - 1) {
  281. /* Only one possible expected token */
  282. *expected_ret = ps->p_grammar->
  283. g_ll.ll_label[s->s_lower].lb_type;
  284. }
  285. else
  286. *expected_ret = -1;
  287. }
  288. return E_SYNTAX;
  289. }
  290. }
  291. #ifdef Py_DEBUG
  292. /* DEBUG OUTPUT */
  293. void
  294. dumptree(grammar *g, node *n)
  295. {
  296. int i;
  297. if (n == NULL)
  298. printf("NIL");
  299. else {
  300. label l;
  301. l.lb_type = TYPE(n);
  302. l.lb_str = STR(n);
  303. printf("%s", PyGrammar_LabelRepr(&l));
  304. if (ISNONTERMINAL(TYPE(n))) {
  305. printf("(");
  306. for (i = 0; i < NCH(n); i++) {
  307. if (i > 0)
  308. printf(",");
  309. dumptree(g, CHILD(n, i));
  310. }
  311. printf(")");
  312. }
  313. }
  314. }
  315. void
  316. showtree(grammar *g, node *n)
  317. {
  318. int i;
  319. if (n == NULL)
  320. return;
  321. if (ISNONTERMINAL(TYPE(n))) {
  322. for (i = 0; i < NCH(n); i++)
  323. showtree(g, CHILD(n, i));
  324. }
  325. else if (ISTERMINAL(TYPE(n))) {
  326. printf("%s", _PyParser_TokenNames[TYPE(n)]);
  327. if (TYPE(n) == NUMBER || TYPE(n) == NAME)
  328. printf("(%s)", STR(n));
  329. printf(" ");
  330. }
  331. else
  332. printf("? ");
  333. }
  334. void
  335. printtree(parser_state *ps)
  336. {
  337. if (Py_DebugFlag) {
  338. printf("Parse tree:\n");
  339. dumptree(ps->p_grammar, ps->p_tree);
  340. printf("\n");
  341. printf("Tokens:\n");
  342. showtree(ps->p_grammar, ps->p_tree);
  343. printf("\n");
  344. }
  345. printf("Listing:\n");
  346. PyNode_ListTree(ps->p_tree);
  347. printf("\n");
  348. }
  349. #endif /* Py_DEBUG */
  350. /*
  351. Description
  352. -----------
  353. The parser's interface is different than usual: the function addtoken()
  354. must be called for each token in the input. This makes it possible to
  355. turn it into an incremental parsing system later. The parsing system
  356. constructs a parse tree as it goes.
  357. A parsing rule is represented as a Deterministic Finite-state Automaton
  358. (DFA). A node in a DFA represents a state of the parser; an arc represents
  359. a transition. Transitions are either labeled with terminal symbols or
  360. with non-terminals. When the parser decides to follow an arc labeled
  361. with a non-terminal, it is invoked recursively with the DFA representing
  362. the parsing rule for that as its initial state; when that DFA accepts,
  363. the parser that invoked it continues. The parse tree constructed by the
  364. recursively called parser is inserted as a child in the current parse tree.
  365. The DFA's can be constructed automatically from a more conventional
  366. language description. An extended LL(1) grammar (ELL(1)) is suitable.
  367. Certain restrictions make the parser's life easier: rules that can produce
  368. the empty string should be outlawed (there are other ways to put loops
  369. or optional parts in the language). To avoid the need to construct
  370. FIRST sets, we can require that all but the last alternative of a rule
  371. (really: arc going out of a DFA's state) must begin with a terminal
  372. symbol.
  373. As an example, consider this grammar:
  374. expr: term (OP term)*
  375. term: CONSTANT | '(' expr ')'
  376. The DFA corresponding to the rule for expr is:
  377. ------->.---term-->.------->
  378. ^ |
  379. | |
  380. \----OP----/
  381. The parse tree generated for the input a+b is:
  382. (expr: (term: (NAME: a)), (OP: +), (term: (NAME: b)))
  383. */