]> git.saurik.com Git - bison.git/blob - src/reader.c
* src/reader.c (reader): Delete obsolete call to function
[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, infile));
753 else
754 obstack_1grow (&attrs_obstack, '\n');
755
756 obstack_sgrow (&attrs_obstack, "typedef union");
757 if (defines_flag)
758 obstack_sgrow (&defines_obstack, "typedef union");
759
760 c = getc (finput);
761
762 while (c != EOF)
763 {
764 obstack_1grow (&attrs_obstack, c);
765 if (defines_flag)
766 obstack_1grow (&defines_obstack, c);
767
768 switch (c)
769 {
770 case '\n':
771 lineno++;
772 break;
773
774 case '/':
775 copy_comment2 (finput, &defines_obstack, &attrs_obstack);
776 break;
777
778 case '{':
779 count++;
780 break;
781
782 case '}':
783 if (count == 0)
784 complain (_("unmatched %s"), "`}'");
785 count--;
786 if (count <= 0)
787 {
788 obstack_sgrow (&attrs_obstack, " YYSTYPE;\n");
789 if (defines_flag)
790 obstack_sgrow (&defines_obstack, " YYSTYPE;\n");
791 /* JF don't choke on trailing semi */
792 c = skip_white_space ();
793 if (c != ';')
794 ungetc (c, finput);
795 return;
796 }
797 }
798
799 c = getc (finput);
800 }
801 }
802
803
804 /*-------------------------------------------------------.
805 | Parse the declaration %expect N which says to expect N |
806 | shift-reduce conflicts. |
807 `-------------------------------------------------------*/
808
809 static void
810 parse_expect_decl (void)
811 {
812 int c = skip_white_space ();
813 ungetc (c, finput);
814
815 if (!isdigit (c))
816 complain (_("argument of %%expect is not an integer"));
817 else
818 expected_conflicts = read_signed_integer (finput);
819 }
820
821
822 /*-------------------------------------------------------------------.
823 | Parse what comes after %thong. the full syntax is |
824 | |
825 | %thong <type> token number literal |
826 | |
827 | the <type> or number may be omitted. The number specifies the |
828 | user_token_number. |
829 | |
830 | Two symbols are entered in the table, one for the token symbol and |
831 | one for the literal. Both are given the <type>, if any, from the |
832 | declaration. The ->user_token_number of the first is SALIAS and |
833 | the ->user_token_number of the second is set to the number, if |
834 | any, from the declaration. The two symbols are linked via |
835 | pointers in their ->alias fields. |
836 | |
837 | During OUTPUT_DEFINES_TABLE, the symbol is reported thereafter, |
838 | only the literal string is retained it is the literal string that |
839 | is output to yytname |
840 `-------------------------------------------------------------------*/
841
842 static void
843 parse_thong_decl (void)
844 {
845 token_t token;
846 struct bucket *symbol;
847 char *typename = 0;
848 int usrtoknum;
849
850 translations = 1;
851 token = lex (); /* fetch typename or first token */
852 if (token == tok_typename)
853 {
854 typename = xstrdup (token_buffer);
855 value_components_used = 1;
856 token = lex (); /* fetch first token */
857 }
858
859 /* process first token */
860
861 if (token != tok_identifier)
862 {
863 complain (_("unrecognized item %s, expected an identifier"),
864 token_buffer);
865 skip_to_char ('%');
866 return;
867 }
868 symval->class = token_sym;
869 symval->type_name = typename;
870 symval->user_token_number = SALIAS;
871 symbol = symval;
872
873 token = lex (); /* get number or literal string */
874
875 if (token == tok_number)
876 {
877 usrtoknum = numval;
878 token = lex (); /* okay, did number, now get literal */
879 }
880 else
881 usrtoknum = 0;
882
883 /* process literal string token */
884
885 if (token != tok_identifier || *symval->tag != '\"')
886 {
887 complain (_("expected string constant instead of %s"), token_buffer);
888 skip_to_char ('%');
889 return;
890 }
891 symval->class = token_sym;
892 symval->type_name = typename;
893 symval->user_token_number = usrtoknum;
894
895 symval->alias = symbol;
896 symbol->alias = symval;
897
898 /* symbol and symval combined are only one symbol. */
899 nsyms--;
900 }
901
902 /* FIXME. */
903
904 static void
905 parse_macro_decl (void)
906 {
907 int ch = ungetc (skip_white_space (), finput);
908 char* macro_key;
909 char* macro_value;
910
911 /* Read key. */
912 if (!isalpha (ch) && ch != '_')
913 {
914 complain (_("invalid %s declaration"), "%define");
915 skip_to_char ('%');
916 return;
917 }
918 copy_identifier (finput, &macro_obstack);
919 obstack_1grow (&macro_obstack, 0);
920 macro_key = obstack_finish (&macro_obstack);
921
922 /* Read value. */
923 ch = skip_white_space ();
924 if (ch != '"')
925 {
926 ungetc (ch, finput);
927 if (ch != EOF)
928 {
929 complain (_("invalid %s declaration"), "%define");
930 skip_to_char ('%');
931 return;
932 }
933 else
934 fatal (_("Premature EOF after %s"), "\"");
935 }
936 copy_string2 (finput, &macro_obstack, '"', 0);
937 obstack_1grow (&macro_obstack, 0);
938 macro_value = obstack_finish (&macro_obstack);
939
940 /* Store the (key, value) pair in the environment. */
941 macro_insert (macro_key, macro_value);
942 }
943
944
945 /*----------------------------------.
946 | Parse what comes after %skeleton. |
947 `----------------------------------*/
948
949 void
950 parse_skel_decl (void)
951 {
952 /* Complete with parse_dquoted_param () on the CVS branch 1.29. */
953 }
954
955 /*------------------------------------------.
956 | Parse what comes after %header_extension. |
957 `------------------------------------------*/
958
959 static void
960 parse_header_extension_decl (void)
961 {
962 char buff[32];
963
964 if (header_extension)
965 complain (_("multiple %%header_extension declarations"));
966 fscanf (finput, "%s", buff);
967 header_extension = xstrdup (buff);
968 }
969
970 /*------------------------------------------.
971 | Parse what comes after %source_extension. |
972 `------------------------------------------*/
973
974 static void
975 parse_source_extension_decl (void)
976 {
977 char buff[32];
978
979 if (src_extension)
980 complain (_("multiple %%source_extension declarations"));
981 fscanf (finput, "%s", buff);
982 src_extension = xstrdup (buff);
983 }
984
985 /*----------------------------------------------------------------.
986 | Read from finput until `%%' is seen. Discard the `%%'. Handle |
987 | any `%' declarations, and copy the contents of any `%{ ... %}' |
988 | groups to ATTRS_OBSTACK. |
989 `----------------------------------------------------------------*/
990
991 static void
992 read_declarations (void)
993 {
994 int c;
995 int tok;
996
997 for (;;)
998 {
999 c = skip_white_space ();
1000
1001 if (c == '%')
1002 {
1003 tok = parse_percent_token ();
1004
1005 switch (tok)
1006 {
1007 case tok_two_percents:
1008 return;
1009
1010 case tok_percent_left_curly:
1011 copy_definition ();
1012 break;
1013
1014 case tok_token:
1015 parse_token_decl (token_sym, nterm_sym);
1016 break;
1017
1018 case tok_nterm:
1019 parse_token_decl (nterm_sym, token_sym);
1020 break;
1021
1022 case tok_type:
1023 parse_type_decl ();
1024 break;
1025
1026 case tok_start:
1027 parse_start_decl ();
1028 break;
1029
1030 case tok_union:
1031 parse_union_decl ();
1032 break;
1033
1034 case tok_expect:
1035 parse_expect_decl ();
1036 break;
1037
1038 case tok_thong:
1039 parse_thong_decl ();
1040 break;
1041
1042 case tok_left:
1043 parse_assoc_decl (left_assoc);
1044 break;
1045
1046 case tok_right:
1047 parse_assoc_decl (right_assoc);
1048 break;
1049
1050 case tok_nonassoc:
1051 parse_assoc_decl (non_assoc);
1052 break;
1053
1054 case tok_hdrext:
1055 parse_header_extension_decl ();
1056 break;
1057
1058 case tok_srcext:
1059 parse_source_extension_decl ();
1060 break;
1061
1062 case tok_define:
1063 parse_macro_decl ();
1064 break;
1065
1066 case tok_skel:
1067 parse_skel_decl ();
1068 break;
1069
1070 case tok_noop:
1071 break;
1072
1073 default:
1074 complain (_("unrecognized: %s"), token_buffer);
1075 skip_to_char ('%');
1076 }
1077 }
1078 else if (c == EOF)
1079 fatal (_("no input grammar"));
1080 else
1081 {
1082 char buf[] = "c";
1083 buf[0] = c;
1084 complain (_("unknown character: %s"), quote (buf));
1085 skip_to_char ('%');
1086 }
1087 }
1088 }
1089 \f
1090 /*-------------------------------------------------------------------.
1091 | Assuming that a `{' has just been seen, copy everything up to the |
1092 | matching `}' into the actions file. STACK_OFFSET is the number of |
1093 | values in the current rule so far, which says where to find `$0' |
1094 | with respect to the top of the stack. |
1095 `-------------------------------------------------------------------*/
1096
1097 static void
1098 copy_action (symbol_list *rule, int stack_offset)
1099 {
1100 int c;
1101 int count;
1102 char buf[4096];
1103
1104 /* offset is always 0 if parser has already popped the stack pointer */
1105 if (semantic_parser)
1106 stack_offset = 0;
1107
1108 sprintf (buf, "\ncase %d:\n", nrules);
1109 obstack_grow (&action_obstack, buf, strlen (buf));
1110
1111 if (!no_lines_flag)
1112 {
1113 sprintf (buf, "#line %d %s\n",
1114 lineno, quotearg_style (c_quoting_style, infile));
1115 obstack_grow (&action_obstack, buf, strlen (buf));
1116 }
1117 obstack_1grow (&action_obstack, '{');
1118
1119 count = 1;
1120 c = getc (finput);
1121
1122 while (count > 0)
1123 {
1124 while (c != '}')
1125 {
1126 switch (c)
1127 {
1128 case '\n':
1129 obstack_1grow (&action_obstack, c);
1130 lineno++;
1131 break;
1132
1133 case '{':
1134 obstack_1grow (&action_obstack, c);
1135 count++;
1136 break;
1137
1138 case '\'':
1139 case '"':
1140 copy_string (finput, &action_obstack, c);
1141 break;
1142
1143 case '/':
1144 copy_comment (finput, &action_obstack);
1145 break;
1146
1147 case '$':
1148 copy_dollar (finput, &action_obstack,
1149 rule, stack_offset);
1150 break;
1151
1152 case '@':
1153 copy_at (finput, &action_obstack,
1154 stack_offset);
1155 break;
1156
1157 case EOF:
1158 fatal (_("unmatched %s"), "`{'");
1159
1160 default:
1161 obstack_1grow (&action_obstack, c);
1162 }
1163
1164 c = getc (finput);
1165 }
1166
1167 /* above loop exits when c is '}' */
1168
1169 if (--count)
1170 {
1171 obstack_1grow (&action_obstack, c);
1172 c = getc (finput);
1173 }
1174 }
1175
1176 obstack_sgrow (&action_obstack, ";\n break;}");
1177 }
1178 \f
1179 /*-------------------------------------------------------------------.
1180 | After `%guard' is seen in the input file, copy the actual guard |
1181 | into the guards file. If the guard is followed by an action, copy |
1182 | that into the actions file. STACK_OFFSET is the number of values |
1183 | in the current rule so far, which says where to find `$0' with |
1184 | respect to the top of the stack, for the simple parser in which |
1185 | the stack is not popped until after the guard is run. |
1186 `-------------------------------------------------------------------*/
1187
1188 static void
1189 copy_guard (symbol_list *rule, int stack_offset)
1190 {
1191 int c;
1192 int count;
1193 int brace_flag = 0;
1194
1195 /* offset is always 0 if parser has already popped the stack pointer */
1196 if (semantic_parser)
1197 stack_offset = 0;
1198
1199 obstack_fgrow1 (&guard_obstack, "\ncase %d:\n", nrules);
1200 if (!no_lines_flag)
1201 obstack_fgrow2 (&guard_obstack, "#line %d %s\n",
1202 lineno, quotearg_style (c_quoting_style, infile));
1203 obstack_1grow (&guard_obstack, '{');
1204
1205 count = 0;
1206 c = getc (finput);
1207
1208 while (brace_flag ? (count > 0) : (c != ';'))
1209 {
1210 switch (c)
1211 {
1212 case '\n':
1213 obstack_1grow (&guard_obstack, c);
1214 lineno++;
1215 break;
1216
1217 case '{':
1218 obstack_1grow (&guard_obstack, c);
1219 brace_flag = 1;
1220 count++;
1221 break;
1222
1223 case '}':
1224 obstack_1grow (&guard_obstack, c);
1225 if (count > 0)
1226 count--;
1227 else
1228 {
1229 complain (_("unmatched %s"), "`}'");
1230 c = getc (finput); /* skip it */
1231 }
1232 break;
1233
1234 case '\'':
1235 case '"':
1236 copy_string (finput, &guard_obstack, c);
1237 break;
1238
1239 case '/':
1240 copy_comment (finput, &guard_obstack);
1241 break;
1242
1243 case '$':
1244 copy_dollar (finput, &guard_obstack, rule, stack_offset);
1245 break;
1246
1247 case '@':
1248 copy_at (finput, &guard_obstack, stack_offset);
1249 break;
1250
1251 case EOF:
1252 fatal ("%s", _("unterminated %guard clause"));
1253
1254 default:
1255 obstack_1grow (&guard_obstack, c);
1256 }
1257
1258 if (c != '}' || count != 0)
1259 c = getc (finput);
1260 }
1261
1262 c = skip_white_space ();
1263
1264 obstack_sgrow (&guard_obstack, ";\n break;}");
1265 if (c == '{')
1266 copy_action (rule, stack_offset);
1267 else if (c == '=')
1268 {
1269 c = getc (finput); /* why not skip_white_space -wjh */
1270 if (c == '{')
1271 copy_action (rule, stack_offset);
1272 }
1273 else
1274 ungetc (c, finput);
1275 }
1276 \f
1277
1278 static void
1279 record_rule_line (void)
1280 {
1281 /* Record each rule's source line number in rline table. */
1282
1283 if (nrules >= rline_allocated)
1284 {
1285 rline_allocated = nrules * 2;
1286 rline = XREALLOC (rline, short, rline_allocated);
1287 }
1288 rline[nrules] = lineno;
1289 }
1290
1291
1292 /*-------------------------------------------------------------------.
1293 | Generate a dummy symbol, a nonterminal, whose name cannot conflict |
1294 | with the user's names. |
1295 `-------------------------------------------------------------------*/
1296
1297 static bucket *
1298 gensym (void)
1299 {
1300 /* Incremented for each generated symbol */
1301 static int gensym_count = 0;
1302 static char buf[256];
1303
1304 bucket *sym;
1305
1306 sprintf (buf, "@%d", ++gensym_count);
1307 token_buffer = buf;
1308 sym = getsym (token_buffer);
1309 sym->class = nterm_sym;
1310 sym->value = nvars++;
1311 return sym;
1312 }
1313
1314 #if 0
1315 /*------------------------------------------------------------------.
1316 | read in a %type declaration and record its information for |
1317 | get_type_name to access. This is unused. It is only called from |
1318 | the #if 0 part of readgram |
1319 `------------------------------------------------------------------*/
1320
1321 static int
1322 get_type (void)
1323 {
1324 int k;
1325 token_t token;
1326 char *name;
1327
1328 token = lex ();
1329
1330 if (token != tok_typename)
1331 {
1332 complain (_("invalid %s declaration"), "%type");
1333 return t;
1334 }
1335
1336 name = xstrdup (token_buffer);
1337
1338 for (;;)
1339 {
1340 token = lex ();
1341
1342 switch (token)
1343 {
1344 case tok_semicolon:
1345 return lex ();
1346
1347 case tok_comma:
1348 break;
1349
1350 case tok_identifier:
1351 if (symval->type_name == NULL)
1352 symval->type_name = name;
1353 else if (strcmp (name, symval->type_name) != 0)
1354 complain (_("type redeclaration for %s"), symval->tag);
1355
1356 break;
1357
1358 default:
1359 return token;
1360 }
1361 }
1362 }
1363
1364 #endif
1365 \f
1366 /*------------------------------------------------------------------.
1367 | Parse the input grammar into a one symbol_list structure. Each |
1368 | rule is represented by a sequence of symbols: the left hand side |
1369 | followed by the contents of the right hand side, followed by a |
1370 | null pointer instead of a symbol to terminate the rule. The next |
1371 | symbol is the lhs of the following rule. |
1372 | |
1373 | All guards and actions are copied out to the appropriate files, |
1374 | labelled by the rule number they apply to. |
1375 `------------------------------------------------------------------*/
1376
1377 static void
1378 readgram (void)
1379 {
1380 token_t t;
1381 bucket *lhs = NULL;
1382 symbol_list *p;
1383 symbol_list *p1;
1384 bucket *bp;
1385
1386 /* Points to first symbol_list of current rule. its symbol is the
1387 lhs of the rule. */
1388 symbol_list *crule;
1389 /* Points to the symbol_list preceding crule. */
1390 symbol_list *crule1;
1391
1392 p1 = NULL;
1393
1394 t = lex ();
1395
1396 while (t != tok_two_percents && t != tok_eof)
1397 {
1398 if (t == tok_identifier || t == tok_bar)
1399 {
1400 int action_flag = 0;
1401 /* Number of symbols in rhs of this rule so far */
1402 int rulelength = 0;
1403 int xactions = 0; /* JF for error checking */
1404 bucket *first_rhs = 0;
1405
1406 if (t == tok_identifier)
1407 {
1408 lhs = symval;
1409
1410 if (!start_flag)
1411 {
1412 startval = lhs;
1413 start_flag = 1;
1414 }
1415
1416 t = lex ();
1417 if (t != tok_colon)
1418 {
1419 complain (_("ill-formed rule: initial symbol not followed by colon"));
1420 unlex (t);
1421 }
1422 }
1423
1424 if (nrules == 0 && t == tok_bar)
1425 {
1426 complain (_("grammar starts with vertical bar"));
1427 lhs = symval; /* BOGUS: use a random symval */
1428 }
1429 /* start a new rule and record its lhs. */
1430
1431 nrules++;
1432 nitems++;
1433
1434 record_rule_line ();
1435
1436 p = XCALLOC (symbol_list, 1);
1437 p->sym = lhs;
1438
1439 crule1 = p1;
1440 if (p1)
1441 p1->next = p;
1442 else
1443 grammar = p;
1444
1445 p1 = p;
1446 crule = p;
1447
1448 /* mark the rule's lhs as a nonterminal if not already so. */
1449
1450 if (lhs->class == unknown_sym)
1451 {
1452 lhs->class = nterm_sym;
1453 lhs->value = nvars;
1454 nvars++;
1455 }
1456 else if (lhs->class == token_sym)
1457 complain (_("rule given for %s, which is a token"), lhs->tag);
1458
1459 /* read the rhs of the rule. */
1460
1461 for (;;)
1462 {
1463 t = lex ();
1464 if (t == tok_prec)
1465 {
1466 t = lex ();
1467 crule->ruleprec = symval;
1468 t = lex ();
1469 }
1470
1471 if (!(t == tok_identifier || t == tok_left_curly))
1472 break;
1473
1474 /* If next token is an identifier, see if a colon follows it.
1475 If one does, exit this rule now. */
1476 if (t == tok_identifier)
1477 {
1478 bucket *ssave;
1479 token_t t1;
1480
1481 ssave = symval;
1482 t1 = lex ();
1483 unlex (t1);
1484 symval = ssave;
1485 if (t1 == tok_colon)
1486 break;
1487
1488 if (!first_rhs) /* JF */
1489 first_rhs = symval;
1490 /* Not followed by colon =>
1491 process as part of this rule's rhs. */
1492 }
1493
1494 /* If we just passed an action, that action was in the middle
1495 of a rule, so make a dummy rule to reduce it to a
1496 non-terminal. */
1497 if (action_flag)
1498 {
1499 bucket *sdummy;
1500
1501 /* Since the action was written out with this rule's
1502 number, we must give the new rule this number by
1503 inserting the new rule before it. */
1504
1505 /* Make a dummy nonterminal, a gensym. */
1506 sdummy = gensym ();
1507
1508 /* Make a new rule, whose body is empty,
1509 before the current one, so that the action
1510 just read can belong to it. */
1511 nrules++;
1512 nitems++;
1513 record_rule_line ();
1514 p = XCALLOC (symbol_list, 1);
1515 if (crule1)
1516 crule1->next = p;
1517 else
1518 grammar = p;
1519 p->sym = sdummy;
1520 crule1 = XCALLOC (symbol_list, 1);
1521 p->next = crule1;
1522 crule1->next = crule;
1523
1524 /* Insert the dummy generated by that rule into this
1525 rule. */
1526 nitems++;
1527 p = XCALLOC (symbol_list, 1);
1528 p->sym = sdummy;
1529 p1->next = p;
1530 p1 = p;
1531
1532 action_flag = 0;
1533 }
1534
1535 if (t == tok_identifier)
1536 {
1537 nitems++;
1538 p = XCALLOC (symbol_list, 1);
1539 p->sym = symval;
1540 p1->next = p;
1541 p1 = p;
1542 }
1543 else /* handle an action. */
1544 {
1545 copy_action (crule, rulelength);
1546 action_flag = 1;
1547 xactions++; /* JF */
1548 }
1549 rulelength++;
1550 } /* end of read rhs of rule */
1551
1552 /* Put an empty link in the list to mark the end of this rule */
1553 p = XCALLOC (symbol_list, 1);
1554 p1->next = p;
1555 p1 = p;
1556
1557 if (t == tok_prec)
1558 {
1559 complain (_("two @prec's in a row"));
1560 t = lex ();
1561 crule->ruleprec = symval;
1562 t = lex ();
1563 }
1564 if (t == tok_guard)
1565 {
1566 if (!semantic_parser)
1567 complain (_("%%guard present but %%semantic_parser not specified"));
1568
1569 copy_guard (crule, rulelength);
1570 t = lex ();
1571 }
1572 else if (t == tok_left_curly)
1573 {
1574 /* This case never occurs -wjh */
1575 if (action_flag)
1576 complain (_("two actions at end of one rule"));
1577 copy_action (crule, rulelength);
1578 action_flag = 1;
1579 xactions++; /* -wjh */
1580 t = lex ();
1581 }
1582 /* If $$ is being set in default way, report if any type
1583 mismatch. */
1584 else if (!xactions
1585 && first_rhs && lhs->type_name != first_rhs->type_name)
1586 {
1587 if (lhs->type_name == 0
1588 || first_rhs->type_name == 0
1589 || strcmp (lhs->type_name, first_rhs->type_name))
1590 complain (_("type clash (`%s' `%s') on default action"),
1591 lhs->type_name ? lhs->type_name : "",
1592 first_rhs->type_name ? first_rhs->type_name : "");
1593 }
1594 /* Warn if there is no default for $$ but we need one. */
1595 else if (!xactions && !first_rhs && lhs->type_name != 0)
1596 complain (_("empty rule for typed nonterminal, and no action"));
1597 if (t == tok_semicolon)
1598 t = lex ();
1599 }
1600 #if 0
1601 /* these things can appear as alternatives to rules. */
1602 /* NO, they cannot.
1603 a) none of the documentation allows them
1604 b) most of them scan forward until finding a next %
1605 thus they may swallow lots of intervening rules
1606 */
1607 else if (t == tok_token)
1608 {
1609 parse_token_decl (token_sym, nterm_sym);
1610 t = lex ();
1611 }
1612 else if (t == tok_nterm)
1613 {
1614 parse_token_decl (nterm_sym, token_sym);
1615 t = lex ();
1616 }
1617 else if (t == tok_type)
1618 {
1619 t = get_type ();
1620 }
1621 else if (t == tok_union)
1622 {
1623 parse_union_decl ();
1624 t = lex ();
1625 }
1626 else if (t == tok_expect)
1627 {
1628 parse_expect_decl ();
1629 t = lex ();
1630 }
1631 else if (t == tok_start)
1632 {
1633 parse_start_decl ();
1634 t = lex ();
1635 }
1636 #endif
1637
1638 else
1639 {
1640 complain (_("invalid input: %s"), quote (token_buffer));
1641 t = lex ();
1642 }
1643 }
1644
1645 /* grammar has been read. Do some checking */
1646
1647 if (nsyms > MAXSHORT)
1648 fatal (_("too many symbols (tokens plus nonterminals); maximum %d"),
1649 MAXSHORT);
1650 if (nrules == 0)
1651 fatal (_("no rules in the input grammar"));
1652
1653 #if 0 /* This code is in the skeleton now. */
1654 /* JF put out same default YYSTYPE as YACC does */
1655 if (typed == 0
1656 && !value_components_used)
1657 {
1658 /* We used to use `unsigned long' as YYSTYPE on MSDOS,
1659 but it seems better to be consistent.
1660 Most programs should declare their own type anyway. */
1661 obstack_sgrow (&attrs_obstack,
1662 "#ifndef YYSTYPE\n#define YYSTYPE int\n#endif\n");
1663 if (defines_flag)
1664 obstack_sgrow (&defines_obstack, "\
1665 # ifndef YYSTYPE\n\
1666 # define YYSTYPE int\n\
1667 # endif\n");
1668 }
1669 #endif
1670
1671 /* Report any undefined symbols and consider them nonterminals. */
1672
1673 for (bp = firstsymbol; bp; bp = bp->next)
1674 if (bp->class == unknown_sym)
1675 {
1676 complain (_
1677 ("symbol %s is used, but is not defined as a token and has no rules"),
1678 bp->tag);
1679 bp->class = nterm_sym;
1680 bp->value = nvars++;
1681 }
1682
1683 ntokens = nsyms - nvars;
1684 }
1685
1686 /* At the end of the grammar file, some C source code must
1687 be stored. It is going to be associated to the user_code
1688 directive. */
1689 static void
1690 read_additionnal_code (void)
1691 {
1692 char c;
1693 struct obstack uc_obstack;
1694
1695 obstack_init (&uc_obstack);
1696
1697 while ((c = getc (finput)) != EOF)
1698 obstack_1grow (&uc_obstack, c);
1699
1700 obstack_1grow (&uc_obstack, 0);
1701 macro_insert ("user_code", obstack_finish (&uc_obstack));
1702 }
1703
1704 \f
1705 /*--------------------------------------------------------------.
1706 | For named tokens, but not literal ones, define the name. The |
1707 | value is the user token number. |
1708 `--------------------------------------------------------------*/
1709
1710 static void
1711 output_token_defines (struct obstack *oout)
1712 {
1713 bucket *bp;
1714 char *cp, *symbol;
1715 char c;
1716
1717 for (bp = firstsymbol; bp; bp = bp->next)
1718 {
1719 symbol = bp->tag; /* get symbol */
1720
1721 if (bp->value >= ntokens)
1722 continue;
1723 if (bp->user_token_number == SALIAS)
1724 continue;
1725 if ('\'' == *symbol)
1726 continue; /* skip literal character */
1727 if (bp == errtoken)
1728 continue; /* skip error token */
1729 if ('\"' == *symbol)
1730 {
1731 /* use literal string only if given a symbol with an alias */
1732 if (bp->alias)
1733 symbol = bp->alias->tag;
1734 else
1735 continue;
1736 }
1737
1738 /* Don't #define nonliteral tokens whose names contain periods. */
1739 cp = symbol;
1740 while ((c = *cp++) && c != '.');
1741 if (c != '\0')
1742 continue;
1743
1744 obstack_fgrow2 (oout, "# define\t%s\t%d\n",
1745 symbol,
1746 (translations ? bp->user_token_number : bp->value));
1747 if (semantic_parser)
1748 obstack_fgrow2 (oout, "# define\tT%s\t%d\n", symbol, bp->value);
1749 }
1750
1751 /* obstack_1grow (oout, '\n'); */
1752 }
1753
1754
1755 /*------------------------------------------------------------------.
1756 | Assign symbol numbers, and write definition of token names into |
1757 | FDEFINES. Set up vectors TAGS and SPREC of names and precedences |
1758 | of symbols. |
1759 `------------------------------------------------------------------*/
1760
1761 static void
1762 packsymbols (void)
1763 {
1764 bucket *bp;
1765 int tokno = 1;
1766 int i;
1767 int last_user_token_number;
1768 static char DOLLAR[] = "$";
1769
1770 /* int lossage = 0; JF set but not used */
1771
1772 tags = XCALLOC (char *, nsyms + 1);
1773 tags[0] = DOLLAR;
1774 user_toknums = XCALLOC (short, nsyms + 1);
1775 user_toknums[0] = 0;
1776
1777 sprec = XCALLOC (short, nsyms);
1778 sassoc = XCALLOC (short, nsyms);
1779
1780 max_user_token_number = 256;
1781 last_user_token_number = 256;
1782
1783 for (bp = firstsymbol; bp; bp = bp->next)
1784 {
1785 if (bp->class == nterm_sym)
1786 {
1787 bp->value += ntokens;
1788 }
1789 else if (bp->alias)
1790 {
1791 /* this symbol and its alias are a single token defn.
1792 allocate a tokno, and assign to both check agreement of
1793 ->prec and ->assoc fields and make both the same */
1794 if (bp->value == 0)
1795 bp->value = bp->alias->value = tokno++;
1796
1797 if (bp->prec != bp->alias->prec)
1798 {
1799 if (bp->prec != 0 && bp->alias->prec != 0
1800 && bp->user_token_number == SALIAS)
1801 complain (_("conflicting precedences for %s and %s"),
1802 bp->tag, bp->alias->tag);
1803 if (bp->prec != 0)
1804 bp->alias->prec = bp->prec;
1805 else
1806 bp->prec = bp->alias->prec;
1807 }
1808
1809 if (bp->assoc != bp->alias->assoc)
1810 {
1811 if (bp->assoc != 0 && bp->alias->assoc != 0
1812 && bp->user_token_number == SALIAS)
1813 complain (_("conflicting assoc values for %s and %s"),
1814 bp->tag, bp->alias->tag);
1815 if (bp->assoc != 0)
1816 bp->alias->assoc = bp->assoc;
1817 else
1818 bp->assoc = bp->alias->assoc;
1819 }
1820
1821 if (bp->user_token_number == SALIAS)
1822 continue; /* do not do processing below for SALIASs */
1823
1824 }
1825 else /* bp->class == token_sym */
1826 {
1827 bp->value = tokno++;
1828 }
1829
1830 if (bp->class == token_sym)
1831 {
1832 if (translations && !(bp->user_token_number))
1833 bp->user_token_number = ++last_user_token_number;
1834 if (bp->user_token_number > max_user_token_number)
1835 max_user_token_number = bp->user_token_number;
1836 }
1837
1838 tags[bp->value] = bp->tag;
1839 user_toknums[bp->value] = bp->user_token_number;
1840 sprec[bp->value] = bp->prec;
1841 sassoc[bp->value] = bp->assoc;
1842
1843 }
1844
1845 if (translations)
1846 {
1847 int j;
1848
1849 token_translations = XCALLOC (short, max_user_token_number + 1);
1850
1851 /* initialize all entries for literal tokens to 2, the internal
1852 token number for $undefined., which represents all invalid
1853 inputs. */
1854 for (j = 0; j <= max_user_token_number; j++)
1855 token_translations[j] = 2;
1856
1857 for (bp = firstsymbol; bp; bp = bp->next)
1858 {
1859 if (bp->value >= ntokens)
1860 continue; /* non-terminal */
1861 if (bp->user_token_number == SALIAS)
1862 continue;
1863 if (token_translations[bp->user_token_number] != 2)
1864 complain (_("tokens %s and %s both assigned number %d"),
1865 tags[token_translations[bp->user_token_number]],
1866 bp->tag, bp->user_token_number);
1867 token_translations[bp->user_token_number] = bp->value;
1868 }
1869 }
1870
1871 error_token_number = errtoken->value;
1872
1873 output_token_defines (&output_obstack);
1874 obstack_1grow (&output_obstack, 0);
1875 macro_insert ("tokendef", obstack_finish (&output_obstack));
1876
1877 /* if (!no_parser_flag)
1878 output_token_defines (&table_obstack); */
1879
1880 if (startval->class == unknown_sym)
1881 fatal (_("the start symbol %s is undefined"), startval->tag);
1882 else if (startval->class == token_sym)
1883 fatal (_("the start symbol %s is a token"), startval->tag);
1884
1885 start_symbol = startval->value;
1886
1887 if (defines_flag)
1888 {
1889 output_token_defines (&defines_obstack);
1890
1891 if (!pure_parser)
1892 {
1893 if (spec_name_prefix)
1894 obstack_fgrow1 (&defines_obstack, "\nextern YYSTYPE %slval;\n",
1895 spec_name_prefix);
1896 else
1897 obstack_sgrow (&defines_obstack,
1898 "\nextern YYSTYPE yylval;\n");
1899 }
1900
1901 if (semantic_parser)
1902 for (i = ntokens; i < nsyms; i++)
1903 {
1904 /* don't make these for dummy nonterminals made by gensym. */
1905 if (*tags[i] != '@')
1906 obstack_fgrow2 (&defines_obstack,
1907 "# define\tNT%s\t%d\n", tags[i], i);
1908 }
1909 #if 0
1910 /* `fdefines' is now a temporary file, so we need to copy its
1911 contents in `done', so we can't close it here. */
1912 fclose (fdefines);
1913 fdefines = NULL;
1914 #endif
1915 }
1916 }
1917
1918
1919 /*---------------------------------------------------------------.
1920 | Convert the rules into the representation using RRHS, RLHS and |
1921 | RITEMS. |
1922 `---------------------------------------------------------------*/
1923
1924 static void
1925 packgram (void)
1926 {
1927 int itemno;
1928 int ruleno;
1929 symbol_list *p;
1930
1931 bucket *ruleprec;
1932
1933 ritem = XCALLOC (short, nitems + 1);
1934 rlhs = XCALLOC (short, nrules) - 1;
1935 rrhs = XCALLOC (short, nrules) - 1;
1936 rprec = XCALLOC (short, nrules) - 1;
1937 rprecsym = XCALLOC (short, nrules) - 1;
1938 rassoc = XCALLOC (short, nrules) - 1;
1939
1940 itemno = 0;
1941 ruleno = 1;
1942
1943 p = grammar;
1944 while (p)
1945 {
1946 rlhs[ruleno] = p->sym->value;
1947 rrhs[ruleno] = itemno;
1948 ruleprec = p->ruleprec;
1949
1950 p = p->next;
1951 while (p && p->sym)
1952 {
1953 ritem[itemno++] = p->sym->value;
1954 /* A rule gets by default the precedence and associativity
1955 of the last token in it. */
1956 if (p->sym->class == token_sym)
1957 {
1958 rprec[ruleno] = p->sym->prec;
1959 rassoc[ruleno] = p->sym->assoc;
1960 }
1961 if (p)
1962 p = p->next;
1963 }
1964
1965 /* If this rule has a %prec,
1966 the specified symbol's precedence replaces the default. */
1967 if (ruleprec)
1968 {
1969 rprec[ruleno] = ruleprec->prec;
1970 rassoc[ruleno] = ruleprec->assoc;
1971 rprecsym[ruleno] = ruleprec->value;
1972 }
1973
1974 ritem[itemno++] = -ruleno;
1975 ruleno++;
1976
1977 if (p)
1978 p = p->next;
1979 }
1980
1981 ritem[itemno] = 0;
1982 }
1983 \f
1984 /*-------------------------------------------------------------------.
1985 | Read in the grammar specification and record it in the format |
1986 | described in gram.h. All guards are copied into the GUARD_OBSTACK |
1987 | and all actions into ACTION_OBSTACK, in each case forming the body |
1988 | of a C function (YYGUARD or YYACTION) which contains a switch |
1989 | statement to decide which guard or action to execute. |
1990 `-------------------------------------------------------------------*/
1991
1992 void
1993 reader (void)
1994 {
1995 start_flag = 0;
1996 startval = NULL; /* start symbol not specified yet. */
1997
1998 #if 0
1999 /* initially assume token number translation not needed. */
2000 translations = 0;
2001 #endif
2002 /* Nowadays translations is always set to 1, since we give `error' a
2003 user-token-number to satisfy the Posix demand for YYERRCODE==256.
2004 */
2005 translations = 1;
2006
2007 nsyms = 1;
2008 nvars = 0;
2009 nrules = 0;
2010 nitems = 0;
2011 rline_allocated = 10;
2012 rline = XCALLOC (short, rline_allocated);
2013
2014 typed = 0;
2015 lastprec = 0;
2016
2017 semantic_parser = 0;
2018 pure_parser = 0;
2019
2020 grammar = NULL;
2021
2022 init_lex ();
2023 lineno = 1;
2024
2025 /* Initialize the macro obstack. */
2026 obstack_init (&macro_obstack);
2027
2028 /* Initialize the symbol table. */
2029 tabinit ();
2030
2031 /* Construct the error token */
2032 errtoken = getsym ("error");
2033 errtoken->class = token_sym;
2034 errtoken->user_token_number = 256; /* Value specified by POSIX. */
2035
2036 /* Construct a token that represents all undefined literal tokens.
2037 It is always token number 2. */
2038 undeftoken = getsym ("$undefined.");
2039 undeftoken->class = token_sym;
2040 undeftoken->user_token_number = 2;
2041
2042 /* Read the declaration section. Copy %{ ... %} groups to
2043 TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left,
2044 etc. found there. */
2045 read_declarations ();
2046 /* Read in the grammar, build grammar in list form. Write out
2047 guards and actions. */
2048 readgram ();
2049 /* Some C code is given at the end of the grammar file. */
2050 read_additionnal_code ();
2051 /* Now we know whether we need the line-number stack. If we do,
2052 write its type into the .tab.h file. */
2053 #if 0
2054 if (defines_flag)
2055 reader_output_yylsp (&defines_obstack);
2056 #endif
2057 /* Write closing delimiters for actions and guards. */
2058 #if 0
2059 if (locations_flag)
2060 obstack_sgrow (&table_obstack, "#define YYLSP_NEEDED 1\n\n");
2061 #endif
2062 /* Assign the symbols their symbol numbers. Write #defines for the
2063 token symbols into FDEFINES if requested. */
2064 packsymbols ();
2065 /* Convert the grammar into the format described in gram.h. */
2066 packgram ();
2067 /* Free the symbol table data structure since symbols are now all
2068 referred to by symbol number. */
2069 free_symtab ();
2070 }