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