]> git.saurik.com Git - bison.git/blob - src/parse-gram.y
portability: fix several issues with M4 subprocess.
[bison.git] / src / parse-gram.y
1 %{/* Bison Grammar Parser -*- C -*-
2
3 Copyright (C) 2002-2010 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 This program 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 3 of the License, or
10 (at your option) any later version.
11
12 This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include <config.h>
21 #include "system.h"
22
23 #include "complain.h"
24 #include "conflicts.h"
25 #include "files.h"
26 #include "getargs.h"
27 #include "gram.h"
28 #include "muscle_tab.h"
29 #include "quotearg.h"
30 #include "reader.h"
31 #include "symlist.h"
32 #include "scan-gram.h"
33 #include "scan-code.h"
34
35 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
36 static YYLTYPE lloc_default (YYLTYPE const *, int);
37
38 #define YY_LOCATION_PRINT(File, Loc) \
39 location_print (File, Loc)
40
41 static void version_check (location const *loc, char const *version);
42
43 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
44 FIXME: depends on the undocumented availability of YYLLOC. */
45 #undef yyerror
46 #define yyerror(Msg) \
47 gram_error (&yylloc, Msg)
48 static void gram_error (location const *, char const *);
49
50 static char const *char_name (char);
51
52 /** Add a lex-param or a parse-param.
53 *
54 * \param type \a lex_param or \a parse_param
55 * \param decl the formal argument
56 * \param loc the location in the source.
57 */
58 static void add_param (char const *type, char *decl, location loc);
59
60
61 static symbol_class current_class = unknown_sym;
62 static uniqstr current_type = NULL;
63 static symbol *current_lhs;
64 static location current_lhs_location;
65 static int current_prec = 0;
66
67 #define YYTYPE_INT16 int_fast16_t
68 #define YYTYPE_INT8 int_fast8_t
69 #define YYTYPE_UINT16 uint_fast16_t
70 #define YYTYPE_UINT8 uint_fast8_t
71 %}
72
73 %debug
74 %verbose
75 %defines
76 %locations
77 %pure-parser
78 %error-verbose
79 %name-prefix="gram_"
80 %expect 0
81
82 %initial-action
83 {
84 /* Bison's grammar can initial empty locations, hence a default
85 location is needed. */
86 boundary_set (&@$.start, current_file, 1, 1);
87 boundary_set (&@$.end, current_file, 1, 1);
88 }
89
90 %union
91 {
92 symbol *symbol;
93 symbol_list *list;
94 int integer;
95 char const *chars;
96 char *code;
97 assoc assoc;
98 uniqstr uniqstr;
99 unsigned char character;
100 };
101
102 /* Define the tokens together with their human representation. */
103 %token GRAM_EOF 0 "end of file"
104 %token STRING "string"
105 %token INT "integer"
106
107 %token PERCENT_TOKEN "%token"
108 %token PERCENT_NTERM "%nterm"
109
110 %token PERCENT_TYPE "%type"
111 %token PERCENT_DESTRUCTOR "%destructor"
112 %token PERCENT_PRINTER "%printer"
113
114 %token PERCENT_LEFT "%left"
115 %token PERCENT_RIGHT "%right"
116 %token PERCENT_NONASSOC "%nonassoc"
117
118 %token PERCENT_PREC "%prec"
119 %token PERCENT_DPREC "%dprec"
120 %token PERCENT_MERGE "%merge"
121
122
123 /*----------------------.
124 | Global Declarations. |
125 `----------------------*/
126
127 %token
128 PERCENT_CODE "%code"
129 PERCENT_DEBUG "%debug"
130 PERCENT_DEFAULT_PREC "%default-prec"
131 PERCENT_DEFINE "%define"
132 PERCENT_DEFINES "%defines"
133 PERCENT_ERROR_VERBOSE "%error-verbose"
134 PERCENT_EXPECT "%expect"
135 PERCENT_EXPECT_RR "%expect-rr"
136 PERCENT_FILE_PREFIX "%file-prefix"
137 PERCENT_GLR_PARSER "%glr-parser"
138 PERCENT_INITIAL_ACTION "%initial-action"
139 PERCENT_LANGUAGE "%language"
140 PERCENT_LEX_PARAM "%lex-param"
141 PERCENT_LOCATIONS "%locations"
142 PERCENT_NAME_PREFIX "%name-prefix"
143 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
144 PERCENT_NO_LINES "%no-lines"
145 PERCENT_NONDETERMINISTIC_PARSER
146 "%nondeterministic-parser"
147 PERCENT_OUTPUT "%output"
148 PERCENT_PARSE_PARAM "%parse-param"
149 PERCENT_PURE_PARSER "%pure-parser"
150 PERCENT_REQUIRE "%require"
151 PERCENT_SKELETON "%skeleton"
152 PERCENT_START "%start"
153 PERCENT_TOKEN_TABLE "%token-table"
154 PERCENT_VERBOSE "%verbose"
155 PERCENT_YACC "%yacc"
156 ;
157
158 %token BRACED_CODE "{...}"
159 %token CHAR "char"
160 %token EPILOGUE "epilogue"
161 %token EQUAL "="
162 %token ID "identifier"
163 %token ID_COLON "identifier:"
164 %token PERCENT_PERCENT "%%"
165 %token PIPE "|"
166 %token PROLOGUE "%{...%}"
167 %token SEMICOLON ";"
168 %token TYPE "type"
169 %token TYPE_TAG_ANY "<*>"
170 %token TYPE_TAG_NONE "<>"
171
172 %type <character> CHAR
173 %printer { fputs (char_name ($$), stderr); } CHAR
174
175 /* braceless is not to be used for rule or symbol actions, as it
176 calls code_props_plain_init. */
177 %type <chars> STRING "%{...%}" EPILOGUE braceless content.opt
178 %type <code> "{...}"
179 %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); }
180 STRING
181 %printer { fprintf (stderr, "{\n%s\n}", $$); }
182 braceless content.opt "{...}" "%{...%}" EPILOGUE
183
184 %type <uniqstr> TYPE ID ID_COLON variable
185 %printer { fprintf (stderr, "<%s>", $$); } TYPE
186 %printer { fputs ($$, stderr); } ID variable
187 %printer { fprintf (stderr, "%s:", $$); } ID_COLON
188
189 %type <integer> INT
190 %printer { fprintf (stderr, "%d", $$); } INT
191
192 %type <symbol> id id_colon symbol symbol.prec string_as_id
193 %printer { fprintf (stderr, "%s", $$->tag); } id symbol string_as_id
194 %printer { fprintf (stderr, "%s:", $$->tag); } id_colon
195
196 %type <assoc> precedence_declarator
197 %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item
198 %%
199
200 input:
201 prologue_declarations "%%" grammar epilogue.opt
202 ;
203
204
205 /*------------------------------------.
206 | Declarations: before the first %%. |
207 `------------------------------------*/
208
209 prologue_declarations:
210 /* Nothing */
211 | prologue_declarations prologue_declaration
212 ;
213
214 prologue_declaration:
215 grammar_declaration
216 | "%{...%}"
217 {
218 code_props plain_code;
219 code_props_plain_init (&plain_code, $1, @1);
220 code_props_translate_code (&plain_code);
221 gram_scanner_last_string_free ();
222 muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
223 plain_code.code, @1);
224 code_scanner_last_string_free ();
225 }
226 | "%debug" { debug_flag = true; }
227 | "%define" variable content.opt
228 {
229 muscle_percent_define_insert ($2, @2, $3);
230 }
231 | "%defines" { defines_flag = true; }
232 | "%defines" STRING
233 {
234 defines_flag = true;
235 spec_defines_file = xstrdup ($2);
236 }
237 | "%error-verbose" { error_verbose = true; }
238 | "%expect" INT { expected_sr_conflicts = $2; }
239 | "%expect-rr" INT { expected_rr_conflicts = $2; }
240 | "%file-prefix" STRING { spec_file_prefix = $2; }
241 | "%file-prefix" "=" STRING { spec_file_prefix = $3; } /* deprecated */
242 | "%glr-parser"
243 {
244 nondeterministic_parser = true;
245 glr_parser = true;
246 }
247 | "%initial-action" "{...}"
248 {
249 code_props action;
250 code_props_symbol_action_init (&action, $2, @2);
251 code_props_translate_code (&action);
252 gram_scanner_last_string_free ();
253 muscle_code_grow ("initial_action", action.code, @2);
254 code_scanner_last_string_free ();
255 }
256 | "%language" STRING { language_argmatch ($2, grammar_prio, &@1); }
257 | "%lex-param" "{...}" { add_param ("lex_param", $2, @2); }
258 | "%locations" { locations_flag = true; }
259 | "%name-prefix" STRING { spec_name_prefix = $2; }
260 | "%name-prefix" "=" STRING { spec_name_prefix = $3; } /* deprecated */
261 | "%no-lines" { no_lines_flag = true; }
262 | "%nondeterministic-parser" { nondeterministic_parser = true; }
263 | "%output" STRING { spec_outfile = $2; }
264 | "%output" "=" STRING { spec_outfile = $3; } /* deprecated */
265 | "%parse-param" "{...}" { add_param ("parse_param", $2, @2); }
266 | "%pure-parser"
267 {
268 /* %pure-parser is deprecated in favor of `%define api.pure', so use
269 `%define api.pure' in a backward-compatible manner here. First, don't
270 complain if %pure-parser is specified multiple times. */
271 if (!muscle_find_const ("percent_define(api.pure)"))
272 muscle_percent_define_insert ("api.pure", @1, "");
273 /* In all cases, use api.pure now so that the backend doesn't complain if
274 the skeleton ignores api.pure, but do warn now if there's a previous
275 conflicting definition from an actual %define. */
276 if (!muscle_percent_define_flag_if ("api.pure"))
277 muscle_percent_define_insert ("api.pure", @1, "");
278 }
279 | "%require" STRING { version_check (&@2, $2); }
280 | "%skeleton" STRING
281 {
282 char const *skeleton_user = $2;
283 if (strchr (skeleton_user, '/'))
284 {
285 size_t dir_length = strlen (current_file);
286 char *skeleton_build;
287 while (dir_length && current_file[dir_length - 1] != '/')
288 --dir_length;
289 while (dir_length && current_file[dir_length - 1] == '/')
290 --dir_length;
291 skeleton_build =
292 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
293 if (dir_length > 0)
294 {
295 strncpy (skeleton_build, current_file, dir_length);
296 skeleton_build[dir_length++] = '/';
297 }
298 strcpy (skeleton_build + dir_length, skeleton_user);
299 skeleton_user = uniqstr_new (skeleton_build);
300 free (skeleton_build);
301 }
302 skeleton_arg (skeleton_user, grammar_prio, &@1);
303 }
304 | "%token-table" { token_table_flag = true; }
305 | "%verbose" { report_flag |= report_states; }
306 | "%yacc" { yacc_flag = true; }
307 | /*FIXME: Err? What is this horror doing here? */ ";"
308 ;
309
310 grammar_declaration:
311 precedence_declaration
312 | symbol_declaration
313 | "%start" symbol
314 {
315 grammar_start_symbol_set ($2, @2);
316 }
317 | "%destructor" "{...}" generic_symlist
318 {
319 symbol_list *list;
320 for (list = $3; list; list = list->next)
321 symbol_list_destructor_set (list, $2, @2);
322 symbol_list_free ($3);
323 }
324 | "%printer" "{...}" generic_symlist
325 {
326 symbol_list *list;
327 for (list = $3; list; list = list->next)
328 symbol_list_printer_set (list, $2, @2);
329 symbol_list_free ($3);
330 }
331 | "%default-prec"
332 {
333 default_prec = true;
334 }
335 | "%no-default-prec"
336 {
337 default_prec = false;
338 }
339 | "%code" braceless
340 {
341 /* Do not invoke muscle_percent_code_grow here since it invokes
342 muscle_user_name_list_grow. */
343 muscle_code_grow ("percent_code()", $2, @2);
344 code_scanner_last_string_free ();
345 }
346 | "%code" ID braceless
347 {
348 muscle_percent_code_grow ($2, @2, $3, @3);
349 code_scanner_last_string_free ();
350 }
351 ;
352
353
354 /*----------*
355 | %union. |
356 *----------*/
357
358 %token PERCENT_UNION "%union";
359
360 union_name:
361 /* Nothing. */ {}
362 | ID { muscle_code_grow ("union_name", $1, @1); }
363 ;
364
365 grammar_declaration:
366 "%union" union_name braceless
367 {
368 union_seen = true;
369 muscle_code_grow ("stype", $3, @3);
370 code_scanner_last_string_free ();
371 }
372 ;
373
374
375
376
377 symbol_declaration:
378 "%nterm" { current_class = nterm_sym; } symbol_defs.1
379 {
380 current_class = unknown_sym;
381 current_type = NULL;
382 }
383 | "%token" { current_class = token_sym; } symbol_defs.1
384 {
385 current_class = unknown_sym;
386 current_type = NULL;
387 }
388 | "%type" TYPE symbols.1
389 {
390 symbol_list *list;
391 tag_seen = true;
392 for (list = $3; list; list = list->next)
393 symbol_type_set (list->content.sym, $2, @2);
394 symbol_list_free ($3);
395 }
396 ;
397
398 precedence_declaration:
399 precedence_declarator type.opt symbols.prec
400 {
401 symbol_list *list;
402 ++current_prec;
403 for (list = $3; list; list = list->next)
404 {
405 symbol_type_set (list->content.sym, current_type, @2);
406 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
407 }
408 symbol_list_free ($3);
409 current_type = NULL;
410 }
411 ;
412
413 precedence_declarator:
414 "%left" { $$ = left_assoc; }
415 | "%right" { $$ = right_assoc; }
416 | "%nonassoc" { $$ = non_assoc; }
417 ;
418
419 type.opt:
420 /* Nothing. */ { current_type = NULL; }
421 | TYPE { current_type = $1; tag_seen = true; }
422 ;
423
424 /* Just like symbols.1 but accept INT for the sake of POSIX. */
425 symbols.prec:
426 symbol.prec
427 { $$ = symbol_list_sym_new ($1, @1); }
428 | symbols.prec symbol.prec
429 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
430 ;
431
432 symbol.prec:
433 symbol { $$ = $1; }
434 | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
435 ;
436
437 /* One or more symbols to be %typed. */
438 symbols.1:
439 symbol
440 { $$ = symbol_list_sym_new ($1, @1); }
441 | symbols.1 symbol
442 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
443 ;
444
445 generic_symlist:
446 generic_symlist_item { $$ = $1; }
447 | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
448 ;
449
450 generic_symlist_item:
451 symbol { $$ = symbol_list_sym_new ($1, @1); }
452 | TYPE { $$ = symbol_list_type_new ($1, @1); }
453 | "<*>" { $$ = symbol_list_default_tagged_new (@1); }
454 | "<>" { $$ = symbol_list_default_tagless_new (@1); }
455 ;
456
457 /* One token definition. */
458 symbol_def:
459 TYPE
460 {
461 current_type = $1;
462 tag_seen = true;
463 }
464 | id
465 {
466 symbol_class_set ($1, current_class, @1, true);
467 symbol_type_set ($1, current_type, @1);
468 }
469 | id INT
470 {
471 symbol_class_set ($1, current_class, @1, true);
472 symbol_type_set ($1, current_type, @1);
473 symbol_user_token_number_set ($1, $2, @2);
474 }
475 | id string_as_id
476 {
477 symbol_class_set ($1, current_class, @1, true);
478 symbol_type_set ($1, current_type, @1);
479 symbol_make_alias ($1, $2, @$);
480 }
481 | id INT string_as_id
482 {
483 symbol_class_set ($1, current_class, @1, true);
484 symbol_type_set ($1, current_type, @1);
485 symbol_user_token_number_set ($1, $2, @2);
486 symbol_make_alias ($1, $3, @$);
487 }
488 ;
489
490 /* One or more symbol definitions. */
491 symbol_defs.1:
492 symbol_def
493 | symbol_defs.1 symbol_def
494 ;
495
496
497 /*------------------------------------------.
498 | The grammar section: between the two %%. |
499 `------------------------------------------*/
500
501 grammar:
502 rules_or_grammar_declaration
503 | grammar rules_or_grammar_declaration
504 ;
505
506 /* As a Bison extension, one can use the grammar declarations in the
507 body of the grammar. */
508 rules_or_grammar_declaration:
509 rules
510 | grammar_declaration ";"
511 | error ";"
512 {
513 yyerrok;
514 }
515 ;
516
517 rules:
518 id_colon { current_lhs = $1; current_lhs_location = @1; } rhses.1
519 ;
520
521 rhses.1:
522 rhs { grammar_current_rule_end (@1); }
523 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
524 | rhses.1 ";"
525 ;
526
527 rhs:
528 /* Nothing. */
529 { grammar_current_rule_begin (current_lhs, current_lhs_location); }
530 | rhs symbol
531 { grammar_current_rule_symbol_append ($2, @2); }
532 | rhs "{...}"
533 { grammar_current_rule_action_append ($2, @2); }
534 | rhs "%prec" symbol
535 { grammar_current_rule_prec_set ($3, @3); }
536 | rhs "%dprec" INT
537 { grammar_current_rule_dprec_set ($3, @3); }
538 | rhs "%merge" TYPE
539 { grammar_current_rule_merge_set ($3, @3); }
540 ;
541
542
543 /*----------------------------*
544 | variable and content.opt. |
545 *---------------------------*/
546
547 variable:
548 ID
549 | STRING { $$ = uniqstr_new ($1); } /* deprecated and not M4-friendly */
550 ;
551
552 /* Some content or empty by default. */
553 content.opt:
554 /* Nothing. */
555 {
556 $$ = "";
557 }
558 | STRING
559 ;
560
561
562 /*-------------*
563 | braceless. |
564 *-------------*/
565
566 braceless:
567 "{...}"
568 {
569 code_props plain_code;
570 $1[strlen ($1) - 1] = '\n';
571 code_props_plain_init (&plain_code, $1+1, @1);
572 code_props_translate_code (&plain_code);
573 gram_scanner_last_string_free ();
574 $$ = plain_code.code;
575 }
576 ;
577
578
579 /*---------------*
580 | Identifiers. |
581 *---------------*/
582
583 /* Identifiers are returned as uniqstr values by the scanner.
584 Depending on their use, we may need to make them genuine symbols. */
585
586 id:
587 ID
588 { $$ = symbol_from_uniqstr ($1, @1); }
589 | CHAR
590 {
591 $$ = symbol_get (char_name ($1), @1);
592 symbol_class_set ($$, token_sym, @1, false);
593 symbol_user_token_number_set ($$, $1, @1);
594 }
595 ;
596
597 id_colon:
598 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
599 ;
600
601
602 symbol:
603 id
604 | string_as_id
605 ;
606
607 /* A string used as an ID: quote it. */
608 string_as_id:
609 STRING
610 {
611 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
612 symbol_class_set ($$, token_sym, @1, false);
613 }
614 ;
615
616 epilogue.opt:
617 /* Nothing. */
618 | "%%" EPILOGUE
619 {
620 code_props plain_code;
621 code_props_plain_init (&plain_code, $2, @2);
622 code_props_translate_code (&plain_code);
623 gram_scanner_last_string_free ();
624 muscle_code_grow ("epilogue", plain_code.code, @2);
625 code_scanner_last_string_free ();
626 }
627 ;
628
629 %%
630
631
632 /* Return the location of the left-hand side of a rule whose
633 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
634 the right-hand side, and return an empty location equal to the end
635 boundary of RHS[0] if the right-hand side is empty. */
636
637 static YYLTYPE
638 lloc_default (YYLTYPE const *rhs, int n)
639 {
640 int i;
641 YYLTYPE loc;
642
643 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
644 The bug is fixed in 7.4.2m, but play it safe for now. */
645 loc.start = rhs[n].end;
646 loc.end = rhs[n].end;
647
648 /* Ignore empty nonterminals the start of the the right-hand side.
649 Do not bother to ignore them at the end of the right-hand side,
650 since empty nonterminals have the same end as their predecessors. */
651 for (i = 1; i <= n; i++)
652 if (! equal_boundaries (rhs[i].start, rhs[i].end))
653 {
654 loc.start = rhs[i].start;
655 break;
656 }
657
658 return loc;
659 }
660
661
662 /* Add a lex-param or a parse-param (depending on TYPE) with
663 declaration DECL and location LOC. */
664
665 static void
666 add_param (char const *type, char *decl, location loc)
667 {
668 static char const alphanum[26 + 26 + 1 + 10] =
669 "abcdefghijklmnopqrstuvwxyz"
670 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
671 "_"
672 "0123456789";
673 char const *name_start = NULL;
674 char *p;
675
676 /* Stop on last actual character. */
677 for (p = decl; p[1]; p++)
678 if ((p == decl
679 || ! memchr (alphanum, p[-1], sizeof alphanum))
680 && memchr (alphanum, p[0], sizeof alphanum - 10))
681 name_start = p;
682
683 /* Strip the surrounding '{' and '}', and any blanks just inside
684 the braces. */
685 while (*--p == ' ' || *p == '\t')
686 continue;
687 p[1] = '\0';
688 while (*++decl == ' ' || *decl == '\t')
689 continue;
690
691 if (! name_start)
692 complain_at (loc, _("missing identifier in parameter declaration"));
693 else
694 {
695 char *name;
696 size_t name_len;
697
698 for (name_len = 1;
699 memchr (alphanum, name_start[name_len], sizeof alphanum);
700 name_len++)
701 continue;
702
703 name = xmalloc (name_len + 1);
704 memcpy (name, name_start, name_len);
705 name[name_len] = '\0';
706 muscle_pair_list_grow (type, decl, name);
707 free (name);
708 }
709
710 gram_scanner_last_string_free ();
711 }
712
713
714 static void
715 version_check (location const *loc, char const *version)
716 {
717 if (strverscmp (version, PACKAGE_VERSION) > 0)
718 {
719 complain_at (*loc, "require bison %s, but have %s",
720 version, PACKAGE_VERSION);
721 exit (63);
722 }
723 }
724
725 static void
726 gram_error (location const *loc, char const *msg)
727 {
728 complain_at (*loc, "%s", msg);
729 }
730
731 char const *
732 token_name (int type)
733 {
734 return yytname[YYTRANSLATE (type)];
735 }
736
737 static char const *
738 char_name (char c)
739 {
740 if (c == '\'')
741 return "'\\''";
742 else
743 {
744 char buf[4];
745 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
746 return quotearg_style (escape_quoting_style, buf);
747 }
748 }