]>
Commit | Line | Data |
---|---|---|
1 | /* Input parser for bison | |
2 | Copyright (C) 1984, 1986, 1989, 1992, 1998, 2000, 2001, 2002 | |
3 | Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
7 | Bison is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | Bison is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with Bison; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | ||
23 | #include "system.h" | |
24 | #include "quotearg.h" | |
25 | #include "quote.h" | |
26 | #include "getargs.h" | |
27 | #include "files.h" | |
28 | #include "symtab.h" | |
29 | #include "options.h" | |
30 | #include "lex.h" | |
31 | #include "gram.h" | |
32 | #include "complain.h" | |
33 | #include "output.h" | |
34 | #include "reader.h" | |
35 | #include "conflicts.h" | |
36 | #include "muscle_tab.h" | |
37 | ||
38 | typedef struct symbol_list | |
39 | { | |
40 | struct symbol_list *next; | |
41 | symbol_t *sym; | |
42 | int line; | |
43 | ||
44 | /* The action is attached to the LHS of a rule. */ | |
45 | const char *action; | |
46 | int action_line; | |
47 | ||
48 | symbol_t *ruleprec; | |
49 | } symbol_list; | |
50 | ||
51 | int lineno; | |
52 | static symbol_list *grammar = NULL; | |
53 | static int start_flag = 0; | |
54 | ||
55 | /* Nonzero if %union has been seen. */ | |
56 | static int typed = 0; | |
57 | ||
58 | /* Incremented for each %left, %right or %nonassoc seen */ | |
59 | static int lastprec = 0; | |
60 | ||
61 | static symbol_list * | |
62 | symbol_list_new (symbol_t *sym) | |
63 | { | |
64 | symbol_list *res = XMALLOC (symbol_list, 1); | |
65 | res->next = NULL; | |
66 | res->sym = sym; | |
67 | res->line = lineno; | |
68 | res->action = NULL; | |
69 | res->action_line = 0; | |
70 | res->ruleprec = NULL; | |
71 | return res; | |
72 | } | |
73 | ||
74 | /*===================\ | |
75 | | Low level lexing. | | |
76 | \===================*/ | |
77 | ||
78 | static void | |
79 | skip_to_char (int target) | |
80 | { | |
81 | int c; | |
82 | if (target == '\n') | |
83 | complain (_(" Skipping to next \\n")); | |
84 | else | |
85 | complain (_(" Skipping to next %c"), target); | |
86 | ||
87 | do | |
88 | c = skip_white_space (); | |
89 | while (c != target && c != EOF); | |
90 | if (c != EOF) | |
91 | ungetc (c, finput); | |
92 | } | |
93 | ||
94 | ||
95 | /*---------------------------------------------------------. | |
96 | | Read a signed integer from STREAM and return its value. | | |
97 | `---------------------------------------------------------*/ | |
98 | ||
99 | static inline int | |
100 | read_signed_integer (FILE *stream) | |
101 | { | |
102 | int c = getc (stream); | |
103 | int sign = 1; | |
104 | int n = 0; | |
105 | ||
106 | if (c == '-') | |
107 | { | |
108 | c = getc (stream); | |
109 | sign = -1; | |
110 | } | |
111 | ||
112 | while (isdigit (c)) | |
113 | { | |
114 | n = 10 * n + (c - '0'); | |
115 | c = getc (stream); | |
116 | } | |
117 | ||
118 | ungetc (c, stream); | |
119 | ||
120 | return sign * n; | |
121 | } | |
122 | \f | |
123 | /*--------------------------------------------------------------. | |
124 | | Get the data type (alternative in the union) of the value for | | |
125 | | symbol N in rule RULE. | | |
126 | `--------------------------------------------------------------*/ | |
127 | ||
128 | static char * | |
129 | get_type_name (int n, symbol_list *rule) | |
130 | { | |
131 | int i; | |
132 | symbol_list *rp; | |
133 | ||
134 | if (n < 0) | |
135 | { | |
136 | complain (_("invalid $ value")); | |
137 | return NULL; | |
138 | } | |
139 | ||
140 | rp = rule; | |
141 | i = 0; | |
142 | ||
143 | while (i < n) | |
144 | { | |
145 | rp = rp->next; | |
146 | if (rp == NULL || rp->sym == NULL) | |
147 | { | |
148 | complain (_("invalid $ value")); | |
149 | return NULL; | |
150 | } | |
151 | ++i; | |
152 | } | |
153 | ||
154 | return rp->sym->type_name; | |
155 | } | |
156 | \f | |
157 | /*------------------------------------------------------------------. | |
158 | | Copy the character C to OOUT, and insert quadigraphs when needed. | | |
159 | `------------------------------------------------------------------*/ | |
160 | ||
161 | static inline void | |
162 | copy_character (struct obstack *oout, int c) | |
163 | { | |
164 | switch (c) | |
165 | { | |
166 | case '[': | |
167 | obstack_sgrow (oout, "@<:@"); | |
168 | break; | |
169 | ||
170 | case ']': | |
171 | obstack_sgrow (oout, "@:>@"); | |
172 | break; | |
173 | ||
174 | default: | |
175 | obstack_1grow (oout, c); | |
176 | } | |
177 | } | |
178 | ||
179 | /*------------------------------------------------------------. | |
180 | | Dump the string from FIN to OOUT if non null. MATCH is the | | |
181 | | delimiter of the string (either ' or "). | | |
182 | `------------------------------------------------------------*/ | |
183 | ||
184 | static inline void | |
185 | copy_string2 (FILE *fin, struct obstack *oout, int match, int store) | |
186 | { | |
187 | int c; | |
188 | ||
189 | if (store) | |
190 | obstack_1grow (oout, match); | |
191 | ||
192 | c = getc (fin); | |
193 | ||
194 | while (c != match) | |
195 | { | |
196 | if (c == EOF) | |
197 | fatal (_("unterminated string at end of file")); | |
198 | if (c == '\n') | |
199 | { | |
200 | complain (_("unterminated string")); | |
201 | ungetc (c, fin); | |
202 | c = match; /* invent terminator */ | |
203 | continue; | |
204 | } | |
205 | ||
206 | copy_character (oout, c); | |
207 | ||
208 | if (c == '\\') | |
209 | { | |
210 | c = getc (fin); | |
211 | if (c == EOF) | |
212 | fatal (_("unterminated string at end of file")); | |
213 | copy_character (oout, c); | |
214 | ||
215 | if (c == '\n') | |
216 | ++lineno; | |
217 | } | |
218 | ||
219 | c = getc (fin); | |
220 | } | |
221 | ||
222 | if (store) | |
223 | obstack_1grow (oout, c); | |
224 | } | |
225 | ||
226 | /* FIXME. */ | |
227 | ||
228 | static inline void | |
229 | copy_string (FILE *fin, struct obstack *oout, int match) | |
230 | { | |
231 | copy_string2 (fin, oout, match, 1); | |
232 | } | |
233 | ||
234 | /* FIXME. */ | |
235 | ||
236 | static inline void | |
237 | copy_identifier (FILE *fin, struct obstack *oout) | |
238 | { | |
239 | int c; | |
240 | ||
241 | while (isalnum (c = getc (fin)) || c == '_') | |
242 | obstack_1grow (oout, c); | |
243 | ||
244 | ungetc (c, fin); | |
245 | } | |
246 | ||
247 | ||
248 | /*------------------------------------------------------------------. | |
249 | | Dump the wannabee comment from IN to OOUT. In fact we just saw a | | |
250 | | `/', which might or might not be a comment. In any case, copy | | |
251 | | what we saw. | | |
252 | `------------------------------------------------------------------*/ | |
253 | ||
254 | static inline void | |
255 | copy_comment (FILE *fin, struct obstack *oout) | |
256 | { | |
257 | int cplus_comment; | |
258 | int ended; | |
259 | int c; | |
260 | ||
261 | /* We read a `/', output it. */ | |
262 | obstack_1grow (oout, '/'); | |
263 | ||
264 | switch ((c = getc (fin))) | |
265 | { | |
266 | case '/': | |
267 | cplus_comment = 1; | |
268 | break; | |
269 | case '*': | |
270 | cplus_comment = 0; | |
271 | break; | |
272 | default: | |
273 | ungetc (c, fin); | |
274 | return; | |
275 | } | |
276 | ||
277 | obstack_1grow (oout, c); | |
278 | c = getc (fin); | |
279 | ||
280 | ended = 0; | |
281 | while (!ended) | |
282 | { | |
283 | if (!cplus_comment && c == '*') | |
284 | { | |
285 | while (c == '*') | |
286 | { | |
287 | obstack_1grow (oout, c); | |
288 | c = getc (fin); | |
289 | } | |
290 | ||
291 | if (c == '/') | |
292 | { | |
293 | obstack_1grow (oout, c); | |
294 | ended = 1; | |
295 | } | |
296 | } | |
297 | else if (c == '\n') | |
298 | { | |
299 | ++lineno; | |
300 | obstack_1grow (oout, c); | |
301 | if (cplus_comment) | |
302 | ended = 1; | |
303 | else | |
304 | c = getc (fin); | |
305 | } | |
306 | else if (c == EOF) | |
307 | fatal (_("unterminated comment")); | |
308 | else | |
309 | { | |
310 | copy_character (oout, c); | |
311 | c = getc (fin); | |
312 | } | |
313 | } | |
314 | } | |
315 | ||
316 | ||
317 | /*-------------------------------------------------------------------. | |
318 | | FIN is pointing to a location (i.e., a `@'). Output to OOUT a | | |
319 | | reference to this location. RULE_LENGTH is the number of values in | | |
320 | | 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 rule_length) | |
326 | { | |
327 | int c = getc (fin); | |
328 | locations_flag = 1; | |
329 | ||
330 | if (c == '$') | |
331 | { | |
332 | obstack_sgrow (oout, "]b4_lhs_location["); | |
333 | } | |
334 | else if (isdigit (c) || c == '-') | |
335 | { | |
336 | int n; | |
337 | ||
338 | ungetc (c, fin); | |
339 | n = read_signed_integer (fin); | |
340 | if (n > rule_length) | |
341 | complain (_("invalid value: %s%d"), "@", n); | |
342 | else | |
343 | obstack_fgrow2 (oout, "]b4_rhs_location([%d], [%d])[", | |
344 | rule_length, n); | |
345 | } | |
346 | else | |
347 | { | |
348 | char buf[] = "@c"; | |
349 | buf[1] = c; | |
350 | complain (_("%s is invalid"), quote (buf)); | |
351 | } | |
352 | } | |
353 | ||
354 | ||
355 | /*------------------------------------------------------------------. | |
356 | | FIN is pointing to a wannabee semantic value (i.e., a `$'). | | |
357 | | | | |
358 | | Possible inputs: $[<TYPENAME>]($|integer) | | |
359 | | | | |
360 | | Output to OOUT a reference to this semantic value. RULE_LENGTH is | | |
361 | | the number of values in the current rule so far, which says where | | |
362 | | to find `$0' with respect to the top of the stack. | | |
363 | `------------------------------------------------------------------*/ | |
364 | ||
365 | static inline void | |
366 | copy_dollar (FILE *fin, struct obstack *oout, | |
367 | symbol_list *rule, int rule_length) | |
368 | { | |
369 | int c = getc (fin); | |
370 | const char *type_name = NULL; | |
371 | ||
372 | /* Get the type name if explicit. */ | |
373 | if (c == '<') | |
374 | { | |
375 | read_type_name (fin); | |
376 | type_name = token_buffer; | |
377 | c = getc (fin); | |
378 | } | |
379 | ||
380 | if (c == '$') | |
381 | { | |
382 | if (!type_name) | |
383 | type_name = get_type_name (0, rule); | |
384 | if (!type_name && typed) | |
385 | complain (_("$$ of `%s' has no declared type"), | |
386 | rule->sym->tag); | |
387 | if (!type_name) | |
388 | type_name = ""; | |
389 | obstack_fgrow1 (oout, | |
390 | "]b4_lhs_value([%s])[", type_name); | |
391 | } | |
392 | else if (isdigit (c) || c == '-') | |
393 | { | |
394 | int n; | |
395 | ungetc (c, fin); | |
396 | n = read_signed_integer (fin); | |
397 | ||
398 | if (n > rule_length) | |
399 | complain (_("invalid value: %s%d"), "$", n); | |
400 | else | |
401 | { | |
402 | if (!type_name && n > 0) | |
403 | type_name = get_type_name (n, rule); | |
404 | if (!type_name && typed) | |
405 | complain (_("$%d of `%s' has no declared type"), | |
406 | n, rule->sym->tag); | |
407 | if (!type_name) | |
408 | type_name = ""; | |
409 | obstack_fgrow3 (oout, "]b4_rhs_value([%d], [%d], [%s])[", | |
410 | rule_length, n, type_name); | |
411 | } | |
412 | } | |
413 | else | |
414 | { | |
415 | char buf[] = "$c"; | |
416 | buf[1] = c; | |
417 | complain (_("%s is invalid"), quote (buf)); | |
418 | } | |
419 | } | |
420 | \f | |
421 | /*-------------------------------------------------------------------. | |
422 | | Copy the contents of a `%{ ... %}' into the definitions file. The | | |
423 | | `%{' has already been read. Return after reading the `%}'. | | |
424 | `-------------------------------------------------------------------*/ | |
425 | ||
426 | static void | |
427 | copy_definition (struct obstack *oout) | |
428 | { | |
429 | int c; | |
430 | /* -1 while reading a character if prev char was %. */ | |
431 | int after_percent; | |
432 | ||
433 | if (!no_lines_flag) | |
434 | { | |
435 | obstack_fgrow2 (oout, muscle_find ("linef"), | |
436 | lineno, quotearg_style (c_quoting_style, | |
437 | muscle_find ("filename"))); | |
438 | } | |
439 | ||
440 | after_percent = 0; | |
441 | ||
442 | c = getc (finput); | |
443 | ||
444 | for (;;) | |
445 | { | |
446 | switch (c) | |
447 | { | |
448 | case '\n': | |
449 | obstack_1grow (oout, c); | |
450 | ++lineno; | |
451 | break; | |
452 | ||
453 | case '%': | |
454 | after_percent = -1; | |
455 | break; | |
456 | ||
457 | case '\'': | |
458 | case '"': | |
459 | copy_string (finput, oout, c); | |
460 | break; | |
461 | ||
462 | case '/': | |
463 | copy_comment (finput, oout); | |
464 | break; | |
465 | ||
466 | case EOF: | |
467 | fatal ("%s", _("unterminated `%{' definition")); | |
468 | ||
469 | default: | |
470 | copy_character (oout, c); | |
471 | } | |
472 | ||
473 | c = getc (finput); | |
474 | ||
475 | if (after_percent) | |
476 | { | |
477 | if (c == '}') | |
478 | return; | |
479 | obstack_1grow (oout, '%'); | |
480 | } | |
481 | after_percent = 0; | |
482 | } | |
483 | } | |
484 | ||
485 | ||
486 | /*------------------------------------------. | |
487 | | Parse what comes after %token or %nterm. | | |
488 | `------------------------------------------*/ | |
489 | ||
490 | static void | |
491 | parse_token_decl (symbol_class class) | |
492 | { | |
493 | token_t token = tok_undef; | |
494 | char *typename = NULL; | |
495 | ||
496 | /* The symbol being defined. */ | |
497 | symbol_t *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 | switch (token) | |
514 | { | |
515 | case tok_comma: | |
516 | symbol = NULL; | |
517 | break; | |
518 | ||
519 | case tok_typename: | |
520 | typename = xstrdup (token_buffer); | |
521 | symbol = NULL; | |
522 | break; | |
523 | ||
524 | case tok_identifier: | |
525 | if (*symval->tag == '\"' && symbol) | |
526 | { | |
527 | symbol_make_alias (symbol, symval, typename); | |
528 | symbol = NULL; | |
529 | } | |
530 | else | |
531 | { | |
532 | symbol = symval; | |
533 | symbol_class_set (symbol, class); | |
534 | if (typename) | |
535 | symbol_type_set (symbol, typename); | |
536 | } | |
537 | break; | |
538 | ||
539 | case tok_number: | |
540 | symbol_user_token_number_set (symbol, numval); | |
541 | break; | |
542 | ||
543 | default: | |
544 | complain (_("`%s' is invalid in %s"), | |
545 | token_buffer, | |
546 | (class == token_sym) ? "%token" : "%nterm"); | |
547 | skip_to_char ('%'); | |
548 | } | |
549 | } | |
550 | ||
551 | } | |
552 | ||
553 | ||
554 | /*------------------------------. | |
555 | | Parse what comes after %start | | |
556 | `------------------------------*/ | |
557 | ||
558 | static void | |
559 | parse_start_decl (void) | |
560 | { | |
561 | if (start_flag) | |
562 | complain (_("multiple %s declarations"), "%start"); | |
563 | if (lex () != tok_identifier) | |
564 | complain (_("invalid %s declaration"), "%start"); | |
565 | else | |
566 | { | |
567 | start_flag = 1; | |
568 | startsymbol = symval; | |
569 | } | |
570 | } | |
571 | ||
572 | /*-----------------------------------------------------------. | |
573 | | read in a %type declaration and record its information for | | |
574 | | get_type_name to access | | |
575 | `-----------------------------------------------------------*/ | |
576 | ||
577 | static void | |
578 | parse_type_decl (void) | |
579 | { | |
580 | char *name; | |
581 | ||
582 | if (lex () != tok_typename) | |
583 | { | |
584 | complain ("%s", _("%type declaration has no <typename>")); | |
585 | skip_to_char ('%'); | |
586 | return; | |
587 | } | |
588 | ||
589 | name = xstrdup (token_buffer); | |
590 | ||
591 | for (;;) | |
592 | { | |
593 | token_t t; | |
594 | int tmp_char = ungetc (skip_white_space (), finput); | |
595 | ||
596 | if (tmp_char == '%') | |
597 | return; | |
598 | if (tmp_char == EOF) | |
599 | fatal (_("Premature EOF after %s"), token_buffer); | |
600 | ||
601 | t = lex (); | |
602 | ||
603 | switch (t) | |
604 | { | |
605 | case tok_comma: | |
606 | case tok_semicolon: | |
607 | break; | |
608 | ||
609 | case tok_identifier: | |
610 | symbol_type_set (symval, name); | |
611 | break; | |
612 | ||
613 | default: | |
614 | complain (_("invalid %%type declaration due to item: %s"), | |
615 | token_buffer); | |
616 | skip_to_char ('%'); | |
617 | } | |
618 | } | |
619 | } | |
620 | ||
621 | ||
622 | ||
623 | /*----------------------------------------------------------------. | |
624 | | Read in a %left, %right or %nonassoc declaration and record its | | |
625 | | information. | | |
626 | `----------------------------------------------------------------*/ | |
627 | ||
628 | static void | |
629 | parse_assoc_decl (associativity assoc) | |
630 | { | |
631 | char *name = NULL; | |
632 | int prev = 0; | |
633 | ||
634 | /* Assign a new precedence level, never 0. */ | |
635 | ++lastprec; | |
636 | ||
637 | for (;;) | |
638 | { | |
639 | token_t t; | |
640 | int tmp_char = ungetc (skip_white_space (), finput); | |
641 | ||
642 | if (tmp_char == '%') | |
643 | return; | |
644 | if (tmp_char == EOF) | |
645 | fatal (_("Premature EOF after %s"), token_buffer); | |
646 | ||
647 | t = lex (); | |
648 | ||
649 | switch (t) | |
650 | { | |
651 | case tok_typename: | |
652 | name = xstrdup (token_buffer); | |
653 | break; | |
654 | ||
655 | case tok_comma: | |
656 | break; | |
657 | ||
658 | case tok_identifier: | |
659 | symbol_class_set (symval, token_sym); | |
660 | symbol_precedence_set (symval, lastprec, assoc); | |
661 | if (name) | |
662 | symbol_type_set (symval, name); | |
663 | break; | |
664 | ||
665 | case tok_number: | |
666 | if (prev == tok_identifier) | |
667 | { | |
668 | symbol_user_token_number_set (symval, numval); | |
669 | } | |
670 | else | |
671 | { | |
672 | complain | |
673 | (_("invalid text (%s) - number should be after identifier"), | |
674 | token_buffer); | |
675 | skip_to_char ('%'); | |
676 | } | |
677 | break; | |
678 | ||
679 | case tok_semicolon: | |
680 | return; | |
681 | ||
682 | default: | |
683 | complain (_("unexpected item: %s"), token_buffer); | |
684 | skip_to_char ('%'); | |
685 | } | |
686 | ||
687 | prev = t; | |
688 | } | |
689 | } | |
690 | ||
691 | ||
692 | ||
693 | /*--------------------------------------------------------------. | |
694 | | Copy the union declaration into the stype muscle | | |
695 | | (and fdefines), where it is made into the definition of | | |
696 | | YYSTYPE, the type of elements of the parser value stack. | | |
697 | `--------------------------------------------------------------*/ | |
698 | ||
699 | static void | |
700 | parse_union_decl (void) | |
701 | { | |
702 | int c; | |
703 | int count = 0; | |
704 | bool done = FALSE; | |
705 | struct obstack union_obstack; | |
706 | if (typed) | |
707 | complain (_("multiple %s declarations"), "%union"); | |
708 | ||
709 | typed = 1; | |
710 | ||
711 | MUSCLE_INSERT_INT ("stype_line", lineno); | |
712 | obstack_init (&union_obstack); | |
713 | obstack_sgrow (&union_obstack, "union"); | |
714 | ||
715 | while (!done) | |
716 | { | |
717 | c = xgetc (finput); | |
718 | ||
719 | /* If C contains '/', it is output by copy_comment (). */ | |
720 | if (c != '/') | |
721 | obstack_1grow (&union_obstack, c); | |
722 | ||
723 | switch (c) | |
724 | { | |
725 | case '\n': | |
726 | ++lineno; | |
727 | break; | |
728 | ||
729 | case '/': | |
730 | copy_comment (finput, &union_obstack); | |
731 | break; | |
732 | ||
733 | case '{': | |
734 | ++count; | |
735 | break; | |
736 | ||
737 | case '}': | |
738 | /* FIXME: Errr. How could this happen???. --akim */ | |
739 | if (count == 0) | |
740 | complain (_("unmatched %s"), "`}'"); | |
741 | count--; | |
742 | if (!count) | |
743 | done = TRUE; | |
744 | break; | |
745 | } | |
746 | } | |
747 | ||
748 | /* JF don't choke on trailing semi */ | |
749 | c = skip_white_space (); | |
750 | if (c != ';') | |
751 | ungetc (c, finput); | |
752 | obstack_1grow (&union_obstack, 0); | |
753 | muscle_insert ("stype", obstack_finish (&union_obstack)); | |
754 | } | |
755 | ||
756 | ||
757 | /*-------------------------------------------------------. | |
758 | | Parse the declaration %expect N which says to expect N | | |
759 | | shift-reduce conflicts. | | |
760 | `-------------------------------------------------------*/ | |
761 | ||
762 | static void | |
763 | parse_expect_decl (void) | |
764 | { | |
765 | int c = skip_white_space (); | |
766 | ungetc (c, finput); | |
767 | ||
768 | if (!isdigit (c)) | |
769 | complain (_("argument of %%expect is not an integer")); | |
770 | else | |
771 | expected_conflicts = read_signed_integer (finput); | |
772 | } | |
773 | ||
774 | ||
775 | static void | |
776 | parse_muscle_decl (void) | |
777 | { | |
778 | int ch = ungetc (skip_white_space (), finput); | |
779 | char *muscle_key; | |
780 | char *muscle_value; | |
781 | ||
782 | /* Read key. */ | |
783 | if (!isalpha (ch) && ch != '_') | |
784 | { | |
785 | complain (_("invalid %s declaration"), "%define"); | |
786 | skip_to_char ('%'); | |
787 | return; | |
788 | } | |
789 | copy_identifier (finput, &muscle_obstack); | |
790 | obstack_1grow (&muscle_obstack, 0); | |
791 | muscle_key = obstack_finish (&muscle_obstack); | |
792 | ||
793 | /* Read value. */ | |
794 | ch = skip_white_space (); | |
795 | if (ch != '"') | |
796 | { | |
797 | ungetc (ch, finput); | |
798 | if (ch != EOF) | |
799 | { | |
800 | complain (_("invalid %s declaration"), "%define"); | |
801 | skip_to_char ('%'); | |
802 | return; | |
803 | } | |
804 | else | |
805 | fatal (_("Premature EOF after %s"), "\""); | |
806 | } | |
807 | copy_string2 (finput, &muscle_obstack, '"', 0); | |
808 | obstack_1grow (&muscle_obstack, 0); | |
809 | muscle_value = obstack_finish (&muscle_obstack); | |
810 | ||
811 | /* Store the (key, value) pair in the environment. */ | |
812 | muscle_insert (muscle_key, muscle_value); | |
813 | } | |
814 | ||
815 | ||
816 | ||
817 | /*---------------------------------. | |
818 | | Parse a double quoted parameter. | | |
819 | `---------------------------------*/ | |
820 | ||
821 | static const char * | |
822 | parse_dquoted_param (const char *from) | |
823 | { | |
824 | struct obstack param_obstack; | |
825 | const char *param = NULL; | |
826 | int c; | |
827 | ||
828 | obstack_init (¶m_obstack); | |
829 | c = skip_white_space (); | |
830 | ||
831 | if (c != '"') | |
832 | { | |
833 | complain (_("invalid %s declaration"), from); | |
834 | ungetc (c, finput); | |
835 | skip_to_char ('%'); | |
836 | return NULL; | |
837 | } | |
838 | ||
839 | while ((c = literalchar ()) != '"') | |
840 | obstack_1grow (¶m_obstack, c); | |
841 | ||
842 | obstack_1grow (¶m_obstack, '\0'); | |
843 | param = obstack_finish (¶m_obstack); | |
844 | ||
845 | if (c != '"' || strlen (param) == 0) | |
846 | { | |
847 | complain (_("invalid %s declaration"), from); | |
848 | if (c != '"') | |
849 | ungetc (c, finput); | |
850 | skip_to_char ('%'); | |
851 | return NULL; | |
852 | } | |
853 | ||
854 | return param; | |
855 | } | |
856 | ||
857 | /*----------------------------------. | |
858 | | Parse what comes after %skeleton. | | |
859 | `----------------------------------*/ | |
860 | ||
861 | static void | |
862 | parse_skel_decl (void) | |
863 | { | |
864 | skeleton = parse_dquoted_param ("%skeleton"); | |
865 | } | |
866 | ||
867 | /*----------------------------------------------------------------. | |
868 | | Read from finput until `%%' is seen. Discard the `%%'. Handle | | |
869 | | any `%' declarations, and copy the contents of any `%{ ... %}' | | |
870 | | groups to PRE_PROLOGUE_OBSTACK or POST_PROLOGUE_OBSTACK. | | |
871 | `----------------------------------------------------------------*/ | |
872 | ||
873 | static void | |
874 | read_declarations (void) | |
875 | { | |
876 | for (;;) | |
877 | { | |
878 | int c = skip_white_space (); | |
879 | ||
880 | if (c == '%') | |
881 | { | |
882 | token_t tok = parse_percent_token (); | |
883 | ||
884 | switch (tok) | |
885 | { | |
886 | case tok_two_percents: | |
887 | return; | |
888 | ||
889 | case tok_percent_left_curly: | |
890 | if (!typed) | |
891 | copy_definition (&pre_prologue_obstack); | |
892 | else | |
893 | copy_definition (&post_prologue_obstack); | |
894 | break; | |
895 | ||
896 | case tok_token: | |
897 | parse_token_decl (token_sym); | |
898 | break; | |
899 | ||
900 | case tok_nterm: | |
901 | parse_token_decl (nterm_sym); | |
902 | break; | |
903 | ||
904 | case tok_type: | |
905 | parse_type_decl (); | |
906 | break; | |
907 | ||
908 | case tok_start: | |
909 | parse_start_decl (); | |
910 | break; | |
911 | ||
912 | case tok_union: | |
913 | parse_union_decl (); | |
914 | break; | |
915 | ||
916 | case tok_expect: | |
917 | parse_expect_decl (); | |
918 | break; | |
919 | ||
920 | case tok_left: | |
921 | parse_assoc_decl (left_assoc); | |
922 | break; | |
923 | ||
924 | case tok_right: | |
925 | parse_assoc_decl (right_assoc); | |
926 | break; | |
927 | ||
928 | case tok_nonassoc: | |
929 | parse_assoc_decl (non_assoc); | |
930 | break; | |
931 | ||
932 | case tok_define: | |
933 | parse_muscle_decl (); | |
934 | break; | |
935 | ||
936 | case tok_skel: | |
937 | parse_skel_decl (); | |
938 | break; | |
939 | ||
940 | case tok_noop: | |
941 | break; | |
942 | ||
943 | case tok_stropt: | |
944 | case tok_intopt: | |
945 | case tok_obsolete: | |
946 | assert (0); | |
947 | break; | |
948 | ||
949 | case tok_illegal: | |
950 | default: | |
951 | complain (_("unrecognized: %s"), token_buffer); | |
952 | skip_to_char ('%'); | |
953 | } | |
954 | } | |
955 | else if (c == EOF) | |
956 | fatal (_("no input grammar")); | |
957 | else | |
958 | { | |
959 | char buf[] = "c"; | |
960 | buf[0] = c; | |
961 | complain (_("unknown character: %s"), quote (buf)); | |
962 | skip_to_char ('%'); | |
963 | } | |
964 | } | |
965 | } | |
966 | \f | |
967 | /*------------------------------------------------------------------. | |
968 | | Assuming that a `{' has just been seen, copy everything up to the | | |
969 | | matching `}' into ACTION_OBSTACK, and return a pointer to this | | |
970 | | action. | | |
971 | `------------------------------------------------------------------*/ | |
972 | ||
973 | static char * | |
974 | parse_action (symbol_list *rule) | |
975 | { | |
976 | int count = 1; | |
977 | ||
978 | /* RULE_LENGTH is the number of values in the current rule so far, | |
979 | which says where to find `$0' with respect to the top of the | |
980 | stack. It is not the same as the rule->length in the case of mid | |
981 | rule actions. */ | |
982 | int rule_length = 0; | |
983 | symbol_list *rhs; | |
984 | for (rhs = rule->next; rhs; rhs = rhs->next) | |
985 | ++rule_length; | |
986 | ||
987 | while (count > 0) | |
988 | { | |
989 | int c; | |
990 | while ((c = getc (finput)) != '}') | |
991 | switch (c) | |
992 | { | |
993 | case '\n': | |
994 | copy_character (&action_obstack, c); | |
995 | ++lineno; | |
996 | break; | |
997 | ||
998 | case '{': | |
999 | copy_character (&action_obstack, c); | |
1000 | ++count; | |
1001 | break; | |
1002 | ||
1003 | case '\'': | |
1004 | case '"': | |
1005 | copy_string (finput, &action_obstack, c); | |
1006 | break; | |
1007 | ||
1008 | case '/': | |
1009 | copy_comment (finput, &action_obstack); | |
1010 | break; | |
1011 | ||
1012 | case '$': | |
1013 | copy_dollar (finput, &action_obstack, rule, rule_length); | |
1014 | break; | |
1015 | ||
1016 | case '@': | |
1017 | copy_at (finput, &action_obstack, rule_length); | |
1018 | break; | |
1019 | ||
1020 | case EOF: | |
1021 | fatal (_("unmatched %s"), "`{'"); | |
1022 | ||
1023 | default: | |
1024 | copy_character (&action_obstack, c); | |
1025 | } | |
1026 | ||
1027 | /* Above loop exits when C is '}'. */ | |
1028 | if (--count) | |
1029 | copy_character (&action_obstack, c); | |
1030 | } | |
1031 | ||
1032 | obstack_1grow (&action_obstack, '\0'); | |
1033 | return obstack_finish (&action_obstack); | |
1034 | } | |
1035 | ||
1036 | \f | |
1037 | ||
1038 | /*-------------------------------------------------------------------. | |
1039 | | Generate a dummy symbol, a nonterminal, whose name cannot conflict | | |
1040 | | with the user's names. | | |
1041 | `-------------------------------------------------------------------*/ | |
1042 | ||
1043 | static symbol_t * | |
1044 | gensym (void) | |
1045 | { | |
1046 | /* Incremented for each generated symbol */ | |
1047 | static int gensym_count = 0; | |
1048 | static char buf[256]; | |
1049 | ||
1050 | symbol_t *sym; | |
1051 | ||
1052 | sprintf (buf, "@%d", ++gensym_count); | |
1053 | token_buffer = buf; | |
1054 | sym = getsym (token_buffer); | |
1055 | sym->class = nterm_sym; | |
1056 | sym->number = nvars++; | |
1057 | return sym; | |
1058 | } | |
1059 | \f | |
1060 | /*-------------------------------------------------------------------. | |
1061 | | Parse the input grammar into a one symbol_list structure. Each | | |
1062 | | rule is represented by a sequence of symbols: the left hand side | | |
1063 | | followed by the contents of the right hand side, followed by a | | |
1064 | | null pointer instead of a symbol to terminate the rule. The next | | |
1065 | | symbol is the lhs of the following rule. | | |
1066 | | | | |
1067 | | All actions are copied out, labelled by the rule number they apply | | |
1068 | | to. | | |
1069 | | | | |
1070 | | Bison used to allow some %directives in the rules sections, but | | |
1071 | | this is no longer consider appropriate: (i) the documented grammar | | |
1072 | | doesn't claim it, (ii), it would promote bad style, (iii), error | | |
1073 | | recovery for %directives consists in skipping the junk until a `%' | | |
1074 | | is seen and helrp synchronizing. This scheme is definitely wrong | | |
1075 | | in the rules section. | | |
1076 | `-------------------------------------------------------------------*/ | |
1077 | ||
1078 | /* The (currently) last symbol of GRAMMAR. */ | |
1079 | symbol_list *grammar_end = NULL; | |
1080 | ||
1081 | /* Append S to the GRAMMAR. */ | |
1082 | static void | |
1083 | grammar_symbol_append (symbol_t *s) | |
1084 | { | |
1085 | symbol_list *p = symbol_list_new (s); | |
1086 | ||
1087 | if (grammar_end) | |
1088 | grammar_end->next = p; | |
1089 | else | |
1090 | grammar = p; | |
1091 | ||
1092 | grammar_end = p; | |
1093 | } | |
1094 | ||
1095 | /* The rule currently being defined, and the previous rule. Point to | |
1096 | the first symbol of each list: their lhs. */ | |
1097 | symbol_list *current_rule = NULL; | |
1098 | symbol_list *previous_rule = NULL; | |
1099 | ||
1100 | ||
1101 | /* Create a new rule for LHS in to the GRAMMAR. */ | |
1102 | ||
1103 | static void | |
1104 | grammar_rule_begin (symbol_t *lhs) | |
1105 | { | |
1106 | if (!start_flag) | |
1107 | { | |
1108 | startsymbol = lhs; | |
1109 | start_flag = 1; | |
1110 | } | |
1111 | ||
1112 | /* Start a new rule and record its lhs. */ | |
1113 | ++nrules; | |
1114 | ++nritems; | |
1115 | ||
1116 | previous_rule = grammar_end; | |
1117 | grammar_symbol_append (lhs); | |
1118 | current_rule = grammar_end; | |
1119 | ||
1120 | /* Mark the rule's lhs as a nonterminal if not already so. */ | |
1121 | ||
1122 | if (lhs->class == unknown_sym) | |
1123 | { | |
1124 | lhs->class = nterm_sym; | |
1125 | lhs->number = nvars; | |
1126 | ++nvars; | |
1127 | } | |
1128 | else if (lhs->class == token_sym) | |
1129 | complain (_("rule given for %s, which is a token"), lhs->tag); | |
1130 | } | |
1131 | ||
1132 | /* The previous action turns out the be a mid-rule action. Attach it | |
1133 | to the current rule, i.e., create a dummy symbol, attach it this | |
1134 | mid-rule action, and append this dummy nonterminal to the current | |
1135 | rule. */ | |
1136 | ||
1137 | static void | |
1138 | grammar_midrule_action (void) | |
1139 | { | |
1140 | /* Since the action was written out with this rule's number, we must | |
1141 | give the new rule this number by inserting the new rule before | |
1142 | it. */ | |
1143 | ||
1144 | /* Make a dummy nonterminal, a gensym. */ | |
1145 | symbol_t *sdummy = gensym (); | |
1146 | symbol_list *midrule_action = symbol_list_new (sdummy); | |
1147 | ||
1148 | /* Make a new rule, whose body is empty, before the current one, so | |
1149 | that the action just read can belong to it. */ | |
1150 | ++nrules; | |
1151 | ++nritems; | |
1152 | /* Attach its lineno to that of the host rule. */ | |
1153 | midrule_action->line = current_rule->line; | |
1154 | /* Move the action from the host rule to this one. */ | |
1155 | midrule_action->action = current_rule->action; | |
1156 | midrule_action->action_line = current_rule->action_line; | |
1157 | current_rule->action = NULL; | |
1158 | ||
1159 | if (previous_rule) | |
1160 | previous_rule->next = midrule_action; | |
1161 | else | |
1162 | grammar = midrule_action; | |
1163 | ||
1164 | /* End of the rule. */ | |
1165 | previous_rule = symbol_list_new (NULL); | |
1166 | previous_rule->next = current_rule; | |
1167 | ||
1168 | midrule_action->next = previous_rule; | |
1169 | ||
1170 | /* Insert the dummy generated by that rule into this rule. */ | |
1171 | ++nritems; | |
1172 | grammar_symbol_append (sdummy); | |
1173 | } | |
1174 | ||
1175 | /* Set the precedence symbol of the current rule to PRECSYM. */ | |
1176 | ||
1177 | static void | |
1178 | grammar_current_rule_prec_set (symbol_t *precsym) | |
1179 | { | |
1180 | if (current_rule->ruleprec) | |
1181 | complain (_("two @prec's in a row")); | |
1182 | current_rule->ruleprec = precsym; | |
1183 | } | |
1184 | ||
1185 | /* Check that the last rule (CURRENT_RULE) is properly defined. For | |
1186 | instance, there should be no type clash on the default action. */ | |
1187 | ||
1188 | static void | |
1189 | grammar_current_rule_check (void) | |
1190 | { | |
1191 | symbol_t *lhs = current_rule->sym; | |
1192 | symbol_t *first_rhs = current_rule->next->sym; | |
1193 | ||
1194 | /* If there is an action, then there is nothing we can do: the user | |
1195 | is allowed to shoot in her foot. */ | |
1196 | if (current_rule->action) | |
1197 | return; | |
1198 | ||
1199 | /* If $$ is being set in default way, report if any type mismatch. | |
1200 | */ | |
1201 | if (first_rhs) | |
1202 | { | |
1203 | const char *lhs_type = lhs->type_name ? lhs->type_name : ""; | |
1204 | const char *rhs_type = first_rhs->type_name ? first_rhs->type_name : ""; | |
1205 | if (strcmp (lhs_type, rhs_type)) | |
1206 | complain (_("type clash (`%s' `%s') on default action"), | |
1207 | lhs_type, rhs_type); | |
1208 | } | |
1209 | /* Warn if there is no default for $$ but we need one. */ | |
1210 | else | |
1211 | { | |
1212 | if (lhs->type_name) | |
1213 | complain (_("empty rule for typed nonterminal, and no action")); | |
1214 | } | |
1215 | } | |
1216 | ||
1217 | ||
1218 | /* Attach a SYMBOL to the current rule. If needed, move the previous | |
1219 | action as a mid-rule action. */ | |
1220 | ||
1221 | static void | |
1222 | grammar_current_rule_symbol_append (symbol_t *symbol) | |
1223 | { | |
1224 | if (current_rule->action) | |
1225 | grammar_midrule_action (); | |
1226 | ++nritems; | |
1227 | grammar_symbol_append (symbol); | |
1228 | } | |
1229 | ||
1230 | ||
1231 | /* Attach an ACTION to the current rule. If needed, move the previous | |
1232 | action as a mid-rule action. */ | |
1233 | ||
1234 | static void | |
1235 | grammar_current_rule_action_append (char *action, int action_line) | |
1236 | { | |
1237 | if (current_rule->action) | |
1238 | grammar_midrule_action (); | |
1239 | current_rule->action = action; | |
1240 | current_rule->action_line = action_line; | |
1241 | } | |
1242 | ||
1243 | ||
1244 | ||
1245 | static void | |
1246 | readgram (void) | |
1247 | { | |
1248 | token_t t; | |
1249 | symbol_t *lhs = NULL; | |
1250 | ||
1251 | t = lex (); | |
1252 | ||
1253 | while (t != tok_two_percents && t != tok_eof) | |
1254 | if (t == tok_identifier || t == tok_bar) | |
1255 | { | |
1256 | if (t == tok_identifier) | |
1257 | { | |
1258 | lhs = symval; | |
1259 | ||
1260 | t = lex (); | |
1261 | if (t != tok_colon) | |
1262 | { | |
1263 | complain (_("ill-formed rule: initial symbol not followed by colon")); | |
1264 | unlex (t); | |
1265 | } | |
1266 | } | |
1267 | if (nrules == 0 && t == tok_bar) | |
1268 | { | |
1269 | complain (_("grammar starts with vertical bar")); | |
1270 | lhs = symval; /* BOGUS: use a random symval */ | |
1271 | } | |
1272 | ||
1273 | grammar_rule_begin (lhs); | |
1274 | /* read the rhs of the rule. */ | |
1275 | ||
1276 | for (;;) | |
1277 | { | |
1278 | t = lex (); | |
1279 | if (t == tok_prec) | |
1280 | { | |
1281 | t = lex (); | |
1282 | grammar_current_rule_prec_set (symval); | |
1283 | t = lex (); | |
1284 | } | |
1285 | ||
1286 | if (!(t == tok_identifier || t == tok_left_curly)) | |
1287 | break; | |
1288 | ||
1289 | /* If next token is an identifier, see if a colon follows it. | |
1290 | If one does, exit this rule now. */ | |
1291 | if (t == tok_identifier) | |
1292 | { | |
1293 | symbol_t *ssave; | |
1294 | token_t t1; | |
1295 | ||
1296 | ssave = symval; | |
1297 | t1 = lex (); | |
1298 | unlex (t1); | |
1299 | symval = ssave; | |
1300 | if (t1 == tok_colon) | |
1301 | { | |
1302 | warn (_("previous rule lacks an ending `;'")); | |
1303 | break; | |
1304 | } | |
1305 | /* Not followed by colon => process as part of this | |
1306 | rule's rhs. */ | |
1307 | } | |
1308 | ||
1309 | if (t == tok_identifier) | |
1310 | { | |
1311 | grammar_current_rule_symbol_append (symval); | |
1312 | } | |
1313 | else /* handle an action. */ | |
1314 | { | |
1315 | int action_line = lineno; | |
1316 | char *action = parse_action (current_rule); | |
1317 | grammar_current_rule_action_append (action, action_line); | |
1318 | } | |
1319 | } /* end of read rhs of rule */ | |
1320 | ||
1321 | /* Put an empty link in the list to mark the end of this rule */ | |
1322 | grammar_symbol_append (NULL); | |
1323 | ||
1324 | if (t == tok_prec) | |
1325 | { | |
1326 | t = lex (); | |
1327 | grammar_current_rule_prec_set (symval); | |
1328 | t = lex (); | |
1329 | } | |
1330 | ||
1331 | if (t == tok_left_curly) | |
1332 | { | |
1333 | int action_line = lineno; | |
1334 | char *action = parse_action (current_rule); | |
1335 | grammar_current_rule_action_append (action, action_line); | |
1336 | t = lex (); | |
1337 | } | |
1338 | ||
1339 | grammar_current_rule_check (); | |
1340 | ||
1341 | if (t == tok_two_percents || t == tok_eof) | |
1342 | warn (_("previous rule lacks an ending `;'")); | |
1343 | if (t == tok_semicolon) | |
1344 | t = lex (); | |
1345 | } | |
1346 | else | |
1347 | { | |
1348 | complain (_("invalid input: %s"), quote (token_buffer)); | |
1349 | t = lex (); | |
1350 | } | |
1351 | ||
1352 | /* grammar has been read. Do some checking */ | |
1353 | ||
1354 | if (nrules == 0) | |
1355 | fatal (_("no rules in the input grammar")); | |
1356 | ||
1357 | /* Report any undefined symbols and consider them nonterminals. */ | |
1358 | symbols_check_defined (); | |
1359 | ||
1360 | /* Insert the initial rule, which line is that of the first rule | |
1361 | (not that of the start symbol): | |
1362 | ||
1363 | axiom: %start EOF. */ | |
1364 | { | |
1365 | symbol_list *p = symbol_list_new (axiom); | |
1366 | p->line = grammar->line; | |
1367 | p->next = symbol_list_new (startsymbol); | |
1368 | p->next->next = symbol_list_new (eoftoken); | |
1369 | p->next->next->next = symbol_list_new (NULL); | |
1370 | p->next->next->next->next = grammar; | |
1371 | nrules += 1; | |
1372 | nritems += 3; | |
1373 | grammar = p; | |
1374 | } | |
1375 | ||
1376 | if (nsyms > SHRT_MAX) | |
1377 | fatal (_("too many symbols (tokens plus nonterminals); maximum %d"), | |
1378 | SHRT_MAX); | |
1379 | ||
1380 | assert (nsyms == ntokens + nvars); | |
1381 | } | |
1382 | ||
1383 | /* At the end of the grammar file, some C source code must | |
1384 | be stored. It is going to be associated to the epilogue | |
1385 | directive. */ | |
1386 | static void | |
1387 | read_additionnal_code (void) | |
1388 | { | |
1389 | int c; | |
1390 | struct obstack el_obstack; | |
1391 | ||
1392 | obstack_init (&el_obstack); | |
1393 | ||
1394 | if (!no_lines_flag) | |
1395 | { | |
1396 | obstack_fgrow2 (&el_obstack, muscle_find ("linef"), | |
1397 | lineno, quotearg_style (c_quoting_style, | |
1398 | muscle_find ("filename"))); | |
1399 | } | |
1400 | ||
1401 | while ((c = getc (finput)) != EOF) | |
1402 | copy_character (&el_obstack, c); | |
1403 | ||
1404 | obstack_1grow (&el_obstack, 0); | |
1405 | muscle_insert ("epilogue", obstack_finish (&el_obstack)); | |
1406 | } | |
1407 | ||
1408 | \f | |
1409 | /*---------------------------------------------------------------. | |
1410 | | Convert the rules into the representation using RRHS, RLHS and | | |
1411 | | RITEM. | | |
1412 | `---------------------------------------------------------------*/ | |
1413 | ||
1414 | static void | |
1415 | packgram (void) | |
1416 | { | |
1417 | unsigned int itemno; | |
1418 | int ruleno; | |
1419 | symbol_list *p; | |
1420 | ||
1421 | ritem = XCALLOC (item_number_t, nritems); | |
1422 | rules = XCALLOC (rule_t, nrules) - 1; | |
1423 | ||
1424 | itemno = 0; | |
1425 | ruleno = 1; | |
1426 | ||
1427 | p = grammar; | |
1428 | while (p) | |
1429 | { | |
1430 | symbol_t *ruleprec = p->ruleprec; | |
1431 | rules[ruleno].user_number = ruleno; | |
1432 | rules[ruleno].number = ruleno; | |
1433 | rules[ruleno].lhs = p->sym; | |
1434 | rules[ruleno].rhs = ritem + itemno; | |
1435 | rules[ruleno].line = p->line; | |
1436 | rules[ruleno].useful = TRUE; | |
1437 | rules[ruleno].action = p->action; | |
1438 | rules[ruleno].action_line = p->action_line; | |
1439 | ||
1440 | p = p->next; | |
1441 | while (p && p->sym) | |
1442 | { | |
1443 | /* item_number_t = symbol_number_t. | |
1444 | But the former needs to contain more: negative rule numbers. */ | |
1445 | ritem[itemno++] = symbol_number_as_item_number (p->sym->number); | |
1446 | /* A rule gets by default the precedence and associativity | |
1447 | of the last token in it. */ | |
1448 | if (p->sym->class == token_sym) | |
1449 | rules[ruleno].prec = p->sym; | |
1450 | if (p) | |
1451 | p = p->next; | |
1452 | } | |
1453 | ||
1454 | /* If this rule has a %prec, | |
1455 | the specified symbol's precedence replaces the default. */ | |
1456 | if (ruleprec) | |
1457 | { | |
1458 | rules[ruleno].precsym = ruleprec; | |
1459 | rules[ruleno].prec = ruleprec; | |
1460 | } | |
1461 | ritem[itemno++] = -ruleno; | |
1462 | ++ruleno; | |
1463 | ||
1464 | if (p) | |
1465 | p = p->next; | |
1466 | } | |
1467 | ||
1468 | assert (itemno == nritems); | |
1469 | ||
1470 | if (trace_flag) | |
1471 | ritem_print (stderr); | |
1472 | } | |
1473 | \f | |
1474 | /*------------------------------------------------------------------. | |
1475 | | Read in the grammar specification and record it in the format | | |
1476 | | described in gram.h. All actions are copied into ACTION_OBSTACK, | | |
1477 | | in each case forming the body of a C function (YYACTION) which | | |
1478 | | contains a switch statement to decide which action to execute. | | |
1479 | `------------------------------------------------------------------*/ | |
1480 | ||
1481 | void | |
1482 | reader (void) | |
1483 | { | |
1484 | lex_init (); | |
1485 | lineno = 1; | |
1486 | ||
1487 | /* Initialize the muscle obstack. */ | |
1488 | obstack_init (&muscle_obstack); | |
1489 | ||
1490 | /* Initialize the symbol table. */ | |
1491 | symbols_new (); | |
1492 | ||
1493 | /* Construct the axiom symbol. */ | |
1494 | axiom = getsym ("$axiom"); | |
1495 | axiom->class = nterm_sym; | |
1496 | axiom->number = nvars++; | |
1497 | ||
1498 | /* Construct the error token */ | |
1499 | errtoken = getsym ("error"); | |
1500 | errtoken->class = token_sym; | |
1501 | errtoken->number = ntokens++; | |
1502 | ||
1503 | /* Construct a token that represents all undefined literal tokens. | |
1504 | It is always token number 2. */ | |
1505 | undeftoken = getsym ("$undefined."); | |
1506 | undeftoken->class = token_sym; | |
1507 | undeftoken->number = ntokens++; | |
1508 | ||
1509 | /* Initialize the obstacks. */ | |
1510 | obstack_init (&action_obstack); | |
1511 | obstack_init (&output_obstack); | |
1512 | obstack_init (&pre_prologue_obstack); | |
1513 | obstack_init (&post_prologue_obstack); | |
1514 | ||
1515 | finput = xfopen (infile, "r"); | |
1516 | ||
1517 | /* Read the declaration section. Copy %{ ... %} groups to | |
1518 | TABLE_OBSTACK and FDEFINES file. Also notice any %token, %left, | |
1519 | etc. found there. */ | |
1520 | read_declarations (); | |
1521 | ||
1522 | /* If the user did not define her EOFTOKEN, do it now. */ | |
1523 | if (!eoftoken) | |
1524 | { | |
1525 | eoftoken = getsym ("$"); | |
1526 | eoftoken->class = token_sym; | |
1527 | eoftoken->number = 0; | |
1528 | /* Value specified by POSIX. */ | |
1529 | eoftoken->user_token_number = 0; | |
1530 | } | |
1531 | ||
1532 | /* Read in the grammar, build grammar in list form. Write out | |
1533 | actions. */ | |
1534 | readgram (); | |
1535 | /* Some C code is given at the end of the grammar file. */ | |
1536 | read_additionnal_code (); | |
1537 | ||
1538 | lex_free (); | |
1539 | xfclose (finput); | |
1540 | ||
1541 | /* Assign the symbols their symbol numbers. Write #defines for the | |
1542 | token symbols into FDEFINES if requested. */ | |
1543 | symbols_pack (); | |
1544 | ||
1545 | /* Convert the grammar into the format described in gram.h. */ | |
1546 | packgram (); | |
1547 | ||
1548 | /* The grammar as a symbol_list is no longer needed. */ | |
1549 | LIST_FREE (symbol_list, grammar); | |
1550 | } |