]> git.saurik.com Git - bison.git/blob - src/reader.c
70629837b215717c1d9d215c205975baae88fe7f
[bison.git] / src / reader.c
1 /* Input parser for bison
2 Copyright (C) 1984, 1986, 1989, 1992, 1998, 2000, 2001, 2002
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 "quotearg.h"
25 #include "quote.h"
26 #include "getargs.h"
27 #include "files.h"
28 #include "symtab.h"
29 #include "options.h"
30 #include "lex.h"
31 #include "gram.h"
32 #include "complain.h"
33 #include "output.h"
34 #include "reader.h"
35 #include "conflicts.h"
36 #include "muscle_tab.h"
37
38 typedef struct symbol_list
39 {
40 struct symbol_list *next;
41 symbol_t *sym;
42 int line;
43
44 /* The action is attached to the LHS of a rule. */
45 const char *action;
46 int action_line;
47
48 symbol_t *ruleprec;
49 } symbol_list;
50
51 int lineno;
52 static symbol_list *grammar = NULL;
53 static int start_flag = 0;
54
55 /* Nonzero if %union has been seen. */
56 static int typed = 0;
57
58 /* Incremented for each %left, %right or %nonassoc seen */
59 static int lastprec = 0;
60
61 static symbol_list *
62 symbol_list_new (symbol_t *sym)
63 {
64 symbol_list *res = XMALLOC (symbol_list, 1);
65 res->next = NULL;
66 res->sym = sym;
67 res->line = lineno;
68 res->action = NULL;
69 res->action_line = 0;
70 res->ruleprec = NULL;
71 return res;
72 }
73
74 /*===================\
75 | Low level lexing. |
76 \===================*/
77
78 static void
79 skip_to_char (int target)
80 {
81 int c;
82 if (target == '\n')
83 complain (_(" Skipping to next \\n"));
84 else
85 complain (_(" Skipping to next %c"), target);
86
87 do
88 c = skip_white_space ();
89 while (c != target && c != EOF);
90 if (c != EOF)
91 ungetc (c, finput);
92 }
93
94
95 /*---------------------------------------------------------.
96 | Read a signed integer from STREAM and return its value. |
97 `---------------------------------------------------------*/
98
99 static inline int
100 read_signed_integer (FILE *stream)
101 {
102 int c = getc (stream);
103 int sign = 1;
104 int n = 0;
105
106 if (c == '-')
107 {
108 c = getc (stream);
109 sign = -1;
110 }
111
112 while (isdigit (c))
113 {
114 n = 10 * n + (c - '0');
115 c = getc (stream);
116 }
117
118 ungetc (c, stream);
119
120 return sign * n;
121 }
122 \f
123 /*--------------------------------------------------------------.
124 | Get the data type (alternative in the union) of the value for |
125 | symbol N in rule RULE. |
126 `--------------------------------------------------------------*/
127
128 static char *
129 get_type_name (int n, symbol_list *rule)
130 {
131 int i;
132 symbol_list *rp;
133
134 if (n < 0)
135 {
136 complain (_("invalid $ value"));
137 return NULL;
138 }
139
140 rp = rule;
141 i = 0;
142
143 while (i < n)
144 {
145 rp = rp->next;
146 if (rp == NULL || rp->sym == NULL)
147 {
148 complain (_("invalid $ value"));
149 return NULL;
150 }
151 ++i;
152 }
153
154 return rp->sym->type_name;
155 }
156 \f
157 /*------------------------------------------------------------------.
158 | Copy the character C to OOUT, and insert quadigraphs when needed. |
159 `------------------------------------------------------------------*/
160
161 static inline void
162 copy_character (struct obstack *oout, int c)
163 {
164 switch (c)
165 {
166 case '[':
167 obstack_sgrow (oout, "@<:@");
168 break;
169
170 case ']':
171 obstack_sgrow (oout, "@:>@");
172 break;
173
174 default:
175 obstack_1grow (oout, c);
176 }
177 }
178
179 /*------------------------------------------------------------.
180 | Dump the string from FIN to OOUT if non null. MATCH is the |
181 | delimiter of the string (either ' or "). |
182 `------------------------------------------------------------*/
183
184 static inline void
185 copy_string2 (FILE *fin, struct obstack *oout, int match, int store)
186 {
187 int c;
188
189 if (store)
190 obstack_1grow (oout, match);
191
192 c = getc (fin);
193
194 while (c != match)
195 {
196 if (c == EOF)
197 fatal (_("unterminated string at end of file"));
198 if (c == '\n')
199 {
200 complain (_("unterminated string"));
201 ungetc (c, fin);
202 c = match; /* invent terminator */
203 continue;
204 }
205
206 copy_character (oout, c);
207
208 if (c == '\\')
209 {
210 c = getc (fin);
211 if (c == EOF)
212 fatal (_("unterminated string at end of file"));
213 copy_character (oout, c);
214
215 if (c == '\n')
216 ++lineno;
217 }
218
219 c = getc (fin);
220 }
221
222 if (store)
223 obstack_1grow (oout, c);
224 }
225
226 /* FIXME. */
227
228 static inline void
229 copy_string (FILE *fin, struct obstack *oout, int match)
230 {
231 copy_string2 (fin, oout, match, 1);
232 }
233
234 /* FIXME. */
235
236 static inline void
237 copy_identifier (FILE *fin, struct obstack *oout)
238 {
239 int c;
240
241 while (isalnum (c = getc (fin)) || c == '_')
242 obstack_1grow (oout, c);
243
244 ungetc (c, fin);
245 }
246
247
248 /*------------------------------------------------------------------.
249 | Dump the wannabee comment from IN to OOUT. In fact we just saw a |
250 | `/', which might or might not be a comment. In any case, copy |
251 | what we saw. |
252 `------------------------------------------------------------------*/
253
254 static inline void
255 copy_comment (FILE *fin, struct obstack *oout)
256 {
257 int cplus_comment;
258 int ended;
259 int c;
260
261 /* We read a `/', output it. */
262 obstack_1grow (oout, '/');
263
264 switch ((c = getc (fin)))
265 {
266 case '/':
267 cplus_comment = 1;
268 break;
269 case '*':
270 cplus_comment = 0;
271 break;
272 default:
273 ungetc (c, fin);
274 return;
275 }
276
277 obstack_1grow (oout, c);
278 c = getc (fin);
279
280 ended = 0;
281 while (!ended)
282 {
283 if (!cplus_comment && c == '*')
284 {
285 while (c == '*')
286 {
287 obstack_1grow (oout, c);
288 c = getc (fin);
289 }
290
291 if (c == '/')
292 {
293 obstack_1grow (oout, c);
294 ended = 1;
295 }
296 }
297 else if (c == '\n')
298 {
299 ++lineno;
300 obstack_1grow (oout, c);
301 if (cplus_comment)
302 ended = 1;
303 else
304 c = getc (fin);
305 }
306 else if (c == EOF)
307 fatal (_("unterminated comment"));
308 else
309 {
310 copy_character (oout, c);
311 c = getc (fin);
312 }
313 }
314 }
315
316
317 /*-------------------------------------------------------------------.
318 | FIN is pointing to a location (i.e., a `@'). Output to OOUT a |
319 | reference to this location. RULE_LENGTH is the number of values in |
320 | the current rule so far, which says where to find `$0' with |
321 | respect to the top of the stack. |
322 `-------------------------------------------------------------------*/
323
324 static inline void
325 copy_at (FILE *fin, struct obstack *oout, int rule_length)
326 {
327 int c = getc (fin);
328 locations_flag = 1;
329
330 if (c == '$')
331 {
332 obstack_sgrow (oout, "]b4_lhs_location[");
333 }
334 else if (isdigit (c) || c == '-')
335 {
336 int n;
337
338 ungetc (c, fin);
339 n = read_signed_integer (fin);
340 if (n > rule_length)
341 complain (_("invalid value: %s%d"), "@", n);
342 else
343 obstack_fgrow2 (oout, "]b4_rhs_location([%d], [%d])[",
344 rule_length, n);
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. RULE_LENGTH 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 rule_length)
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 c = getc (fin);
378 }
379
380 if (c == '$')
381 {
382 if (!type_name)
383 type_name = get_type_name (0, rule);
384 if (!type_name && typed)
385 complain (_("$$ of `%s' has no declared type"),
386 rule->sym->tag);
387 if (!type_name)
388 type_name = "";
389 obstack_fgrow1 (oout,
390 "]b4_lhs_value([%s])[", type_name);
391 }
392 else if (isdigit (c) || c == '-')
393 {
394 int n;
395 ungetc (c, fin);
396 n = read_signed_integer (fin);
397
398 if (n > rule_length)
399 complain (_("invalid value: %s%d"), "$", n);
400 else
401 {
402 if (!type_name && n > 0)
403 type_name = get_type_name (n, rule);
404 if (!type_name && typed)
405 complain (_("$%d of `%s' has no declared type"),
406 n, rule->sym->tag);
407 if (!type_name)
408 type_name = "";
409 obstack_fgrow3 (oout, "]b4_rhs_value([%d], [%d], [%s])[",
410 rule_length, n, type_name);
411 }
412 }
413 else
414 {
415 char buf[] = "$c";
416 buf[1] = c;
417 complain (_("%s is invalid"), quote (buf));
418 }
419 }
420 \f
421 /*-------------------------------------------------------------------.
422 | Copy the contents of a `%{ ... %}' into the definitions file. The |
423 | `%{' has already been read. Return after reading the `%}'. |
424 `-------------------------------------------------------------------*/
425
426 static void
427 copy_definition (struct obstack *oout)
428 {
429 int c;
430 /* -1 while reading a character if prev char was %. */
431 int after_percent;
432
433 if (!no_lines_flag)
434 {
435 obstack_fgrow2 (oout, muscle_find ("linef"),
436 lineno, quotearg_style (c_quoting_style,
437 muscle_find ("filename")));
438 }
439
440 after_percent = 0;
441
442 c = getc (finput);
443
444 for (;;)
445 {
446 switch (c)
447 {
448 case '\n':
449 obstack_1grow (oout, c);
450 ++lineno;
451 break;
452
453 case '%':
454 after_percent = -1;
455 break;
456
457 case '\'':
458 case '"':
459 copy_string (finput, oout, c);
460 break;
461
462 case '/':
463 copy_comment (finput, oout);
464 break;
465
466 case EOF:
467 fatal ("%s", _("unterminated `%{' definition"));
468
469 default:
470 copy_character (oout, c);
471 }
472
473 c = getc (finput);
474
475 if (after_percent)
476 {
477 if (c == '}')
478 return;
479 obstack_1grow (oout, '%');
480 }
481 after_percent = 0;
482 }
483 }
484
485
486 /*-------------------------------------------------------------------.
487 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
488 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
489 | are reversed. |
490 `-------------------------------------------------------------------*/
491
492 static void
493 parse_token_decl (symbol_class what_is, symbol_class what_is_not)
494 {
495 token_t token = tok_undef;
496 char *typename = NULL;
497
498 /* The symbol being defined. */
499 symbol_t *symbol = NULL;
500
501 /* After `%token' and `%nterm', any number of symbols maybe be
502 defined. */
503 for (;;)
504 {
505 int tmp_char = ungetc (skip_white_space (), finput);
506
507 /* `%' (for instance from `%token', or from `%%' etc.) is the
508 only valid means to end this declaration. */
509 if (tmp_char == '%')
510 return;
511 if (tmp_char == EOF)
512 fatal (_("Premature EOF after %s"), token_buffer);
513
514 token = lex ();
515 if (token == tok_comma)
516 {
517 symbol = NULL;
518 continue;
519 }
520 if (token == tok_typename)
521 {
522 typename = xstrdup (token_buffer);
523 symbol = NULL;
524 }
525 else if (token == tok_identifier && *symval->tag == '\"' && symbol)
526 {
527 symbol_make_alias (symbol, symval, typename);
528 symbol = NULL;
529 }
530 else if (token == tok_identifier)
531 {
532 int oldclass = symval->class;
533 symbol = symval;
534
535 if (symbol->class == what_is_not)
536 complain (_("symbol %s redefined"), symbol->tag);
537 symbol->class = what_is;
538 if (what_is == nterm_sym && oldclass != nterm_sym)
539 symbol->number = nvars++;
540 if (what_is == token_sym && symbol->number == NUMBER_UNDEFINED)
541 symbol->number = ntokens++;
542
543 if (typename)
544 {
545 if (symbol->type_name == NULL)
546 symbol->type_name = typename;
547 else if (strcmp (typename, symbol->type_name) != 0)
548 complain (_("type redeclaration for %s"), symbol->tag);
549 }
550 }
551 else if (symbol && token == tok_number)
552 {
553 symbol->user_token_number = numval;
554 /* User defined EOF token? */
555 if (numval == 0)
556 {
557 eoftoken = symbol;
558 eoftoken->number = 0;
559 /* It is always mapped to 0, so it was already counted in
560 NTOKENS. */
561 --ntokens;
562 }
563 }
564 else
565 {
566 complain (_("`%s' is invalid in %s"),
567 token_buffer,
568 (what_is == token_sym) ? "%token" : "%nterm");
569 skip_to_char ('%');
570 }
571 }
572
573 }
574
575
576 /*------------------------------.
577 | Parse what comes after %start |
578 `------------------------------*/
579
580 static void
581 parse_start_decl (void)
582 {
583 if (start_flag)
584 complain (_("multiple %s declarations"), "%start");
585 if (lex () != tok_identifier)
586 complain (_("invalid %s declaration"), "%start");
587 else
588 {
589 start_flag = 1;
590 startsymbol = symval;
591 }
592 }
593
594 /*-----------------------------------------------------------.
595 | read in a %type declaration and record its information for |
596 | get_type_name to access |
597 `-----------------------------------------------------------*/
598
599 static void
600 parse_type_decl (void)
601 {
602 char *name;
603
604 if (lex () != tok_typename)
605 {
606 complain ("%s", _("%type declaration has no <typename>"));
607 skip_to_char ('%');
608 return;
609 }
610
611 name = xstrdup (token_buffer);
612
613 for (;;)
614 {
615 token_t t;
616 int tmp_char = ungetc (skip_white_space (), finput);
617
618 if (tmp_char == '%')
619 return;
620 if (tmp_char == EOF)
621 fatal (_("Premature EOF after %s"), token_buffer);
622
623 t = lex ();
624
625 switch (t)
626 {
627 case tok_comma:
628 case tok_semicolon:
629 break;
630
631 case tok_identifier:
632 symbol_type_set (symval, name);
633 break;
634
635 default:
636 complain (_("invalid %%type declaration due to item: %s"),
637 token_buffer);
638 skip_to_char ('%');
639 }
640 }
641 }
642
643
644
645 /*----------------------------------------------------------------.
646 | Read in a %left, %right or %nonassoc declaration and record its |
647 | information. |
648 `----------------------------------------------------------------*/
649
650 static void
651 parse_assoc_decl (associativity assoc)
652 {
653 char *name = NULL;
654 int prev = 0;
655
656 /* Assign a new precedence level, never 0. */
657 ++lastprec;
658
659 for (;;)
660 {
661 token_t t;
662 int tmp_char = ungetc (skip_white_space (), finput);
663
664 if (tmp_char == '%')
665 return;
666 if (tmp_char == EOF)
667 fatal (_("Premature EOF after %s"), token_buffer);
668
669 t = lex ();
670
671 switch (t)
672 {
673 case tok_typename:
674 name = xstrdup (token_buffer);
675 break;
676
677 case tok_comma:
678 break;
679
680 case tok_identifier:
681 if (symval->class == nterm_sym)
682 complain (_("symbol %s redefined"), symval->tag);
683 if (symval->number == NUMBER_UNDEFINED)
684 {
685 symval->number = ntokens++;
686 symval->class = token_sym;
687 }
688 symbol_precedence_set (symval, lastprec, assoc);
689 if (name)
690 symbol_type_set (symval, name);
691 break;
692
693 case tok_number:
694 if (prev == tok_identifier)
695 {
696 symval->user_token_number = numval;
697 }
698 else
699 {
700 complain
701 (_("invalid text (%s) - number should be after identifier"),
702 token_buffer);
703 skip_to_char ('%');
704 }
705 break;
706
707 case tok_semicolon:
708 return;
709
710 default:
711 complain (_("unexpected item: %s"), token_buffer);
712 skip_to_char ('%');
713 }
714
715 prev = t;
716 }
717 }
718
719
720
721 /*--------------------------------------------------------------.
722 | Copy the union declaration into the stype muscle |
723 | (and fdefines), where it is made into the definition of |
724 | YYSTYPE, the type of elements of the parser value stack. |
725 `--------------------------------------------------------------*/
726
727 static void
728 parse_union_decl (void)
729 {
730 int c;
731 int count = 0;
732 bool done = FALSE;
733 struct obstack union_obstack;
734 if (typed)
735 complain (_("multiple %s declarations"), "%union");
736
737 typed = 1;
738
739 MUSCLE_INSERT_INT ("stype_line", lineno);
740 obstack_init (&union_obstack);
741 obstack_sgrow (&union_obstack, "union");
742
743 while (!done)
744 {
745 c = xgetc (finput);
746
747 /* If C contains '/', it is output by copy_comment (). */
748 if (c != '/')
749 obstack_1grow (&union_obstack, c);
750
751 switch (c)
752 {
753 case '\n':
754 ++lineno;
755 break;
756
757 case '/':
758 copy_comment (finput, &union_obstack);
759 break;
760
761 case '{':
762 ++count;
763 break;
764
765 case '}':
766 /* FIXME: Errr. How could this happen???. --akim */
767 if (count == 0)
768 complain (_("unmatched %s"), "`}'");
769 count--;
770 if (!count)
771 done = TRUE;
772 break;
773 }
774 }
775
776 /* JF don't choke on trailing semi */
777 c = skip_white_space ();
778 if (c != ';')
779 ungetc (c, finput);
780 obstack_1grow (&union_obstack, 0);
781 muscle_insert ("stype", obstack_finish (&union_obstack));
782 }
783
784
785 /*-------------------------------------------------------.
786 | Parse the declaration %expect N which says to expect N |
787 | shift-reduce conflicts. |
788 `-------------------------------------------------------*/
789
790 static void
791 parse_expect_decl (void)
792 {
793 int c = skip_white_space ();
794 ungetc (c, finput);
795
796 if (!isdigit (c))
797 complain (_("argument of %%expect is not an integer"));
798 else
799 expected_conflicts = read_signed_integer (finput);
800 }
801
802
803 /*-------------------------------------------------------------------.
804 | Parse what comes after %thong. the full syntax is |
805 | |
806 | %thong <type> token number literal |
807 | |
808 | the <type> or number may be omitted. The number specifies the |
809 | user_token_number. |
810 | |
811 | Two symbols are entered in the table, one for the token symbol and |
812 | one for the literal. Both are given the <type>, if any, from the |
813 | declaration. The ->user_token_number of the first is |
814 | USER_NUMBER_ALIAS and the ->user_token_number of the second is set |
815 | to the number, if any, from the declaration. The two symbols are |
816 | linked via pointers in their ->alias fields. |
817 | |
818 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
819 | only the literal string is retained it is the literal string that |
820 | is output to yytname |
821 `-------------------------------------------------------------------*/
822
823 static void
824 parse_thong_decl (void)
825 {
826 token_t token;
827 symbol_t *symbol;
828 char *typename = 0;
829 int usrtoknum = USER_NUMBER_UNDEFINED;
830
831 token = lex (); /* fetch typename or first token */
832 if (token == tok_typename)
833 {
834 typename = xstrdup (token_buffer);
835 token = lex (); /* fetch first token */
836 }
837
838 /* process first token */
839
840 if (token != tok_identifier)
841 {
842 complain (_("unrecognized item %s, expected an identifier"),
843 token_buffer);
844 skip_to_char ('%');
845 return;
846 }
847 symval->class = token_sym;
848 symval->type_name = typename;
849 symval->user_token_number = USER_NUMBER_ALIAS;
850 symbol = symval;
851
852 token = lex (); /* get number or literal string */
853
854 if (token == tok_number)
855 {
856 usrtoknum = numval;
857 token = lex (); /* okay, did number, now get literal */
858 }
859
860 /* process literal string token */
861
862 if (token != tok_identifier || *symval->tag != '\"')
863 {
864 complain (_("expected string constant instead of %s"), token_buffer);
865 skip_to_char ('%');
866 return;
867 }
868 symval->class = token_sym;
869 symval->type_name = typename;
870 symval->user_token_number = usrtoknum;
871
872 symval->alias = symbol;
873 symbol->alias = symval;
874
875 /* symbol and symval combined are only one symbol. */
876 nsyms--;
877 }
878
879
880 static void
881 parse_muscle_decl (void)
882 {
883 int ch = ungetc (skip_white_space (), finput);
884 char *muscle_key;
885 char *muscle_value;
886
887 /* Read key. */
888 if (!isalpha (ch) && ch != '_')
889 {
890 complain (_("invalid %s declaration"), "%define");
891 skip_to_char ('%');
892 return;
893 }
894 copy_identifier (finput, &muscle_obstack);
895 obstack_1grow (&muscle_obstack, 0);
896 muscle_key = obstack_finish (&muscle_obstack);
897
898 /* Read value. */
899 ch = skip_white_space ();
900 if (ch != '"')
901 {
902 ungetc (ch, finput);
903 if (ch != EOF)
904 {
905 complain (_("invalid %s declaration"), "%define");
906 skip_to_char ('%');
907 return;
908 }
909 else
910 fatal (_("Premature EOF after %s"), "\"");
911 }
912 copy_string2 (finput, &muscle_obstack, '"', 0);
913 obstack_1grow (&muscle_obstack, 0);
914 muscle_value = obstack_finish (&muscle_obstack);
915
916 /* Store the (key, value) pair in the environment. */
917 muscle_insert (muscle_key, muscle_value);
918 }
919
920
921
922 /*---------------------------------.
923 | Parse a double quoted parameter. |
924 `---------------------------------*/
925
926 static const char *
927 parse_dquoted_param (const char *from)
928 {
929 struct obstack param_obstack;
930 const char *param = NULL;
931 int c;
932
933 obstack_init (&param_obstack);
934 c = skip_white_space ();
935
936 if (c != '"')
937 {
938 complain (_("invalid %s declaration"), from);
939 ungetc (c, finput);
940 skip_to_char ('%');
941 return NULL;
942 }
943
944 while ((c = literalchar ()) != '"')
945 obstack_1grow (&param_obstack, c);
946
947 obstack_1grow (&param_obstack, '\0');
948 param = obstack_finish (&param_obstack);
949
950 if (c != '"' || strlen (param) == 0)
951 {
952 complain (_("invalid %s declaration"), from);
953 if (c != '"')
954 ungetc (c, finput);
955 skip_to_char ('%');
956 return NULL;
957 }
958
959 return param;
960 }
961
962 /*----------------------------------.
963 | Parse what comes after %skeleton. |
964 `----------------------------------*/
965
966 static void
967 parse_skel_decl (void)
968 {
969 skeleton = parse_dquoted_param ("%skeleton");
970 }
971
972 /*----------------------------------------------------------------.
973 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
974 | any `%' declarations, and copy the contents of any `%{ ... %}' |
975 | groups to PRE_PROLOGUE_OBSTACK or POST_PROLOGUE_OBSTACK. |
976 `----------------------------------------------------------------*/
977
978 static void
979 read_declarations (void)
980 {
981 for (;;)
982 {
983 int c = skip_white_space ();
984
985 if (c == '%')
986 {
987 token_t tok = parse_percent_token ();
988
989 switch (tok)
990 {
991 case tok_two_percents:
992 return;
993
994 case tok_percent_left_curly:
995 if (!typed)
996 copy_definition (&pre_prologue_obstack);
997 else
998 copy_definition (&post_prologue_obstack);
999 break;
1000
1001 case tok_token:
1002 parse_token_decl (token_sym, nterm_sym);
1003 break;
1004
1005 case tok_nterm:
1006 parse_token_decl (nterm_sym, token_sym);
1007 break;
1008
1009 case tok_type:
1010 parse_type_decl ();
1011 break;
1012
1013 case tok_start:
1014 parse_start_decl ();
1015 break;
1016
1017 case tok_union:
1018 parse_union_decl ();
1019 break;
1020
1021 case tok_expect:
1022 parse_expect_decl ();
1023 break;
1024
1025 case tok_thong:
1026 parse_thong_decl ();
1027 break;
1028
1029 case tok_left:
1030 parse_assoc_decl (left_assoc);
1031 break;
1032
1033 case tok_right:
1034 parse_assoc_decl (right_assoc);
1035 break;
1036
1037 case tok_nonassoc:
1038 parse_assoc_decl (non_assoc);
1039 break;
1040
1041 case tok_define:
1042 parse_muscle_decl ();
1043 break;
1044
1045 case tok_skel:
1046 parse_skel_decl ();
1047 break;
1048
1049 case tok_noop:
1050 break;
1051
1052 case tok_stropt:
1053 case tok_intopt:
1054 case tok_obsolete:
1055 assert (0);
1056 break;
1057
1058 case tok_illegal:
1059 default:
1060 complain (_("unrecognized: %s"), token_buffer);
1061 skip_to_char ('%');
1062 }
1063 }
1064 else if (c == EOF)
1065 fatal (_("no input grammar"));
1066 else
1067 {
1068 char buf[] = "c";
1069 buf[0] = c;
1070 complain (_("unknown character: %s"), quote (buf));
1071 skip_to_char ('%');
1072 }
1073 }
1074 }
1075 \f
1076 /*------------------------------------------------------------------.
1077 | Assuming that a `{' has just been seen, copy everything up to the |
1078 | matching `}' into ACTION_OBSTACK. |
1079 | |
1080 | RULE_LENGTH is the number of values in the current rule so far, |
1081 | which says where to find `$0' with respect to the top of the |
1082 | stack. It is not the same as the rule->length in the case of mid |
1083 | rule actions. |
1084 | |
1085 | This routine is used for actions. |
1086 `------------------------------------------------------------------*/
1087
1088 static void
1089 parse_action (symbol_list *rule, int rule_length)
1090 {
1091 int count = 1;
1092 rule->action_line = lineno;
1093 while (count > 0)
1094 {
1095 int c;
1096 while ((c = getc (finput)) != '}')
1097 switch (c)
1098 {
1099 case '\n':
1100 copy_character (&action_obstack, c);
1101 ++lineno;
1102 break;
1103
1104 case '{':
1105 copy_character (&action_obstack, c);
1106 ++count;
1107 break;
1108
1109 case '\'':
1110 case '"':
1111 copy_string (finput, &action_obstack, c);
1112 break;
1113
1114 case '/':
1115 copy_comment (finput, &action_obstack);
1116 break;
1117
1118 case '$':
1119 copy_dollar (finput, &action_obstack, rule, rule_length);
1120 break;
1121
1122 case '@':
1123 copy_at (finput, &action_obstack, rule_length);
1124 break;
1125
1126 case EOF:
1127 fatal (_("unmatched %s"), "`{'");
1128
1129 default:
1130 copy_character (&action_obstack, c);
1131 }
1132
1133 /* Above loop exits when C is '}'. */
1134 if (--count)
1135 copy_character (&action_obstack, c);
1136 }
1137
1138 obstack_1grow (&action_obstack, '\0');
1139 rule->action = obstack_finish (&action_obstack);
1140 }
1141
1142 \f
1143
1144 /*-------------------------------------------------------------------.
1145 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1146 | with the user's names. |
1147 `-------------------------------------------------------------------*/
1148
1149 static symbol_t *
1150 gensym (void)
1151 {
1152 /* Incremented for each generated symbol */
1153 static int gensym_count = 0;
1154 static char buf[256];
1155
1156 symbol_t *sym;
1157
1158 sprintf (buf, "@%d", ++gensym_count);
1159 token_buffer = buf;
1160 sym = getsym (token_buffer);
1161 sym->class = nterm_sym;
1162 sym->number = nvars++;
1163 return sym;
1164 }
1165 \f
1166 /*-------------------------------------------------------------------.
1167 | Parse the input grammar into a one symbol_list structure. Each |
1168 | rule is represented by a sequence of symbols: the left hand side |
1169 | followed by the contents of the right hand side, followed by a |
1170 | null pointer instead of a symbol to terminate the rule. The next |
1171 | symbol is the lhs of the following rule. |
1172 | |
1173 | All actions are copied out, labelled by the rule number they apply |
1174 | to. |
1175 | |
1176 | Bison used to allow some %directives in the rules sections, but |
1177 | this is no longer consider appropriate: (i) the documented grammar |
1178 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1179 | recovery for %directives consists in skipping the junk until a `%' |
1180 | is seen and helrp synchronizing. This scheme is definitely wrong |
1181 | in the rules section. |
1182 `-------------------------------------------------------------------*/
1183
1184 static void
1185 readgram (void)
1186 {
1187 token_t t;
1188 symbol_t *lhs = NULL;
1189 symbol_list *p = NULL;
1190 symbol_list *p1 = NULL;
1191
1192 /* Points to first symbol_list of current rule. its symbol is the
1193 lhs of the rule. */
1194 symbol_list *crule = NULL;
1195 /* Points to the symbol_list preceding crule. */
1196 symbol_list *crule1 = NULL;
1197
1198 t = lex ();
1199
1200 while (t != tok_two_percents && t != tok_eof)
1201 if (t == tok_identifier || t == tok_bar)
1202 {
1203 int action_flag = 0;
1204 /* Number of symbols in rhs of this rule so far */
1205 int rulelength = 0;
1206 int xactions = 0; /* JF for error checking */
1207 symbol_t *first_rhs = 0;
1208
1209 if (t == tok_identifier)
1210 {
1211 lhs = symval;
1212
1213 if (!start_flag)
1214 {
1215 startsymbol = lhs;
1216 start_flag = 1;
1217 }
1218
1219 t = lex ();
1220 if (t != tok_colon)
1221 {
1222 complain (_("ill-formed rule: initial symbol not followed by colon"));
1223 unlex (t);
1224 }
1225 }
1226
1227 if (nrules == 0 && t == tok_bar)
1228 {
1229 complain (_("grammar starts with vertical bar"));
1230 lhs = symval; /* BOGUS: use a random symval */
1231 }
1232 /* start a new rule and record its lhs. */
1233
1234 ++nrules;
1235 ++nritems;
1236
1237 p = symbol_list_new (lhs);
1238
1239 crule1 = p1;
1240 if (p1)
1241 p1->next = p;
1242 else
1243 grammar = p;
1244
1245 p1 = p;
1246 crule = p;
1247
1248 /* mark the rule's lhs as a nonterminal if not already so. */
1249
1250 if (lhs->class == unknown_sym)
1251 {
1252 lhs->class = nterm_sym;
1253 lhs->number = nvars;
1254 ++nvars;
1255 }
1256 else if (lhs->class == token_sym)
1257 complain (_("rule given for %s, which is a token"), lhs->tag);
1258
1259 /* read the rhs of the rule. */
1260
1261 for (;;)
1262 {
1263 t = lex ();
1264 if (t == tok_prec)
1265 {
1266 t = lex ();
1267 crule->ruleprec = symval;
1268 t = lex ();
1269 }
1270
1271 if (!(t == tok_identifier || t == tok_left_curly))
1272 break;
1273
1274 /* If next token is an identifier, see if a colon follows it.
1275 If one does, exit this rule now. */
1276 if (t == tok_identifier)
1277 {
1278 symbol_t *ssave;
1279 token_t t1;
1280
1281 ssave = symval;
1282 t1 = lex ();
1283 unlex (t1);
1284 symval = ssave;
1285 if (t1 == tok_colon)
1286 {
1287 warn (_("previous rule lacks an ending `;'"));
1288 break;
1289 }
1290
1291 if (!first_rhs) /* JF */
1292 first_rhs = symval;
1293 /* Not followed by colon =>
1294 process as part of this rule's rhs. */
1295 }
1296
1297 /* If we just passed an action, that action was in the middle
1298 of a rule, so make a dummy rule to reduce it to a
1299 non-terminal. */
1300 if (action_flag)
1301 {
1302 /* Since the action was written out with this rule's
1303 number, we must give the new rule this number by
1304 inserting the new rule before it. */
1305
1306 /* Make a dummy nonterminal, a gensym. */
1307 symbol_t *sdummy = gensym ();
1308
1309 /* Make a new rule, whose body is empty, before the
1310 current one, so that the action just read can
1311 belong to it. */
1312 ++nrules;
1313 ++nritems;
1314 p = symbol_list_new (sdummy);
1315 /* Attach its lineno to that of the host rule. */
1316 p->line = crule->line;
1317 /* Move the action from the host rule to this one. */
1318 p->action = crule->action;
1319 p->action_line = crule->action_line;
1320 crule->action = NULL;
1321
1322 if (crule1)
1323 crule1->next = p;
1324 else
1325 grammar = p;
1326 /* End of the rule. */
1327 crule1 = symbol_list_new (NULL);
1328 crule1->next = crule;
1329
1330 p->next = crule1;
1331
1332 /* Insert the dummy generated by that rule into this
1333 rule. */
1334 ++nritems;
1335 p = symbol_list_new (sdummy);
1336 p1->next = p;
1337 p1 = p;
1338
1339 action_flag = 0;
1340 }
1341
1342 if (t == tok_identifier)
1343 {
1344 ++nritems;
1345 p = symbol_list_new (symval);
1346 p1->next = p;
1347 p1 = p;
1348 }
1349 else /* handle an action. */
1350 {
1351 parse_action (crule, rulelength);
1352 action_flag = 1;
1353 ++xactions; /* JF */
1354 }
1355 ++rulelength;
1356 } /* end of read rhs of rule */
1357
1358 /* Put an empty link in the list to mark the end of this rule */
1359 p = symbol_list_new (NULL);
1360 p1->next = p;
1361 p1 = p;
1362
1363 if (t == tok_prec)
1364 {
1365 complain (_("two @prec's in a row"));
1366 t = lex ();
1367 crule->ruleprec = symval;
1368 t = lex ();
1369 }
1370
1371 if (t == tok_left_curly)
1372 {
1373 /* This case never occurs -wjh */
1374 if (action_flag)
1375 complain (_("two actions at end of one rule"));
1376 parse_action (crule, rulelength);
1377 action_flag = 1;
1378 ++xactions; /* -wjh */
1379 t = lex ();
1380 }
1381 /* If $$ is being set in default way, report if any type
1382 mismatch. */
1383 else if (!xactions
1384 && first_rhs && lhs->type_name != first_rhs->type_name)
1385 {
1386 if (lhs->type_name == 0
1387 || first_rhs->type_name == 0
1388 || strcmp (lhs->type_name, first_rhs->type_name))
1389 complain (_("type clash (`%s' `%s') on default action"),
1390 lhs->type_name ? lhs->type_name : "",
1391 first_rhs->type_name ? first_rhs->type_name : "");
1392 }
1393 /* Warn if there is no default for $$ but we need one. */
1394 else if (!xactions && !first_rhs && lhs->type_name != 0)
1395 complain (_("empty rule for typed nonterminal, and no action"));
1396 if (t == tok_two_percents || t == tok_eof)
1397 warn (_("previous rule lacks an ending `;'"));
1398 if (t == tok_semicolon)
1399 t = lex ();
1400 }
1401 else
1402 {
1403 complain (_("invalid input: %s"), quote (token_buffer));
1404 t = lex ();
1405 }
1406
1407 /* grammar has been read. Do some checking */
1408
1409 if (nrules == 0)
1410 fatal (_("no rules in the input grammar"));
1411
1412 /* Report any undefined symbols and consider them nonterminals. */
1413 symbols_check_defined ();
1414
1415 /* Insert the initial rule, which line is that of the first rule
1416 (not that of the start symbol):
1417
1418 axiom: %start EOF. */
1419 p = symbol_list_new (axiom);
1420 p->line = grammar->line;
1421 p->next = symbol_list_new (startsymbol);
1422 p->next->next = symbol_list_new (eoftoken);
1423 p->next->next->next = symbol_list_new (NULL);
1424 p->next->next->next->next = grammar;
1425 nrules += 1;
1426 nritems += 3;
1427 grammar = p;
1428
1429 if (nsyms > SHRT_MAX)
1430 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1431 SHRT_MAX);
1432
1433 assert (nsyms == ntokens + nvars);
1434 }
1435
1436 /* At the end of the grammar file, some C source code must
1437 be stored. It is going to be associated to the epilogue
1438 directive. */
1439 static void
1440 read_additionnal_code (void)
1441 {
1442 int c;
1443 struct obstack el_obstack;
1444
1445 obstack_init (&el_obstack);
1446
1447 if (!no_lines_flag)
1448 {
1449 obstack_fgrow2 (&el_obstack, muscle_find ("linef"),
1450 lineno, quotearg_style (c_quoting_style,
1451 muscle_find ("filename")));
1452 }
1453
1454 while ((c = getc (finput)) != EOF)
1455 copy_character (&el_obstack, c);
1456
1457 obstack_1grow (&el_obstack, 0);
1458 muscle_insert ("epilogue", obstack_finish (&el_obstack));
1459 }
1460
1461 \f
1462 /*---------------------------------------------------------------.
1463 | Convert the rules into the representation using RRHS, RLHS and |
1464 | RITEM. |
1465 `---------------------------------------------------------------*/
1466
1467 static void
1468 packgram (void)
1469 {
1470 unsigned int itemno;
1471 int ruleno;
1472 symbol_list *p;
1473
1474 ritem = XCALLOC (item_number_t, nritems);
1475 rules = XCALLOC (rule_t, nrules) - 1;
1476
1477 itemno = 0;
1478 ruleno = 1;
1479
1480 p = grammar;
1481 while (p)
1482 {
1483 symbol_t *ruleprec = p->ruleprec;
1484 rules[ruleno].user_number = ruleno;
1485 rules[ruleno].number = ruleno;
1486 rules[ruleno].lhs = p->sym;
1487 rules[ruleno].rhs = ritem + itemno;
1488 rules[ruleno].line = p->line;
1489 rules[ruleno].useful = TRUE;
1490 rules[ruleno].action = p->action;
1491 rules[ruleno].action_line = p->action_line;
1492
1493 p = p->next;
1494 while (p && p->sym)
1495 {
1496 /* item_number_t = symbol_number_t.
1497 But the former needs to contain more: negative rule numbers. */
1498 ritem[itemno++] = symbol_number_as_item_number (p->sym->number);
1499 /* A rule gets by default the precedence and associativity
1500 of the last token in it. */
1501 if (p->sym->class == token_sym)
1502 rules[ruleno].prec = p->sym;
1503 if (p)
1504 p = p->next;
1505 }
1506
1507 /* If this rule has a %prec,
1508 the specified symbol's precedence replaces the default. */
1509 if (ruleprec)
1510 {
1511 rules[ruleno].precsym = ruleprec;
1512 rules[ruleno].prec = ruleprec;
1513 }
1514 ritem[itemno++] = -ruleno;
1515 ++ruleno;
1516
1517 if (p)
1518 p = p->next;
1519 }
1520
1521 assert (itemno == nritems);
1522
1523 if (trace_flag)
1524 ritem_print (stderr);
1525 }
1526 \f
1527 /*------------------------------------------------------------------.
1528 | Read in the grammar specification and record it in the format |
1529 | described in gram.h. All actions are copied into ACTION_OBSTACK, |
1530 | in each case forming the body of a C function (YYACTION) which |
1531 | contains a switch statement to decide which action to execute. |
1532 `------------------------------------------------------------------*/
1533
1534 void
1535 reader (void)
1536 {
1537 lex_init ();
1538 lineno = 1;
1539
1540 /* Initialize the muscle obstack. */
1541 obstack_init (&muscle_obstack);
1542
1543 /* Initialize the symbol table. */
1544 symbols_new ();
1545
1546 /* Construct the axiom symbol. */
1547 axiom = getsym ("$axiom");
1548 axiom->class = nterm_sym;
1549 axiom->number = nvars++;
1550
1551 /* Construct the error token */
1552 errtoken = getsym ("error");
1553 errtoken->class = token_sym;
1554 errtoken->number = ntokens++;
1555
1556 /* Construct a token that represents all undefined literal tokens.
1557 It is always token number 2. */
1558 undeftoken = getsym ("$undefined.");
1559 undeftoken->class = token_sym;
1560 undeftoken->number = ntokens++;
1561
1562 /* Initialize the obstacks. */
1563 obstack_init (&action_obstack);
1564 obstack_init (&output_obstack);
1565 obstack_init (&pre_prologue_obstack);
1566 obstack_init (&post_prologue_obstack);
1567
1568 finput = xfopen (infile, "r");
1569
1570 /* Read the declaration section. Copy %{ ... %} groups to
1571 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1572 etc. found there. */
1573 read_declarations ();
1574
1575 /* If the user did not define her EOFTOKEN, do it now. */
1576 if (!eoftoken)
1577 {
1578 eoftoken = getsym ("$");
1579 eoftoken->class = token_sym;
1580 eoftoken->number = 0;
1581 /* Value specified by POSIX. */
1582 eoftoken->user_token_number = 0;
1583 }
1584
1585 /* Read in the grammar, build grammar in list form. Write out
1586 actions. */
1587 readgram ();
1588 /* Some C code is given at the end of the grammar file. */
1589 read_additionnal_code ();
1590
1591 lex_free ();
1592 xfclose (finput);
1593
1594 /* Assign the symbols their symbol numbers. Write #defines for the
1595 token symbols into FDEFINES if requested. */
1596 symbols_pack ();
1597
1598 /* Convert the grammar into the format described in gram.h. */
1599 packgram ();
1600
1601 /* The grammar as a symbol_list is no longer needed. */
1602 LIST_FREE (symbol_list, grammar);
1603 }