2 /* Parser implementation */
4 /* For a description, see the comments at end of this file */
6 /* XXX To do: error recovery */
8 #include "pgenheaders.h"
18 extern int Py_DebugFlag
;
19 #define D(x) if (!Py_DebugFlag); else x
27 static void s_reset(stack
*);
32 s
->s_top
= &s
->s_base
[MAXSTACK
];
35 #define s_empty(s) ((s)->s_top == &(s)->s_base[MAXSTACK])
38 s_push(register stack
*s
, dfa
*d
, node
*parent
)
40 register stackentry
*top
;
41 if (s
->s_top
== s
->s_base
) {
42 fprintf(stderr
, "s_push: parser stack overflow\n");
47 top
->s_parent
= parent
;
55 s_pop(register stack
*s
)
58 Py_FatalError("s_pop: parser stack underflow -- FATAL");
64 #define s_pop(s) (s)->s_top++
72 PyParser_New(grammar
*g
, int start
)
77 PyGrammar_AddAccelerators(g
);
78 ps
= PyMem_NEW(parser_state
, 1);
82 ps
->p_tree
= PyNode_New(start
);
83 if (ps
->p_tree
== NULL
) {
87 s_reset(&ps
->p_stack
);
88 (void) s_push(&ps
->p_stack
, PyGrammar_FindDFA(g
, start
), ps
->p_tree
);
93 PyParser_Delete(parser_state
*ps
)
95 /* NB If you want to save the parse tree,
96 you must set p_tree to NULL before calling delparser! */
97 PyNode_Free(ps
->p_tree
);
102 /* PARSER STACK OPERATIONS */
105 shift(register stack
*s
, int type
, char *str
, int newstate
, int lineno
)
109 err
= PyNode_AddChild(s
->s_top
->s_parent
, type
, str
, lineno
);
112 s
->s_top
->s_state
= newstate
;
117 push(register stack
*s
, int type
, dfa
*d
, int newstate
, int lineno
)
121 n
= s
->s_top
->s_parent
;
123 err
= PyNode_AddChild(n
, type
, (char *)NULL
, lineno
);
126 s
->s_top
->s_state
= newstate
;
127 return s_push(s
, d
, CHILD(n
, NCH(n
)-1));
134 classify(grammar
*g
, int type
, char *str
)
136 register int n
= g
->g_ll
.ll_nlabels
;
139 register char *s
= str
;
140 register label
*l
= g
->g_ll
.ll_label
;
142 for (i
= n
; i
> 0; i
--, l
++) {
143 if (l
->lb_type
== NAME
&& l
->lb_str
!= NULL
&&
144 l
->lb_str
[0] == s
[0] &&
145 strcmp(l
->lb_str
, s
) == 0) {
146 D(printf("It's a keyword\n"));
153 register label
*l
= g
->g_ll
.ll_label
;
155 for (i
= n
; i
> 0; i
--, l
++) {
156 if (l
->lb_type
== type
&& l
->lb_str
== NULL
) {
157 D(printf("It's a token we know\n"));
163 D(printf("Illegal token\n"));
168 PyParser_AddToken(register parser_state
*ps
, register int type
, char *str
,
169 int lineno
, int *expected_ret
)
174 D(printf("Token %s/'%s' ... ", _PyParser_TokenNames
[type
], str
));
176 /* Find out which label this token is */
177 ilabel
= classify(ps
->p_grammar
, type
, str
);
181 /* Loop until the token is shifted or an error occurred */
183 /* Fetch the current dfa and state */
184 register dfa
*d
= ps
->p_stack
.s_top
->s_dfa
;
185 register state
*s
= &d
->d_state
[ps
->p_stack
.s_top
->s_state
];
187 D(printf(" DFA '%s', state %d:",
188 d
->d_name
, ps
->p_stack
.s_top
->s_state
));
190 /* Check accelerator */
191 if (s
->s_lower
<= ilabel
&& ilabel
< s
->s_upper
) {
192 register int x
= s
->s_accel
[ilabel
- s
->s_lower
];
195 /* Push non-terminal */
196 int nt
= (x
>> 8) + NT_OFFSET
;
197 int arrow
= x
& ((1<<7)-1);
198 dfa
*d1
= PyGrammar_FindDFA(
200 if ((err
= push(&ps
->p_stack
, nt
, d1
,
201 arrow
, lineno
)) > 0) {
202 D(printf(" MemError: push\n"));
205 D(printf(" Push ...\n"));
209 /* Shift the token */
210 if ((err
= shift(&ps
->p_stack
, type
, str
,
212 D(printf(" MemError: shift.\n"));
215 D(printf(" Shift.\n"));
216 /* Pop while we are in an accept-only state */
217 while (s
= &d
->d_state
218 [ps
->p_stack
.s_top
->s_state
],
219 s
->s_accept
&& s
->s_narcs
== 1) {
220 D(printf(" Direct pop.\n"));
222 if (s_empty(&ps
->p_stack
)) {
223 D(printf(" ACCEPT.\n"));
226 d
= ps
->p_stack
.s_top
->s_dfa
;
233 /* Pop this dfa and try again */
235 D(printf(" Pop ...\n"));
236 if (s_empty(&ps
->p_stack
)) {
237 D(printf(" Error: bottom of stack.\n"));
243 /* Stuck, report syntax error */
244 D(printf(" Error.\n"));
246 if (s
->s_lower
== s
->s_upper
- 1) {
247 /* Only one possible expected token */
248 *expected_ret
= ps
->p_grammar
->
249 g_ll
.ll_label
[s
->s_lower
].lb_type
;
264 dumptree(grammar
*g
, node
*n
)
274 printf("%s", PyGrammar_LabelRepr(&l
));
275 if (ISNONTERMINAL(TYPE(n
))) {
277 for (i
= 0; i
< NCH(n
); i
++) {
280 dumptree(g
, CHILD(n
, i
));
288 showtree(grammar
*g
, node
*n
)
294 if (ISNONTERMINAL(TYPE(n
))) {
295 for (i
= 0; i
< NCH(n
); i
++)
296 showtree(g
, CHILD(n
, i
));
298 else if (ISTERMINAL(TYPE(n
))) {
299 printf("%s", _PyParser_TokenNames
[TYPE(n
)]);
300 if (TYPE(n
) == NUMBER
|| TYPE(n
) == NAME
)
301 printf("(%s)", STR(n
));
309 printtree(parser_state
*ps
)
312 printf("Parse tree:\n");
313 dumptree(ps
->p_grammar
, ps
->p_tree
);
316 showtree(ps
->p_grammar
, ps
->p_tree
);
319 printf("Listing:\n");
320 PyNode_ListTree(ps
->p_tree
);
324 #endif /* Py_DEBUG */
331 The parser's interface is different than usual: the function addtoken()
332 must be called for each token in the input. This makes it possible to
333 turn it into an incremental parsing system later. The parsing system
334 constructs a parse tree as it goes.
336 A parsing rule is represented as a Deterministic Finite-state Automaton
337 (DFA). A node in a DFA represents a state of the parser; an arc represents
338 a transition. Transitions are either labeled with terminal symbols or
339 with non-terminals. When the parser decides to follow an arc labeled
340 with a non-terminal, it is invoked recursively with the DFA representing
341 the parsing rule for that as its initial state; when that DFA accepts,
342 the parser that invoked it continues. The parse tree constructed by the
343 recursively called parser is inserted as a child in the current parse tree.
345 The DFA's can be constructed automatically from a more conventional
346 language description. An extended LL(1) grammar (ELL(1)) is suitable.
347 Certain restrictions make the parser's life easier: rules that can produce
348 the empty string should be outlawed (there are other ways to put loops
349 or optional parts in the language). To avoid the need to construct
350 FIRST sets, we can require that all but the last alternative of a rule
351 (really: arc going out of a DFA's state) must begin with a terminal
354 As an example, consider this grammar:
356 expr: term (OP term)*
357 term: CONSTANT | '(' expr ')'
359 The DFA corresponding to the rule for expr is:
361 ------->.---term-->.------->
366 The parse tree generated for the input a+b is:
368 (expr: (term: (NAME: a)), (OP: +), (term: (NAME: b)))