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