1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Parser implementation */
27 /* For a description, see the comments at end of this file */
29 /* XXX To do: error recovery */
31 #include "pgenheaders.h"
42 #define D(x) if (!debugging); else x
50 static void s_reset
PROTO((stack
*));
56 s
->s_top
= &s
->s_base
[MAXSTACK
];
59 #define s_empty(s) ((s)->s_top == &(s)->s_base[MAXSTACK])
61 static int s_push
PROTO((stack
*, dfa
*, node
*));
69 register stackentry
*top
;
70 if (s
->s_top
== s
->s_base
) {
71 fprintf(stderr
, "s_push: parser stack overflow\n");
76 top
->s_parent
= parent
;
83 static void s_pop
PROTO((stack
*));
90 fatal("s_pop: parser stack underflow -- FATAL");
96 #define s_pop(s) (s)->s_top++
101 /* PARSER CREATION */
112 ps
= NEW(parser_state
, 1);
116 ps
->p_tree
= newtree(start
);
117 if (ps
->p_tree
== NULL
) {
121 s_reset(&ps
->p_stack
);
122 (void) s_push(&ps
->p_stack
, finddfa(g
, start
), ps
->p_tree
);
130 /* NB If you want to save the parse tree,
131 you must set p_tree to NULL before calling delparser! */
132 freetree(ps
->p_tree
);
137 /* PARSER STACK OPERATIONS */
139 static int shift
PROTO((stack
*, int, char *, int, int));
142 shift(s
, type
, str
, newstate
, lineno
)
150 if (addchild(s
->s_top
->s_parent
, type
, str
, lineno
) == NULL
) {
151 fprintf(stderr
, "shift: no mem in addchild\n");
154 s
->s_top
->s_state
= newstate
;
158 static int push
PROTO((stack
*, int, dfa
*, int, int));
161 push(s
, type
, d
, newstate
, lineno
)
169 n
= s
->s_top
->s_parent
;
171 if (addchild(n
, type
, (char *)NULL
, lineno
) == NULL
) {
172 fprintf(stderr
, "push: no mem in addchild\n");
175 s
->s_top
->s_state
= newstate
;
176 return s_push(s
, d
, CHILD(n
, NCH(n
)-1));
182 static int classify
PROTO((grammar
*, int, char *));
185 classify(g
, type
, str
)
190 register int n
= g
->g_ll
.ll_nlabels
;
193 register char *s
= str
;
194 register label
*l
= g
->g_ll
.ll_label
;
196 for (i
= n
; i
> 0; i
--, l
++) {
197 if (l
->lb_type
== NAME
&& l
->lb_str
!= NULL
&&
198 l
->lb_str
[0] == s
[0] &&
199 strcmp(l
->lb_str
, s
) == 0) {
200 D(printf("It's a keyword\n"));
207 register label
*l
= g
->g_ll
.ll_label
;
209 for (i
= n
; i
> 0; i
--, l
++) {
210 if (l
->lb_type
== type
&& l
->lb_str
== NULL
) {
211 D(printf("It's a token we know\n"));
217 D(printf("Illegal token\n"));
222 addtoken(ps
, type
, str
, lineno
)
223 register parser_state
*ps
;
230 D(printf("Token %s/'%s' ... ", tok_name
[type
], str
));
232 /* Find out which label this token is */
233 ilabel
= classify(ps
->p_grammar
, type
, str
);
237 /* Loop until the token is shifted or an error occurred */
239 /* Fetch the current dfa and state */
240 register dfa
*d
= ps
->p_stack
.s_top
->s_dfa
;
241 register state
*s
= &d
->d_state
[ps
->p_stack
.s_top
->s_state
];
243 D(printf(" DFA '%s', state %d:",
244 d
->d_name
, ps
->p_stack
.s_top
->s_state
));
246 /* Check accelerator */
247 if (s
->s_lower
<= ilabel
&& ilabel
< s
->s_upper
) {
248 register int x
= s
->s_accel
[ilabel
- s
->s_lower
];
251 /* Push non-terminal */
252 int nt
= (x
>> 8) + NT_OFFSET
;
253 int arrow
= x
& ((1<<7)-1);
254 dfa
*d1
= finddfa(ps
->p_grammar
, nt
);
255 if (push(&ps
->p_stack
, nt
, d1
,
256 arrow
, lineno
) < 0) {
257 D(printf(" MemError: push.\n"));
260 D(printf(" Push ...\n"));
264 /* Shift the token */
265 if (shift(&ps
->p_stack
, type
, str
,
267 D(printf(" MemError: shift.\n"));
270 D(printf(" Shift.\n"));
271 /* Pop while we are in an accept-only state */
272 while (s
= &d
->d_state
273 [ps
->p_stack
.s_top
->s_state
],
274 s
->s_accept
&& s
->s_narcs
== 1) {
275 D(printf(" Direct pop.\n"));
277 if (s_empty(&ps
->p_stack
)) {
278 D(printf(" ACCEPT.\n"));
281 d
= ps
->p_stack
.s_top
->s_dfa
;
288 /* Pop this dfa and try again */
290 D(printf(" Pop ...\n"));
291 if (s_empty(&ps
->p_stack
)) {
292 D(printf(" Error: bottom of stack.\n"));
298 /* Stuck, report syntax error */
299 D(printf(" Error.\n"));
322 printf("%s", labelrepr(&l
));
323 if (ISNONTERMINAL(TYPE(n
))) {
325 for (i
= 0; i
< NCH(n
); i
++) {
328 dumptree(g
, CHILD(n
, i
));
344 if (ISNONTERMINAL(TYPE(n
))) {
345 for (i
= 0; i
< NCH(n
); i
++)
346 showtree(g
, CHILD(n
, i
));
348 else if (ISTERMINAL(TYPE(n
))) {
349 printf("%s", tok_name
[TYPE(n
)]);
350 if (TYPE(n
) == NUMBER
|| TYPE(n
) == NAME
)
351 printf("(%s)", STR(n
));
363 printf("Parse tree:\n");
364 dumptree(ps
->p_grammar
, ps
->p_tree
);
367 showtree(ps
->p_grammar
, ps
->p_tree
);
370 printf("Listing:\n");
371 listtree(ps
->p_tree
);
382 The parser's interface is different than usual: the function addtoken()
383 must be called for each token in the input. This makes it possible to
384 turn it into an incremental parsing system later. The parsing system
385 constructs a parse tree as it goes.
387 A parsing rule is represented as a Deterministic Finite-state Automaton
388 (DFA). A node in a DFA represents a state of the parser; an arc represents
389 a transition. Transitions are either labeled with terminal symbols or
390 with non-terminals. When the parser decides to follow an arc labeled
391 with a non-terminal, it is invoked recursively with the DFA representing
392 the parsing rule for that as its initial state; when that DFA accepts,
393 the parser that invoked it continues. The parse tree constructed by the
394 recursively called parser is inserted as a child in the current parse tree.
396 The DFA's can be constructed automatically from a more conventional
397 language description. An extended LL(1) grammar (ELL(1)) is suitable.
398 Certain restrictions make the parser's life easier: rules that can produce
399 the empty string should be outlawed (there are other ways to put loops
400 or optional parts in the language). To avoid the need to construct
401 FIRST sets, we can require that all but the last alternative of a rule
402 (really: arc going out of a DFA's state) must begin with a terminal
405 As an example, consider this grammar:
407 expr: term (OP term)*
408 term: CONSTANT | '(' expr ')'
410 The DFA corresponding to the rule for expr is:
412 ------->.---term-->.------->
417 The parse tree generated for the input a+b is:
419 (expr: (term: (NAME: a)), (OP: +), (term: (NAME: b)))