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