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