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