]>
Commit | Line | Data |
---|---|---|
1 | /* Input parser for bison | |
2 | Copyright 1984, 1986, 1989, 1992, 1998, 2000, 2001 | |
3 | Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
7 | Bison is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | Bison is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with Bison; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | ||
23 | #include "system.h" | |
24 | #include "obstack.h" | |
25 | #include "quotearg.h" | |
26 | #include "quote.h" | |
27 | #include "getargs.h" | |
28 | #include "files.h" | |
29 | #include "symtab.h" | |
30 | #include "options.h" | |
31 | #include "lex.h" | |
32 | #include "gram.h" | |
33 | #include "complain.h" | |
34 | #include "output.h" | |
35 | #include "reader.h" | |
36 | #include "conflicts.h" | |
37 | #include "muscle_tab.h" | |
38 | ||
39 | typedef struct symbol_list | |
40 | { | |
41 | struct symbol_list *next; | |
42 | bucket *sym; | |
43 | int line; | |
44 | bucket *ruleprec; | |
45 | } | |
46 | symbol_list; | |
47 | ||
48 | int lineno; | |
49 | char **tags; | |
50 | short *user_toknums; | |
51 | static symbol_list *grammar; | |
52 | static int start_flag; | |
53 | static bucket *startval; | |
54 | ||
55 | /* Nonzero if components of semantic values are used, implying | |
56 | they must be unions. */ | |
57 | static int value_components_used; | |
58 | ||
59 | /* Nonzero if %union has been seen. */ | |
60 | static int typed; | |
61 | ||
62 | /* Incremented for each %left, %right or %nonassoc seen */ | |
63 | static int lastprec; | |
64 | ||
65 | static bucket *errtoken; | |
66 | static bucket *undeftoken; | |
67 | ||
68 | ||
69 | static symbol_list * | |
70 | symbol_list_new (bucket *sym) | |
71 | { | |
72 | symbol_list *res = XMALLOC (symbol_list, 1); | |
73 | res->next = NULL; | |
74 | res->sym = sym; | |
75 | res->line = lineno; | |
76 | res->ruleprec = NULL; | |
77 | return res; | |
78 | } | |
79 | ||
80 | \f | |
81 | ||
82 | /*===================\ | |
83 | | Low level lexing. | | |
84 | \===================*/ | |
85 | ||
86 | static void | |
87 | skip_to_char (int target) | |
88 | { | |
89 | int c; | |
90 | if (target == '\n') | |
91 | complain (_(" Skipping to next \\n")); | |
92 | else | |
93 | complain (_(" Skipping to next %c"), target); | |
94 | ||
95 | do | |
96 | c = skip_white_space (); | |
97 | while (c != target && c != EOF); | |
98 | if (c != EOF) | |
99 | ungetc (c, finput); | |
100 | } | |
101 | ||
102 | ||
103 | /*---------------------------------------------------------. | |
104 | | Read a signed integer from STREAM and return its value. | | |
105 | `---------------------------------------------------------*/ | |
106 | ||
107 | static inline int | |
108 | read_signed_integer (FILE *stream) | |
109 | { | |
110 | int c = getc (stream); | |
111 | int sign = 1; | |
112 | int n = 0; | |
113 | ||
114 | if (c == '-') | |
115 | { | |
116 | c = getc (stream); | |
117 | sign = -1; | |
118 | } | |
119 | ||
120 | while (isdigit (c)) | |
121 | { | |
122 | n = 10 * n + (c - '0'); | |
123 | c = getc (stream); | |
124 | } | |
125 | ||
126 | ungetc (c, stream); | |
127 | ||
128 | return sign * n; | |
129 | } | |
130 | \f | |
131 | /*--------------------------------------------------------------. | |
132 | | Get the data type (alternative in the union) of the value for | | |
133 | | symbol N in rule RULE. | | |
134 | `--------------------------------------------------------------*/ | |
135 | ||
136 | static char * | |
137 | get_type_name (int n, symbol_list *rule) | |
138 | { | |
139 | int i; | |
140 | symbol_list *rp; | |
141 | ||
142 | if (n < 0) | |
143 | { | |
144 | complain (_("invalid $ value")); | |
145 | return NULL; | |
146 | } | |
147 | ||
148 | rp = rule; | |
149 | i = 0; | |
150 | ||
151 | while (i < n) | |
152 | { | |
153 | rp = rp->next; | |
154 | if (rp == NULL || rp->sym == NULL) | |
155 | { | |
156 | complain (_("invalid $ value")); | |
157 | return NULL; | |
158 | } | |
159 | i++; | |
160 | } | |
161 | ||
162 | return rp->sym->type_name; | |
163 | } | |
164 | \f | |
165 | /*------------------------------------------------------------. | |
166 | | Dump the string from FIN to OOUT if non null. MATCH is the | | |
167 | | delimiter of the string (either ' or "). | | |
168 | `------------------------------------------------------------*/ | |
169 | ||
170 | static inline void | |
171 | copy_string2 (FILE *fin, struct obstack *oout, int match, int store) | |
172 | { | |
173 | int c; | |
174 | ||
175 | if (store) | |
176 | obstack_1grow (oout, match); | |
177 | ||
178 | c = getc (fin); | |
179 | ||
180 | while (c != match) | |
181 | { | |
182 | if (c == EOF) | |
183 | fatal (_("unterminated string at end of file")); | |
184 | if (c == '\n') | |
185 | { | |
186 | complain (_("unterminated string")); | |
187 | ungetc (c, fin); | |
188 | c = match; /* invent terminator */ | |
189 | continue; | |
190 | } | |
191 | ||
192 | obstack_1grow (oout, c); | |
193 | ||
194 | if (c == '\\') | |
195 | { | |
196 | c = getc (fin); | |
197 | if (c == EOF) | |
198 | fatal (_("unterminated string at end of file")); | |
199 | obstack_1grow (oout, c); | |
200 | ||
201 | if (c == '\n') | |
202 | lineno++; | |
203 | } | |
204 | ||
205 | c = getc (fin); | |
206 | } | |
207 | ||
208 | if (store) | |
209 | obstack_1grow (oout, c); | |
210 | } | |
211 | ||
212 | /* FIXME. */ | |
213 | ||
214 | static inline void | |
215 | copy_string (FILE *fin, struct obstack *oout, int match) | |
216 | { | |
217 | copy_string2 (fin, oout, match, 1); | |
218 | } | |
219 | ||
220 | /* FIXME. */ | |
221 | ||
222 | static inline void | |
223 | copy_identifier (FILE *fin, struct obstack *oout) | |
224 | { | |
225 | int c; | |
226 | ||
227 | while (isalnum (c = getc (fin)) || c == '_') | |
228 | obstack_1grow (oout, c); | |
229 | ||
230 | ungetc (c, fin); | |
231 | } | |
232 | ||
233 | /*-----------------------------------------------------------------. | |
234 | | Dump the wannabee comment from IN to OUT1 and OUT2 (which can be | | |
235 | | NULL). In fact we just saw a `/', which might or might not be a | | |
236 | | comment. In any case, copy what we saw. | | |
237 | | | | |
238 | | OUT2 might be NULL. | | |
239 | `-----------------------------------------------------------------*/ | |
240 | ||
241 | static inline void | |
242 | copy_comment2 (FILE *fin, struct obstack *oout1, struct obstack *oout2) | |
243 | { | |
244 | int cplus_comment; | |
245 | int ended; | |
246 | int c; | |
247 | ||
248 | /* We read a `/', output it. */ | |
249 | obstack_1grow (oout1, '/'); | |
250 | if (oout2) | |
251 | obstack_1grow (oout2, '/'); | |
252 | ||
253 | switch ((c = getc (fin))) | |
254 | { | |
255 | case '/': | |
256 | cplus_comment = 1; | |
257 | break; | |
258 | case '*': | |
259 | cplus_comment = 0; | |
260 | break; | |
261 | default: | |
262 | ungetc (c, fin); | |
263 | return; | |
264 | } | |
265 | ||
266 | obstack_1grow (oout1, c); | |
267 | if (oout2) | |
268 | obstack_1grow (oout2, c); | |
269 | c = getc (fin); | |
270 | ||
271 | ended = 0; | |
272 | while (!ended) | |
273 | { | |
274 | if (!cplus_comment && c == '*') | |
275 | { | |
276 | while (c == '*') | |
277 | { | |
278 | obstack_1grow (oout1, c); | |
279 | if (oout2) | |
280 | obstack_1grow (oout2, c); | |
281 | c = getc (fin); | |
282 | } | |
283 | ||
284 | if (c == '/') | |
285 | { | |
286 | obstack_1grow (oout1, c); | |
287 | if (oout2) | |
288 | obstack_1grow (oout2, c); | |
289 | ended = 1; | |
290 | } | |
291 | } | |
292 | else if (c == '\n') | |
293 | { | |
294 | lineno++; | |
295 | obstack_1grow (oout1, c); | |
296 | if (oout2) | |
297 | obstack_1grow (oout2, c); | |
298 | if (cplus_comment) | |
299 | ended = 1; | |
300 | else | |
301 | c = getc (fin); | |
302 | } | |
303 | else if (c == EOF) | |
304 | fatal (_("unterminated comment")); | |
305 | else | |
306 | { | |
307 | obstack_1grow (oout1, c); | |
308 | if (oout2) | |
309 | obstack_1grow (oout2, c); | |
310 | c = getc (fin); | |
311 | } | |
312 | } | |
313 | } | |
314 | ||
315 | ||
316 | /*-------------------------------------------------------------------. | |
317 | | Dump the comment (actually the current string starting with a `/') | | |
318 | | from FIN to OOUT. | | |
319 | `-------------------------------------------------------------------*/ | |
320 | ||
321 | static inline void | |
322 | copy_comment (FILE *fin, struct obstack *oout) | |
323 | { | |
324 | copy_comment2 (fin, oout, NULL); | |
325 | } | |
326 | ||
327 | ||
328 | /*-----------------------------------------------------------------. | |
329 | | FIN is pointing to a location (i.e., a `@'). Output to OOUT a | | |
330 | | reference to this location. STACK_OFFSET is the number of values | | |
331 | | in the current rule so far, which says where to find `$0' with | | |
332 | | respect to the top of the stack. | | |
333 | `-----------------------------------------------------------------*/ | |
334 | ||
335 | static inline void | |
336 | copy_at (FILE *fin, struct obstack *oout, int stack_offset) | |
337 | { | |
338 | int c; | |
339 | ||
340 | c = getc (fin); | |
341 | if (c == '$') | |
342 | { | |
343 | obstack_sgrow (oout, "yyloc"); | |
344 | locations_flag = 1; | |
345 | } | |
346 | else if (isdigit (c) || c == '-') | |
347 | { | |
348 | int n; | |
349 | ||
350 | ungetc (c, fin); | |
351 | n = read_signed_integer (fin); | |
352 | ||
353 | obstack_fgrow1 (oout, "yylsp[%d]", n - stack_offset); | |
354 | locations_flag = 1; | |
355 | } | |
356 | else | |
357 | { | |
358 | char buf[] = "@c"; | |
359 | buf[1] = c; | |
360 | complain (_("%s is invalid"), quote (buf)); | |
361 | } | |
362 | } | |
363 | ||
364 | ||
365 | /*-------------------------------------------------------------------. | |
366 | | FIN is pointing to a wannabee semantic value (i.e., a `$'). | | |
367 | | | | |
368 | | Possible inputs: $[<TYPENAME>]($|integer) | | |
369 | | | | |
370 | | Output to OOUT a reference to this semantic value. STACK_OFFSET is | | |
371 | | the number of values in the current rule so far, which says where | | |
372 | | to find `$0' with respect to the top of the stack. | | |
373 | `-------------------------------------------------------------------*/ | |
374 | ||
375 | static inline void | |
376 | copy_dollar (FILE *fin, struct obstack *oout, | |
377 | symbol_list *rule, int stack_offset) | |
378 | { | |
379 | int c = getc (fin); | |
380 | const char *type_name = NULL; | |
381 | ||
382 | /* Get the type name if explicit. */ | |
383 | if (c == '<') | |
384 | { | |
385 | read_type_name (fin); | |
386 | type_name = token_buffer; | |
387 | value_components_used = 1; | |
388 | c = getc (fin); | |
389 | } | |
390 | ||
391 | if (c == '$') | |
392 | { | |
393 | obstack_sgrow (oout, "yyval"); | |
394 | ||
395 | if (!type_name) | |
396 | type_name = get_type_name (0, rule); | |
397 | if (type_name) | |
398 | obstack_fgrow1 (oout, ".%s", type_name); | |
399 | if (!type_name && typed) | |
400 | complain (_("$$ of `%s' has no declared type"), | |
401 | rule->sym->tag); | |
402 | } | |
403 | else if (isdigit (c) || c == '-') | |
404 | { | |
405 | int n; | |
406 | ungetc (c, fin); | |
407 | n = read_signed_integer (fin); | |
408 | ||
409 | if (!type_name && n > 0) | |
410 | type_name = get_type_name (n, rule); | |
411 | ||
412 | obstack_fgrow1 (oout, "yyvsp[%d]", n - stack_offset); | |
413 | ||
414 | if (type_name) | |
415 | obstack_fgrow1 (oout, ".%s", type_name); | |
416 | if (!type_name && typed) | |
417 | complain (_("$%d of `%s' has no declared type"), | |
418 | n, rule->sym->tag); | |
419 | } | |
420 | else | |
421 | { | |
422 | char buf[] = "$c"; | |
423 | buf[1] = c; | |
424 | complain (_("%s is invalid"), quote (buf)); | |
425 | } | |
426 | } | |
427 | \f | |
428 | /*-------------------------------------------------------------------. | |
429 | | Copy the contents of a `%{ ... %}' into the definitions file. The | | |
430 | | `%{' has already been read. Return after reading the `%}'. | | |
431 | `-------------------------------------------------------------------*/ | |
432 | ||
433 | static void | |
434 | copy_definition (void) | |
435 | { | |
436 | int c; | |
437 | /* -1 while reading a character if prev char was %. */ | |
438 | int after_percent; | |
439 | ||
440 | if (!no_lines_flag) | |
441 | { | |
442 | obstack_fgrow2 (&attrs_obstack, muscle_find ("linef"), | |
443 | lineno, quotearg_style (c_quoting_style, | |
444 | muscle_find("filename"))); | |
445 | } | |
446 | ||
447 | after_percent = 0; | |
448 | ||
449 | c = getc (finput); | |
450 | ||
451 | for (;;) | |
452 | { | |
453 | switch (c) | |
454 | { | |
455 | case '\n': | |
456 | obstack_1grow (&attrs_obstack, c); | |
457 | lineno++; | |
458 | break; | |
459 | ||
460 | case '%': | |
461 | after_percent = -1; | |
462 | break; | |
463 | ||
464 | case '\'': | |
465 | case '"': | |
466 | copy_string (finput, &attrs_obstack, c); | |
467 | break; | |
468 | ||
469 | case '/': | |
470 | copy_comment (finput, &attrs_obstack); | |
471 | break; | |
472 | ||
473 | case EOF: | |
474 | fatal ("%s", _("unterminated `%{' definition")); | |
475 | ||
476 | default: | |
477 | obstack_1grow (&attrs_obstack, c); | |
478 | } | |
479 | ||
480 | c = getc (finput); | |
481 | ||
482 | if (after_percent) | |
483 | { | |
484 | if (c == '}') | |
485 | return; | |
486 | obstack_1grow (&attrs_obstack, '%'); | |
487 | } | |
488 | after_percent = 0; | |
489 | } | |
490 | } | |
491 | ||
492 | ||
493 | /*-------------------------------------------------------------------. | |
494 | | Parse what comes after %token or %nterm. For %token, WHAT_IS is | | |
495 | | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments | | |
496 | | are reversed. | | |
497 | `-------------------------------------------------------------------*/ | |
498 | ||
499 | static void | |
500 | parse_token_decl (symbol_class what_is, symbol_class what_is_not) | |
501 | { | |
502 | token_t token = tok_undef; | |
503 | char *typename = NULL; | |
504 | ||
505 | /* The symbol being defined. */ | |
506 | struct bucket *symbol = NULL; | |
507 | ||
508 | /* After `%token' and `%nterm', any number of symbols maybe be | |
509 | defined. */ | |
510 | for (;;) | |
511 | { | |
512 | int tmp_char = ungetc (skip_white_space (), finput); | |
513 | ||
514 | /* `%' (for instance from `%token', or from `%%' etc.) is the | |
515 | only valid means to end this declaration. */ | |
516 | if (tmp_char == '%') | |
517 | return; | |
518 | if (tmp_char == EOF) | |
519 | fatal (_("Premature EOF after %s"), token_buffer); | |
520 | ||
521 | token = lex (); | |
522 | if (token == tok_comma) | |
523 | { | |
524 | symbol = NULL; | |
525 | continue; | |
526 | } | |
527 | if (token == tok_typename) | |
528 | { | |
529 | typename = xstrdup (token_buffer); | |
530 | value_components_used = 1; | |
531 | symbol = NULL; | |
532 | } | |
533 | else if (token == tok_identifier && *symval->tag == '\"' && symbol) | |
534 | { | |
535 | if (symval->alias) | |
536 | warn (_("symbol `%s' used more than once as a literal string"), | |
537 | symval->tag); | |
538 | else if (symbol->alias) | |
539 | warn (_("symbol `%s' given more than one literal string"), | |
540 | symbol->tag); | |
541 | else | |
542 | { | |
543 | symval->class = token_sym; | |
544 | symval->type_name = typename; | |
545 | symval->user_token_number = symbol->user_token_number; | |
546 | symbol->user_token_number = SALIAS; | |
547 | symval->alias = symbol; | |
548 | symbol->alias = symval; | |
549 | /* symbol and symval combined are only one symbol */ | |
550 | nsyms--; | |
551 | } | |
552 | symbol = NULL; | |
553 | } | |
554 | else if (token == tok_identifier) | |
555 | { | |
556 | int oldclass = symval->class; | |
557 | symbol = symval; | |
558 | ||
559 | if (symbol->class == what_is_not) | |
560 | complain (_("symbol %s redefined"), symbol->tag); | |
561 | symbol->class = what_is; | |
562 | if (what_is == nterm_sym && oldclass != nterm_sym) | |
563 | symbol->value = nvars++; | |
564 | ||
565 | if (typename) | |
566 | { | |
567 | if (symbol->type_name == NULL) | |
568 | symbol->type_name = typename; | |
569 | else if (strcmp (typename, symbol->type_name) != 0) | |
570 | complain (_("type redeclaration for %s"), symbol->tag); | |
571 | } | |
572 | } | |
573 | else if (symbol && token == tok_number) | |
574 | { | |
575 | symbol->user_token_number = numval; | |
576 | } | |
577 | else | |
578 | { | |
579 | complain (_("`%s' is invalid in %s"), | |
580 | token_buffer, | |
581 | (what_is == token_sym) ? "%token" : "%nterm"); | |
582 | skip_to_char ('%'); | |
583 | } | |
584 | } | |
585 | ||
586 | } | |
587 | ||
588 | ||
589 | /*------------------------------. | |
590 | | Parse what comes after %start | | |
591 | `------------------------------*/ | |
592 | ||
593 | static void | |
594 | parse_start_decl (void) | |
595 | { | |
596 | if (start_flag) | |
597 | complain (_("multiple %s declarations"), "%start"); | |
598 | if (lex () != tok_identifier) | |
599 | complain (_("invalid %s declaration"), "%start"); | |
600 | else | |
601 | { | |
602 | start_flag = 1; | |
603 | startval = symval; | |
604 | } | |
605 | } | |
606 | ||
607 | /*-----------------------------------------------------------. | |
608 | | read in a %type declaration and record its information for | | |
609 | | get_type_name to access | | |
610 | `-----------------------------------------------------------*/ | |
611 | ||
612 | static void | |
613 | parse_type_decl (void) | |
614 | { | |
615 | char *name; | |
616 | ||
617 | if (lex () != tok_typename) | |
618 | { | |
619 | complain ("%s", _("%type declaration has no <typename>")); | |
620 | skip_to_char ('%'); | |
621 | return; | |
622 | } | |
623 | ||
624 | name = xstrdup (token_buffer); | |
625 | ||
626 | for (;;) | |
627 | { | |
628 | token_t t; | |
629 | int tmp_char = ungetc (skip_white_space (), finput); | |
630 | ||
631 | if (tmp_char == '%') | |
632 | return; | |
633 | if (tmp_char == EOF) | |
634 | fatal (_("Premature EOF after %s"), token_buffer); | |
635 | ||
636 | t = lex (); | |
637 | ||
638 | switch (t) | |
639 | { | |
640 | ||
641 | case tok_comma: | |
642 | case tok_semicolon: | |
643 | break; | |
644 | ||
645 | case tok_identifier: | |
646 | if (symval->type_name == NULL) | |
647 | symval->type_name = name; | |
648 | else if (strcmp (name, symval->type_name) != 0) | |
649 | complain (_("type redeclaration for %s"), symval->tag); | |
650 | ||
651 | break; | |
652 | ||
653 | default: | |
654 | complain (_("invalid %%type declaration due to item: %s"), | |
655 | token_buffer); | |
656 | skip_to_char ('%'); | |
657 | } | |
658 | } | |
659 | } | |
660 | ||
661 | ||
662 | ||
663 | /*----------------------------------------------------------------. | |
664 | | Read in a %left, %right or %nonassoc declaration and record its | | |
665 | | information. | | |
666 | `----------------------------------------------------------------*/ | |
667 | ||
668 | static void | |
669 | parse_assoc_decl (associativity assoc) | |
670 | { | |
671 | char *name = NULL; | |
672 | int prev = 0; | |
673 | ||
674 | lastprec++; /* Assign a new precedence level, never 0. */ | |
675 | ||
676 | for (;;) | |
677 | { | |
678 | token_t t; | |
679 | int tmp_char = ungetc (skip_white_space (), finput); | |
680 | ||
681 | if (tmp_char == '%') | |
682 | return; | |
683 | if (tmp_char == EOF) | |
684 | fatal (_("Premature EOF after %s"), token_buffer); | |
685 | ||
686 | t = lex (); | |
687 | ||
688 | switch (t) | |
689 | { | |
690 | case tok_typename: | |
691 | name = xstrdup (token_buffer); | |
692 | break; | |
693 | ||
694 | case tok_comma: | |
695 | break; | |
696 | ||
697 | case tok_identifier: | |
698 | if (symval->prec != 0) | |
699 | complain (_("redefining precedence of %s"), symval->tag); | |
700 | symval->prec = lastprec; | |
701 | symval->assoc = assoc; | |
702 | if (symval->class == nterm_sym) | |
703 | complain (_("symbol %s redefined"), symval->tag); | |
704 | symval->class = token_sym; | |
705 | if (name) | |
706 | { /* record the type, if one is specified */ | |
707 | if (symval->type_name == NULL) | |
708 | symval->type_name = name; | |
709 | else if (strcmp (name, symval->type_name) != 0) | |
710 | complain (_("type redeclaration for %s"), symval->tag); | |
711 | } | |
712 | break; | |
713 | ||
714 | case tok_number: | |
715 | if (prev == tok_identifier) | |
716 | { | |
717 | symval->user_token_number = numval; | |
718 | } | |
719 | else | |
720 | { | |
721 | complain (_ | |
722 | ("invalid text (%s) - number should be after identifier"), | |
723 | token_buffer); | |
724 | skip_to_char ('%'); | |
725 | } | |
726 | break; | |
727 | ||
728 | case tok_semicolon: | |
729 | return; | |
730 | ||
731 | default: | |
732 | complain (_("unexpected item: %s"), token_buffer); | |
733 | skip_to_char ('%'); | |
734 | } | |
735 | ||
736 | prev = t; | |
737 | } | |
738 | } | |
739 | ||
740 | ||
741 | ||
742 | /*--------------------------------------------------------------. | |
743 | | Copy the union declaration into the stype muscle | | |
744 | | (and fdefines), where it is made into the definition of | | |
745 | | YYSTYPE, the type of elements of the parser value stack. | | |
746 | `--------------------------------------------------------------*/ | |
747 | ||
748 | static void | |
749 | parse_union_decl (void) | |
750 | { | |
751 | int c; | |
752 | int count = 0; | |
753 | struct obstack union_obstack; | |
754 | const char *prologue = "\ | |
755 | #ifndef YYSTYPE\n\ | |
756 | typedef union"; | |
757 | const char *epilogue = "\ | |
758 | yystype;\n\ | |
759 | # define YYSTYPE yystype\n\ | |
760 | #endif\n"; | |
761 | ||
762 | if (typed) | |
763 | complain (_("multiple %s declarations"), "%union"); | |
764 | ||
765 | typed = 1; | |
766 | ||
767 | /* FIXME: I'm worried: are you sure attrs_obstack is properly | |
768 | filled? */ | |
769 | /* I don't see any reasons to keep this line, because we should | |
770 | create a special skeleton for this option. */ | |
771 | if (no_lines_flag) | |
772 | obstack_1grow (&attrs_obstack, '\n'); | |
773 | ||
774 | obstack_init (&union_obstack); | |
775 | obstack_sgrow (&union_obstack, "union"); | |
776 | if (defines_flag) | |
777 | obstack_sgrow (&defines_obstack, prologue); | |
778 | ||
779 | c = getc (finput); | |
780 | ||
781 | while (c != EOF) | |
782 | { | |
783 | /* If C contains '/', it is output by copy_comment (). */ | |
784 | if (c != '/') | |
785 | { | |
786 | obstack_1grow (&union_obstack, c); | |
787 | if (defines_flag) | |
788 | obstack_1grow (&defines_obstack, c); | |
789 | } | |
790 | ||
791 | switch (c) | |
792 | { | |
793 | case '\n': | |
794 | lineno++; | |
795 | break; | |
796 | ||
797 | case '/': | |
798 | copy_comment2 (finput, &defines_obstack, &union_obstack); | |
799 | break; | |
800 | ||
801 | case '{': | |
802 | count++; | |
803 | break; | |
804 | ||
805 | case '}': | |
806 | if (count == 0) | |
807 | complain (_("unmatched %s"), "`}'"); | |
808 | count--; | |
809 | if (count <= 0) | |
810 | { | |
811 | if (defines_flag) | |
812 | obstack_sgrow (&defines_obstack, epilogue); | |
813 | /* JF don't choke on trailing semi */ | |
814 | c = skip_white_space (); | |
815 | if (c != ';') | |
816 | ungetc (c, finput); | |
817 | obstack_1grow (&union_obstack, 0); | |
818 | muscle_insert ("stype", obstack_finish (&union_obstack)); | |
819 | return; | |
820 | } | |
821 | } | |
822 | ||
823 | c = getc (finput); | |
824 | } | |
825 | ||
826 | } | |
827 | ||
828 | ||
829 | /*-------------------------------------------------------. | |
830 | | Parse the declaration %expect N which says to expect N | | |
831 | | shift-reduce conflicts. | | |
832 | `-------------------------------------------------------*/ | |
833 | ||
834 | static void | |
835 | parse_expect_decl (void) | |
836 | { | |
837 | int c = skip_white_space (); | |
838 | ungetc (c, finput); | |
839 | ||
840 | if (!isdigit (c)) | |
841 | complain (_("argument of %%expect is not an integer")); | |
842 | else | |
843 | expected_conflicts = read_signed_integer (finput); | |
844 | } | |
845 | ||
846 | ||
847 | /*-------------------------------------------------------------------. | |
848 | | Parse what comes after %thong. the full syntax is | | |
849 | | | | |
850 | | %thong <type> token number literal | | |
851 | | | | |
852 | | the <type> or number may be omitted. The number specifies the | | |
853 | | user_token_number. | | |
854 | | | | |
855 | | Two symbols are entered in the table, one for the token symbol and | | |
856 | | one for the literal. Both are given the <type>, if any, from the | | |
857 | | declaration. The ->user_token_number of the first is SALIAS and | | |
858 | | the ->user_token_number of the second is set to the number, if | | |
859 | | any, from the declaration. The two symbols are linked via | | |
860 | | pointers in their ->alias fields. | | |
861 | | | | |
862 | | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, | | |
863 | | only the literal string is retained it is the literal string that | | |
864 | | is output to yytname | | |
865 | `-------------------------------------------------------------------*/ | |
866 | ||
867 | static void | |
868 | parse_thong_decl (void) | |
869 | { | |
870 | token_t token; | |
871 | struct bucket *symbol; | |
872 | char *typename = 0; | |
873 | int usrtoknum = SUNDEF; | |
874 | ||
875 | token = lex (); /* fetch typename or first token */ | |
876 | if (token == tok_typename) | |
877 | { | |
878 | typename = xstrdup (token_buffer); | |
879 | value_components_used = 1; | |
880 | token = lex (); /* fetch first token */ | |
881 | } | |
882 | ||
883 | /* process first token */ | |
884 | ||
885 | if (token != tok_identifier) | |
886 | { | |
887 | complain (_("unrecognized item %s, expected an identifier"), | |
888 | token_buffer); | |
889 | skip_to_char ('%'); | |
890 | return; | |
891 | } | |
892 | symval->class = token_sym; | |
893 | symval->type_name = typename; | |
894 | symval->user_token_number = SALIAS; | |
895 | symbol = symval; | |
896 | ||
897 | token = lex (); /* get number or literal string */ | |
898 | ||
899 | if (token == tok_number) | |
900 | { | |
901 | usrtoknum = numval; | |
902 | token = lex (); /* okay, did number, now get literal */ | |
903 | } | |
904 | ||
905 | /* process literal string token */ | |
906 | ||
907 | if (token != tok_identifier || *symval->tag != '\"') | |
908 | { | |
909 | complain (_("expected string constant instead of %s"), token_buffer); | |
910 | skip_to_char ('%'); | |
911 | return; | |
912 | } | |
913 | symval->class = token_sym; | |
914 | symval->type_name = typename; | |
915 | symval->user_token_number = usrtoknum; | |
916 | ||
917 | symval->alias = symbol; | |
918 | symbol->alias = symval; | |
919 | ||
920 | /* symbol and symval combined are only one symbol. */ | |
921 | nsyms--; | |
922 | } | |
923 | ||
924 | static void | |
925 | parse_muscle_decl (void) | |
926 | { | |
927 | int ch = ungetc (skip_white_space (), finput); | |
928 | char* muscle_key; | |
929 | char* muscle_value; | |
930 | ||
931 | /* Read key. */ | |
932 | if (!isalpha (ch) && ch != '_') | |
933 | { | |
934 | complain (_("invalid %s declaration"), "%define"); | |
935 | skip_to_char ('%'); | |
936 | return; | |
937 | } | |
938 | copy_identifier (finput, &muscle_obstack); | |
939 | obstack_1grow (&muscle_obstack, 0); | |
940 | muscle_key = obstack_finish (&muscle_obstack); | |
941 | ||
942 | /* Read value. */ | |
943 | ch = skip_white_space (); | |
944 | if (ch != '"') | |
945 | { | |
946 | ungetc (ch, finput); | |
947 | if (ch != EOF) | |
948 | { | |
949 | complain (_("invalid %s declaration"), "%define"); | |
950 | skip_to_char ('%'); | |
951 | return; | |
952 | } | |
953 | else | |
954 | fatal (_("Premature EOF after %s"), "\""); | |
955 | } | |
956 | copy_string2 (finput, &muscle_obstack, '"', 0); | |
957 | obstack_1grow (&muscle_obstack, 0); | |
958 | muscle_value = obstack_finish (&muscle_obstack); | |
959 | ||
960 | /* Store the (key, value) pair in the environment. */ | |
961 | muscle_insert (muscle_key, muscle_value); | |
962 | } | |
963 | ||
964 | ||
965 | ||
966 | /*---------------------------------. | |
967 | | Parse a double quoted parameter. | | |
968 | `---------------------------------*/ | |
969 | ||
970 | static const char * | |
971 | parse_dquoted_param (const char *from) | |
972 | { | |
973 | struct obstack param_obstack; | |
974 | const char *param = NULL; | |
975 | int c; | |
976 | ||
977 | obstack_init (¶m_obstack); | |
978 | c = skip_white_space (); | |
979 | ||
980 | if (c != '"') | |
981 | { | |
982 | complain (_("invalid %s declaration"), from); | |
983 | ungetc (c, finput); | |
984 | skip_to_char ('%'); | |
985 | return NULL; | |
986 | } | |
987 | ||
988 | for (;;) | |
989 | { | |
990 | if (literalchar (NULL, &c, '\"')) | |
991 | obstack_1grow (¶m_obstack, c); | |
992 | else | |
993 | break; | |
994 | } | |
995 | ||
996 | obstack_1grow (¶m_obstack, '\0'); | |
997 | param = obstack_finish (¶m_obstack); | |
998 | ||
999 | if (c != '"' || strlen (param) == 0) | |
1000 | { | |
1001 | complain (_("invalid %s declaration"), from); | |
1002 | if (c != '"') | |
1003 | ungetc (c, finput); | |
1004 | skip_to_char ('%'); | |
1005 | return NULL; | |
1006 | } | |
1007 | ||
1008 | return param; | |
1009 | } | |
1010 | ||
1011 | /*----------------------------------. | |
1012 | | Parse what comes after %skeleton. | | |
1013 | `----------------------------------*/ | |
1014 | ||
1015 | static void | |
1016 | parse_skel_decl (void) | |
1017 | { | |
1018 | skeleton = parse_dquoted_param ("%skeleton"); | |
1019 | } | |
1020 | ||
1021 | /*----------------------------------------------------------------. | |
1022 | | Read from finput until `%%' is seen. Discard the `%%'. Handle | | |
1023 | | any `%' declarations, and copy the contents of any `%{ ... %}' | | |
1024 | | groups to ATTRS_OBSTACK. | | |
1025 | `----------------------------------------------------------------*/ | |
1026 | ||
1027 | static void | |
1028 | read_declarations (void) | |
1029 | { | |
1030 | for (;;) | |
1031 | { | |
1032 | int c = skip_white_space (); | |
1033 | ||
1034 | if (c == '%') | |
1035 | { | |
1036 | token_t tok = parse_percent_token (); | |
1037 | ||
1038 | switch (tok) | |
1039 | { | |
1040 | case tok_two_percents: | |
1041 | return; | |
1042 | ||
1043 | case tok_percent_left_curly: | |
1044 | copy_definition (); | |
1045 | break; | |
1046 | ||
1047 | case tok_token: | |
1048 | parse_token_decl (token_sym, nterm_sym); | |
1049 | break; | |
1050 | ||
1051 | case tok_nterm: | |
1052 | parse_token_decl (nterm_sym, token_sym); | |
1053 | break; | |
1054 | ||
1055 | case tok_type: | |
1056 | parse_type_decl (); | |
1057 | break; | |
1058 | ||
1059 | case tok_start: | |
1060 | parse_start_decl (); | |
1061 | break; | |
1062 | ||
1063 | case tok_union: | |
1064 | parse_union_decl (); | |
1065 | break; | |
1066 | ||
1067 | case tok_expect: | |
1068 | parse_expect_decl (); | |
1069 | break; | |
1070 | ||
1071 | case tok_thong: | |
1072 | parse_thong_decl (); | |
1073 | break; | |
1074 | ||
1075 | case tok_left: | |
1076 | parse_assoc_decl (left_assoc); | |
1077 | break; | |
1078 | ||
1079 | case tok_right: | |
1080 | parse_assoc_decl (right_assoc); | |
1081 | break; | |
1082 | ||
1083 | case tok_nonassoc: | |
1084 | parse_assoc_decl (non_assoc); | |
1085 | break; | |
1086 | ||
1087 | case tok_define: | |
1088 | parse_muscle_decl (); | |
1089 | break; | |
1090 | ||
1091 | case tok_skel: | |
1092 | parse_skel_decl (); | |
1093 | break; | |
1094 | ||
1095 | case tok_noop: | |
1096 | break; | |
1097 | ||
1098 | case tok_stropt: | |
1099 | case tok_intopt: | |
1100 | case tok_obsolete: | |
1101 | abort (); | |
1102 | break; | |
1103 | ||
1104 | case tok_illegal: | |
1105 | default: | |
1106 | complain (_("unrecognized: %s"), token_buffer); | |
1107 | skip_to_char ('%'); | |
1108 | } | |
1109 | } | |
1110 | else if (c == EOF) | |
1111 | fatal (_("no input grammar")); | |
1112 | else | |
1113 | { | |
1114 | char buf[] = "c"; | |
1115 | buf[0] = c; | |
1116 | complain (_("unknown character: %s"), quote (buf)); | |
1117 | skip_to_char ('%'); | |
1118 | } | |
1119 | } | |
1120 | } | |
1121 | \f | |
1122 | /*-------------------------------------------------------------------. | |
1123 | | Assuming that a `{' has just been seen, copy everything up to the | | |
1124 | | matching `}' into the actions file. STACK_OFFSET is the number of | | |
1125 | | values in the current rule so far, which says where to find `$0' | | |
1126 | | with respect to the top of the stack. | | |
1127 | `-------------------------------------------------------------------*/ | |
1128 | ||
1129 | static void | |
1130 | copy_action (symbol_list *rule, int stack_offset) | |
1131 | { | |
1132 | int c; | |
1133 | int count; | |
1134 | ||
1135 | /* offset is always 0 if parser has already popped the stack pointer */ | |
1136 | if (semantic_parser) | |
1137 | stack_offset = 0; | |
1138 | ||
1139 | obstack_fgrow1 (&action_obstack, "\ncase %d:\n", nrules); | |
1140 | ||
1141 | if (!no_lines_flag) | |
1142 | { | |
1143 | obstack_fgrow2 (&action_obstack, muscle_find ("linef"), | |
1144 | lineno, quotearg_style (c_quoting_style, | |
1145 | muscle_find ("filename"))); | |
1146 | } | |
1147 | obstack_1grow (&action_obstack, '{'); | |
1148 | ||
1149 | count = 1; | |
1150 | c = getc (finput); | |
1151 | ||
1152 | while (count > 0) | |
1153 | { | |
1154 | while (c != '}') | |
1155 | { | |
1156 | switch (c) | |
1157 | { | |
1158 | case '\n': | |
1159 | obstack_1grow (&action_obstack, c); | |
1160 | lineno++; | |
1161 | break; | |
1162 | ||
1163 | case '{': | |
1164 | obstack_1grow (&action_obstack, c); | |
1165 | count++; | |
1166 | break; | |
1167 | ||
1168 | case '\'': | |
1169 | case '"': | |
1170 | copy_string (finput, &action_obstack, c); | |
1171 | break; | |
1172 | ||
1173 | case '/': | |
1174 | copy_comment (finput, &action_obstack); | |
1175 | break; | |
1176 | ||
1177 | case '$': | |
1178 | copy_dollar (finput, &action_obstack, | |
1179 | rule, stack_offset); | |
1180 | break; | |
1181 | ||
1182 | case '@': | |
1183 | copy_at (finput, &action_obstack, | |
1184 | stack_offset); | |
1185 | break; | |
1186 | ||
1187 | case EOF: | |
1188 | fatal (_("unmatched %s"), "`{'"); | |
1189 | ||
1190 | default: | |
1191 | obstack_1grow (&action_obstack, c); | |
1192 | } | |
1193 | ||
1194 | c = getc (finput); | |
1195 | } | |
1196 | ||
1197 | /* above loop exits when c is '}' */ | |
1198 | ||
1199 | if (--count) | |
1200 | { | |
1201 | obstack_1grow (&action_obstack, c); | |
1202 | c = getc (finput); | |
1203 | } | |
1204 | } | |
1205 | ||
1206 | obstack_sgrow (&action_obstack, ";\n break;}"); | |
1207 | } | |
1208 | \f | |
1209 | /*-------------------------------------------------------------------. | |
1210 | | After `%guard' is seen in the input file, copy the actual guard | | |
1211 | | into the guards file. If the guard is followed by an action, copy | | |
1212 | | that into the actions file. STACK_OFFSET is the number of values | | |
1213 | | in the current rule so far, which says where to find `$0' with | | |
1214 | | respect to the top of the stack, for the simple parser in which | | |
1215 | | the stack is not popped until after the guard is run. | | |
1216 | `-------------------------------------------------------------------*/ | |
1217 | ||
1218 | static void | |
1219 | copy_guard (symbol_list *rule, int stack_offset) | |
1220 | { | |
1221 | int c; | |
1222 | int count; | |
1223 | int brace_flag = 0; | |
1224 | ||
1225 | /* offset is always 0 if parser has already popped the stack pointer */ | |
1226 | if (semantic_parser) | |
1227 | stack_offset = 0; | |
1228 | ||
1229 | obstack_fgrow1 (&guard_obstack, "\ncase %d:\n", nrules); | |
1230 | if (!no_lines_flag) | |
1231 | obstack_fgrow2 (&guard_obstack, muscle_find ("linef"), | |
1232 | lineno, quotearg_style (c_quoting_style, | |
1233 | muscle_find ("filename"))); | |
1234 | obstack_1grow (&guard_obstack, '{'); | |
1235 | ||
1236 | count = 0; | |
1237 | c = getc (finput); | |
1238 | ||
1239 | while (brace_flag ? (count > 0) : (c != ';')) | |
1240 | { | |
1241 | switch (c) | |
1242 | { | |
1243 | case '\n': | |
1244 | obstack_1grow (&guard_obstack, c); | |
1245 | lineno++; | |
1246 | break; | |
1247 | ||
1248 | case '{': | |
1249 | obstack_1grow (&guard_obstack, c); | |
1250 | brace_flag = 1; | |
1251 | count++; | |
1252 | break; | |
1253 | ||
1254 | case '}': | |
1255 | obstack_1grow (&guard_obstack, c); | |
1256 | if (count > 0) | |
1257 | count--; | |
1258 | else | |
1259 | { | |
1260 | complain (_("unmatched %s"), "`}'"); | |
1261 | c = getc (finput); /* skip it */ | |
1262 | } | |
1263 | break; | |
1264 | ||
1265 | case '\'': | |
1266 | case '"': | |
1267 | copy_string (finput, &guard_obstack, c); | |
1268 | break; | |
1269 | ||
1270 | case '/': | |
1271 | copy_comment (finput, &guard_obstack); | |
1272 | break; | |
1273 | ||
1274 | case '$': | |
1275 | copy_dollar (finput, &guard_obstack, rule, stack_offset); | |
1276 | break; | |
1277 | ||
1278 | case '@': | |
1279 | copy_at (finput, &guard_obstack, stack_offset); | |
1280 | break; | |
1281 | ||
1282 | case EOF: | |
1283 | fatal ("%s", _("unterminated %guard clause")); | |
1284 | ||
1285 | default: | |
1286 | obstack_1grow (&guard_obstack, c); | |
1287 | } | |
1288 | ||
1289 | if (c != '}' || count != 0) | |
1290 | c = getc (finput); | |
1291 | } | |
1292 | ||
1293 | c = skip_white_space (); | |
1294 | ||
1295 | obstack_sgrow (&guard_obstack, ";\n break;}"); | |
1296 | if (c == '{') | |
1297 | copy_action (rule, stack_offset); | |
1298 | else if (c == '=') | |
1299 | { | |
1300 | c = getc (finput); /* why not skip_white_space -wjh */ | |
1301 | if (c == '{') | |
1302 | copy_action (rule, stack_offset); | |
1303 | } | |
1304 | else | |
1305 | ungetc (c, finput); | |
1306 | } | |
1307 | \f | |
1308 | ||
1309 | /*-------------------------------------------------------------------. | |
1310 | | Generate a dummy symbol, a nonterminal, whose name cannot conflict | | |
1311 | | with the user's names. | | |
1312 | `-------------------------------------------------------------------*/ | |
1313 | ||
1314 | static bucket * | |
1315 | gensym (void) | |
1316 | { | |
1317 | /* Incremented for each generated symbol */ | |
1318 | static int gensym_count = 0; | |
1319 | static char buf[256]; | |
1320 | ||
1321 | bucket *sym; | |
1322 | ||
1323 | sprintf (buf, "@%d", ++gensym_count); | |
1324 | token_buffer = buf; | |
1325 | sym = getsym (token_buffer); | |
1326 | sym->class = nterm_sym; | |
1327 | sym->value = nvars++; | |
1328 | return sym; | |
1329 | } | |
1330 | ||
1331 | #if 0 | |
1332 | /*------------------------------------------------------------------. | |
1333 | | read in a %type declaration and record its information for | | |
1334 | | get_type_name to access. This is unused. It is only called from | | |
1335 | | the #if 0 part of readgram | | |
1336 | `------------------------------------------------------------------*/ | |
1337 | ||
1338 | static int | |
1339 | get_type (void) | |
1340 | { | |
1341 | int k; | |
1342 | token_t token; | |
1343 | char *name; | |
1344 | ||
1345 | token = lex (); | |
1346 | ||
1347 | if (token != tok_typename) | |
1348 | { | |
1349 | complain (_("invalid %s declaration"), "%type"); | |
1350 | return t; | |
1351 | } | |
1352 | ||
1353 | name = xstrdup (token_buffer); | |
1354 | ||
1355 | for (;;) | |
1356 | { | |
1357 | token = lex (); | |
1358 | ||
1359 | switch (token) | |
1360 | { | |
1361 | case tok_semicolon: | |
1362 | return lex (); | |
1363 | ||
1364 | case tok_comma: | |
1365 | break; | |
1366 | ||
1367 | case tok_identifier: | |
1368 | if (symval->type_name == NULL) | |
1369 | symval->type_name = name; | |
1370 | else if (strcmp (name, symval->type_name) != 0) | |
1371 | complain (_("type redeclaration for %s"), symval->tag); | |
1372 | ||
1373 | break; | |
1374 | ||
1375 | default: | |
1376 | return token; | |
1377 | } | |
1378 | } | |
1379 | } | |
1380 | ||
1381 | #endif | |
1382 | \f | |
1383 | /*------------------------------------------------------------------. | |
1384 | | Parse the input grammar into a one symbol_list structure. Each | | |
1385 | | rule is represented by a sequence of symbols: the left hand side | | |
1386 | | followed by the contents of the right hand side, followed by a | | |
1387 | | null pointer instead of a symbol to terminate the rule. The next | | |
1388 | | symbol is the lhs of the following rule. | | |
1389 | | | | |
1390 | | All guards and actions are copied out to the appropriate files, | | |
1391 | | labelled by the rule number they apply to. | | |
1392 | `------------------------------------------------------------------*/ | |
1393 | ||
1394 | static void | |
1395 | readgram (void) | |
1396 | { | |
1397 | token_t t; | |
1398 | bucket *lhs = NULL; | |
1399 | symbol_list *p; | |
1400 | symbol_list *p1; | |
1401 | bucket *bp; | |
1402 | ||
1403 | /* Points to first symbol_list of current rule. its symbol is the | |
1404 | lhs of the rule. */ | |
1405 | symbol_list *crule; | |
1406 | /* Points to the symbol_list preceding crule. */ | |
1407 | symbol_list *crule1; | |
1408 | ||
1409 | p1 = NULL; | |
1410 | ||
1411 | t = lex (); | |
1412 | ||
1413 | while (t != tok_two_percents && t != tok_eof) | |
1414 | { | |
1415 | if (t == tok_identifier || t == tok_bar) | |
1416 | { | |
1417 | int action_flag = 0; | |
1418 | /* Number of symbols in rhs of this rule so far */ | |
1419 | int rulelength = 0; | |
1420 | int xactions = 0; /* JF for error checking */ | |
1421 | bucket *first_rhs = 0; | |
1422 | ||
1423 | if (t == tok_identifier) | |
1424 | { | |
1425 | lhs = symval; | |
1426 | ||
1427 | if (!start_flag) | |
1428 | { | |
1429 | startval = lhs; | |
1430 | start_flag = 1; | |
1431 | } | |
1432 | ||
1433 | t = lex (); | |
1434 | if (t != tok_colon) | |
1435 | { | |
1436 | complain (_("ill-formed rule: initial symbol not followed by colon")); | |
1437 | unlex (t); | |
1438 | } | |
1439 | } | |
1440 | ||
1441 | if (nrules == 0 && t == tok_bar) | |
1442 | { | |
1443 | complain (_("grammar starts with vertical bar")); | |
1444 | lhs = symval; /* BOGUS: use a random symval */ | |
1445 | } | |
1446 | /* start a new rule and record its lhs. */ | |
1447 | ||
1448 | nrules++; | |
1449 | nitems++; | |
1450 | ||
1451 | p = symbol_list_new (lhs); | |
1452 | ||
1453 | crule1 = p1; | |
1454 | if (p1) | |
1455 | p1->next = p; | |
1456 | else | |
1457 | grammar = p; | |
1458 | ||
1459 | p1 = p; | |
1460 | crule = p; | |
1461 | ||
1462 | /* mark the rule's lhs as a nonterminal if not already so. */ | |
1463 | ||
1464 | if (lhs->class == unknown_sym) | |
1465 | { | |
1466 | lhs->class = nterm_sym; | |
1467 | lhs->value = nvars; | |
1468 | nvars++; | |
1469 | } | |
1470 | else if (lhs->class == token_sym) | |
1471 | complain (_("rule given for %s, which is a token"), lhs->tag); | |
1472 | ||
1473 | /* read the rhs of the rule. */ | |
1474 | ||
1475 | for (;;) | |
1476 | { | |
1477 | t = lex (); | |
1478 | if (t == tok_prec) | |
1479 | { | |
1480 | t = lex (); | |
1481 | crule->ruleprec = symval; | |
1482 | t = lex (); | |
1483 | } | |
1484 | ||
1485 | if (!(t == tok_identifier || t == tok_left_curly)) | |
1486 | break; | |
1487 | ||
1488 | /* If next token is an identifier, see if a colon follows it. | |
1489 | If one does, exit this rule now. */ | |
1490 | if (t == tok_identifier) | |
1491 | { | |
1492 | bucket *ssave; | |
1493 | token_t t1; | |
1494 | ||
1495 | ssave = symval; | |
1496 | t1 = lex (); | |
1497 | unlex (t1); | |
1498 | symval = ssave; | |
1499 | if (t1 == tok_colon) | |
1500 | break; | |
1501 | ||
1502 | if (!first_rhs) /* JF */ | |
1503 | first_rhs = symval; | |
1504 | /* Not followed by colon => | |
1505 | process as part of this rule's rhs. */ | |
1506 | } | |
1507 | ||
1508 | /* If we just passed an action, that action was in the middle | |
1509 | of a rule, so make a dummy rule to reduce it to a | |
1510 | non-terminal. */ | |
1511 | if (action_flag) | |
1512 | { | |
1513 | /* Since the action was written out with this rule's | |
1514 | number, we must give the new rule this number by | |
1515 | inserting the new rule before it. */ | |
1516 | ||
1517 | /* Make a dummy nonterminal, a gensym. */ | |
1518 | bucket *sdummy = gensym (); | |
1519 | ||
1520 | /* Make a new rule, whose body is empty, before the | |
1521 | current one, so that the action just read can | |
1522 | belong to it. */ | |
1523 | nrules++; | |
1524 | nitems++; | |
1525 | p = symbol_list_new (sdummy); | |
1526 | /* Attach its lineno to that of the host rule. */ | |
1527 | p->line = crule->line; | |
1528 | if (crule1) | |
1529 | crule1->next = p; | |
1530 | else | |
1531 | grammar = p; | |
1532 | /* End of the rule. */ | |
1533 | crule1 = symbol_list_new (NULL); | |
1534 | crule1->next = crule; | |
1535 | ||
1536 | p->next = crule1; | |
1537 | ||
1538 | /* Insert the dummy generated by that rule into this | |
1539 | rule. */ | |
1540 | nitems++; | |
1541 | p = symbol_list_new (sdummy); | |
1542 | p1->next = p; | |
1543 | p1 = p; | |
1544 | ||
1545 | action_flag = 0; | |
1546 | } | |
1547 | ||
1548 | if (t == tok_identifier) | |
1549 | { | |
1550 | nitems++; | |
1551 | p = symbol_list_new (symval); | |
1552 | p1->next = p; | |
1553 | p1 = p; | |
1554 | } | |
1555 | else /* handle an action. */ | |
1556 | { | |
1557 | copy_action (crule, rulelength); | |
1558 | action_flag = 1; | |
1559 | xactions++; /* JF */ | |
1560 | } | |
1561 | rulelength++; | |
1562 | } /* end of read rhs of rule */ | |
1563 | ||
1564 | /* Put an empty link in the list to mark the end of this rule */ | |
1565 | p = symbol_list_new (NULL); | |
1566 | p1->next = p; | |
1567 | p1 = p; | |
1568 | ||
1569 | if (t == tok_prec) | |
1570 | { | |
1571 | complain (_("two @prec's in a row")); | |
1572 | t = lex (); | |
1573 | crule->ruleprec = symval; | |
1574 | t = lex (); | |
1575 | } | |
1576 | if (t == tok_guard) | |
1577 | { | |
1578 | if (!semantic_parser) | |
1579 | complain (_("%%guard present but %%semantic_parser not specified")); | |
1580 | ||
1581 | copy_guard (crule, rulelength); | |
1582 | t = lex (); | |
1583 | } | |
1584 | else if (t == tok_left_curly) | |
1585 | { | |
1586 | /* This case never occurs -wjh */ | |
1587 | if (action_flag) | |
1588 | complain (_("two actions at end of one rule")); | |
1589 | copy_action (crule, rulelength); | |
1590 | action_flag = 1; | |
1591 | xactions++; /* -wjh */ | |
1592 | t = lex (); | |
1593 | } | |
1594 | /* If $$ is being set in default way, report if any type | |
1595 | mismatch. */ | |
1596 | else if (!xactions | |
1597 | && first_rhs && lhs->type_name != first_rhs->type_name) | |
1598 | { | |
1599 | if (lhs->type_name == 0 | |
1600 | || first_rhs->type_name == 0 | |
1601 | || strcmp (lhs->type_name, first_rhs->type_name)) | |
1602 | complain (_("type clash (`%s' `%s') on default action"), | |
1603 | lhs->type_name ? lhs->type_name : "", | |
1604 | first_rhs->type_name ? first_rhs->type_name : ""); | |
1605 | } | |
1606 | /* Warn if there is no default for $$ but we need one. */ | |
1607 | else if (!xactions && !first_rhs && lhs->type_name != 0) | |
1608 | complain (_("empty rule for typed nonterminal, and no action")); | |
1609 | if (t == tok_semicolon) | |
1610 | t = lex (); | |
1611 | } | |
1612 | #if 0 | |
1613 | /* these things can appear as alternatives to rules. */ | |
1614 | /* NO, they cannot. | |
1615 | a) none of the documentation allows them | |
1616 | b) most of them scan forward until finding a next % | |
1617 | thus they may swallow lots of intervening rules | |
1618 | */ | |
1619 | else if (t == tok_token) | |
1620 | { | |
1621 | parse_token_decl (token_sym, nterm_sym); | |
1622 | t = lex (); | |
1623 | } | |
1624 | else if (t == tok_nterm) | |
1625 | { | |
1626 | parse_token_decl (nterm_sym, token_sym); | |
1627 | t = lex (); | |
1628 | } | |
1629 | else if (t == tok_type) | |
1630 | { | |
1631 | t = get_type (); | |
1632 | } | |
1633 | else if (t == tok_union) | |
1634 | { | |
1635 | parse_union_decl (); | |
1636 | t = lex (); | |
1637 | } | |
1638 | else if (t == tok_expect) | |
1639 | { | |
1640 | parse_expect_decl (); | |
1641 | t = lex (); | |
1642 | } | |
1643 | else if (t == tok_start) | |
1644 | { | |
1645 | parse_start_decl (); | |
1646 | t = lex (); | |
1647 | } | |
1648 | #endif | |
1649 | ||
1650 | else | |
1651 | { | |
1652 | complain (_("invalid input: %s"), quote (token_buffer)); | |
1653 | t = lex (); | |
1654 | } | |
1655 | } | |
1656 | ||
1657 | /* grammar has been read. Do some checking */ | |
1658 | ||
1659 | if (nsyms > MAXSHORT) | |
1660 | fatal (_("too many symbols (tokens plus nonterminals); maximum %d"), | |
1661 | MAXSHORT); | |
1662 | if (nrules == 0) | |
1663 | fatal (_("no rules in the input grammar")); | |
1664 | ||
1665 | /* Report any undefined symbols and consider them nonterminals. */ | |
1666 | ||
1667 | for (bp = firstsymbol; bp; bp = bp->next) | |
1668 | if (bp->class == unknown_sym) | |
1669 | { | |
1670 | complain (_ | |
1671 | ("symbol %s is used, but is not defined as a token and has no rules"), | |
1672 | bp->tag); | |
1673 | bp->class = nterm_sym; | |
1674 | bp->value = nvars++; | |
1675 | } | |
1676 | ||
1677 | ntokens = nsyms - nvars; | |
1678 | } | |
1679 | ||
1680 | /* At the end of the grammar file, some C source code must | |
1681 | be stored. It is going to be associated to the epilogue | |
1682 | directive. */ | |
1683 | static void | |
1684 | read_additionnal_code (void) | |
1685 | { | |
1686 | char c; | |
1687 | struct obstack el_obstack; | |
1688 | ||
1689 | obstack_init (&el_obstack); | |
1690 | ||
1691 | while ((c = getc (finput)) != EOF) | |
1692 | obstack_1grow (&el_obstack, c); | |
1693 | ||
1694 | obstack_1grow (&el_obstack, 0); | |
1695 | muscle_insert ("epilogue", obstack_finish (&el_obstack)); | |
1696 | } | |
1697 | ||
1698 | \f | |
1699 | /*--------------------------------------------------------------. | |
1700 | | For named tokens, but not literal ones, define the name. The | | |
1701 | | value is the user token number. | | |
1702 | `--------------------------------------------------------------*/ | |
1703 | ||
1704 | static void | |
1705 | output_token_defines (struct obstack *oout) | |
1706 | { | |
1707 | bucket *bp; | |
1708 | char *cp, *symbol; | |
1709 | char c; | |
1710 | ||
1711 | for (bp = firstsymbol; bp; bp = bp->next) | |
1712 | { | |
1713 | symbol = bp->tag; /* get symbol */ | |
1714 | ||
1715 | if (bp->value >= ntokens) | |
1716 | continue; | |
1717 | if (bp->user_token_number == SALIAS) | |
1718 | continue; | |
1719 | if ('\'' == *symbol) | |
1720 | continue; /* skip literal character */ | |
1721 | if (bp == errtoken) | |
1722 | continue; /* skip error token */ | |
1723 | if ('\"' == *symbol) | |
1724 | { | |
1725 | /* use literal string only if given a symbol with an alias */ | |
1726 | if (bp->alias) | |
1727 | symbol = bp->alias->tag; | |
1728 | else | |
1729 | continue; | |
1730 | } | |
1731 | ||
1732 | /* Don't #define nonliteral tokens whose names contain periods. */ | |
1733 | cp = symbol; | |
1734 | while ((c = *cp++) && c != '.'); | |
1735 | if (c != '\0') | |
1736 | continue; | |
1737 | ||
1738 | obstack_fgrow2 (oout, "# define\t%s\t%d\n", | |
1739 | symbol, bp->user_token_number); | |
1740 | if (semantic_parser) | |
1741 | /* FIXME: This is certainly dead wrong, and should be just as | |
1742 | above. --akim. */ | |
1743 | obstack_fgrow2 (oout, "# define\tT%s\t%d\n", symbol, bp->value); | |
1744 | } | |
1745 | } | |
1746 | ||
1747 | ||
1748 | /*------------------------------------------------------------------. | |
1749 | | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same | | |
1750 | | number. | | |
1751 | `------------------------------------------------------------------*/ | |
1752 | ||
1753 | static void | |
1754 | token_translations_init (void) | |
1755 | { | |
1756 | bucket *bp = NULL; | |
1757 | int i; | |
1758 | ||
1759 | token_translations = XCALLOC (short, max_user_token_number + 1); | |
1760 | ||
1761 | /* Initialize all entries for literal tokens to 2, the internal | |
1762 | token number for $undefined., which represents all invalid | |
1763 | inputs. */ | |
1764 | for (i = 0; i <= max_user_token_number; i++) | |
1765 | token_translations[i] = 2; | |
1766 | ||
1767 | for (bp = firstsymbol; bp; bp = bp->next) | |
1768 | { | |
1769 | /* Non-terminal? */ | |
1770 | if (bp->value >= ntokens) | |
1771 | continue; | |
1772 | /* A token string alias? */ | |
1773 | if (bp->user_token_number == SALIAS) | |
1774 | continue; | |
1775 | ||
1776 | assert (bp->user_token_number != SUNDEF); | |
1777 | ||
1778 | /* A token which translation has already been set? */ | |
1779 | if (token_translations[bp->user_token_number] != 2) | |
1780 | complain (_("tokens %s and %s both assigned number %d"), | |
1781 | tags[token_translations[bp->user_token_number]], | |
1782 | bp->tag, bp->user_token_number); | |
1783 | token_translations[bp->user_token_number] = bp->value; | |
1784 | } | |
1785 | } | |
1786 | ||
1787 | ||
1788 | /*------------------------------------------------------------------. | |
1789 | | Assign symbol numbers, and write definition of token names into | | |
1790 | | FDEFINES. Set up vectors TAGS and SPREC of names and precedences | | |
1791 | | of symbols. | | |
1792 | `------------------------------------------------------------------*/ | |
1793 | ||
1794 | static void | |
1795 | packsymbols (void) | |
1796 | { | |
1797 | bucket *bp = NULL; | |
1798 | int tokno = 1; | |
1799 | int last_user_token_number; | |
1800 | static char DOLLAR[] = "$"; | |
1801 | ||
1802 | tags = XCALLOC (char *, nsyms + 1); | |
1803 | user_toknums = XCALLOC (short, nsyms + 1); | |
1804 | ||
1805 | sprec = XCALLOC (short, nsyms); | |
1806 | sassoc = XCALLOC (short, nsyms); | |
1807 | ||
1808 | /* The EOF token. */ | |
1809 | tags[0] = DOLLAR; | |
1810 | user_toknums[0] = 0; | |
1811 | ||
1812 | max_user_token_number = 256; | |
1813 | last_user_token_number = 256; | |
1814 | ||
1815 | for (bp = firstsymbol; bp; bp = bp->next) | |
1816 | { | |
1817 | if (bp->class == nterm_sym) | |
1818 | { | |
1819 | bp->value += ntokens; | |
1820 | } | |
1821 | else if (bp->alias) | |
1822 | { | |
1823 | /* this symbol and its alias are a single token defn. | |
1824 | allocate a tokno, and assign to both check agreement of | |
1825 | ->prec and ->assoc fields and make both the same */ | |
1826 | if (bp->value == 0) | |
1827 | bp->value = bp->alias->value = tokno++; | |
1828 | ||
1829 | if (bp->prec != bp->alias->prec) | |
1830 | { | |
1831 | if (bp->prec != 0 && bp->alias->prec != 0 | |
1832 | && bp->user_token_number == SALIAS) | |
1833 | complain (_("conflicting precedences for %s and %s"), | |
1834 | bp->tag, bp->alias->tag); | |
1835 | if (bp->prec != 0) | |
1836 | bp->alias->prec = bp->prec; | |
1837 | else | |
1838 | bp->prec = bp->alias->prec; | |
1839 | } | |
1840 | ||
1841 | if (bp->assoc != bp->alias->assoc) | |
1842 | { | |
1843 | if (bp->assoc != 0 && bp->alias->assoc != 0 | |
1844 | && bp->user_token_number == SALIAS) | |
1845 | complain (_("conflicting assoc values for %s and %s"), | |
1846 | bp->tag, bp->alias->tag); | |
1847 | if (bp->assoc != 0) | |
1848 | bp->alias->assoc = bp->assoc; | |
1849 | else | |
1850 | bp->assoc = bp->alias->assoc; | |
1851 | } | |
1852 | ||
1853 | if (bp->user_token_number == SALIAS) | |
1854 | continue; /* do not do processing below for SALIASs */ | |
1855 | ||
1856 | } | |
1857 | else /* bp->class == token_sym */ | |
1858 | { | |
1859 | bp->value = tokno++; | |
1860 | } | |
1861 | ||
1862 | if (bp->class == token_sym) | |
1863 | { | |
1864 | if (bp->user_token_number == SUNDEF) | |
1865 | bp->user_token_number = ++last_user_token_number; | |
1866 | if (bp->user_token_number > max_user_token_number) | |
1867 | max_user_token_number = bp->user_token_number; | |
1868 | } | |
1869 | ||
1870 | tags[bp->value] = bp->tag; | |
1871 | user_toknums[bp->value] = bp->user_token_number; | |
1872 | sprec[bp->value] = bp->prec; | |
1873 | sassoc[bp->value] = bp->assoc; | |
1874 | } | |
1875 | ||
1876 | token_translations_init (); | |
1877 | ||
1878 | error_token_number = errtoken->value; | |
1879 | ||
1880 | if (startval->class == unknown_sym) | |
1881 | fatal (_("the start symbol %s is undefined"), startval->tag); | |
1882 | else if (startval->class == token_sym) | |
1883 | fatal (_("the start symbol %s is a token"), startval->tag); | |
1884 | ||
1885 | start_symbol = startval->value; | |
1886 | } | |
1887 | ||
1888 | ||
1889 | /*-----------------------------------. | |
1890 | | Output definition of token names. | | |
1891 | `-----------------------------------*/ | |
1892 | ||
1893 | static void | |
1894 | symbols_output (void) | |
1895 | { | |
1896 | { | |
1897 | struct obstack tokendefs; | |
1898 | obstack_init (&tokendefs); | |
1899 | output_token_defines (&tokendefs); | |
1900 | obstack_1grow (&tokendefs, 0); | |
1901 | muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs))); | |
1902 | obstack_free (&tokendefs, NULL); | |
1903 | } | |
1904 | ||
1905 | if (defines_flag) | |
1906 | { | |
1907 | output_token_defines (&defines_obstack); | |
1908 | ||
1909 | if (!pure_parser) | |
1910 | obstack_fgrow1 (&defines_obstack, "\nextern YYSTYPE %slval;\n", | |
1911 | spec_name_prefix); | |
1912 | if (semantic_parser) | |
1913 | { | |
1914 | int i; | |
1915 | ||
1916 | for (i = ntokens; i < nsyms; i++) | |
1917 | { | |
1918 | /* don't make these for dummy nonterminals made by gensym. */ | |
1919 | if (*tags[i] != '@') | |
1920 | obstack_fgrow2 (&defines_obstack, | |
1921 | "# define\tNT%s\t%d\n", tags[i], i); | |
1922 | } | |
1923 | #if 0 | |
1924 | /* `fdefines' is now a temporary file, so we need to copy its | |
1925 | contents in `done', so we can't close it here. */ | |
1926 | fclose (fdefines); | |
1927 | fdefines = NULL; | |
1928 | #endif | |
1929 | } | |
1930 | } | |
1931 | } | |
1932 | ||
1933 | ||
1934 | /*---------------------------------------------------------------. | |
1935 | | Convert the rules into the representation using RRHS, RLHS and | | |
1936 | | RITEMS. | | |
1937 | `---------------------------------------------------------------*/ | |
1938 | ||
1939 | static void | |
1940 | packgram (void) | |
1941 | { | |
1942 | int itemno; | |
1943 | int ruleno; | |
1944 | symbol_list *p; | |
1945 | ||
1946 | ritem = XCALLOC (short, nitems + 1); | |
1947 | rule_table = XCALLOC (rule_t, nrules) - 1; | |
1948 | ||
1949 | itemno = 0; | |
1950 | ruleno = 1; | |
1951 | ||
1952 | p = grammar; | |
1953 | while (p) | |
1954 | { | |
1955 | bucket *ruleprec = p->ruleprec; | |
1956 | rule_table[ruleno].lhs = p->sym->value; | |
1957 | rule_table[ruleno].rhs = itemno; | |
1958 | rule_table[ruleno].line = p->line; | |
1959 | rule_table[ruleno].useful = TRUE; | |
1960 | ||
1961 | p = p->next; | |
1962 | while (p && p->sym) | |
1963 | { | |
1964 | ritem[itemno++] = p->sym->value; | |
1965 | /* A rule gets by default the precedence and associativity | |
1966 | of the last token in it. */ | |
1967 | if (p->sym->class == token_sym) | |
1968 | { | |
1969 | rule_table[ruleno].prec = p->sym->prec; | |
1970 | rule_table[ruleno].assoc = p->sym->assoc; | |
1971 | } | |
1972 | if (p) | |
1973 | p = p->next; | |
1974 | } | |
1975 | ||
1976 | /* If this rule has a %prec, | |
1977 | the specified symbol's precedence replaces the default. */ | |
1978 | if (ruleprec) | |
1979 | { | |
1980 | rule_table[ruleno].prec = ruleprec->prec; | |
1981 | rule_table[ruleno].assoc = ruleprec->assoc; | |
1982 | rule_table[ruleno].precsym = ruleprec->value; | |
1983 | } | |
1984 | ||
1985 | ritem[itemno++] = -ruleno; | |
1986 | ruleno++; | |
1987 | ||
1988 | if (p) | |
1989 | p = p->next; | |
1990 | } | |
1991 | ||
1992 | ritem[itemno] = 0; | |
1993 | ||
1994 | if (trace_flag) | |
1995 | ritem_print (stderr); | |
1996 | } | |
1997 | \f | |
1998 | /*-------------------------------------------------------------------. | |
1999 | | Read in the grammar specification and record it in the format | | |
2000 | | described in gram.h. All guards are copied into the GUARD_OBSTACK | | |
2001 | | and all actions into ACTION_OBSTACK, in each case forming the body | | |
2002 | | of a C function (YYGUARD or YYACTION) which contains a switch | | |
2003 | | statement to decide which guard or action to execute. | | |
2004 | `-------------------------------------------------------------------*/ | |
2005 | ||
2006 | void | |
2007 | reader (void) | |
2008 | { | |
2009 | start_flag = 0; | |
2010 | startval = NULL; /* start symbol not specified yet. */ | |
2011 | ||
2012 | nsyms = 1; | |
2013 | nvars = 0; | |
2014 | nrules = 0; | |
2015 | nitems = 0; | |
2016 | ||
2017 | typed = 0; | |
2018 | lastprec = 0; | |
2019 | ||
2020 | semantic_parser = 0; | |
2021 | pure_parser = 0; | |
2022 | ||
2023 | grammar = NULL; | |
2024 | ||
2025 | lex_init (); | |
2026 | lineno = 1; | |
2027 | ||
2028 | /* Initialize the muscle obstack. */ | |
2029 | obstack_init (&muscle_obstack); | |
2030 | ||
2031 | /* Initialize the symbol table. */ | |
2032 | tabinit (); | |
2033 | ||
2034 | /* Construct the error token */ | |
2035 | errtoken = getsym ("error"); | |
2036 | errtoken->class = token_sym; | |
2037 | errtoken->user_token_number = 256; /* Value specified by POSIX. */ | |
2038 | ||
2039 | /* Construct a token that represents all undefined literal tokens. | |
2040 | It is always token number 2. */ | |
2041 | undeftoken = getsym ("$undefined."); | |
2042 | undeftoken->class = token_sym; | |
2043 | undeftoken->user_token_number = 2; | |
2044 | ||
2045 | /* Read the declaration section. Copy %{ ... %} groups to | |
2046 | TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left, | |
2047 | etc. found there. */ | |
2048 | read_declarations (); | |
2049 | /* Read in the grammar, build grammar in list form. Write out | |
2050 | guards and actions. */ | |
2051 | readgram (); | |
2052 | /* Some C code is given at the end of the grammar file. */ | |
2053 | read_additionnal_code (); | |
2054 | ||
2055 | /* Now we know whether we need the line-number stack. If we do, | |
2056 | write its type into the .tab.h file. | |
2057 | This is no longer need with header skeleton. */ | |
2058 | ||
2059 | /* Assign the symbols their symbol numbers. Write #defines for the | |
2060 | token symbols into FDEFINES if requested. */ | |
2061 | packsymbols (); | |
2062 | /* Convert the grammar into the format described in gram.h. */ | |
2063 | packgram (); | |
2064 | /* Output the headers. */ | |
2065 | symbols_output (); | |
2066 | } |