]> git.saurik.com Git - bison.git/blob - src/reader.c
Still making progress in separating Bison into (i) input, (ii)
[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 struct obstack union_obstack;
757 const char *prologue = "\
758 #ifndef YYSTYPE\n\
759 typedef union";
760 const char *epilogue = "\
761 yystype;\n\
762 # define YYSTYPE yystype\n\
763 #endif\n";
764
765 if (typed)
766 complain (_("multiple %s declarations"), "%union");
767
768 typed = 1;
769
770 /* FIXME: I'm worried: are you sure attrs_obstack is properly
771 filled? */
772 /* I don't see any reasons to keep this line, because we should
773 create a special skeleton for this option. */
774 if (no_lines_flag)
775 obstack_1grow (&attrs_obstack, '\n');
776
777 obstack_init (&union_obstack);
778 obstack_sgrow (&union_obstack, "union");
779 if (defines_flag)
780 obstack_sgrow (&defines_obstack, prologue);
781
782 c = getc (finput);
783
784 while (c != EOF)
785 {
786 /* If C contains '/', it is output by copy_comment (). */
787 if (c != '/')
788 {
789 obstack_1grow (&union_obstack, c);
790 if (defines_flag)
791 obstack_1grow (&defines_obstack, c);
792 }
793
794 switch (c)
795 {
796 case '\n':
797 lineno++;
798 break;
799
800 case '/':
801 copy_comment2 (finput, &defines_obstack, &union_obstack);
802 break;
803
804 case '{':
805 count++;
806 break;
807
808 case '}':
809 if (count == 0)
810 complain (_("unmatched %s"), "`}'");
811 count--;
812 if (count <= 0)
813 {
814 if (defines_flag)
815 obstack_sgrow (&defines_obstack, epilogue);
816 /* JF don't choke on trailing semi */
817 c = skip_white_space ();
818 if (c != ';')
819 ungetc (c, finput);
820 obstack_1grow (&union_obstack, 0);
821 muscle_insert ("stype", obstack_finish (&union_obstack));
822 return;
823 }
824 }
825
826 c = getc (finput);
827 }
828
829 }
830
831
832 /*-------------------------------------------------------.
833 | Parse the declaration %expect N which says to expect N |
834 | shift-reduce conflicts. |
835 `-------------------------------------------------------*/
836
837 static void
838 parse_expect_decl (void)
839 {
840 int c = skip_white_space ();
841 ungetc (c, finput);
842
843 if (!isdigit (c))
844 complain (_("argument of %%expect is not an integer"));
845 else
846 expected_conflicts = read_signed_integer (finput);
847 }
848
849
850 /*-------------------------------------------------------------------.
851 | Parse what comes after %thong. the full syntax is |
852 | |
853 | %thong <type> token number literal |
854 | |
855 | the <type> or number may be omitted. The number specifies the |
856 | user_token_number. |
857 | |
858 | Two symbols are entered in the table, one for the token symbol and |
859 | one for the literal. Both are given the <type>, if any, from the |
860 | declaration. The ->user_token_number of the first is SALIAS and |
861 | the ->user_token_number of the second is set to the number, if |
862 | any, from the declaration. The two symbols are linked via |
863 | pointers in their ->alias fields. |
864 | |
865 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
866 | only the literal string is retained it is the literal string that |
867 | is output to yytname |
868 `-------------------------------------------------------------------*/
869
870 static void
871 parse_thong_decl (void)
872 {
873 token_t token;
874 struct bucket *symbol;
875 char *typename = 0;
876 int usrtoknum = SUNDEF;
877
878 token = lex (); /* fetch typename or first token */
879 if (token == tok_typename)
880 {
881 typename = xstrdup (token_buffer);
882 value_components_used = 1;
883 token = lex (); /* fetch first token */
884 }
885
886 /* process first token */
887
888 if (token != tok_identifier)
889 {
890 complain (_("unrecognized item %s, expected an identifier"),
891 token_buffer);
892 skip_to_char ('%');
893 return;
894 }
895 symval->class = token_sym;
896 symval->type_name = typename;
897 symval->user_token_number = SALIAS;
898 symbol = symval;
899
900 token = lex (); /* get number or literal string */
901
902 if (token == tok_number)
903 {
904 usrtoknum = numval;
905 token = lex (); /* okay, did number, now get literal */
906 }
907
908 /* process literal string token */
909
910 if (token != tok_identifier || *symval->tag != '\"')
911 {
912 complain (_("expected string constant instead of %s"), token_buffer);
913 skip_to_char ('%');
914 return;
915 }
916 symval->class = token_sym;
917 symval->type_name = typename;
918 symval->user_token_number = usrtoknum;
919
920 symval->alias = symbol;
921 symbol->alias = symval;
922
923 /* symbol and symval combined are only one symbol. */
924 nsyms--;
925 }
926
927 static void
928 parse_muscle_decl (void)
929 {
930 int ch = ungetc (skip_white_space (), finput);
931 char* muscle_key;
932 char* muscle_value;
933
934 /* Read key. */
935 if (!isalpha (ch) && ch != '_')
936 {
937 complain (_("invalid %s declaration"), "%define");
938 skip_to_char ('%');
939 return;
940 }
941 copy_identifier (finput, &muscle_obstack);
942 obstack_1grow (&muscle_obstack, 0);
943 muscle_key = obstack_finish (&muscle_obstack);
944
945 /* Read value. */
946 ch = skip_white_space ();
947 if (ch != '"')
948 {
949 ungetc (ch, finput);
950 if (ch != EOF)
951 {
952 complain (_("invalid %s declaration"), "%define");
953 skip_to_char ('%');
954 return;
955 }
956 else
957 fatal (_("Premature EOF after %s"), "\"");
958 }
959 copy_string2 (finput, &muscle_obstack, '"', 0);
960 obstack_1grow (&muscle_obstack, 0);
961 muscle_value = obstack_finish (&muscle_obstack);
962
963 /* Store the (key, value) pair in the environment. */
964 muscle_insert (muscle_key, muscle_value);
965 }
966
967
968
969 /*---------------------------------.
970 | Parse a double quoted parameter. |
971 `---------------------------------*/
972
973 static const char *
974 parse_dquoted_param (const char *from)
975 {
976 struct obstack param_obstack;
977 const char *param = NULL;
978 int c;
979
980 obstack_init (&param_obstack);
981 c = skip_white_space ();
982
983 if (c != '"')
984 {
985 complain (_("invalid %s declaration"), from);
986 ungetc (c, finput);
987 skip_to_char ('%');
988 return NULL;
989 }
990
991 while ((c = literalchar ()) != '"')
992 obstack_1grow (&param_obstack, c);
993
994 obstack_1grow (&param_obstack, '\0');
995 param = obstack_finish (&param_obstack);
996
997 if (c != '"' || strlen (param) == 0)
998 {
999 complain (_("invalid %s declaration"), from);
1000 if (c != '"')
1001 ungetc (c, finput);
1002 skip_to_char ('%');
1003 return NULL;
1004 }
1005
1006 return param;
1007 }
1008
1009 /*----------------------------------.
1010 | Parse what comes after %skeleton. |
1011 `----------------------------------*/
1012
1013 static void
1014 parse_skel_decl (void)
1015 {
1016 skeleton = parse_dquoted_param ("%skeleton");
1017 }
1018
1019 /*----------------------------------------------------------------.
1020 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
1021 | any `%' declarations, and copy the contents of any `%{ ... %}' |
1022 | groups to ATTRS_OBSTACK. |
1023 `----------------------------------------------------------------*/
1024
1025 static void
1026 read_declarations (void)
1027 {
1028 for (;;)
1029 {
1030 int c = skip_white_space ();
1031
1032 if (c == '%')
1033 {
1034 token_t tok = parse_percent_token ();
1035
1036 switch (tok)
1037 {
1038 case tok_two_percents:
1039 return;
1040
1041 case tok_percent_left_curly:
1042 copy_definition ();
1043 break;
1044
1045 case tok_token:
1046 parse_token_decl (token_sym, nterm_sym);
1047 break;
1048
1049 case tok_nterm:
1050 parse_token_decl (nterm_sym, token_sym);
1051 break;
1052
1053 case tok_type:
1054 parse_type_decl ();
1055 break;
1056
1057 case tok_start:
1058 parse_start_decl ();
1059 break;
1060
1061 case tok_union:
1062 parse_union_decl ();
1063 break;
1064
1065 case tok_expect:
1066 parse_expect_decl ();
1067 break;
1068
1069 case tok_thong:
1070 parse_thong_decl ();
1071 break;
1072
1073 case tok_left:
1074 parse_assoc_decl (left_assoc);
1075 break;
1076
1077 case tok_right:
1078 parse_assoc_decl (right_assoc);
1079 break;
1080
1081 case tok_nonassoc:
1082 parse_assoc_decl (non_assoc);
1083 break;
1084
1085 case tok_define:
1086 parse_muscle_decl ();
1087 break;
1088
1089 case tok_skel:
1090 parse_skel_decl ();
1091 break;
1092
1093 case tok_noop:
1094 break;
1095
1096 case tok_stropt:
1097 case tok_intopt:
1098 case tok_obsolete:
1099 abort ();
1100 break;
1101
1102 case tok_illegal:
1103 default:
1104 complain (_("unrecognized: %s"), token_buffer);
1105 skip_to_char ('%');
1106 }
1107 }
1108 else if (c == EOF)
1109 fatal (_("no input grammar"));
1110 else
1111 {
1112 char buf[] = "c";
1113 buf[0] = c;
1114 complain (_("unknown character: %s"), quote (buf));
1115 skip_to_char ('%');
1116 }
1117 }
1118 }
1119 \f
1120 /*-------------------------------------------------------------------.
1121 | Assuming that a `{' has just been seen, copy everything up to the |
1122 | matching `}' into the actions file. STACK_OFFSET is the number of |
1123 | values in the current rule so far, which says where to find `$0' |
1124 | with respect to the top of the stack. |
1125 `-------------------------------------------------------------------*/
1126
1127 static void
1128 copy_action (symbol_list *rule, int stack_offset)
1129 {
1130 int c;
1131 int count;
1132
1133 /* offset is always 0 if parser has already popped the stack pointer */
1134 if (semantic_parser)
1135 stack_offset = 0;
1136
1137 count = 1;
1138 c = getc (finput);
1139
1140 while (count > 0)
1141 {
1142 while (c != '}')
1143 {
1144 switch (c)
1145 {
1146 case '\n':
1147 obstack_1grow (&action_obstack, c);
1148 lineno++;
1149 break;
1150
1151 case '{':
1152 obstack_1grow (&action_obstack, c);
1153 count++;
1154 break;
1155
1156 case '\'':
1157 case '"':
1158 copy_string (finput, &action_obstack, c);
1159 break;
1160
1161 case '/':
1162 copy_comment (finput, &action_obstack);
1163 break;
1164
1165 case '$':
1166 copy_dollar (finput, &action_obstack,
1167 rule, stack_offset);
1168 break;
1169
1170 case '@':
1171 copy_at (finput, &action_obstack,
1172 stack_offset);
1173 break;
1174
1175 case EOF:
1176 fatal (_("unmatched %s"), "`{'");
1177
1178 default:
1179 obstack_1grow (&action_obstack, c);
1180 }
1181
1182 c = getc (finput);
1183 }
1184
1185 /* above loop exits when c is '}' */
1186
1187 if (--count)
1188 {
1189 obstack_1grow (&action_obstack, c);
1190 c = getc (finput);
1191 }
1192 }
1193
1194 obstack_1grow (&action_obstack, '\0');
1195 rule->action = obstack_finish (&action_obstack);
1196 rule->action_line = lineno;
1197 }
1198 \f
1199 /*-------------------------------------------------------------------.
1200 | After `%guard' is seen in the input file, copy the actual guard |
1201 | into the guards file. If the guard is followed by an action, copy |
1202 | that into the actions file. STACK_OFFSET is the number of values |
1203 | in the current rule so far, which says where to find `$0' with |
1204 | respect to the top of the stack, for the simple parser in which |
1205 | the stack is not popped until after the guard is run. |
1206 `-------------------------------------------------------------------*/
1207
1208 static void
1209 copy_guard (symbol_list *rule, int stack_offset)
1210 {
1211 int c;
1212 int count;
1213 int brace_flag = 0;
1214
1215 /* offset is always 0 if parser has already popped the stack pointer */
1216 if (semantic_parser)
1217 stack_offset = 0;
1218
1219 obstack_fgrow1 (&guard_obstack, "\ncase %d:\n", nrules);
1220 if (!no_lines_flag)
1221 obstack_fgrow2 (&guard_obstack, muscle_find ("linef"),
1222 lineno, quotearg_style (c_quoting_style,
1223 muscle_find ("filename")));
1224 obstack_1grow (&guard_obstack, '{');
1225
1226 count = 0;
1227 c = getc (finput);
1228
1229 while (brace_flag ? (count > 0) : (c != ';'))
1230 {
1231 switch (c)
1232 {
1233 case '\n':
1234 obstack_1grow (&guard_obstack, c);
1235 lineno++;
1236 break;
1237
1238 case '{':
1239 obstack_1grow (&guard_obstack, c);
1240 brace_flag = 1;
1241 count++;
1242 break;
1243
1244 case '}':
1245 obstack_1grow (&guard_obstack, c);
1246 if (count > 0)
1247 count--;
1248 else
1249 {
1250 complain (_("unmatched %s"), "`}'");
1251 c = getc (finput); /* skip it */
1252 }
1253 break;
1254
1255 case '\'':
1256 case '"':
1257 copy_string (finput, &guard_obstack, c);
1258 break;
1259
1260 case '/':
1261 copy_comment (finput, &guard_obstack);
1262 break;
1263
1264 case '$':
1265 copy_dollar (finput, &guard_obstack, rule, stack_offset);
1266 break;
1267
1268 case '@':
1269 copy_at (finput, &guard_obstack, stack_offset);
1270 break;
1271
1272 case EOF:
1273 fatal ("%s", _("unterminated %guard clause"));
1274
1275 default:
1276 obstack_1grow (&guard_obstack, c);
1277 }
1278
1279 if (c != '}' || count != 0)
1280 c = getc (finput);
1281 }
1282
1283 c = skip_white_space ();
1284
1285 obstack_sgrow (&guard_obstack, ";\n break;}");
1286 if (c == '{')
1287 copy_action (rule, stack_offset);
1288 else if (c == '=')
1289 {
1290 c = getc (finput); /* why not skip_white_space -wjh */
1291 if (c == '{')
1292 copy_action (rule, stack_offset);
1293 }
1294 else
1295 ungetc (c, finput);
1296 }
1297 \f
1298
1299 /*-------------------------------------------------------------------.
1300 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1301 | with the user's names. |
1302 `-------------------------------------------------------------------*/
1303
1304 static bucket *
1305 gensym (void)
1306 {
1307 /* Incremented for each generated symbol */
1308 static int gensym_count = 0;
1309 static char buf[256];
1310
1311 bucket *sym;
1312
1313 sprintf (buf, "@%d", ++gensym_count);
1314 token_buffer = buf;
1315 sym = getsym (token_buffer);
1316 sym->class = nterm_sym;
1317 sym->value = nvars++;
1318 return sym;
1319 }
1320 \f
1321 /*-------------------------------------------------------------------.
1322 | Parse the input grammar into a one symbol_list structure. Each |
1323 | rule is represented by a sequence of symbols: the left hand side |
1324 | followed by the contents of the right hand side, followed by a |
1325 | null pointer instead of a symbol to terminate the rule. The next |
1326 | symbol is the lhs of the following rule. |
1327 | |
1328 | All guards and actions are copied out to the appropriate files, |
1329 | labelled by the rule number they apply to. |
1330 | |
1331 | Bison used to allow some %directives in the rules sections, but |
1332 | this is no longer consider appropriate: (i) the documented grammar |
1333 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1334 | recovery for %directives consists in skipping the junk until a `%' |
1335 | is seen and helrp synchronizing. This scheme is definitely wrong |
1336 | in the rules section. |
1337 `-------------------------------------------------------------------*/
1338
1339 static void
1340 readgram (void)
1341 {
1342 token_t t;
1343 bucket *lhs = NULL;
1344 symbol_list *p = NULL;
1345 symbol_list *p1 = NULL;
1346 bucket *bp;
1347
1348 /* Points to first symbol_list of current rule. its symbol is the
1349 lhs of the rule. */
1350 symbol_list *crule = NULL;
1351 /* Points to the symbol_list preceding crule. */
1352 symbol_list *crule1 = NULL;
1353
1354 t = lex ();
1355
1356 while (t != tok_two_percents && t != tok_eof)
1357 if (t == tok_identifier || t == tok_bar)
1358 {
1359 int action_flag = 0;
1360 /* Number of symbols in rhs of this rule so far */
1361 int rulelength = 0;
1362 int xactions = 0; /* JF for error checking */
1363 bucket *first_rhs = 0;
1364
1365 if (t == tok_identifier)
1366 {
1367 lhs = symval;
1368
1369 if (!start_flag)
1370 {
1371 startval = lhs;
1372 start_flag = 1;
1373 }
1374
1375 t = lex ();
1376 if (t != tok_colon)
1377 {
1378 complain (_("ill-formed rule: initial symbol not followed by colon"));
1379 unlex (t);
1380 }
1381 }
1382
1383 if (nrules == 0 && t == tok_bar)
1384 {
1385 complain (_("grammar starts with vertical bar"));
1386 lhs = symval; /* BOGUS: use a random symval */
1387 }
1388 /* start a new rule and record its lhs. */
1389
1390 nrules++;
1391 nitems++;
1392
1393 p = symbol_list_new (lhs);
1394
1395 crule1 = p1;
1396 if (p1)
1397 p1->next = p;
1398 else
1399 grammar = p;
1400
1401 p1 = p;
1402 crule = p;
1403
1404 /* mark the rule's lhs as a nonterminal if not already so. */
1405
1406 if (lhs->class == unknown_sym)
1407 {
1408 lhs->class = nterm_sym;
1409 lhs->value = nvars;
1410 nvars++;
1411 }
1412 else if (lhs->class == token_sym)
1413 complain (_("rule given for %s, which is a token"), lhs->tag);
1414
1415 /* read the rhs of the rule. */
1416
1417 for (;;)
1418 {
1419 t = lex ();
1420 if (t == tok_prec)
1421 {
1422 t = lex ();
1423 crule->ruleprec = symval;
1424 t = lex ();
1425 }
1426
1427 if (!(t == tok_identifier || t == tok_left_curly))
1428 break;
1429
1430 /* If next token is an identifier, see if a colon follows it.
1431 If one does, exit this rule now. */
1432 if (t == tok_identifier)
1433 {
1434 bucket *ssave;
1435 token_t t1;
1436
1437 ssave = symval;
1438 t1 = lex ();
1439 unlex (t1);
1440 symval = ssave;
1441 if (t1 == tok_colon)
1442 break;
1443
1444 if (!first_rhs) /* JF */
1445 first_rhs = symval;
1446 /* Not followed by colon =>
1447 process as part of this rule's rhs. */
1448 }
1449
1450 /* If we just passed an action, that action was in the middle
1451 of a rule, so make a dummy rule to reduce it to a
1452 non-terminal. */
1453 if (action_flag)
1454 {
1455 /* Since the action was written out with this rule's
1456 number, we must give the new rule this number by
1457 inserting the new rule before it. */
1458
1459 /* Make a dummy nonterminal, a gensym. */
1460 bucket *sdummy = gensym ();
1461
1462 /* Make a new rule, whose body is empty, before the
1463 current one, so that the action just read can
1464 belong to it. */
1465 nrules++;
1466 nitems++;
1467 p = symbol_list_new (sdummy);
1468 /* Attach its lineno to that of the host rule. */
1469 p->line = crule->line;
1470 if (crule1)
1471 crule1->next = p;
1472 else
1473 grammar = p;
1474 /* End of the rule. */
1475 crule1 = symbol_list_new (NULL);
1476 crule1->next = crule;
1477
1478 p->next = crule1;
1479
1480 /* Insert the dummy generated by that rule into this
1481 rule. */
1482 nitems++;
1483 p = symbol_list_new (sdummy);
1484 p1->next = p;
1485 p1 = p;
1486
1487 action_flag = 0;
1488 }
1489
1490 if (t == tok_identifier)
1491 {
1492 nitems++;
1493 p = symbol_list_new (symval);
1494 p1->next = p;
1495 p1 = p;
1496 }
1497 else /* handle an action. */
1498 {
1499 copy_action (crule, rulelength);
1500 action_flag = 1;
1501 xactions++; /* JF */
1502 }
1503 rulelength++;
1504 } /* end of read rhs of rule */
1505
1506 /* Put an empty link in the list to mark the end of this rule */
1507 p = symbol_list_new (NULL);
1508 p1->next = p;
1509 p1 = p;
1510
1511 if (t == tok_prec)
1512 {
1513 complain (_("two @prec's in a row"));
1514 t = lex ();
1515 crule->ruleprec = symval;
1516 t = lex ();
1517 }
1518 if (t == tok_guard)
1519 {
1520 if (!semantic_parser)
1521 complain (_("%%guard present but %%semantic_parser not specified"));
1522
1523 copy_guard (crule, rulelength);
1524 t = lex ();
1525 }
1526 else if (t == tok_left_curly)
1527 {
1528 /* This case never occurs -wjh */
1529 if (action_flag)
1530 complain (_("two actions at end of one rule"));
1531 copy_action (crule, rulelength);
1532 action_flag = 1;
1533 xactions++; /* -wjh */
1534 t = lex ();
1535 }
1536 /* If $$ is being set in default way, report if any type
1537 mismatch. */
1538 else if (!xactions
1539 && first_rhs && lhs->type_name != first_rhs->type_name)
1540 {
1541 if (lhs->type_name == 0
1542 || first_rhs->type_name == 0
1543 || strcmp (lhs->type_name, first_rhs->type_name))
1544 complain (_("type clash (`%s' `%s') on default action"),
1545 lhs->type_name ? lhs->type_name : "",
1546 first_rhs->type_name ? first_rhs->type_name : "");
1547 }
1548 /* Warn if there is no default for $$ but we need one. */
1549 else if (!xactions && !first_rhs && lhs->type_name != 0)
1550 complain (_("empty rule for typed nonterminal, and no action"));
1551 if (t == tok_semicolon)
1552 t = lex ();
1553 }
1554 else
1555 {
1556 complain (_("invalid input: %s"), quote (token_buffer));
1557 t = lex ();
1558 }
1559
1560
1561 /* grammar has been read. Do some checking */
1562
1563 if (nsyms > MAXSHORT)
1564 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1565 MAXSHORT);
1566 if (nrules == 0)
1567 fatal (_("no rules in the input grammar"));
1568
1569 /* Report any undefined symbols and consider them nonterminals. */
1570
1571 for (bp = firstsymbol; bp; bp = bp->next)
1572 if (bp->class == unknown_sym)
1573 {
1574 complain (_
1575 ("symbol %s is used, but is not defined as a token and has no rules"),
1576 bp->tag);
1577 bp->class = nterm_sym;
1578 bp->value = nvars++;
1579 }
1580
1581 ntokens = nsyms - nvars;
1582 }
1583
1584 /* At the end of the grammar file, some C source code must
1585 be stored. It is going to be associated to the epilogue
1586 directive. */
1587 static void
1588 read_additionnal_code (void)
1589 {
1590 char c;
1591 struct obstack el_obstack;
1592
1593 obstack_init (&el_obstack);
1594
1595 if (!no_lines_flag)
1596 {
1597 obstack_fgrow2 (&el_obstack, muscle_find ("linef"),
1598 lineno, quotearg_style (c_quoting_style,
1599 muscle_find("filename")));
1600 }
1601
1602 while ((c = getc (finput)) != EOF)
1603 obstack_1grow (&el_obstack, c);
1604
1605 obstack_1grow (&el_obstack, 0);
1606 muscle_insert ("epilogue", obstack_finish (&el_obstack));
1607 }
1608
1609 \f
1610 /*--------------------------------------------------------------.
1611 | For named tokens, but not literal ones, define the name. The |
1612 | value is the user token number. |
1613 `--------------------------------------------------------------*/
1614
1615 static void
1616 output_token_defines (struct obstack *oout)
1617 {
1618 bucket *bp;
1619 char *cp, *symbol;
1620 char c;
1621
1622 for (bp = firstsymbol; bp; bp = bp->next)
1623 {
1624 symbol = bp->tag; /* get symbol */
1625
1626 if (bp->value >= ntokens)
1627 continue;
1628 if (bp->user_token_number == SALIAS)
1629 continue;
1630 if ('\'' == *symbol)
1631 continue; /* skip literal character */
1632 if (bp == errtoken)
1633 continue; /* skip error token */
1634 if ('\"' == *symbol)
1635 {
1636 /* use literal string only if given a symbol with an alias */
1637 if (bp->alias)
1638 symbol = bp->alias->tag;
1639 else
1640 continue;
1641 }
1642
1643 /* Don't #define nonliteral tokens whose names contain periods. */
1644 cp = symbol;
1645 while ((c = *cp++) && c != '.');
1646 if (c != '\0')
1647 continue;
1648
1649 obstack_fgrow2 (oout, "# define\t%s\t%d\n",
1650 symbol, bp->user_token_number);
1651 if (semantic_parser)
1652 /* FIXME: This is certainly dead wrong, and should be just as
1653 above. --akim. */
1654 obstack_fgrow2 (oout, "# define\tT%s\t%d\n", symbol, bp->value);
1655 }
1656 }
1657
1658
1659 /*------------------------------------------------------------------.
1660 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1661 | number. |
1662 `------------------------------------------------------------------*/
1663
1664 static void
1665 token_translations_init (void)
1666 {
1667 bucket *bp = NULL;
1668 int i;
1669
1670 token_translations = XCALLOC (short, max_user_token_number + 1);
1671
1672 /* Initialize all entries for literal tokens to 2, the internal
1673 token number for $undefined., which represents all invalid
1674 inputs. */
1675 for (i = 0; i <= max_user_token_number; i++)
1676 token_translations[i] = 2;
1677
1678 for (bp = firstsymbol; bp; bp = bp->next)
1679 {
1680 /* Non-terminal? */
1681 if (bp->value >= ntokens)
1682 continue;
1683 /* A token string alias? */
1684 if (bp->user_token_number == SALIAS)
1685 continue;
1686
1687 assert (bp->user_token_number != SUNDEF);
1688
1689 /* A token which translation has already been set? */
1690 if (token_translations[bp->user_token_number] != 2)
1691 complain (_("tokens %s and %s both assigned number %d"),
1692 tags[token_translations[bp->user_token_number]],
1693 bp->tag, bp->user_token_number);
1694 token_translations[bp->user_token_number] = bp->value;
1695 }
1696 }
1697
1698
1699 /*------------------------------------------------------------------.
1700 | Assign symbol numbers, and write definition of token names into |
1701 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1702 | of symbols. |
1703 `------------------------------------------------------------------*/
1704
1705 static void
1706 packsymbols (void)
1707 {
1708 bucket *bp = NULL;
1709 int tokno = 1;
1710 int last_user_token_number;
1711 static char DOLLAR[] = "$";
1712
1713 tags = XCALLOC (char *, nsyms + 1);
1714 user_toknums = XCALLOC (short, nsyms + 1);
1715
1716 sprec = XCALLOC (short, nsyms);
1717 sassoc = XCALLOC (short, nsyms);
1718
1719 /* The EOF token. */
1720 tags[0] = DOLLAR;
1721 user_toknums[0] = 0;
1722
1723 max_user_token_number = 256;
1724 last_user_token_number = 256;
1725
1726 for (bp = firstsymbol; bp; bp = bp->next)
1727 {
1728 if (bp->class == nterm_sym)
1729 {
1730 bp->value += ntokens;
1731 }
1732 else if (bp->alias)
1733 {
1734 /* this symbol and its alias are a single token defn.
1735 allocate a tokno, and assign to both check agreement of
1736 ->prec and ->assoc fields and make both the same */
1737 if (bp->value == 0)
1738 bp->value = bp->alias->value = tokno++;
1739
1740 if (bp->prec != bp->alias->prec)
1741 {
1742 if (bp->prec != 0 && bp->alias->prec != 0
1743 && bp->user_token_number == SALIAS)
1744 complain (_("conflicting precedences for %s and %s"),
1745 bp->tag, bp->alias->tag);
1746 if (bp->prec != 0)
1747 bp->alias->prec = bp->prec;
1748 else
1749 bp->prec = bp->alias->prec;
1750 }
1751
1752 if (bp->assoc != bp->alias->assoc)
1753 {
1754 if (bp->assoc != 0 && bp->alias->assoc != 0
1755 && bp->user_token_number == SALIAS)
1756 complain (_("conflicting assoc values for %s and %s"),
1757 bp->tag, bp->alias->tag);
1758 if (bp->assoc != 0)
1759 bp->alias->assoc = bp->assoc;
1760 else
1761 bp->assoc = bp->alias->assoc;
1762 }
1763
1764 if (bp->user_token_number == SALIAS)
1765 continue; /* do not do processing below for SALIASs */
1766
1767 }
1768 else /* bp->class == token_sym */
1769 {
1770 bp->value = tokno++;
1771 }
1772
1773 if (bp->class == token_sym)
1774 {
1775 if (bp->user_token_number == SUNDEF)
1776 bp->user_token_number = ++last_user_token_number;
1777 if (bp->user_token_number > max_user_token_number)
1778 max_user_token_number = bp->user_token_number;
1779 }
1780
1781 tags[bp->value] = bp->tag;
1782 user_toknums[bp->value] = bp->user_token_number;
1783 sprec[bp->value] = bp->prec;
1784 sassoc[bp->value] = bp->assoc;
1785 }
1786
1787 token_translations_init ();
1788
1789 error_token_number = errtoken->value;
1790
1791 if (startval->class == unknown_sym)
1792 fatal (_("the start symbol %s is undefined"), startval->tag);
1793 else if (startval->class == token_sym)
1794 fatal (_("the start symbol %s is a token"), startval->tag);
1795
1796 start_symbol = startval->value;
1797 }
1798
1799
1800 /*-----------------------------------.
1801 | Output definition of token names. |
1802 `-----------------------------------*/
1803
1804 static void
1805 symbols_output (void)
1806 {
1807 {
1808 struct obstack tokendefs;
1809 obstack_init (&tokendefs);
1810 output_token_defines (&tokendefs);
1811 obstack_1grow (&tokendefs, 0);
1812 muscle_insert ("tokendef", xstrdup (obstack_finish (&tokendefs)));
1813 obstack_free (&tokendefs, NULL);
1814 }
1815
1816 if (defines_flag)
1817 {
1818 output_token_defines (&defines_obstack);
1819
1820 if (!pure_parser)
1821 obstack_fgrow1 (&defines_obstack, "\nextern YYSTYPE %slval;\n",
1822 spec_name_prefix);
1823 if (semantic_parser)
1824 {
1825 int i;
1826
1827 for (i = ntokens; i < nsyms; i++)
1828 {
1829 /* don't make these for dummy nonterminals made by gensym. */
1830 if (*tags[i] != '@')
1831 obstack_fgrow2 (&defines_obstack,
1832 "# define\tNT%s\t%d\n", tags[i], i);
1833 }
1834 #if 0
1835 /* `fdefines' is now a temporary file, so we need to copy its
1836 contents in `done', so we can't close it here. */
1837 fclose (fdefines);
1838 fdefines = NULL;
1839 #endif
1840 }
1841 }
1842 }
1843
1844
1845 /*---------------------------------------------------------------.
1846 | Convert the rules into the representation using RRHS, RLHS and |
1847 | RITEMS. |
1848 `---------------------------------------------------------------*/
1849
1850 static void
1851 packgram (void)
1852 {
1853 int itemno;
1854 int ruleno;
1855 symbol_list *p;
1856
1857 ritem = XCALLOC (short, nitems + 1);
1858 rule_table = XCALLOC (rule_t, nrules) - 1;
1859
1860 itemno = 0;
1861 ruleno = 1;
1862
1863 p = grammar;
1864 while (p)
1865 {
1866 bucket *ruleprec = p->ruleprec;
1867 rule_table[ruleno].lhs = p->sym->value;
1868 rule_table[ruleno].rhs = itemno;
1869 rule_table[ruleno].line = p->line;
1870 rule_table[ruleno].useful = TRUE;
1871 rule_table[ruleno].action = p->action;
1872 rule_table[ruleno].action_line = p->action_line;
1873
1874 p = p->next;
1875 while (p && p->sym)
1876 {
1877 ritem[itemno++] = p->sym->value;
1878 /* A rule gets by default the precedence and associativity
1879 of the last token in it. */
1880 if (p->sym->class == token_sym)
1881 {
1882 rule_table[ruleno].prec = p->sym->prec;
1883 rule_table[ruleno].assoc = p->sym->assoc;
1884 }
1885 if (p)
1886 p = p->next;
1887 }
1888
1889 /* If this rule has a %prec,
1890 the specified symbol's precedence replaces the default. */
1891 if (ruleprec)
1892 {
1893 rule_table[ruleno].prec = ruleprec->prec;
1894 rule_table[ruleno].assoc = ruleprec->assoc;
1895 rule_table[ruleno].precsym = ruleprec->value;
1896 }
1897
1898 ritem[itemno++] = -ruleno;
1899 ruleno++;
1900
1901 if (p)
1902 p = p->next;
1903 }
1904
1905 ritem[itemno] = 0;
1906
1907 if (trace_flag)
1908 ritem_print (stderr);
1909 }
1910 \f
1911 /*-------------------------------------------------------------------.
1912 | Read in the grammar specification and record it in the format |
1913 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1914 | and all actions into ACTION_OBSTACK, in each case forming the body |
1915 | of a C function (YYGUARD or YYACTION) which contains a switch |
1916 | statement to decide which guard or action to execute. |
1917 `-------------------------------------------------------------------*/
1918
1919 void
1920 reader (void)
1921 {
1922 start_flag = 0;
1923 startval = NULL; /* start symbol not specified yet. */
1924
1925 nsyms = 1;
1926 nvars = 0;
1927 nrules = 0;
1928 nitems = 0;
1929
1930 typed = 0;
1931 lastprec = 0;
1932
1933 semantic_parser = 0;
1934 pure_parser = 0;
1935
1936 grammar = NULL;
1937
1938 lex_init ();
1939 lineno = 1;
1940
1941 /* Initialize the muscle obstack. */
1942 obstack_init (&muscle_obstack);
1943
1944 /* Initialize the symbol table. */
1945 tabinit ();
1946
1947 /* Construct the error token */
1948 errtoken = getsym ("error");
1949 errtoken->class = token_sym;
1950 errtoken->user_token_number = 256; /* Value specified by POSIX. */
1951
1952 /* Construct a token that represents all undefined literal tokens.
1953 It is always token number 2. */
1954 undeftoken = getsym ("$undefined.");
1955 undeftoken->class = token_sym;
1956 undeftoken->user_token_number = 2;
1957
1958 /* Read the declaration section. Copy %{ ... %} groups to
1959 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1960 etc. found there. */
1961 read_declarations ();
1962 /* Read in the grammar, build grammar in list form. Write out
1963 guards and actions. */
1964 readgram ();
1965 /* Some C code is given at the end of the grammar file. */
1966 read_additionnal_code ();
1967
1968 /* Now we know whether we need the line-number stack. If we do,
1969 write its type into the .tab.h file.
1970 This is no longer need with header skeleton. */
1971
1972 /* Assign the symbols their symbol numbers. Write #defines for the
1973 token symbols into FDEFINES if requested. */
1974 packsymbols ();
1975 /* Convert the grammar into the format described in gram.h. */
1976 packgram ();
1977 /* Output the headers. */
1978 symbols_output ();
1979 }