1 /***********************************************************
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3 Amsterdam, The Netherlands.
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 fprintf(stderr
, "s_pop: parser stack underflow -- FATAL\n");
98 #define s_pop(s) (s)->s_top++
103 /* PARSER CREATION */
114 ps
= NEW(parser_state
, 1);
118 ps
->p_tree
= newtree(start
);
119 if (ps
->p_tree
== NULL
) {
123 s_reset(&ps
->p_stack
);
124 (void) s_push(&ps
->p_stack
, finddfa(g
, start
), ps
->p_tree
);
132 /* NB If you want to save the parse tree,
133 you must set p_tree to NULL before calling delparser! */
134 freetree(ps
->p_tree
);
139 /* PARSER STACK OPERATIONS */
141 static int shift
PROTO((stack
*, int, char *, int, int));
144 shift(s
, type
, str
, newstate
, lineno
)
152 if (addchild(s
->s_top
->s_parent
, type
, str
, lineno
) == NULL
) {
153 fprintf(stderr
, "shift: no mem in addchild\n");
156 s
->s_top
->s_state
= newstate
;
160 static int push
PROTO((stack
*, int, dfa
*, int, int));
163 push(s
, type
, d
, newstate
, lineno
)
171 n
= s
->s_top
->s_parent
;
173 if (addchild(n
, type
, (char *)NULL
, lineno
) == NULL
) {
174 fprintf(stderr
, "push: no mem in addchild\n");
177 s
->s_top
->s_state
= newstate
;
178 return s_push(s
, d
, CHILD(n
, NCH(n
)-1));
184 static int classify
PROTO((grammar
*, int, char *));
187 classify(g
, type
, str
)
192 register int n
= g
->g_ll
.ll_nlabels
;
195 register char *s
= str
;
196 register label
*l
= g
->g_ll
.ll_label
;
198 for (i
= n
; i
> 0; i
--, l
++) {
199 if (l
->lb_type
== NAME
&& l
->lb_str
!= NULL
&&
200 l
->lb_str
[0] == s
[0] &&
201 strcmp(l
->lb_str
, s
) == 0) {
202 D(printf("It's a keyword\n"));
209 register label
*l
= g
->g_ll
.ll_label
;
211 for (i
= n
; i
> 0; i
--, l
++) {
212 if (l
->lb_type
== type
&& l
->lb_str
== NULL
) {
213 D(printf("It's a token we know\n"));
219 D(printf("Illegal token\n"));
224 addtoken(ps
, type
, str
, lineno
)
225 register parser_state
*ps
;
232 D(printf("Token %s/'%s' ... ", tok_name
[type
], str
));
234 /* Find out which label this token is */
235 ilabel
= classify(ps
->p_grammar
, type
, str
);
239 /* Loop until the token is shifted or an error occurred */
241 /* Fetch the current dfa and state */
242 register dfa
*d
= ps
->p_stack
.s_top
->s_dfa
;
243 register state
*s
= &d
->d_state
[ps
->p_stack
.s_top
->s_state
];
245 D(printf(" DFA '%s', state %d:",
246 d
->d_name
, ps
->p_stack
.s_top
->s_state
));
248 /* Check accelerator */
249 if (s
->s_lower
<= ilabel
&& ilabel
< s
->s_upper
) {
250 register int x
= s
->s_accel
[ilabel
- s
->s_lower
];
253 /* Push non-terminal */
254 int nt
= (x
>> 8) + NT_OFFSET
;
255 int arrow
= x
& ((1<<7)-1);
256 dfa
*d1
= finddfa(ps
->p_grammar
, nt
);
257 if (push(&ps
->p_stack
, nt
, d1
,
258 arrow
, lineno
) < 0) {
259 D(printf(" MemError: push.\n"));
262 D(printf(" Push ...\n"));
266 /* Shift the token */
267 if (shift(&ps
->p_stack
, type
, str
,
269 D(printf(" MemError: shift.\n"));
272 D(printf(" Shift.\n"));
273 /* Pop while we are in an accept-only state */
274 while (s
= &d
->d_state
275 [ps
->p_stack
.s_top
->s_state
],
276 s
->s_accept
&& s
->s_narcs
== 1) {
277 D(printf(" Direct pop.\n"));
279 if (s_empty(&ps
->p_stack
)) {
280 D(printf(" ACCEPT.\n"));
283 d
= ps
->p_stack
.s_top
->s_dfa
;
290 /* Pop this dfa and try again */
292 D(printf(" Pop ...\n"));
293 if (s_empty(&ps
->p_stack
)) {
294 D(printf(" Error: bottom of stack.\n"));
300 /* Stuck, report syntax error */
301 D(printf(" Error.\n"));
324 printf("%s", labelrepr(&l
));
325 if (ISNONTERMINAL(TYPE(n
))) {
327 for (i
= 0; i
< NCH(n
); i
++) {
330 dumptree(g
, CHILD(n
, i
));
346 if (ISNONTERMINAL(TYPE(n
))) {
347 for (i
= 0; i
< NCH(n
); i
++)
348 showtree(g
, CHILD(n
, i
));
350 else if (ISTERMINAL(TYPE(n
))) {
351 printf("%s", tok_name
[TYPE(n
)]);
352 if (TYPE(n
) == NUMBER
|| TYPE(n
) == NAME
)
353 printf("(%s)", STR(n
));
365 printf("Parse tree:\n");
366 dumptree(ps
->p_grammar
, ps
->p_tree
);
369 showtree(ps
->p_grammar
, ps
->p_tree
);
372 printf("Listing:\n");
373 listtree(ps
->p_tree
);
384 The parser's interface is different than usual: the function addtoken()
385 must be called for each token in the input. This makes it possible to
386 turn it into an incremental parsing system later. The parsing system
387 constructs a parse tree as it goes.
389 A parsing rule is represented as a Deterministic Finite-state Automaton
390 (DFA). A node in a DFA represents a state of the parser; an arc represents
391 a transition. Transitions are either labeled with terminal symbols or
392 with non-terminals. When the parser decides to follow an arc labeled
393 with a non-terminal, it is invoked recursively with the DFA representing
394 the parsing rule for that as its initial state; when that DFA accepts,
395 the parser that invoked it continues. The parse tree constructed by the
396 recursively called parser is inserted as a child in the current parse tree.
398 The DFA's can be constructed automatically from a more conventional
399 language description. An extended LL(1) grammar (ELL(1)) is suitable.
400 Certain restrictions make the parser's life easier: rules that can produce
401 the empty string should be outlawed (there are other ways to put loops
402 or optional parts in the language). To avoid the need to construct
403 FIRST sets, we can require that all but the last alternative of a rule
404 (really: arc going out of a DFA's state) must begin with a terminal
407 As an example, consider this grammar:
409 expr: term (OP term)*
410 term: CONSTANT | '(' expr ')'
412 The DFA corresponding to the rule for expr is:
414 ------->.---term-->.------->
419 The parse tree generated for the input a+b is:
421 (expr: (term: (NAME: a)), (OP: +), (term: (NAME: b)))