]> git.saurik.com Git - bison.git/blob - src/reader.c
6c7a9eec7699ceadbe1184c4a121594f18f97b33
[bison.git] / src / reader.c
1 /* Input parser for bison
2 Copyright 1984, 1986, 1989, 1992, 1998, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include "system.h"
24 #include "obstack.h"
25 #include "quotearg.h"
26 #include "quote.h"
27 #include "getargs.h"
28 #include "files.h"
29 #include "symtab.h"
30 #include "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 = 0;
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
856 /* process literal string token */
857
858 if (token != tok_identifier || *symval->tag != '\"')
859 {
860 complain (_("expected string constant instead of %s"), token_buffer);
861 skip_to_char ('%');
862 return;
863 }
864 symval->class = token_sym;
865 symval->type_name = typename;
866 symval->user_token_number = usrtoknum;
867
868 symval->alias = symbol;
869 symbol->alias = symval;
870
871 /* symbol and symval combined are only one symbol. */
872 nsyms--;
873 }
874
875
876 /*------------------------------------------------------------------.
877 | Parse a double quoted parameter. It was used for |
878 | %{source,header}_extension. For the moment, It is not used since |
879 | extension features have been removed. |
880 `------------------------------------------------------------------*/
881
882 #if 0
883
884 static const char *
885 parse_dquoted_param (const char *from)
886 {
887 char buff[32];
888 int c;
889 int i;
890
891 c = skip_white_space ();
892
893 if (c != '"')
894 {
895 ungetc (c, finput);
896 complain (_("invalid %s declaration"), from);
897 return NULL;
898 }
899
900 c = getc (finput);
901 for (i = 0; (c >= '!') && (c <= '~'); i++)
902 {
903 if (c == '"')
904 break;
905
906 if (c == '\\')
907 {
908 c = getc (finput);
909 if ((c < '!') && (c > '~'))
910 break;
911 }
912
913 buff[i] = c;
914 c = getc (finput);
915 }
916 buff[i] = '\0';
917
918 if (c != '"')
919 {
920 ungetc (c, finput);
921 complain (_("invalid %s declaration"), from);
922 return NULL;
923 }
924
925 return xstrdup (buff);
926 }
927
928 #endif
929
930
931 /*----------------------------------------------------------------.
932 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
933 | any `%' declarations, and copy the contents of any `%{ ... %}' |
934 | groups to ATTRS_OBSTACK. |
935 `----------------------------------------------------------------*/
936
937 static void
938 read_declarations (void)
939 {
940 int c;
941 int tok;
942
943 for (;;)
944 {
945 c = skip_white_space ();
946
947 if (c == '%')
948 {
949 tok = parse_percent_token ();
950
951 switch (tok)
952 {
953 case tok_two_percents:
954 return;
955
956 case tok_percent_left_curly:
957 copy_definition ();
958 break;
959
960 case tok_token:
961 parse_token_decl (token_sym, nterm_sym);
962 break;
963
964 case tok_nterm:
965 parse_token_decl (nterm_sym, token_sym);
966 break;
967
968 case tok_type:
969 parse_type_decl ();
970 break;
971
972 case tok_start:
973 parse_start_decl ();
974 break;
975
976 case tok_union:
977 parse_union_decl ();
978 break;
979
980 case tok_expect:
981 parse_expect_decl ();
982 break;
983
984 case tok_thong:
985 parse_thong_decl ();
986 break;
987
988 case tok_left:
989 parse_assoc_decl (left_assoc);
990 break;
991
992 case tok_right:
993 parse_assoc_decl (right_assoc);
994 break;
995
996 case tok_nonassoc:
997 parse_assoc_decl (non_assoc);
998 break;
999
1000 case tok_noop:
1001 break;
1002
1003 default:
1004 complain (_("unrecognized: %s"), token_buffer);
1005 skip_to_char ('%');
1006 }
1007 }
1008 else if (c == EOF)
1009 fatal (_("no input grammar"));
1010 else
1011 {
1012 char buf[] = "c";
1013 buf[0] = c;
1014 complain (_("unknown character: %s"), quote (buf));
1015 skip_to_char ('%');
1016 }
1017 }
1018 }
1019 \f
1020 /*-------------------------------------------------------------------.
1021 | Assuming that a `{' has just been seen, copy everything up to the |
1022 | matching `}' into the actions file. STACK_OFFSET is the number of |
1023 | values in the current rule so far, which says where to find `$0' |
1024 | with respect to the top of the stack. |
1025 `-------------------------------------------------------------------*/
1026
1027 static void
1028 copy_action (symbol_list *rule, int stack_offset)
1029 {
1030 int c;
1031 int count;
1032 char buf[4096];
1033
1034 /* offset is always 0 if parser has already popped the stack pointer */
1035 if (semantic_parser)
1036 stack_offset = 0;
1037
1038 sprintf (buf, "\ncase %d:\n", nrules);
1039 obstack_grow (&action_obstack, buf, strlen (buf));
1040
1041 if (!no_lines_flag)
1042 {
1043 sprintf (buf, "#line %d %s\n",
1044 lineno, quotearg_style (c_quoting_style, infile));
1045 obstack_grow (&action_obstack, buf, strlen (buf));
1046 }
1047 obstack_1grow (&action_obstack, '{');
1048
1049 count = 1;
1050 c = getc (finput);
1051
1052 while (count > 0)
1053 {
1054 while (c != '}')
1055 {
1056 switch (c)
1057 {
1058 case '\n':
1059 obstack_1grow (&action_obstack, c);
1060 lineno++;
1061 break;
1062
1063 case '{':
1064 obstack_1grow (&action_obstack, c);
1065 count++;
1066 break;
1067
1068 case '\'':
1069 case '"':
1070 copy_string (finput, &action_obstack, c);
1071 break;
1072
1073 case '/':
1074 copy_comment (finput, &action_obstack);
1075 break;
1076
1077 case '$':
1078 copy_dollar (finput, &action_obstack,
1079 rule, stack_offset);
1080 break;
1081
1082 case '@':
1083 copy_at (finput, &action_obstack,
1084 stack_offset);
1085 break;
1086
1087 case EOF:
1088 fatal (_("unmatched %s"), "`{'");
1089
1090 default:
1091 obstack_1grow (&action_obstack, c);
1092 }
1093
1094 c = getc (finput);
1095 }
1096
1097 /* above loop exits when c is '}' */
1098
1099 if (--count)
1100 {
1101 obstack_1grow (&action_obstack, c);
1102 c = getc (finput);
1103 }
1104 }
1105
1106 obstack_sgrow (&action_obstack, ";\n break;}");
1107 }
1108 \f
1109 /*-------------------------------------------------------------------.
1110 | After `%guard' is seen in the input file, copy the actual guard |
1111 | into the guards file. If the guard is followed by an action, copy |
1112 | that into the actions file. STACK_OFFSET is the number of values |
1113 | in the current rule so far, which says where to find `$0' with |
1114 | respect to the top of the stack, for the simple parser in which |
1115 | the stack is not popped until after the guard is run. |
1116 `-------------------------------------------------------------------*/
1117
1118 static void
1119 copy_guard (symbol_list *rule, int stack_offset)
1120 {
1121 int c;
1122 int count;
1123 int brace_flag = 0;
1124
1125 /* offset is always 0 if parser has already popped the stack pointer */
1126 if (semantic_parser)
1127 stack_offset = 0;
1128
1129 obstack_fgrow1 (&guard_obstack, "\ncase %d:\n", nrules);
1130 if (!no_lines_flag)
1131 obstack_fgrow2 (&guard_obstack, "#line %d %s\n",
1132 lineno, quotearg_style (c_quoting_style, infile));
1133 obstack_1grow (&guard_obstack, '{');
1134
1135 count = 0;
1136 c = getc (finput);
1137
1138 while (brace_flag ? (count > 0) : (c != ';'))
1139 {
1140 switch (c)
1141 {
1142 case '\n':
1143 obstack_1grow (&guard_obstack, c);
1144 lineno++;
1145 break;
1146
1147 case '{':
1148 obstack_1grow (&guard_obstack, c);
1149 brace_flag = 1;
1150 count++;
1151 break;
1152
1153 case '}':
1154 obstack_1grow (&guard_obstack, c);
1155 if (count > 0)
1156 count--;
1157 else
1158 {
1159 complain (_("unmatched %s"), "`}'");
1160 c = getc (finput); /* skip it */
1161 }
1162 break;
1163
1164 case '\'':
1165 case '"':
1166 copy_string (finput, &guard_obstack, c);
1167 break;
1168
1169 case '/':
1170 copy_comment (finput, &guard_obstack);
1171 break;
1172
1173 case '$':
1174 copy_dollar (finput, &guard_obstack, rule, stack_offset);
1175 break;
1176
1177 case '@':
1178 copy_at (finput, &guard_obstack, stack_offset);
1179 break;
1180
1181 case EOF:
1182 fatal ("%s", _("unterminated %guard clause"));
1183
1184 default:
1185 obstack_1grow (&guard_obstack, c);
1186 }
1187
1188 if (c != '}' || count != 0)
1189 c = getc (finput);
1190 }
1191
1192 c = skip_white_space ();
1193
1194 obstack_sgrow (&guard_obstack, ";\n break;}");
1195 if (c == '{')
1196 copy_action (rule, stack_offset);
1197 else if (c == '=')
1198 {
1199 c = getc (finput); /* why not skip_white_space -wjh */
1200 if (c == '{')
1201 copy_action (rule, stack_offset);
1202 }
1203 else
1204 ungetc (c, finput);
1205 }
1206 \f
1207
1208 static void
1209 record_rule_line (void)
1210 {
1211 /* Record each rule's source line number in rline table. */
1212
1213 if (nrules >= rline_allocated)
1214 {
1215 rline_allocated = nrules * 2;
1216 rline = XREALLOC (rline, short, rline_allocated);
1217 }
1218 rline[nrules] = lineno;
1219 }
1220
1221
1222 /*-------------------------------------------------------------------.
1223 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1224 | with the user's names. |
1225 `-------------------------------------------------------------------*/
1226
1227 static bucket *
1228 gensym (void)
1229 {
1230 /* Incremented for each generated symbol */
1231 static int gensym_count = 0;
1232 static char buf[256];
1233
1234 bucket *sym;
1235
1236 sprintf (buf, "@%d", ++gensym_count);
1237 token_buffer = buf;
1238 sym = getsym (token_buffer);
1239 sym->class = nterm_sym;
1240 sym->value = nvars++;
1241 return sym;
1242 }
1243
1244 #if 0
1245 /*------------------------------------------------------------------.
1246 | read in a %type declaration and record its information for |
1247 | get_type_name to access. This is unused. It is only called from |
1248 | the #if 0 part of readgram |
1249 `------------------------------------------------------------------*/
1250
1251 static int
1252 get_type (void)
1253 {
1254 int k;
1255 token_t token;
1256 char *name;
1257
1258 token = lex ();
1259
1260 if (token != tok_typename)
1261 {
1262 complain (_("invalid %s declaration"), "%type");
1263 return t;
1264 }
1265
1266 name = xstrdup (token_buffer);
1267
1268 for (;;)
1269 {
1270 token = lex ();
1271
1272 switch (token)
1273 {
1274 case tok_semicolon:
1275 return lex ();
1276
1277 case tok_comma:
1278 break;
1279
1280 case tok_identifier:
1281 if (symval->type_name == NULL)
1282 symval->type_name = name;
1283 else if (strcmp (name, symval->type_name) != 0)
1284 complain (_("type redeclaration for %s"), symval->tag);
1285
1286 break;
1287
1288 default:
1289 return token;
1290 }
1291 }
1292 }
1293
1294 #endif
1295 \f
1296 /*------------------------------------------------------------------.
1297 | Parse the input grammar into a one symbol_list structure. Each |
1298 | rule is represented by a sequence of symbols: the left hand side |
1299 | followed by the contents of the right hand side, followed by a |
1300 | null pointer instead of a symbol to terminate the rule. The next |
1301 | symbol is the lhs of the following rule. |
1302 | |
1303 | All guards and actions are copied out to the appropriate files, |
1304 | labelled by the rule number they apply to. |
1305 `------------------------------------------------------------------*/
1306
1307 static void
1308 readgram (void)
1309 {
1310 token_t t;
1311 bucket *lhs = NULL;
1312 symbol_list *p;
1313 symbol_list *p1;
1314 bucket *bp;
1315
1316 /* Points to first symbol_list of current rule. its symbol is the
1317 lhs of the rule. */
1318 symbol_list *crule;
1319 /* Points to the symbol_list preceding crule. */
1320 symbol_list *crule1;
1321
1322 p1 = NULL;
1323
1324 t = lex ();
1325
1326 while (t != tok_two_percents && t != tok_eof)
1327 {
1328 if (t == tok_identifier || t == tok_bar)
1329 {
1330 int action_flag = 0;
1331 /* Number of symbols in rhs of this rule so far */
1332 int rulelength = 0;
1333 int xactions = 0; /* JF for error checking */
1334 bucket *first_rhs = 0;
1335
1336 if (t == tok_identifier)
1337 {
1338 lhs = symval;
1339
1340 if (!start_flag)
1341 {
1342 startval = lhs;
1343 start_flag = 1;
1344 }
1345
1346 t = lex ();
1347 if (t != tok_colon)
1348 {
1349 complain (_("ill-formed rule: initial symbol not followed by colon"));
1350 unlex (t);
1351 }
1352 }
1353
1354 if (nrules == 0 && t == tok_bar)
1355 {
1356 complain (_("grammar starts with vertical bar"));
1357 lhs = symval; /* BOGUS: use a random symval */
1358 }
1359 /* start a new rule and record its lhs. */
1360
1361 nrules++;
1362 nitems++;
1363
1364 record_rule_line ();
1365
1366 p = XCALLOC (symbol_list, 1);
1367 p->sym = lhs;
1368
1369 crule1 = p1;
1370 if (p1)
1371 p1->next = p;
1372 else
1373 grammar = p;
1374
1375 p1 = p;
1376 crule = p;
1377
1378 /* mark the rule's lhs as a nonterminal if not already so. */
1379
1380 if (lhs->class == unknown_sym)
1381 {
1382 lhs->class = nterm_sym;
1383 lhs->value = nvars;
1384 nvars++;
1385 }
1386 else if (lhs->class == token_sym)
1387 complain (_("rule given for %s, which is a token"), lhs->tag);
1388
1389 /* read the rhs of the rule. */
1390
1391 for (;;)
1392 {
1393 t = lex ();
1394 if (t == tok_prec)
1395 {
1396 t = lex ();
1397 crule->ruleprec = symval;
1398 t = lex ();
1399 }
1400
1401 if (!(t == tok_identifier || t == tok_left_curly))
1402 break;
1403
1404 /* If next token is an identifier, see if a colon follows it.
1405 If one does, exit this rule now. */
1406 if (t == tok_identifier)
1407 {
1408 bucket *ssave;
1409 token_t t1;
1410
1411 ssave = symval;
1412 t1 = lex ();
1413 unlex (t1);
1414 symval = ssave;
1415 if (t1 == tok_colon)
1416 break;
1417
1418 if (!first_rhs) /* JF */
1419 first_rhs = symval;
1420 /* Not followed by colon =>
1421 process as part of this rule's rhs. */
1422 }
1423
1424 /* If we just passed an action, that action was in the middle
1425 of a rule, so make a dummy rule to reduce it to a
1426 non-terminal. */
1427 if (action_flag)
1428 {
1429 bucket *sdummy;
1430
1431 /* Since the action was written out with this rule's
1432 number, we must give the new rule this number by
1433 inserting the new rule before it. */
1434
1435 /* Make a dummy nonterminal, a gensym. */
1436 sdummy = gensym ();
1437
1438 /* Make a new rule, whose body is empty,
1439 before the current one, so that the action
1440 just read can belong to it. */
1441 nrules++;
1442 nitems++;
1443 record_rule_line ();
1444 p = XCALLOC (symbol_list, 1);
1445 if (crule1)
1446 crule1->next = p;
1447 else
1448 grammar = p;
1449 p->sym = sdummy;
1450 crule1 = XCALLOC (symbol_list, 1);
1451 p->next = crule1;
1452 crule1->next = crule;
1453
1454 /* Insert the dummy generated by that rule into this
1455 rule. */
1456 nitems++;
1457 p = XCALLOC (symbol_list, 1);
1458 p->sym = sdummy;
1459 p1->next = p;
1460 p1 = p;
1461
1462 action_flag = 0;
1463 }
1464
1465 if (t == tok_identifier)
1466 {
1467 nitems++;
1468 p = XCALLOC (symbol_list, 1);
1469 p->sym = symval;
1470 p1->next = p;
1471 p1 = p;
1472 }
1473 else /* handle an action. */
1474 {
1475 copy_action (crule, rulelength);
1476 action_flag = 1;
1477 xactions++; /* JF */
1478 }
1479 rulelength++;
1480 } /* end of read rhs of rule */
1481
1482 /* Put an empty link in the list to mark the end of this rule */
1483 p = XCALLOC (symbol_list, 1);
1484 p1->next = p;
1485 p1 = p;
1486
1487 if (t == tok_prec)
1488 {
1489 complain (_("two @prec's in a row"));
1490 t = lex ();
1491 crule->ruleprec = symval;
1492 t = lex ();
1493 }
1494 if (t == tok_guard)
1495 {
1496 if (!semantic_parser)
1497 complain (_("%%guard present but %%semantic_parser not specified"));
1498
1499 copy_guard (crule, rulelength);
1500 t = lex ();
1501 }
1502 else if (t == tok_left_curly)
1503 {
1504 /* This case never occurs -wjh */
1505 if (action_flag)
1506 complain (_("two actions at end of one rule"));
1507 copy_action (crule, rulelength);
1508 action_flag = 1;
1509 xactions++; /* -wjh */
1510 t = lex ();
1511 }
1512 /* If $$ is being set in default way, report if any type
1513 mismatch. */
1514 else if (!xactions
1515 && first_rhs && lhs->type_name != first_rhs->type_name)
1516 {
1517 if (lhs->type_name == 0
1518 || first_rhs->type_name == 0
1519 || strcmp (lhs->type_name, first_rhs->type_name))
1520 complain (_("type clash (`%s' `%s') on default action"),
1521 lhs->type_name ? lhs->type_name : "",
1522 first_rhs->type_name ? first_rhs->type_name : "");
1523 }
1524 /* Warn if there is no default for $$ but we need one. */
1525 else if (!xactions && !first_rhs && lhs->type_name != 0)
1526 complain (_("empty rule for typed nonterminal, and no action"));
1527 if (t == tok_semicolon)
1528 t = lex ();
1529 }
1530 #if 0
1531 /* these things can appear as alternatives to rules. */
1532 /* NO, they cannot.
1533 a) none of the documentation allows them
1534 b) most of them scan forward until finding a next %
1535 thus they may swallow lots of intervening rules
1536 */
1537 else if (t == tok_token)
1538 {
1539 parse_token_decl (token_sym, nterm_sym);
1540 t = lex ();
1541 }
1542 else if (t == tok_nterm)
1543 {
1544 parse_token_decl (nterm_sym, token_sym);
1545 t = lex ();
1546 }
1547 else if (t == tok_type)
1548 {
1549 t = get_type ();
1550 }
1551 else if (t == tok_union)
1552 {
1553 parse_union_decl ();
1554 t = lex ();
1555 }
1556 else if (t == tok_expect)
1557 {
1558 parse_expect_decl ();
1559 t = lex ();
1560 }
1561 else if (t == tok_start)
1562 {
1563 parse_start_decl ();
1564 t = lex ();
1565 }
1566 #endif
1567
1568 else
1569 {
1570 complain (_("invalid input: %s"), quote (token_buffer));
1571 t = lex ();
1572 }
1573 }
1574
1575 /* grammar has been read. Do some checking */
1576
1577 if (nsyms > MAXSHORT)
1578 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1579 MAXSHORT);
1580 if (nrules == 0)
1581 fatal (_("no rules in the input grammar"));
1582
1583 /* JF put out same default YYSTYPE as YACC does */
1584 if (typed == 0
1585 && !value_components_used)
1586 {
1587 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1588 but it seems better to be consistent.
1589 Most programs should declare their own type anyway. */
1590 obstack_sgrow (&attrs_obstack,
1591 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1592 if (defines_flag)
1593 obstack_sgrow (&defines_obstack, "\
1594 # ifndef YYSTYPE\n\
1595 # define YYSTYPE int\n\
1596 # endif\n");
1597 }
1598
1599 /* Report any undefined symbols and consider them nonterminals. */
1600
1601 for (bp = firstsymbol; bp; bp = bp->next)
1602 if (bp->class == unknown_sym)
1603 {
1604 complain (_
1605 ("symbol %s is used, but is not defined as a token and has no rules"),
1606 bp->tag);
1607 bp->class = nterm_sym;
1608 bp->value = nvars++;
1609 }
1610
1611 ntokens = nsyms - nvars;
1612 }
1613 \f
1614 /*--------------------------------------------------------------.
1615 | For named tokens, but not literal ones, define the name. The |
1616 | value is the user token number. |
1617 `--------------------------------------------------------------*/
1618
1619 static void
1620 output_token_defines (struct obstack *oout)
1621 {
1622 bucket *bp;
1623 char *cp, *symbol;
1624 char c;
1625
1626 for (bp = firstsymbol; bp; bp = bp->next)
1627 {
1628 symbol = bp->tag; /* get symbol */
1629
1630 if (bp->value >= ntokens)
1631 continue;
1632 if (bp->user_token_number == SALIAS)
1633 continue;
1634 if ('\'' == *symbol)
1635 continue; /* skip literal character */
1636 if (bp == errtoken)
1637 continue; /* skip error token */
1638 if ('\"' == *symbol)
1639 {
1640 /* use literal string only if given a symbol with an alias */
1641 if (bp->alias)
1642 symbol = bp->alias->tag;
1643 else
1644 continue;
1645 }
1646
1647 /* Don't #define nonliteral tokens whose names contain periods. */
1648 cp = symbol;
1649 while ((c = *cp++) && c != '.');
1650 if (c != '\0')
1651 continue;
1652
1653 obstack_fgrow2 (oout, "# define\t%s\t%d\n",
1654 symbol, bp->user_token_number);
1655 if (semantic_parser)
1656 /* FIXME: This is certainly dead wrong, and should be just as
1657 above. --akim. */
1658 obstack_fgrow2 (oout, "# define\tT%s\t%d\n", symbol, bp->value);
1659 }
1660
1661 obstack_1grow (oout, '\n');
1662 }
1663
1664
1665 /*------------------------------------------------------------------.
1666 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
1667 | number. |
1668 `------------------------------------------------------------------*/
1669
1670 static void
1671 token_translations_init (void)
1672 {
1673 bucket *bp = NULL;
1674 int i;
1675
1676 token_translations = XCALLOC (short, max_user_token_number + 1);
1677
1678 /* Initialize all entries for literal tokens to 2, the internal
1679 token number for $undefined., which represents all invalid
1680 inputs. */
1681 for (i = 0; i <= max_user_token_number; i++)
1682 token_translations[i] = 2;
1683
1684 for (bp = firstsymbol; bp; bp = bp->next)
1685 {
1686 /* Non-terminal? */
1687 if (bp->value >= ntokens)
1688 continue;
1689 /* A token string alias? */
1690 if (bp->user_token_number == SALIAS)
1691 continue;
1692 /* A token which translation has already been set? */
1693 if (token_translations[bp->user_token_number] != 2)
1694 complain (_("tokens %s and %s both assigned number %d"),
1695 tags[token_translations[bp->user_token_number]],
1696 bp->tag, bp->user_token_number);
1697 token_translations[bp->user_token_number] = bp->value;
1698 }
1699 }
1700
1701
1702 /*------------------------------------------------------------------.
1703 | Assign symbol numbers, and write definition of token names into |
1704 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1705 | of symbols. |
1706 `------------------------------------------------------------------*/
1707
1708 static void
1709 packsymbols (void)
1710 {
1711 bucket *bp = NULL;
1712 int tokno = 1;
1713 int last_user_token_number;
1714 static char DOLLAR[] = "$";
1715
1716 tags = XCALLOC (char *, nsyms + 1);
1717 user_toknums = XCALLOC (short, nsyms + 1);
1718
1719 sprec = XCALLOC (short, nsyms);
1720 sassoc = XCALLOC (short, nsyms);
1721
1722 /* The EOF token. */
1723 tags[0] = DOLLAR;
1724 user_toknums[0] = 0;
1725
1726 max_user_token_number = 256;
1727 last_user_token_number = 256;
1728
1729 for (bp = firstsymbol; bp; bp = bp->next)
1730 {
1731 if (bp->class == nterm_sym)
1732 {
1733 bp->value += ntokens;
1734 }
1735 else if (bp->alias)
1736 {
1737 /* this symbol and its alias are a single token defn.
1738 allocate a tokno, and assign to both check agreement of
1739 ->prec and ->assoc fields and make both the same */
1740 if (bp->value == 0)
1741 bp->value = bp->alias->value = tokno++;
1742
1743 if (bp->prec != bp->alias->prec)
1744 {
1745 if (bp->prec != 0 && bp->alias->prec != 0
1746 && bp->user_token_number == SALIAS)
1747 complain (_("conflicting precedences for %s and %s"),
1748 bp->tag, bp->alias->tag);
1749 if (bp->prec != 0)
1750 bp->alias->prec = bp->prec;
1751 else
1752 bp->prec = bp->alias->prec;
1753 }
1754
1755 if (bp->assoc != bp->alias->assoc)
1756 {
1757 if (bp->assoc != 0 && bp->alias->assoc != 0
1758 && bp->user_token_number == SALIAS)
1759 complain (_("conflicting assoc values for %s and %s"),
1760 bp->tag, bp->alias->tag);
1761 if (bp->assoc != 0)
1762 bp->alias->assoc = bp->assoc;
1763 else
1764 bp->assoc = bp->alias->assoc;
1765 }
1766
1767 if (bp->user_token_number == SALIAS)
1768 continue; /* do not do processing below for SALIASs */
1769
1770 }
1771 else /* bp->class == token_sym */
1772 {
1773 bp->value = tokno++;
1774 }
1775
1776 if (bp->class == token_sym)
1777 {
1778 if (bp->user_token_number == 0)
1779 bp->user_token_number = ++last_user_token_number;
1780 if (bp->user_token_number > max_user_token_number)
1781 max_user_token_number = bp->user_token_number;
1782 }
1783
1784 tags[bp->value] = bp->tag;
1785 user_toknums[bp->value] = bp->user_token_number;
1786 sprec[bp->value] = bp->prec;
1787 sassoc[bp->value] = bp->assoc;
1788 }
1789
1790 token_translations_init ();
1791
1792 error_token_number = errtoken->value;
1793
1794 if (!no_parser_flag)
1795 output_token_defines (&table_obstack);
1796
1797 if (startval->class == unknown_sym)
1798 fatal (_("the start symbol %s is undefined"), startval->tag);
1799 else if (startval->class == token_sym)
1800 fatal (_("the start symbol %s is a token"), startval->tag);
1801
1802 start_symbol = startval->value;
1803
1804 if (defines_flag)
1805 {
1806 output_token_defines (&defines_obstack);
1807
1808 if (!pure_parser)
1809 {
1810 if (spec_name_prefix)
1811 obstack_fgrow1 (&defines_obstack, "\nextern YYSTYPE %slval;\n",
1812 spec_name_prefix);
1813 else
1814 obstack_sgrow (&defines_obstack,
1815 "\nextern YYSTYPE yylval;\n");
1816 }
1817
1818 if (semantic_parser)
1819 {
1820 int i;
1821
1822 for (i = ntokens; i < nsyms; i++)
1823 {
1824 /* don't make these for dummy nonterminals made by gensym. */
1825 if (*tags[i] != '@')
1826 obstack_fgrow2 (&defines_obstack,
1827 "# define\tNT%s\t%d\n", tags[i], i);
1828 }
1829 #if 0
1830 /* `fdefines' is now a temporary file, so we need to copy its
1831 contents in `done', so we can't close it here. */
1832 fclose (fdefines);
1833 fdefines = NULL;
1834 #endif
1835 }
1836 }
1837 }
1838
1839
1840 /*---------------------------------------------------------------.
1841 | Convert the rules into the representation using RRHS, RLHS and |
1842 | RITEMS. |
1843 `---------------------------------------------------------------*/
1844
1845 static void
1846 packgram (void)
1847 {
1848 int itemno;
1849 int ruleno;
1850 symbol_list *p;
1851
1852 bucket *ruleprec;
1853
1854 ritem = XCALLOC (short, nitems + 1);
1855 rlhs = XCALLOC (short, nrules) - 1;
1856 rrhs = XCALLOC (short, nrules) - 1;
1857 rprec = XCALLOC (short, nrules) - 1;
1858 rprecsym = XCALLOC (short, nrules) - 1;
1859 rassoc = XCALLOC (short, nrules) - 1;
1860
1861 itemno = 0;
1862 ruleno = 1;
1863
1864 p = grammar;
1865 while (p)
1866 {
1867 rlhs[ruleno] = p->sym->value;
1868 rrhs[ruleno] = itemno;
1869 ruleprec = p->ruleprec;
1870
1871 p = p->next;
1872 while (p && p->sym)
1873 {
1874 ritem[itemno++] = p->sym->value;
1875 /* A rule gets by default the precedence and associativity
1876 of the last token in it. */
1877 if (p->sym->class == token_sym)
1878 {
1879 rprec[ruleno] = p->sym->prec;
1880 rassoc[ruleno] = p->sym->assoc;
1881 }
1882 if (p)
1883 p = p->next;
1884 }
1885
1886 /* If this rule has a %prec,
1887 the specified symbol's precedence replaces the default. */
1888 if (ruleprec)
1889 {
1890 rprec[ruleno] = ruleprec->prec;
1891 rassoc[ruleno] = ruleprec->assoc;
1892 rprecsym[ruleno] = ruleprec->value;
1893 }
1894
1895 ritem[itemno++] = -ruleno;
1896 ruleno++;
1897
1898 if (p)
1899 p = p->next;
1900 }
1901
1902 ritem[itemno] = 0;
1903 }
1904 \f
1905 /*-------------------------------------------------------------------.
1906 | Read in the grammar specification and record it in the format |
1907 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1908 | and all actions into ACTION_OBSTACK, in each case forming the body |
1909 | of a C function (YYGUARD or YYACTION) which contains a switch |
1910 | statement to decide which guard or action to execute. |
1911 `-------------------------------------------------------------------*/
1912
1913 void
1914 reader (void)
1915 {
1916 start_flag = 0;
1917 startval = NULL; /* start symbol not specified yet. */
1918
1919 nsyms = 1;
1920 nvars = 0;
1921 nrules = 0;
1922 nitems = 0;
1923 rline_allocated = 10;
1924 rline = XCALLOC (short, rline_allocated);
1925
1926 typed = 0;
1927 lastprec = 0;
1928
1929 semantic_parser = 0;
1930 pure_parser = 0;
1931
1932 grammar = NULL;
1933
1934 lex_init ();
1935 lineno = 1;
1936
1937 /* Initialize the symbol table. */
1938 tabinit ();
1939 /* Construct the error token */
1940 errtoken = getsym ("error");
1941 errtoken->class = token_sym;
1942 errtoken->user_token_number = 256; /* Value specified by POSIX. */
1943 /* Construct a token that represents all undefined literal tokens.
1944 It is always token number 2. */
1945 undeftoken = getsym ("$undefined.");
1946 undeftoken->class = token_sym;
1947 undeftoken->user_token_number = 2;
1948
1949 /* Read the declaration section. Copy %{ ... %} groups to
1950 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1951 etc. found there. */
1952 obstack_1grow (&table_obstack, '\n');
1953 obstack_fgrow3 (&table_obstack, "\
1954 /* %s, made from %s\n\
1955 by GNU bison %s. */\n\
1956 \n",
1957 no_parser_flag ? "Bison-generated parse tables" : "A Bison parser",
1958 infile, VERSION);
1959
1960 obstack_sgrow (&table_obstack,
1961 "#define YYBISON 1 /* Identify Bison output. */\n\n");
1962 read_declarations ();
1963 /* Start writing the guard and action files, if they are needed. */
1964 output_headers ();
1965 /* Read in the grammar, build grammar in list form. Write out
1966 guards and actions. */
1967 readgram ();
1968 /* Now we know whether we need the line-number stack. If we do,
1969 write its type into the .tab.h file. */
1970 if (defines_flag)
1971 reader_output_yylsp (&defines_obstack);
1972 /* Write closing delimiters for actions and guards. */
1973 output_trailers ();
1974 if (locations_flag)
1975 obstack_sgrow (&table_obstack, "#define YYLSP_NEEDED 1\n\n");
1976 /* Assign the symbols their symbol numbers. Write #defines for the
1977 token symbols into FDEFINES if requested. */
1978 packsymbols ();
1979 /* Convert the grammar into the format described in gram.h. */
1980 packgram ();
1981 }
1982
1983
1984 /*------------------------------------------------------------------.
1985 | Define YYLTYPE. Cannot be in the skeleton since we might have to |
1986 | output it in the headers if --defines is used. |
1987 `------------------------------------------------------------------*/
1988
1989 void
1990 reader_output_yylsp (struct obstack *oout)
1991 {
1992 if (locations_flag)
1993 obstack_sgrow (oout, "\
1994 \n\
1995 #ifndef YYLTYPE\n\
1996 typedef struct yyltype\n\
1997 {\n\
1998 int first_line;\n\
1999 int first_column;\n\
2000 \n\
2001 int last_line;\n\
2002 int last_column;\n\
2003 } yyltype;\n\
2004 \n\
2005 # define YYLTYPE yyltype\n\
2006 #endif\n\
2007 \n");
2008 }