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