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