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