]> git.saurik.com Git - bison.git/blob - src/reader.c
feaa3b67439c96310f03e81b6ff14a6e6dbebe9c
[bison.git] / src / reader.c
1 /* Input parser for bison
2 Copyright 1984, 1986, 1989, 1992, 1998, 2000
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 "xalloc.h"
30 #include "symtab.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 "macrotab.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 obstack_fgrow2 (&attrs_obstack, "#line %d %s\n",
432 lineno, quotearg_style (c_quoting_style, infile));
433 #endif
434
435 after_percent = 0;
436
437 c = getc (finput);
438
439 for (;;)
440 {
441 switch (c)
442 {
443 case '\n':
444 obstack_1grow (&attrs_obstack, c);
445 lineno++;
446 break;
447
448 case '%':
449 after_percent = -1;
450 break;
451
452 case '\'':
453 case '"':
454 copy_string (finput, &attrs_obstack, c);
455 break;
456
457 case '/':
458 copy_comment (finput, &attrs_obstack);
459 break;
460
461 case EOF:
462 fatal ("%s", _("unterminated `%{' definition"));
463
464 default:
465 obstack_1grow (&attrs_obstack, c);
466 }
467
468 c = getc (finput);
469
470 if (after_percent)
471 {
472 if (c == '}')
473 return;
474 obstack_1grow (&attrs_obstack, '%');
475 }
476 after_percent = 0;
477 }
478 }
479
480
481 /*-------------------------------------------------------------------.
482 | Parse what comes after %token or %nterm. For %token, WHAT_IS is |
483 | token_sym and WHAT_IS_NOT is nterm_sym. For %nterm, the arguments |
484 | are reversed. |
485 `-------------------------------------------------------------------*/
486
487 static void
488 parse_token_decl (symbol_class what_is, symbol_class what_is_not)
489 {
490 token_t token = 0;
491 char *typename = 0;
492
493 /* The symbol being defined. */
494 struct bucket *symbol = NULL;
495
496 /* After `%token' and `%nterm', any number of symbols maybe be
497 defined. */
498 for (;;)
499 {
500 int tmp_char = ungetc (skip_white_space (), finput);
501
502 /* `%' (for instance from `%token', or from `%%' etc.) is the
503 only valid means to end this declaration. */
504 if (tmp_char == '%')
505 return;
506 if (tmp_char == EOF)
507 fatal (_("Premature EOF after %s"), token_buffer);
508
509 token = lex ();
510 if (token == tok_comma)
511 {
512 symbol = NULL;
513 continue;
514 }
515 if (token == tok_typename)
516 {
517 typename = xstrdup (token_buffer);
518 value_components_used = 1;
519 symbol = NULL;
520 }
521 else if (token == tok_identifier && *symval->tag == '\"' && symbol)
522 {
523 if (symval->alias)
524 warn (_("symbol `%s' used more than once as a literal string"),
525 symval->tag);
526 else if (symbol->alias)
527 warn (_("symbol `%s' given more than one literal string"),
528 symbol->tag);
529 else
530 {
531 symval->class = token_sym;
532 symval->type_name = typename;
533 symval->user_token_number = symbol->user_token_number;
534 symbol->user_token_number = SALIAS;
535 symval->alias = symbol;
536 symbol->alias = symval;
537 /* symbol and symval combined are only one symbol */
538 nsyms--;
539 }
540 translations = 1;
541 symbol = NULL;
542 }
543 else if (token == tok_identifier)
544 {
545 int oldclass = symval->class;
546 symbol = symval;
547
548 if (symbol->class == what_is_not)
549 complain (_("symbol %s redefined"), symbol->tag);
550 symbol->class = what_is;
551 if (what_is == nterm_sym && oldclass != nterm_sym)
552 symbol->value = nvars++;
553
554 if (typename)
555 {
556 if (symbol->type_name == NULL)
557 symbol->type_name = typename;
558 else if (strcmp (typename, symbol->type_name) != 0)
559 complain (_("type redeclaration for %s"), symbol->tag);
560 }
561 }
562 else if (symbol && token == tok_number)
563 {
564 symbol->user_token_number = numval;
565 translations = 1;
566 }
567 else
568 {
569 complain (_("`%s' is invalid in %s"),
570 token_buffer, (what_is == token_sym) ? "%token" : "%nterm");
571 skip_to_char ('%');
572 }
573 }
574
575 }
576
577
578 /*------------------------------.
579 | Parse what comes after %start |
580 `------------------------------*/
581
582 static void
583 parse_start_decl (void)
584 {
585 if (start_flag)
586 complain (_("multiple %s declarations"), "%start");
587 if (lex () != tok_identifier)
588 complain (_("invalid %s declaration"), "%start");
589 else
590 {
591 start_flag = 1;
592 startval = symval;
593 }
594 }
595
596 /*-----------------------------------------------------------.
597 | read in a %type declaration and record its information for |
598 | get_type_name to access |
599 `-----------------------------------------------------------*/
600
601 static void
602 parse_type_decl (void)
603 {
604 char *name;
605
606 if (lex () != tok_typename)
607 {
608 complain ("%s", _("%type declaration has no <typename>"));
609 skip_to_char ('%');
610 return;
611 }
612
613 name = xstrdup (token_buffer);
614
615 for (;;)
616 {
617 token_t t;
618 int tmp_char = ungetc (skip_white_space (), finput);
619
620 if (tmp_char == '%')
621 return;
622 if (tmp_char == EOF)
623 fatal (_("Premature EOF after %s"), token_buffer);
624
625 t = lex ();
626
627 switch (t)
628 {
629
630 case tok_comma:
631 case tok_semicolon:
632 break;
633
634 case tok_identifier:
635 if (symval->type_name == NULL)
636 symval->type_name = name;
637 else if (strcmp (name, symval->type_name) != 0)
638 complain (_("type redeclaration for %s"), symval->tag);
639
640 break;
641
642 default:
643 complain (_("invalid %%type declaration due to item: %s"),
644 token_buffer);
645 skip_to_char ('%');
646 }
647 }
648 }
649
650
651
652 /*----------------------------------------------------------------.
653 | Read in a %left, %right or %nonassoc declaration and record its |
654 | information. |
655 `----------------------------------------------------------------*/
656
657 static void
658 parse_assoc_decl (associativity assoc)
659 {
660 char *name = NULL;
661 int prev = 0;
662
663 lastprec++; /* Assign a new precedence level, never 0. */
664
665 for (;;)
666 {
667 token_t t;
668 int tmp_char = ungetc (skip_white_space (), finput);
669
670 if (tmp_char == '%')
671 return;
672 if (tmp_char == EOF)
673 fatal (_("Premature EOF after %s"), token_buffer);
674
675 t = lex ();
676
677 switch (t)
678 {
679 case tok_typename:
680 name = xstrdup (token_buffer);
681 break;
682
683 case tok_comma:
684 break;
685
686 case tok_identifier:
687 if (symval->prec != 0)
688 complain (_("redefining precedence of %s"), symval->tag);
689 symval->prec = lastprec;
690 symval->assoc = assoc;
691 if (symval->class == nterm_sym)
692 complain (_("symbol %s redefined"), symval->tag);
693 symval->class = token_sym;
694 if (name)
695 { /* record the type, if one is specified */
696 if (symval->type_name == NULL)
697 symval->type_name = name;
698 else if (strcmp (name, symval->type_name) != 0)
699 complain (_("type redeclaration for %s"), symval->tag);
700 }
701 break;
702
703 case tok_number:
704 if (prev == tok_identifier)
705 {
706 symval->user_token_number = numval;
707 translations = 1;
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 /*--------------------------------------------------------------.
734 | Copy the union declaration into ATTRS_OBSTACK (and fdefines), |
735 | where it is made into the definition of YYSTYPE, the type of |
736 | elements of the parser value stack. |
737 `--------------------------------------------------------------*/
738
739 static void
740 parse_union_decl (void)
741 {
742 int c;
743 int count = 0;
744
745 if (typed)
746 complain (_("multiple %s declarations"), "%union");
747
748 typed = 1;
749
750 if (!no_lines_flag)
751 obstack_fgrow2 (&attrs_obstack, "\n#line %d %s\n",
752 lineno, quotearg_style (c_quoting_style,
753 macro_find("filename")));
754 else
755 obstack_1grow (&attrs_obstack, '\n');
756
757 obstack_sgrow (&attrs_obstack, "typedef union");
758 if (defines_flag)
759 obstack_sgrow (&defines_obstack, "typedef union");
760
761 c = getc (finput);
762
763 while (c != EOF)
764 {
765 obstack_1grow (&attrs_obstack, c);
766 if (defines_flag)
767 obstack_1grow (&defines_obstack, c);
768
769 switch (c)
770 {
771 case '\n':
772 lineno++;
773 break;
774
775 case '/':
776 copy_comment2 (finput, &defines_obstack, &attrs_obstack);
777 break;
778
779 case '{':
780 count++;
781 break;
782
783 case '}':
784 if (count == 0)
785 complain (_("unmatched %s"), "`}'");
786 count--;
787 if (count <= 0)
788 {
789 obstack_sgrow (&attrs_obstack, " YYSTYPE;\n");
790 if (defines_flag)
791 obstack_sgrow (&defines_obstack, " YYSTYPE;\n");
792 /* JF don't choke on trailing semi */
793 c = skip_white_space ();
794 if (c != ';')
795 ungetc (c, finput);
796 return;
797 }
798 }
799
800 c = getc (finput);
801 }
802 }
803
804
805 /*-------------------------------------------------------.
806 | Parse the declaration %expect N which says to expect N |
807 | shift-reduce conflicts. |
808 `-------------------------------------------------------*/
809
810 static void
811 parse_expect_decl (void)
812 {
813 int c = skip_white_space ();
814 ungetc (c, finput);
815
816 if (!isdigit (c))
817 complain (_("argument of %%expect is not an integer"));
818 else
819 expected_conflicts = read_signed_integer (finput);
820 }
821
822
823 /*-------------------------------------------------------------------.
824 | Parse what comes after %thong. the full syntax is |
825 | |
826 | %thong <type> token number literal |
827 | |
828 | the <type> or number may be omitted. The number specifies the |
829 | user_token_number. |
830 | |
831 | Two symbols are entered in the table, one for the token symbol and |
832 | one for the literal. Both are given the <type>, if any, from the |
833 | declaration. The ->user_token_number of the first is SALIAS and |
834 | the ->user_token_number of the second is set to the number, if |
835 | any, from the declaration. The two symbols are linked via |
836 | pointers in their ->alias fields. |
837 | |
838 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
839 | only the literal string is retained it is the literal string that |
840 | is output to yytname |
841 `-------------------------------------------------------------------*/
842
843 static void
844 parse_thong_decl (void)
845 {
846 token_t token;
847 struct bucket *symbol;
848 char *typename = 0;
849 int usrtoknum;
850
851 translations = 1;
852 token = lex (); /* fetch typename or first token */
853 if (token == tok_typename)
854 {
855 typename = xstrdup (token_buffer);
856 value_components_used = 1;
857 token = lex (); /* fetch first token */
858 }
859
860 /* process first token */
861
862 if (token != tok_identifier)
863 {
864 complain (_("unrecognized item %s, expected an identifier"),
865 token_buffer);
866 skip_to_char ('%');
867 return;
868 }
869 symval->class = token_sym;
870 symval->type_name = typename;
871 symval->user_token_number = SALIAS;
872 symbol = symval;
873
874 token = lex (); /* get number or literal string */
875
876 if (token == tok_number)
877 {
878 usrtoknum = numval;
879 token = lex (); /* okay, did number, now get literal */
880 }
881 else
882 usrtoknum = 0;
883
884 /* process literal string token */
885
886 if (token != tok_identifier || *symval->tag != '\"')
887 {
888 complain (_("expected string constant instead of %s"), token_buffer);
889 skip_to_char ('%');
890 return;
891 }
892 symval->class = token_sym;
893 symval->type_name = typename;
894 symval->user_token_number = usrtoknum;
895
896 symval->alias = symbol;
897 symbol->alias = symval;
898
899 /* symbol and symval combined are only one symbol. */
900 nsyms--;
901 }
902
903 /* FIXME. */
904
905 static void
906 parse_macro_decl (void)
907 {
908 int ch = ungetc (skip_white_space (), finput);
909 char* macro_key;
910 char* macro_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, &macro_obstack);
920 obstack_1grow (&macro_obstack, 0);
921 macro_key = obstack_finish (&macro_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, &macro_obstack, '"', 0);
938 obstack_1grow (&macro_obstack, 0);
939 macro_value = obstack_finish (&macro_obstack);
940
941 /* Store the (key, value) pair in the environment. */
942 macro_insert (macro_key, macro_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 | Parse what comes after %header_extension. |
958 `------------------------------------------*/
959
960 static void
961 parse_header_extension_decl (void)
962 {
963 char buff[32];
964
965 if (header_extension)
966 complain (_("multiple %%header_extension declarations"));
967 fscanf (finput, "%s", buff);
968 header_extension = xstrdup (buff);
969 }
970
971 /*------------------------------------------.
972 | Parse what comes after %source_extension. |
973 `------------------------------------------*/
974
975 static void
976 parse_source_extension_decl (void)
977 {
978 char buff[32];
979
980 if (src_extension)
981 complain (_("multiple %%source_extension declarations"));
982 fscanf (finput, "%s", buff);
983 src_extension = xstrdup (buff);
984 }
985
986 /*----------------------------------------------------------------.
987 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
988 | any `%' declarations, and copy the contents of any `%{ ... %}' |
989 | groups to ATTRS_OBSTACK. |
990 `----------------------------------------------------------------*/
991
992 static void
993 read_declarations (void)
994 {
995 int c;
996 int tok;
997
998 for (;;)
999 {
1000 c = skip_white_space ();
1001
1002 if (c == '%')
1003 {
1004 tok = parse_percent_token ();
1005
1006 switch (tok)
1007 {
1008 case tok_two_percents:
1009 return;
1010
1011 case tok_percent_left_curly:
1012 copy_definition ();
1013 break;
1014
1015 case tok_token:
1016 parse_token_decl (token_sym, nterm_sym);
1017 break;
1018
1019 case tok_nterm:
1020 parse_token_decl (nterm_sym, token_sym);
1021 break;
1022
1023 case tok_type:
1024 parse_type_decl ();
1025 break;
1026
1027 case tok_start:
1028 parse_start_decl ();
1029 break;
1030
1031 case tok_union:
1032 parse_union_decl ();
1033 break;
1034
1035 case tok_expect:
1036 parse_expect_decl ();
1037 break;
1038
1039 case tok_thong:
1040 parse_thong_decl ();
1041 break;
1042
1043 case tok_left:
1044 parse_assoc_decl (left_assoc);
1045 break;
1046
1047 case tok_right:
1048 parse_assoc_decl (right_assoc);
1049 break;
1050
1051 case tok_nonassoc:
1052 parse_assoc_decl (non_assoc);
1053 break;
1054
1055 case tok_hdrext:
1056 parse_header_extension_decl ();
1057 break;
1058
1059 case tok_srcext:
1060 parse_source_extension_decl ();
1061 break;
1062
1063 case tok_define:
1064 parse_macro_decl ();
1065 break;
1066
1067 case tok_skel:
1068 parse_skel_decl ();
1069 break;
1070
1071 case tok_noop:
1072 break;
1073
1074 default:
1075 complain (_("unrecognized: %s"), token_buffer);
1076 skip_to_char ('%');
1077 }
1078 }
1079 else if (c == EOF)
1080 fatal (_("no input grammar"));
1081 else
1082 {
1083 char buf[] = "c";
1084 buf[0] = c;
1085 complain (_("unknown character: %s"), quote (buf));
1086 skip_to_char ('%');
1087 }
1088 }
1089 }
1090 \f
1091 /*-------------------------------------------------------------------.
1092 | Assuming that a `{' has just been seen, copy everything up to the |
1093 | matching `}' into the actions file. STACK_OFFSET is the number of |
1094 | values in the current rule so far, which says where to find `$0' |
1095 | with respect to the top of the stack. |
1096 `-------------------------------------------------------------------*/
1097
1098 static void
1099 copy_action (symbol_list *rule, int stack_offset)
1100 {
1101 int c;
1102 int count;
1103 char buf[4096];
1104
1105 /* offset is always 0 if parser has already popped the stack pointer */
1106 if (semantic_parser)
1107 stack_offset = 0;
1108
1109 sprintf (buf, "\ncase %d:\n", nrules);
1110 obstack_grow (&action_obstack, buf, strlen (buf));
1111
1112 if (!no_lines_flag)
1113 {
1114 sprintf (buf, "#line %d %s\n",
1115 lineno, quotearg_style (c_quoting_style,
1116 macro_find ("filename")));
1117 obstack_grow (&action_obstack, buf, strlen (buf));
1118 }
1119 obstack_1grow (&action_obstack, '{');
1120
1121 count = 1;
1122 c = getc (finput);
1123
1124 while (count > 0)
1125 {
1126 while (c != '}')
1127 {
1128 switch (c)
1129 {
1130 case '\n':
1131 obstack_1grow (&action_obstack, c);
1132 lineno++;
1133 break;
1134
1135 case '{':
1136 obstack_1grow (&action_obstack, c);
1137 count++;
1138 break;
1139
1140 case '\'':
1141 case '"':
1142 copy_string (finput, &action_obstack, c);
1143 break;
1144
1145 case '/':
1146 copy_comment (finput, &action_obstack);
1147 break;
1148
1149 case '$':
1150 copy_dollar (finput, &action_obstack,
1151 rule, stack_offset);
1152 break;
1153
1154 case '@':
1155 copy_at (finput, &action_obstack,
1156 stack_offset);
1157 break;
1158
1159 case EOF:
1160 fatal (_("unmatched %s"), "`{'");
1161
1162 default:
1163 obstack_1grow (&action_obstack, c);
1164 }
1165
1166 c = getc (finput);
1167 }
1168
1169 /* above loop exits when c is '}' */
1170
1171 if (--count)
1172 {
1173 obstack_1grow (&action_obstack, c);
1174 c = getc (finput);
1175 }
1176 }
1177
1178 obstack_sgrow (&action_obstack, ";\n break;}");
1179 }
1180 \f
1181 /*-------------------------------------------------------------------.
1182 | After `%guard' is seen in the input file, copy the actual guard |
1183 | into the guards file. If the guard is followed by an action, copy |
1184 | that into the actions file. STACK_OFFSET is the number of values |
1185 | in the current rule so far, which says where to find `$0' with |
1186 | respect to the top of the stack, for the simple parser in which |
1187 | the stack is not popped until after the guard is run. |
1188 `-------------------------------------------------------------------*/
1189
1190 static void
1191 copy_guard (symbol_list *rule, int stack_offset)
1192 {
1193 int c;
1194 int count;
1195 int brace_flag = 0;
1196
1197 /* offset is always 0 if parser has already popped the stack pointer */
1198 if (semantic_parser)
1199 stack_offset = 0;
1200
1201 obstack_fgrow1 (&guard_obstack, "\ncase %d:\n", nrules);
1202 if (!no_lines_flag)
1203 obstack_fgrow2 (&guard_obstack, "#line %d %s\n",
1204 lineno, quotearg_style (c_quoting_style, infile));
1205 obstack_1grow (&guard_obstack, '{');
1206
1207 count = 0;
1208 c = getc (finput);
1209
1210 while (brace_flag ? (count > 0) : (c != ';'))
1211 {
1212 switch (c)
1213 {
1214 case '\n':
1215 obstack_1grow (&guard_obstack, c);
1216 lineno++;
1217 break;
1218
1219 case '{':
1220 obstack_1grow (&guard_obstack, c);
1221 brace_flag = 1;
1222 count++;
1223 break;
1224
1225 case '}':
1226 obstack_1grow (&guard_obstack, c);
1227 if (count > 0)
1228 count--;
1229 else
1230 {
1231 complain (_("unmatched %s"), "`}'");
1232 c = getc (finput); /* skip it */
1233 }
1234 break;
1235
1236 case '\'':
1237 case '"':
1238 copy_string (finput, &guard_obstack, c);
1239 break;
1240
1241 case '/':
1242 copy_comment (finput, &guard_obstack);
1243 break;
1244
1245 case '$':
1246 copy_dollar (finput, &guard_obstack, rule, stack_offset);
1247 break;
1248
1249 case '@':
1250 copy_at (finput, &guard_obstack, stack_offset);
1251 break;
1252
1253 case EOF:
1254 fatal ("%s", _("unterminated %guard clause"));
1255
1256 default:
1257 obstack_1grow (&guard_obstack, c);
1258 }
1259
1260 if (c != '}' || count != 0)
1261 c = getc (finput);
1262 }
1263
1264 c = skip_white_space ();
1265
1266 obstack_sgrow (&guard_obstack, ";\n break;}");
1267 if (c == '{')
1268 copy_action (rule, stack_offset);
1269 else if (c == '=')
1270 {
1271 c = getc (finput); /* why not skip_white_space -wjh */
1272 if (c == '{')
1273 copy_action (rule, stack_offset);
1274 }
1275 else
1276 ungetc (c, finput);
1277 }
1278 \f
1279
1280 static void
1281 record_rule_line (void)
1282 {
1283 /* Record each rule's source line number in rline table. */
1284
1285 if (nrules >= rline_allocated)
1286 {
1287 rline_allocated = nrules * 2;
1288 rline = XREALLOC (rline, short, rline_allocated);
1289 }
1290 rline[nrules] = lineno;
1291 }
1292
1293
1294 /*-------------------------------------------------------------------.
1295 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1296 | with the user's names. |
1297 `-------------------------------------------------------------------*/
1298
1299 static bucket *
1300 gensym (void)
1301 {
1302 /* Incremented for each generated symbol */
1303 static int gensym_count = 0;
1304 static char buf[256];
1305
1306 bucket *sym;
1307
1308 sprintf (buf, "@%d", ++gensym_count);
1309 token_buffer = buf;
1310 sym = getsym (token_buffer);
1311 sym->class = nterm_sym;
1312 sym->value = nvars++;
1313 return sym;
1314 }
1315
1316 #if 0
1317 /*------------------------------------------------------------------.
1318 | read in a %type declaration and record its information for |
1319 | get_type_name to access. This is unused. It is only called from |
1320 | the #if 0 part of readgram |
1321 `------------------------------------------------------------------*/
1322
1323 static int
1324 get_type (void)
1325 {
1326 int k;
1327 token_t token;
1328 char *name;
1329
1330 token = lex ();
1331
1332 if (token != tok_typename)
1333 {
1334 complain (_("invalid %s declaration"), "%type");
1335 return t;
1336 }
1337
1338 name = xstrdup (token_buffer);
1339
1340 for (;;)
1341 {
1342 token = lex ();
1343
1344 switch (token)
1345 {
1346 case tok_semicolon:
1347 return lex ();
1348
1349 case tok_comma:
1350 break;
1351
1352 case tok_identifier:
1353 if (symval->type_name == NULL)
1354 symval->type_name = name;
1355 else if (strcmp (name, symval->type_name) != 0)
1356 complain (_("type redeclaration for %s"), symval->tag);
1357
1358 break;
1359
1360 default:
1361 return token;
1362 }
1363 }
1364 }
1365
1366 #endif
1367 \f
1368 /*------------------------------------------------------------------.
1369 | Parse the input grammar into a one symbol_list structure. Each |
1370 | rule is represented by a sequence of symbols: the left hand side |
1371 | followed by the contents of the right hand side, followed by a |
1372 | null pointer instead of a symbol to terminate the rule. The next |
1373 | symbol is the lhs of the following rule. |
1374 | |
1375 | All guards and actions are copied out to the appropriate files, |
1376 | labelled by the rule number they apply to. |
1377 `------------------------------------------------------------------*/
1378
1379 static void
1380 readgram (void)
1381 {
1382 token_t t;
1383 bucket *lhs = NULL;
1384 symbol_list *p;
1385 symbol_list *p1;
1386 bucket *bp;
1387
1388 /* Points to first symbol_list of current rule. its symbol is the
1389 lhs of the rule. */
1390 symbol_list *crule;
1391 /* Points to the symbol_list preceding crule. */
1392 symbol_list *crule1;
1393
1394 p1 = NULL;
1395
1396 t = lex ();
1397
1398 while (t != tok_two_percents && t != tok_eof)
1399 {
1400 if (t == tok_identifier || t == tok_bar)
1401 {
1402 int action_flag = 0;
1403 /* Number of symbols in rhs of this rule so far */
1404 int rulelength = 0;
1405 int xactions = 0; /* JF for error checking */
1406 bucket *first_rhs = 0;
1407
1408 if (t == tok_identifier)
1409 {
1410 lhs = symval;
1411
1412 if (!start_flag)
1413 {
1414 startval = lhs;
1415 start_flag = 1;
1416 }
1417
1418 t = lex ();
1419 if (t != tok_colon)
1420 {
1421 complain (_("ill-formed rule: initial symbol not followed by colon"));
1422 unlex (t);
1423 }
1424 }
1425
1426 if (nrules == 0 && t == tok_bar)
1427 {
1428 complain (_("grammar starts with vertical bar"));
1429 lhs = symval; /* BOGUS: use a random symval */
1430 }
1431 /* start a new rule and record its lhs. */
1432
1433 nrules++;
1434 nitems++;
1435
1436 record_rule_line ();
1437
1438 p = XCALLOC (symbol_list, 1);
1439 p->sym = lhs;
1440
1441 crule1 = p1;
1442 if (p1)
1443 p1->next = p;
1444 else
1445 grammar = p;
1446
1447 p1 = p;
1448 crule = p;
1449
1450 /* mark the rule's lhs as a nonterminal if not already so. */
1451
1452 if (lhs->class == unknown_sym)
1453 {
1454 lhs->class = nterm_sym;
1455 lhs->value = nvars;
1456 nvars++;
1457 }
1458 else if (lhs->class == token_sym)
1459 complain (_("rule given for %s, which is a token"), lhs->tag);
1460
1461 /* read the rhs of the rule. */
1462
1463 for (;;)
1464 {
1465 t = lex ();
1466 if (t == tok_prec)
1467 {
1468 t = lex ();
1469 crule->ruleprec = symval;
1470 t = lex ();
1471 }
1472
1473 if (!(t == tok_identifier || t == tok_left_curly))
1474 break;
1475
1476 /* If next token is an identifier, see if a colon follows it.
1477 If one does, exit this rule now. */
1478 if (t == tok_identifier)
1479 {
1480 bucket *ssave;
1481 token_t t1;
1482
1483 ssave = symval;
1484 t1 = lex ();
1485 unlex (t1);
1486 symval = ssave;
1487 if (t1 == tok_colon)
1488 break;
1489
1490 if (!first_rhs) /* JF */
1491 first_rhs = symval;
1492 /* Not followed by colon =>
1493 process as part of this rule's rhs. */
1494 }
1495
1496 /* If we just passed an action, that action was in the middle
1497 of a rule, so make a dummy rule to reduce it to a
1498 non-terminal. */
1499 if (action_flag)
1500 {
1501 bucket *sdummy;
1502
1503 /* Since the action was written out with this rule's
1504 number, we must give the new rule this number by
1505 inserting the new rule before it. */
1506
1507 /* Make a dummy nonterminal, a gensym. */
1508 sdummy = gensym ();
1509
1510 /* Make a new rule, whose body is empty,
1511 before the current one, so that the action
1512 just read can belong to it. */
1513 nrules++;
1514 nitems++;
1515 record_rule_line ();
1516 p = XCALLOC (symbol_list, 1);
1517 if (crule1)
1518 crule1->next = p;
1519 else
1520 grammar = p;
1521 p->sym = sdummy;
1522 crule1 = XCALLOC (symbol_list, 1);
1523 p->next = crule1;
1524 crule1->next = crule;
1525
1526 /* Insert the dummy generated by that rule into this
1527 rule. */
1528 nitems++;
1529 p = XCALLOC (symbol_list, 1);
1530 p->sym = sdummy;
1531 p1->next = p;
1532 p1 = p;
1533
1534 action_flag = 0;
1535 }
1536
1537 if (t == tok_identifier)
1538 {
1539 nitems++;
1540 p = XCALLOC (symbol_list, 1);
1541 p->sym = symval;
1542 p1->next = p;
1543 p1 = p;
1544 }
1545 else /* handle an action. */
1546 {
1547 copy_action (crule, rulelength);
1548 action_flag = 1;
1549 xactions++; /* JF */
1550 }
1551 rulelength++;
1552 } /* end of read rhs of rule */
1553
1554 /* Put an empty link in the list to mark the end of this rule */
1555 p = XCALLOC (symbol_list, 1);
1556 p1->next = p;
1557 p1 = p;
1558
1559 if (t == tok_prec)
1560 {
1561 complain (_("two @prec's in a row"));
1562 t = lex ();
1563 crule->ruleprec = symval;
1564 t = lex ();
1565 }
1566 if (t == tok_guard)
1567 {
1568 if (!semantic_parser)
1569 complain (_("%%guard present but %%semantic_parser not specified"));
1570
1571 copy_guard (crule, rulelength);
1572 t = lex ();
1573 }
1574 else if (t == tok_left_curly)
1575 {
1576 /* This case never occurs -wjh */
1577 if (action_flag)
1578 complain (_("two actions at end of one rule"));
1579 copy_action (crule, rulelength);
1580 action_flag = 1;
1581 xactions++; /* -wjh */
1582 t = lex ();
1583 }
1584 /* If $$ is being set in default way, report if any type
1585 mismatch. */
1586 else if (!xactions
1587 && first_rhs && lhs->type_name != first_rhs->type_name)
1588 {
1589 if (lhs->type_name == 0
1590 || first_rhs->type_name == 0
1591 || strcmp (lhs->type_name, first_rhs->type_name))
1592 complain (_("type clash (`%s' `%s') on default action"),
1593 lhs->type_name ? lhs->type_name : "",
1594 first_rhs->type_name ? first_rhs->type_name : "");
1595 }
1596 /* Warn if there is no default for $$ but we need one. */
1597 else if (!xactions && !first_rhs && lhs->type_name != 0)
1598 complain (_("empty rule for typed nonterminal, and no action"));
1599 if (t == tok_semicolon)
1600 t = lex ();
1601 }
1602 #if 0
1603 /* these things can appear as alternatives to rules. */
1604 /* NO, they cannot.
1605 a) none of the documentation allows them
1606 b) most of them scan forward until finding a next %
1607 thus they may swallow lots of intervening rules
1608 */
1609 else if (t == tok_token)
1610 {
1611 parse_token_decl (token_sym, nterm_sym);
1612 t = lex ();
1613 }
1614 else if (t == tok_nterm)
1615 {
1616 parse_token_decl (nterm_sym, token_sym);
1617 t = lex ();
1618 }
1619 else if (t == tok_type)
1620 {
1621 t = get_type ();
1622 }
1623 else if (t == tok_union)
1624 {
1625 parse_union_decl ();
1626 t = lex ();
1627 }
1628 else if (t == tok_expect)
1629 {
1630 parse_expect_decl ();
1631 t = lex ();
1632 }
1633 else if (t == tok_start)
1634 {
1635 parse_start_decl ();
1636 t = lex ();
1637 }
1638 #endif
1639
1640 else
1641 {
1642 complain (_("invalid input: %s"), quote (token_buffer));
1643 t = lex ();
1644 }
1645 }
1646
1647 /* grammar has been read. Do some checking */
1648
1649 if (nsyms > MAXSHORT)
1650 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1651 MAXSHORT);
1652 if (nrules == 0)
1653 fatal (_("no rules in the input grammar"));
1654
1655 /* Report any undefined symbols and consider them nonterminals. */
1656
1657 for (bp = firstsymbol; bp; bp = bp->next)
1658 if (bp->class == unknown_sym)
1659 {
1660 complain (_
1661 ("symbol %s is used, but is not defined as a token and has no rules"),
1662 bp->tag);
1663 bp->class = nterm_sym;
1664 bp->value = nvars++;
1665 }
1666
1667 ntokens = nsyms - nvars;
1668 }
1669
1670 /* At the end of the grammar file, some C source code must
1671 be stored. It is going to be associated to the epilogue
1672 directive. */
1673 static void
1674 read_additionnal_code (void)
1675 {
1676 char c;
1677 struct obstack el_obstack;
1678
1679 obstack_init (&el_obstack);
1680
1681 while ((c = getc (finput)) != EOF)
1682 obstack_1grow (&el_obstack, c);
1683
1684 obstack_1grow (&el_obstack, 0);
1685 macro_insert ("epilogue", obstack_finish (&el_obstack));
1686 }
1687
1688 \f
1689 /*--------------------------------------------------------------.
1690 | For named tokens, but not literal ones, define the name. The |
1691 | value is the user token number. |
1692 `--------------------------------------------------------------*/
1693
1694 static void
1695 output_token_defines (struct obstack *oout)
1696 {
1697 bucket *bp;
1698 char *cp, *symbol;
1699 char c;
1700
1701 for (bp = firstsymbol; bp; bp = bp->next)
1702 {
1703 symbol = bp->tag; /* get symbol */
1704
1705 if (bp->value >= ntokens)
1706 continue;
1707 if (bp->user_token_number == SALIAS)
1708 continue;
1709 if ('\'' == *symbol)
1710 continue; /* skip literal character */
1711 if (bp == errtoken)
1712 continue; /* skip error token */
1713 if ('\"' == *symbol)
1714 {
1715 /* use literal string only if given a symbol with an alias */
1716 if (bp->alias)
1717 symbol = bp->alias->tag;
1718 else
1719 continue;
1720 }
1721
1722 /* Don't #define nonliteral tokens whose names contain periods. */
1723 cp = symbol;
1724 while ((c = *cp++) && c != '.');
1725 if (c != '\0')
1726 continue;
1727
1728 obstack_fgrow2 (oout, "# define\t%s\t%d\n",
1729 symbol,
1730 (translations ? bp->user_token_number : bp->value));
1731 if (semantic_parser)
1732 obstack_fgrow2 (oout, "# define\tT%s\t%d\n", symbol, bp->value);
1733 }
1734 }
1735
1736
1737 /*------------------------------------------------------------------.
1738 | Assign symbol numbers, and write definition of token names into |
1739 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1740 | of symbols. |
1741 `------------------------------------------------------------------*/
1742
1743 static void
1744 packsymbols (void)
1745 {
1746 bucket *bp;
1747 int tokno = 1;
1748 int i;
1749 int last_user_token_number;
1750 static char DOLLAR[] = "$";
1751
1752 tags = XCALLOC (char *, nsyms + 1);
1753 tags[0] = DOLLAR;
1754 user_toknums = XCALLOC (short, nsyms + 1);
1755 user_toknums[0] = 0;
1756
1757 sprec = XCALLOC (short, nsyms);
1758 sassoc = XCALLOC (short, nsyms);
1759
1760 max_user_token_number = 256;
1761 last_user_token_number = 256;
1762
1763 for (bp = firstsymbol; bp; bp = bp->next)
1764 {
1765 if (bp->class == nterm_sym)
1766 {
1767 bp->value += ntokens;
1768 }
1769 else if (bp->alias)
1770 {
1771 /* this symbol and its alias are a single token defn.
1772 allocate a tokno, and assign to both check agreement of
1773 ->prec and ->assoc fields and make both the same */
1774 if (bp->value == 0)
1775 bp->value = bp->alias->value = tokno++;
1776
1777 if (bp->prec != bp->alias->prec)
1778 {
1779 if (bp->prec != 0 && bp->alias->prec != 0
1780 && bp->user_token_number == SALIAS)
1781 complain (_("conflicting precedences for %s and %s"),
1782 bp->tag, bp->alias->tag);
1783 if (bp->prec != 0)
1784 bp->alias->prec = bp->prec;
1785 else
1786 bp->prec = bp->alias->prec;
1787 }
1788
1789 if (bp->assoc != bp->alias->assoc)
1790 {
1791 if (bp->assoc != 0 && bp->alias->assoc != 0
1792 && bp->user_token_number == SALIAS)
1793 complain (_("conflicting assoc values for %s and %s"),
1794 bp->tag, bp->alias->tag);
1795 if (bp->assoc != 0)
1796 bp->alias->assoc = bp->assoc;
1797 else
1798 bp->assoc = bp->alias->assoc;
1799 }
1800
1801 if (bp->user_token_number == SALIAS)
1802 continue; /* do not do processing below for SALIASs */
1803
1804 }
1805 else /* bp->class == token_sym */
1806 {
1807 bp->value = tokno++;
1808 }
1809
1810 if (bp->class == token_sym)
1811 {
1812 if (translations && !(bp->user_token_number))
1813 bp->user_token_number = ++last_user_token_number;
1814 if (bp->user_token_number > max_user_token_number)
1815 max_user_token_number = bp->user_token_number;
1816 }
1817
1818 tags[bp->value] = bp->tag;
1819 user_toknums[bp->value] = bp->user_token_number;
1820 sprec[bp->value] = bp->prec;
1821 sassoc[bp->value] = bp->assoc;
1822
1823 }
1824
1825 if (translations)
1826 {
1827 int j;
1828
1829 token_translations = XCALLOC (short, max_user_token_number + 1);
1830
1831 /* initialize all entries for literal tokens to 2, the internal
1832 token number for $undefined., which represents all invalid
1833 inputs. */
1834 for (j = 0; j <= max_user_token_number; j++)
1835 token_translations[j] = 2;
1836
1837 for (bp = firstsymbol; bp; bp = bp->next)
1838 {
1839 if (bp->value >= ntokens)
1840 continue; /* non-terminal */
1841 if (bp->user_token_number == SALIAS)
1842 continue;
1843 if (token_translations[bp->user_token_number] != 2)
1844 complain (_("tokens %s and %s both assigned number %d"),
1845 tags[token_translations[bp->user_token_number]],
1846 bp->tag, bp->user_token_number);
1847 token_translations[bp->user_token_number] = bp->value;
1848 }
1849 }
1850
1851 error_token_number = errtoken->value;
1852
1853 output_token_defines (&output_obstack);
1854 obstack_1grow (&output_obstack, 0);
1855 macro_insert ("tokendef", obstack_finish (&output_obstack));
1856
1857 #if 0
1858 if (!no_parser_flag)
1859 output_token_defines (&table_obstack);
1860 #endif
1861
1862 if (startval->class == unknown_sym)
1863 fatal (_("the start symbol %s is undefined"), startval->tag);
1864 else if (startval->class == token_sym)
1865 fatal (_("the start symbol %s is a token"), startval->tag);
1866
1867 start_symbol = startval->value;
1868
1869 if (defines_flag)
1870 {
1871 output_token_defines (&defines_obstack);
1872
1873 if (!pure_parser)
1874 {
1875 if (spec_name_prefix)
1876 obstack_fgrow1 (&defines_obstack, "\nextern YYSTYPE %slval;\n",
1877 spec_name_prefix);
1878 else
1879 obstack_sgrow (&defines_obstack,
1880 "\nextern YYSTYPE yylval;\n");
1881 }
1882
1883 if (semantic_parser)
1884 for (i = ntokens; i < nsyms; i++)
1885 {
1886 /* don't make these for dummy nonterminals made by gensym. */
1887 if (*tags[i] != '@')
1888 obstack_fgrow2 (&defines_obstack,
1889 "# define\tNT%s\t%d\n", tags[i], i);
1890 }
1891 #if 0
1892 /* `fdefines' is now a temporary file, so we need to copy its
1893 contents in `done', so we can't close it here. */
1894 fclose (fdefines);
1895 fdefines = NULL;
1896 #endif
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 #if 0
1981 /* initially assume token number translation not needed. */
1982 translations = 0;
1983 #endif
1984 /* Nowadays translations is always set to 1, since we give `error' a
1985 user-token-number to satisfy the Posix demand for YYERRCODE==256.
1986 */
1987 translations = 1;
1988
1989 nsyms = 1;
1990 nvars = 0;
1991 nrules = 0;
1992 nitems = 0;
1993 rline_allocated = 10;
1994 rline = XCALLOC (short, rline_allocated);
1995
1996 typed = 0;
1997 lastprec = 0;
1998
1999 semantic_parser = 0;
2000 pure_parser = 0;
2001
2002 grammar = NULL;
2003
2004 init_lex ();
2005 lineno = 1;
2006
2007 /* Initialize the macro obstack. */
2008 obstack_init (&macro_obstack);
2009
2010 /* Initialize the symbol table. */
2011 tabinit ();
2012
2013 /* Construct the error token */
2014 errtoken = getsym ("error");
2015 errtoken->class = token_sym;
2016 errtoken->user_token_number = 256; /* Value specified by POSIX. */
2017
2018 /* Construct a token that represents all undefined literal tokens.
2019 It is always token number 2. */
2020 undeftoken = getsym ("$undefined.");
2021 undeftoken->class = token_sym;
2022 undeftoken->user_token_number = 2;
2023
2024 /* Read the declaration section. Copy %{ ... %} groups to
2025 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
2026 etc. found there. */
2027 read_declarations ();
2028 /* Read in the grammar, build grammar in list form. Write out
2029 guards and actions. */
2030 readgram ();
2031 /* Some C code is given at the end of the grammar file. */
2032 read_additionnal_code ();
2033 /* Now we know whether we need the line-number stack. If we do,
2034 write its type into the .tab.h file. */
2035 #if 0
2036 if (defines_flag)
2037 reader_output_yylsp (&defines_obstack);
2038 #endif
2039 /* Assign the symbols their symbol numbers. Write #defines for the
2040 token symbols into FDEFINES if requested. */
2041 packsymbols ();
2042 /* Convert the grammar into the format described in gram.h. */
2043 packgram ();
2044 /* Free the symbol table data structure since symbols are now all
2045 referred to by symbol number. */
2046 free_symtab ();
2047 }