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