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