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