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