]> git.saurik.com Git - bison.git/blob - src/reader.c
5587c32acf5ab1dd60aa27603375cec07048af26
[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. |
488 `------------------------------------------*/
489
490 static void
491 parse_token_decl (symbol_class class)
492 {
493 token_t token = tok_undef;
494 char *typename = NULL;
495
496 /* The symbol being defined. */
497 symbol_t *symbol = NULL;
498
499 /* After `%token' and `%nterm', any number of symbols maybe be
500 defined. */
501 for (;;)
502 {
503 int tmp_char = ungetc (skip_white_space (), finput);
504
505 /* `%' (for instance from `%token', or from `%%' etc.) is the
506 only valid means to end this declaration. */
507 if (tmp_char == '%')
508 return;
509 if (tmp_char == EOF)
510 fatal (_("Premature EOF after %s"), token_buffer);
511
512 token = lex ();
513 switch (token)
514 {
515 case tok_comma:
516 symbol = NULL;
517 break;
518
519 case tok_typename:
520 typename = xstrdup (token_buffer);
521 symbol = NULL;
522 break;
523
524 case tok_identifier:
525 if (*symval->tag == '\"' && symbol)
526 {
527 symbol_make_alias (symbol, symval, typename);
528 symbol = NULL;
529 }
530 else
531 {
532 symbol = symval;
533 symbol_class_set (symbol, class);
534 if (typename)
535 symbol_type_set (symbol, typename);
536 }
537 break;
538
539 case tok_number:
540 symbol_user_token_number_set (symbol, numval);
541 break;
542
543 default:
544 complain (_("`%s' is invalid in %s"),
545 token_buffer,
546 (class == token_sym) ? "%token" : "%nterm");
547 skip_to_char ('%');
548 }
549 }
550
551 }
552
553
554 /*------------------------------.
555 | Parse what comes after %start |
556 `------------------------------*/
557
558 static void
559 parse_start_decl (void)
560 {
561 if (start_flag)
562 complain (_("multiple %s declarations"), "%start");
563 if (lex () != tok_identifier)
564 complain (_("invalid %s declaration"), "%start");
565 else
566 {
567 start_flag = 1;
568 startsymbol = symval;
569 }
570 }
571
572 /*-----------------------------------------------------------.
573 | read in a %type declaration and record its information for |
574 | get_type_name to access |
575 `-----------------------------------------------------------*/
576
577 static void
578 parse_type_decl (void)
579 {
580 char *name;
581
582 if (lex () != tok_typename)
583 {
584 complain ("%s", _("%type declaration has no <typename>"));
585 skip_to_char ('%');
586 return;
587 }
588
589 name = xstrdup (token_buffer);
590
591 for (;;)
592 {
593 token_t t;
594 int tmp_char = ungetc (skip_white_space (), finput);
595
596 if (tmp_char == '%')
597 return;
598 if (tmp_char == EOF)
599 fatal (_("Premature EOF after %s"), token_buffer);
600
601 t = lex ();
602
603 switch (t)
604 {
605 case tok_comma:
606 case tok_semicolon:
607 break;
608
609 case tok_identifier:
610 symbol_type_set (symval, name);
611 break;
612
613 default:
614 complain (_("invalid %%type declaration due to item: %s"),
615 token_buffer);
616 skip_to_char ('%');
617 }
618 }
619 }
620
621
622
623 /*----------------------------------------------------------------.
624 | Read in a %left, %right or %nonassoc declaration and record its |
625 | information. |
626 `----------------------------------------------------------------*/
627
628 static void
629 parse_assoc_decl (associativity assoc)
630 {
631 char *name = NULL;
632 int prev = 0;
633
634 /* Assign a new precedence level, never 0. */
635 ++lastprec;
636
637 for (;;)
638 {
639 token_t t;
640 int tmp_char = ungetc (skip_white_space (), finput);
641
642 if (tmp_char == '%')
643 return;
644 if (tmp_char == EOF)
645 fatal (_("Premature EOF after %s"), token_buffer);
646
647 t = lex ();
648
649 switch (t)
650 {
651 case tok_typename:
652 name = xstrdup (token_buffer);
653 break;
654
655 case tok_comma:
656 break;
657
658 case tok_identifier:
659 if (symval->class == nterm_sym)
660 complain (_("symbol %s redefined"), symval->tag);
661 if (symval->number == NUMBER_UNDEFINED)
662 {
663 symval->number = ntokens++;
664 symval->class = token_sym;
665 }
666 symbol_precedence_set (symval, lastprec, assoc);
667 if (name)
668 symbol_type_set (symval, name);
669 break;
670
671 case tok_number:
672 if (prev == tok_identifier)
673 {
674 symval->user_token_number = numval;
675 }
676 else
677 {
678 complain
679 (_("invalid text (%s) - number should be after identifier"),
680 token_buffer);
681 skip_to_char ('%');
682 }
683 break;
684
685 case tok_semicolon:
686 return;
687
688 default:
689 complain (_("unexpected item: %s"), token_buffer);
690 skip_to_char ('%');
691 }
692
693 prev = t;
694 }
695 }
696
697
698
699 /*--------------------------------------------------------------.
700 | Copy the union declaration into the stype muscle |
701 | (and fdefines), where it is made into the definition of |
702 | YYSTYPE, the type of elements of the parser value stack. |
703 `--------------------------------------------------------------*/
704
705 static void
706 parse_union_decl (void)
707 {
708 int c;
709 int count = 0;
710 bool done = FALSE;
711 struct obstack union_obstack;
712 if (typed)
713 complain (_("multiple %s declarations"), "%union");
714
715 typed = 1;
716
717 MUSCLE_INSERT_INT ("stype_line", lineno);
718 obstack_init (&union_obstack);
719 obstack_sgrow (&union_obstack, "union");
720
721 while (!done)
722 {
723 c = xgetc (finput);
724
725 /* If C contains '/', it is output by copy_comment (). */
726 if (c != '/')
727 obstack_1grow (&union_obstack, c);
728
729 switch (c)
730 {
731 case '\n':
732 ++lineno;
733 break;
734
735 case '/':
736 copy_comment (finput, &union_obstack);
737 break;
738
739 case '{':
740 ++count;
741 break;
742
743 case '}':
744 /* FIXME: Errr. How could this happen???. --akim */
745 if (count == 0)
746 complain (_("unmatched %s"), "`}'");
747 count--;
748 if (!count)
749 done = TRUE;
750 break;
751 }
752 }
753
754 /* JF don't choke on trailing semi */
755 c = skip_white_space ();
756 if (c != ';')
757 ungetc (c, finput);
758 obstack_1grow (&union_obstack, 0);
759 muscle_insert ("stype", obstack_finish (&union_obstack));
760 }
761
762
763 /*-------------------------------------------------------.
764 | Parse the declaration %expect N which says to expect N |
765 | shift-reduce conflicts. |
766 `-------------------------------------------------------*/
767
768 static void
769 parse_expect_decl (void)
770 {
771 int c = skip_white_space ();
772 ungetc (c, finput);
773
774 if (!isdigit (c))
775 complain (_("argument of %%expect is not an integer"));
776 else
777 expected_conflicts = read_signed_integer (finput);
778 }
779
780
781 static void
782 parse_muscle_decl (void)
783 {
784 int ch = ungetc (skip_white_space (), finput);
785 char *muscle_key;
786 char *muscle_value;
787
788 /* Read key. */
789 if (!isalpha (ch) && ch != '_')
790 {
791 complain (_("invalid %s declaration"), "%define");
792 skip_to_char ('%');
793 return;
794 }
795 copy_identifier (finput, &muscle_obstack);
796 obstack_1grow (&muscle_obstack, 0);
797 muscle_key = obstack_finish (&muscle_obstack);
798
799 /* Read value. */
800 ch = skip_white_space ();
801 if (ch != '"')
802 {
803 ungetc (ch, finput);
804 if (ch != EOF)
805 {
806 complain (_("invalid %s declaration"), "%define");
807 skip_to_char ('%');
808 return;
809 }
810 else
811 fatal (_("Premature EOF after %s"), "\"");
812 }
813 copy_string2 (finput, &muscle_obstack, '"', 0);
814 obstack_1grow (&muscle_obstack, 0);
815 muscle_value = obstack_finish (&muscle_obstack);
816
817 /* Store the (key, value) pair in the environment. */
818 muscle_insert (muscle_key, muscle_value);
819 }
820
821
822
823 /*---------------------------------.
824 | Parse a double quoted parameter. |
825 `---------------------------------*/
826
827 static const char *
828 parse_dquoted_param (const char *from)
829 {
830 struct obstack param_obstack;
831 const char *param = NULL;
832 int c;
833
834 obstack_init (&param_obstack);
835 c = skip_white_space ();
836
837 if (c != '"')
838 {
839 complain (_("invalid %s declaration"), from);
840 ungetc (c, finput);
841 skip_to_char ('%');
842 return NULL;
843 }
844
845 while ((c = literalchar ()) != '"')
846 obstack_1grow (&param_obstack, c);
847
848 obstack_1grow (&param_obstack, '\0');
849 param = obstack_finish (&param_obstack);
850
851 if (c != '"' || strlen (param) == 0)
852 {
853 complain (_("invalid %s declaration"), from);
854 if (c != '"')
855 ungetc (c, finput);
856 skip_to_char ('%');
857 return NULL;
858 }
859
860 return param;
861 }
862
863 /*----------------------------------.
864 | Parse what comes after %skeleton. |
865 `----------------------------------*/
866
867 static void
868 parse_skel_decl (void)
869 {
870 skeleton = parse_dquoted_param ("%skeleton");
871 }
872
873 /*----------------------------------------------------------------.
874 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
875 | any `%' declarations, and copy the contents of any `%{ ... %}' |
876 | groups to PRE_PROLOGUE_OBSTACK or POST_PROLOGUE_OBSTACK. |
877 `----------------------------------------------------------------*/
878
879 static void
880 read_declarations (void)
881 {
882 for (;;)
883 {
884 int c = skip_white_space ();
885
886 if (c == '%')
887 {
888 token_t tok = parse_percent_token ();
889
890 switch (tok)
891 {
892 case tok_two_percents:
893 return;
894
895 case tok_percent_left_curly:
896 if (!typed)
897 copy_definition (&pre_prologue_obstack);
898 else
899 copy_definition (&post_prologue_obstack);
900 break;
901
902 case tok_token:
903 parse_token_decl (token_sym);
904 break;
905
906 case tok_nterm:
907 parse_token_decl (nterm_sym);
908 break;
909
910 case tok_type:
911 parse_type_decl ();
912 break;
913
914 case tok_start:
915 parse_start_decl ();
916 break;
917
918 case tok_union:
919 parse_union_decl ();
920 break;
921
922 case tok_expect:
923 parse_expect_decl ();
924 break;
925
926 case tok_left:
927 parse_assoc_decl (left_assoc);
928 break;
929
930 case tok_right:
931 parse_assoc_decl (right_assoc);
932 break;
933
934 case tok_nonassoc:
935 parse_assoc_decl (non_assoc);
936 break;
937
938 case tok_define:
939 parse_muscle_decl ();
940 break;
941
942 case tok_skel:
943 parse_skel_decl ();
944 break;
945
946 case tok_noop:
947 break;
948
949 case tok_stropt:
950 case tok_intopt:
951 case tok_obsolete:
952 assert (0);
953 break;
954
955 case tok_illegal:
956 default:
957 complain (_("unrecognized: %s"), token_buffer);
958 skip_to_char ('%');
959 }
960 }
961 else if (c == EOF)
962 fatal (_("no input grammar"));
963 else
964 {
965 char buf[] = "c";
966 buf[0] = c;
967 complain (_("unknown character: %s"), quote (buf));
968 skip_to_char ('%');
969 }
970 }
971 }
972 \f
973 /*------------------------------------------------------------------.
974 | Assuming that a `{' has just been seen, copy everything up to the |
975 | matching `}' into ACTION_OBSTACK. |
976 | |
977 | RULE_LENGTH is the number of values in the current rule so far, |
978 | which says where to find `$0' with respect to the top of the |
979 | stack. It is not the same as the rule->length in the case of mid |
980 | rule actions. |
981 | |
982 | This routine is used for actions. |
983 `------------------------------------------------------------------*/
984
985 static void
986 parse_action (symbol_list *rule, int rule_length)
987 {
988 int count = 1;
989 rule->action_line = lineno;
990 while (count > 0)
991 {
992 int c;
993 while ((c = getc (finput)) != '}')
994 switch (c)
995 {
996 case '\n':
997 copy_character (&action_obstack, c);
998 ++lineno;
999 break;
1000
1001 case '{':
1002 copy_character (&action_obstack, c);
1003 ++count;
1004 break;
1005
1006 case '\'':
1007 case '"':
1008 copy_string (finput, &action_obstack, c);
1009 break;
1010
1011 case '/':
1012 copy_comment (finput, &action_obstack);
1013 break;
1014
1015 case '$':
1016 copy_dollar (finput, &action_obstack, rule, rule_length);
1017 break;
1018
1019 case '@':
1020 copy_at (finput, &action_obstack, rule_length);
1021 break;
1022
1023 case EOF:
1024 fatal (_("unmatched %s"), "`{'");
1025
1026 default:
1027 copy_character (&action_obstack, c);
1028 }
1029
1030 /* Above loop exits when C is '}'. */
1031 if (--count)
1032 copy_character (&action_obstack, c);
1033 }
1034
1035 obstack_1grow (&action_obstack, '\0');
1036 rule->action = obstack_finish (&action_obstack);
1037 }
1038
1039 \f
1040
1041 /*-------------------------------------------------------------------.
1042 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1043 | with the user's names. |
1044 `-------------------------------------------------------------------*/
1045
1046 static symbol_t *
1047 gensym (void)
1048 {
1049 /* Incremented for each generated symbol */
1050 static int gensym_count = 0;
1051 static char buf[256];
1052
1053 symbol_t *sym;
1054
1055 sprintf (buf, "@%d", ++gensym_count);
1056 token_buffer = buf;
1057 sym = getsym (token_buffer);
1058 sym->class = nterm_sym;
1059 sym->number = nvars++;
1060 return sym;
1061 }
1062 \f
1063 /*-------------------------------------------------------------------.
1064 | Parse the input grammar into a one symbol_list structure. Each |
1065 | rule is represented by a sequence of symbols: the left hand side |
1066 | followed by the contents of the right hand side, followed by a |
1067 | null pointer instead of a symbol to terminate the rule. The next |
1068 | symbol is the lhs of the following rule. |
1069 | |
1070 | All actions are copied out, labelled by the rule number they apply |
1071 | to. |
1072 | |
1073 | Bison used to allow some %directives in the rules sections, but |
1074 | this is no longer consider appropriate: (i) the documented grammar |
1075 | doesn't claim it, (ii), it would promote bad style, (iii), error |
1076 | recovery for %directives consists in skipping the junk until a `%' |
1077 | is seen and helrp synchronizing. This scheme is definitely wrong |
1078 | in the rules section. |
1079 `-------------------------------------------------------------------*/
1080
1081 static void
1082 readgram (void)
1083 {
1084 token_t t;
1085 symbol_t *lhs = NULL;
1086 symbol_list *p = NULL;
1087 symbol_list *p1 = NULL;
1088
1089 /* Points to first symbol_list of current rule. its symbol is the
1090 lhs of the rule. */
1091 symbol_list *crule = NULL;
1092 /* Points to the symbol_list preceding crule. */
1093 symbol_list *crule1 = NULL;
1094
1095 t = lex ();
1096
1097 while (t != tok_two_percents && t != tok_eof)
1098 if (t == tok_identifier || t == tok_bar)
1099 {
1100 int action_flag = 0;
1101 /* Number of symbols in rhs of this rule so far */
1102 int rulelength = 0;
1103 int xactions = 0; /* JF for error checking */
1104 symbol_t *first_rhs = 0;
1105
1106 if (t == tok_identifier)
1107 {
1108 lhs = symval;
1109
1110 if (!start_flag)
1111 {
1112 startsymbol = lhs;
1113 start_flag = 1;
1114 }
1115
1116 t = lex ();
1117 if (t != tok_colon)
1118 {
1119 complain (_("ill-formed rule: initial symbol not followed by colon"));
1120 unlex (t);
1121 }
1122 }
1123
1124 if (nrules == 0 && t == tok_bar)
1125 {
1126 complain (_("grammar starts with vertical bar"));
1127 lhs = symval; /* BOGUS: use a random symval */
1128 }
1129 /* start a new rule and record its lhs. */
1130
1131 ++nrules;
1132 ++nritems;
1133
1134 p = symbol_list_new (lhs);
1135
1136 crule1 = p1;
1137 if (p1)
1138 p1->next = p;
1139 else
1140 grammar = p;
1141
1142 p1 = p;
1143 crule = p;
1144
1145 /* mark the rule's lhs as a nonterminal if not already so. */
1146
1147 if (lhs->class == unknown_sym)
1148 {
1149 lhs->class = nterm_sym;
1150 lhs->number = nvars;
1151 ++nvars;
1152 }
1153 else if (lhs->class == token_sym)
1154 complain (_("rule given for %s, which is a token"), lhs->tag);
1155
1156 /* read the rhs of the rule. */
1157
1158 for (;;)
1159 {
1160 t = lex ();
1161 if (t == tok_prec)
1162 {
1163 t = lex ();
1164 crule->ruleprec = symval;
1165 t = lex ();
1166 }
1167
1168 if (!(t == tok_identifier || t == tok_left_curly))
1169 break;
1170
1171 /* If next token is an identifier, see if a colon follows it.
1172 If one does, exit this rule now. */
1173 if (t == tok_identifier)
1174 {
1175 symbol_t *ssave;
1176 token_t t1;
1177
1178 ssave = symval;
1179 t1 = lex ();
1180 unlex (t1);
1181 symval = ssave;
1182 if (t1 == tok_colon)
1183 {
1184 warn (_("previous rule lacks an ending `;'"));
1185 break;
1186 }
1187
1188 if (!first_rhs) /* JF */
1189 first_rhs = symval;
1190 /* Not followed by colon =>
1191 process as part of this rule's rhs. */
1192 }
1193
1194 /* If we just passed an action, that action was in the middle
1195 of a rule, so make a dummy rule to reduce it to a
1196 non-terminal. */
1197 if (action_flag)
1198 {
1199 /* Since the action was written out with this rule's
1200 number, we must give the new rule this number by
1201 inserting the new rule before it. */
1202
1203 /* Make a dummy nonterminal, a gensym. */
1204 symbol_t *sdummy = gensym ();
1205
1206 /* Make a new rule, whose body is empty, before the
1207 current one, so that the action just read can
1208 belong to it. */
1209 ++nrules;
1210 ++nritems;
1211 p = symbol_list_new (sdummy);
1212 /* Attach its lineno to that of the host rule. */
1213 p->line = crule->line;
1214 /* Move the action from the host rule to this one. */
1215 p->action = crule->action;
1216 p->action_line = crule->action_line;
1217 crule->action = NULL;
1218
1219 if (crule1)
1220 crule1->next = p;
1221 else
1222 grammar = p;
1223 /* End of the rule. */
1224 crule1 = symbol_list_new (NULL);
1225 crule1->next = crule;
1226
1227 p->next = crule1;
1228
1229 /* Insert the dummy generated by that rule into this
1230 rule. */
1231 ++nritems;
1232 p = symbol_list_new (sdummy);
1233 p1->next = p;
1234 p1 = p;
1235
1236 action_flag = 0;
1237 }
1238
1239 if (t == tok_identifier)
1240 {
1241 ++nritems;
1242 p = symbol_list_new (symval);
1243 p1->next = p;
1244 p1 = p;
1245 }
1246 else /* handle an action. */
1247 {
1248 parse_action (crule, rulelength);
1249 action_flag = 1;
1250 ++xactions; /* JF */
1251 }
1252 ++rulelength;
1253 } /* end of read rhs of rule */
1254
1255 /* Put an empty link in the list to mark the end of this rule */
1256 p = symbol_list_new (NULL);
1257 p1->next = p;
1258 p1 = p;
1259
1260 if (t == tok_prec)
1261 {
1262 complain (_("two @prec's in a row"));
1263 t = lex ();
1264 crule->ruleprec = symval;
1265 t = lex ();
1266 }
1267
1268 if (t == tok_left_curly)
1269 {
1270 /* This case never occurs -wjh */
1271 if (action_flag)
1272 complain (_("two actions at end of one rule"));
1273 parse_action (crule, rulelength);
1274 action_flag = 1;
1275 ++xactions; /* -wjh */
1276 t = lex ();
1277 }
1278 /* If $$ is being set in default way, report if any type
1279 mismatch. */
1280 else if (!xactions
1281 && first_rhs && lhs->type_name != first_rhs->type_name)
1282 {
1283 if (lhs->type_name == 0
1284 || first_rhs->type_name == 0
1285 || strcmp (lhs->type_name, first_rhs->type_name))
1286 complain (_("type clash (`%s' `%s') on default action"),
1287 lhs->type_name ? lhs->type_name : "",
1288 first_rhs->type_name ? first_rhs->type_name : "");
1289 }
1290 /* Warn if there is no default for $$ but we need one. */
1291 else if (!xactions && !first_rhs && lhs->type_name != 0)
1292 complain (_("empty rule for typed nonterminal, and no action"));
1293 if (t == tok_two_percents || t == tok_eof)
1294 warn (_("previous rule lacks an ending `;'"));
1295 if (t == tok_semicolon)
1296 t = lex ();
1297 }
1298 else
1299 {
1300 complain (_("invalid input: %s"), quote (token_buffer));
1301 t = lex ();
1302 }
1303
1304 /* grammar has been read. Do some checking */
1305
1306 if (nrules == 0)
1307 fatal (_("no rules in the input grammar"));
1308
1309 /* Report any undefined symbols and consider them nonterminals. */
1310 symbols_check_defined ();
1311
1312 /* Insert the initial rule, which line is that of the first rule
1313 (not that of the start symbol):
1314
1315 axiom: %start EOF. */
1316 p = symbol_list_new (axiom);
1317 p->line = grammar->line;
1318 p->next = symbol_list_new (startsymbol);
1319 p->next->next = symbol_list_new (eoftoken);
1320 p->next->next->next = symbol_list_new (NULL);
1321 p->next->next->next->next = grammar;
1322 nrules += 1;
1323 nritems += 3;
1324 grammar = p;
1325
1326 if (nsyms > SHRT_MAX)
1327 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1328 SHRT_MAX);
1329
1330 assert (nsyms == ntokens + nvars);
1331 }
1332
1333 /* At the end of the grammar file, some C source code must
1334 be stored. It is going to be associated to the epilogue
1335 directive. */
1336 static void
1337 read_additionnal_code (void)
1338 {
1339 int c;
1340 struct obstack el_obstack;
1341
1342 obstack_init (&el_obstack);
1343
1344 if (!no_lines_flag)
1345 {
1346 obstack_fgrow2 (&el_obstack, muscle_find ("linef"),
1347 lineno, quotearg_style (c_quoting_style,
1348 muscle_find ("filename")));
1349 }
1350
1351 while ((c = getc (finput)) != EOF)
1352 copy_character (&el_obstack, c);
1353
1354 obstack_1grow (&el_obstack, 0);
1355 muscle_insert ("epilogue", obstack_finish (&el_obstack));
1356 }
1357
1358 \f
1359 /*---------------------------------------------------------------.
1360 | Convert the rules into the representation using RRHS, RLHS and |
1361 | RITEM. |
1362 `---------------------------------------------------------------*/
1363
1364 static void
1365 packgram (void)
1366 {
1367 unsigned int itemno;
1368 int ruleno;
1369 symbol_list *p;
1370
1371 ritem = XCALLOC (item_number_t, nritems);
1372 rules = XCALLOC (rule_t, nrules) - 1;
1373
1374 itemno = 0;
1375 ruleno = 1;
1376
1377 p = grammar;
1378 while (p)
1379 {
1380 symbol_t *ruleprec = p->ruleprec;
1381 rules[ruleno].user_number = ruleno;
1382 rules[ruleno].number = ruleno;
1383 rules[ruleno].lhs = p->sym;
1384 rules[ruleno].rhs = ritem + itemno;
1385 rules[ruleno].line = p->line;
1386 rules[ruleno].useful = TRUE;
1387 rules[ruleno].action = p->action;
1388 rules[ruleno].action_line = p->action_line;
1389
1390 p = p->next;
1391 while (p && p->sym)
1392 {
1393 /* item_number_t = symbol_number_t.
1394 But the former needs to contain more: negative rule numbers. */
1395 ritem[itemno++] = symbol_number_as_item_number (p->sym->number);
1396 /* A rule gets by default the precedence and associativity
1397 of the last token in it. */
1398 if (p->sym->class == token_sym)
1399 rules[ruleno].prec = p->sym;
1400 if (p)
1401 p = p->next;
1402 }
1403
1404 /* If this rule has a %prec,
1405 the specified symbol's precedence replaces the default. */
1406 if (ruleprec)
1407 {
1408 rules[ruleno].precsym = ruleprec;
1409 rules[ruleno].prec = ruleprec;
1410 }
1411 ritem[itemno++] = -ruleno;
1412 ++ruleno;
1413
1414 if (p)
1415 p = p->next;
1416 }
1417
1418 assert (itemno == nritems);
1419
1420 if (trace_flag)
1421 ritem_print (stderr);
1422 }
1423 \f
1424 /*------------------------------------------------------------------.
1425 | Read in the grammar specification and record it in the format |
1426 | described in gram.h. All actions are copied into ACTION_OBSTACK, |
1427 | in each case forming the body of a C function (YYACTION) which |
1428 | contains a switch statement to decide which action to execute. |
1429 `------------------------------------------------------------------*/
1430
1431 void
1432 reader (void)
1433 {
1434 lex_init ();
1435 lineno = 1;
1436
1437 /* Initialize the muscle obstack. */
1438 obstack_init (&muscle_obstack);
1439
1440 /* Initialize the symbol table. */
1441 symbols_new ();
1442
1443 /* Construct the axiom symbol. */
1444 axiom = getsym ("$axiom");
1445 axiom->class = nterm_sym;
1446 axiom->number = nvars++;
1447
1448 /* Construct the error token */
1449 errtoken = getsym ("error");
1450 errtoken->class = token_sym;
1451 errtoken->number = ntokens++;
1452
1453 /* Construct a token that represents all undefined literal tokens.
1454 It is always token number 2. */
1455 undeftoken = getsym ("$undefined.");
1456 undeftoken->class = token_sym;
1457 undeftoken->number = ntokens++;
1458
1459 /* Initialize the obstacks. */
1460 obstack_init (&action_obstack);
1461 obstack_init (&output_obstack);
1462 obstack_init (&pre_prologue_obstack);
1463 obstack_init (&post_prologue_obstack);
1464
1465 finput = xfopen (infile, "r");
1466
1467 /* Read the declaration section. Copy %{ ... %} groups to
1468 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
1469 etc. found there. */
1470 read_declarations ();
1471
1472 /* If the user did not define her EOFTOKEN, do it now. */
1473 if (!eoftoken)
1474 {
1475 eoftoken = getsym ("$");
1476 eoftoken->class = token_sym;
1477 eoftoken->number = 0;
1478 /* Value specified by POSIX. */
1479 eoftoken->user_token_number = 0;
1480 }
1481
1482 /* Read in the grammar, build grammar in list form. Write out
1483 actions. */
1484 readgram ();
1485 /* Some C code is given at the end of the grammar file. */
1486 read_additionnal_code ();
1487
1488 lex_free ();
1489 xfclose (finput);
1490
1491 /* Assign the symbols their symbol numbers. Write #defines for the
1492 token symbols into FDEFINES if requested. */
1493 symbols_pack ();
1494
1495 /* Convert the grammar into the format described in gram.h. */
1496 packgram ();
1497
1498 /* The grammar as a symbol_list is no longer needed. */
1499 LIST_FREE (symbol_list, grammar);
1500 }