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