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