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