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