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