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