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