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