1 %{/* Bison Grammar Parser -*- C -*-
3 Copyright (C) 2002-2012 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
24 #include "conflicts.h"
28 #include "muscle-tab.h"
29 #include "named-ref.h"
33 #include "scan-gram.h"
34 #include "scan-code.h"
37 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
38 static YYLTYPE lloc_default (YYLTYPE const *, int);
40 #define YY_LOCATION_PRINT(File, Loc) \
41 location_print (File, Loc)
43 static void version_check (location const *loc, char const *version);
45 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
46 FIXME: depends on the undocumented availability of YYLLOC. */
48 #define yyerror(Msg) \
49 gram_error (&yylloc, Msg)
50 static void gram_error (location const *, char const *);
52 static char const *char_name (char);
54 /** Add a lex-param or a parse-param.
56 * \param type \a lex_param or \a parse_param
57 * \param decl the formal argument
58 * \param loc the location in the source.
60 static void add_param (char const *type, char *decl, location loc);
63 static symbol_class current_class = unknown_sym;
64 static uniqstr current_type = NULL;
65 static symbol *current_lhs_symbol;
66 static location current_lhs_location;
67 static named_ref *current_lhs_named_ref;
68 static int current_prec = 0;
70 /** Set the new current left-hand side symbol, possibly common
71 * to several right-hand side parts of rule.
75 current_lhs(symbol *sym, location loc, named_ref *ref)
77 current_lhs_symbol = sym;
78 current_lhs_location = loc;
79 /* In order to simplify memory management, named references for lhs
80 are always assigned by deep copy into the current symbol_list
81 node. This is because a single named-ref in the grammar may
82 result in several uses when the user factors lhs between several
83 rules using "|". Therefore free the parser's original copy. */
84 free (current_lhs_named_ref);
85 current_lhs_named_ref = ref;
89 #define YYTYPE_INT16 int_fast16_t
90 #define YYTYPE_INT8 int_fast8_t
91 #define YYTYPE_UINT16 uint_fast16_t
92 #define YYTYPE_UINT8 uint_fast8_t
101 %define parse.lac full
107 /* Bison's grammar can initial empty locations, hence a default
108 location is needed. */
109 boundary_set (&@$.start, current_file, 1, 1);
110 boundary_set (&@$.end, current_file, 1, 1);
122 unsigned char character;
123 named_ref *named_ref;
126 /* Define the tokens together with their human representation. */
127 %token GRAM_EOF 0 "end of file"
128 %token STRING "string"
131 %token PERCENT_TOKEN "%token"
132 %token PERCENT_NTERM "%nterm"
134 %token PERCENT_TYPE "%type"
135 %token PERCENT_DESTRUCTOR "%destructor"
136 %token PERCENT_PRINTER "%printer"
138 %token PERCENT_LEFT "%left"
139 %token PERCENT_RIGHT "%right"
140 %token PERCENT_NONASSOC "%nonassoc"
142 %token PERCENT_PREC "%prec"
143 %token PERCENT_DPREC "%dprec"
144 %token PERCENT_MERGE "%merge"
147 /*----------------------.
148 | Global Declarations. |
149 `----------------------*/
153 PERCENT_DEBUG "%debug"
154 PERCENT_DEFAULT_PREC "%default-prec"
155 PERCENT_DEFINE "%define"
156 PERCENT_DEFINES "%defines"
157 PERCENT_ERROR_VERBOSE "%error-verbose"
158 PERCENT_EXPECT "%expect"
159 PERCENT_EXPECT_RR "%expect-rr"
160 PERCENT_FILE_PREFIX "%file-prefix"
161 PERCENT_GLR_PARSER "%glr-parser"
162 PERCENT_INITIAL_ACTION "%initial-action"
163 PERCENT_LANGUAGE "%language"
164 PERCENT_LEX_PARAM "%lex-param"
165 PERCENT_LOCATIONS "%locations"
166 PERCENT_NAME_PREFIX "%name-prefix"
167 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
168 PERCENT_NO_LINES "%no-lines"
169 PERCENT_NONDETERMINISTIC_PARSER
170 "%nondeterministic-parser"
171 PERCENT_OUTPUT "%output"
172 PERCENT_PARSE_PARAM "%parse-param"
173 PERCENT_PURE_PARSER "%pure-parser"
174 PERCENT_REQUIRE "%require"
175 PERCENT_SKELETON "%skeleton"
176 PERCENT_START "%start"
177 PERCENT_TOKEN_TABLE "%token-table"
178 PERCENT_VERBOSE "%verbose"
182 %token BRACED_CODE "{...}"
183 %token BRACKETED_ID "[identifier]"
185 %token EPILOGUE "epilogue"
187 %token ID "identifier"
188 %token ID_COLON "identifier:"
189 %token PERCENT_PERCENT "%%"
191 %token PROLOGUE "%{...%}"
194 %token TYPE_TAG_ANY "<*>"
195 %token TYPE_TAG_NONE "<>"
197 %type <character> CHAR
198 %printer { fputs (char_name ($$), stderr); } CHAR
200 /* braceless is not to be used for rule or symbol actions, as it
201 calls code_props_plain_init. */
202 %type <chars> STRING "%{...%}" EPILOGUE braceless content.opt
204 %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); }
206 %printer { fprintf (stderr, "{\n%s\n}", $$); }
207 braceless content.opt "{...}" "%{...%}" EPILOGUE
209 %type <uniqstr> BRACKETED_ID ID ID_COLON TYPE variable
210 %printer { fputs ($$, stderr); } <uniqstr>
211 %printer { fprintf (stderr, "[%s]", $$); } BRACKETED_ID
212 %printer { fprintf (stderr, "%s:", $$); } ID_COLON
213 %printer { fprintf (stderr, "<%s>", $$); } TYPE
216 %printer { fprintf (stderr, "%d", $$); } <integer>
218 %type <symbol> id id_colon string_as_id symbol symbol.prec
219 %printer { fprintf (stderr, "%s", $$->tag); } <symbol>
220 %printer { fprintf (stderr, "%s:", $$->tag); } id_colon
222 %type <assoc> precedence_declarator
223 %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item
224 %type <named_ref> named_ref.opt
229 prologue_declarations "%%" grammar epilogue.opt
233 /*------------------------------------.
234 | Declarations: before the first %%. |
235 `------------------------------------*/
237 prologue_declarations:
239 | prologue_declarations prologue_declaration
242 prologue_declaration:
246 code_props plain_code;
247 code_props_plain_init (&plain_code, $1, @1);
248 code_props_translate_code (&plain_code);
249 gram_scanner_last_string_free ();
250 muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
251 plain_code.code, @1);
252 code_scanner_last_string_free ();
254 | "%debug" { debug_flag = true; }
255 | "%define" variable content.opt
257 muscle_percent_define_insert ($2, @2, $3,
258 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
260 | "%defines" { defines_flag = true; }
264 spec_defines_file = xstrdup ($2);
266 | "%error-verbose" { error_verbose = true; }
267 | "%expect" INT { expected_sr_conflicts = $2; }
268 | "%expect-rr" INT { expected_rr_conflicts = $2; }
269 | "%file-prefix" STRING { spec_file_prefix = $2; }
270 | "%file-prefix" "=" STRING { spec_file_prefix = $3; } /* deprecated */
273 nondeterministic_parser = true;
276 | "%initial-action" "{...}"
279 code_props_symbol_action_init (&action, $2, @2);
280 code_props_translate_code (&action);
281 gram_scanner_last_string_free ();
282 muscle_code_grow ("initial_action", action.code, @2);
283 code_scanner_last_string_free ();
285 | "%language" STRING { language_argmatch ($2, grammar_prio, @1); }
286 | "%lex-param" "{...}" { add_param ("lex_param", $2, @2); }
287 | "%locations" { locations_flag = true; }
288 | "%name-prefix" STRING { spec_name_prefix = $2; }
289 | "%name-prefix" "=" STRING { spec_name_prefix = $3; } /* deprecated */
290 | "%no-lines" { no_lines_flag = true; }
291 | "%nondeterministic-parser" { nondeterministic_parser = true; }
292 | "%output" STRING { spec_outfile = $2; }
293 | "%output" "=" STRING { spec_outfile = $3; } /* deprecated */
294 | "%parse-param" "{...}" { add_param ("parse_param", $2, @2); }
297 /* %pure-parser is deprecated in favor of `%define api.pure', so use
298 `%define api.pure' in a backward-compatible manner here. First, don't
299 complain if %pure-parser is specified multiple times. */
300 if (!muscle_find_const ("percent_define(api.pure)"))
301 muscle_percent_define_insert ("api.pure", @1, "",
302 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
303 /* In all cases, use api.pure now so that the backend doesn't complain if
304 the skeleton ignores api.pure, but do warn now if there's a previous
305 conflicting definition from an actual %define. */
306 if (!muscle_percent_define_flag_if ("api.pure"))
307 muscle_percent_define_insert ("api.pure", @1, "",
308 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
310 | "%require" STRING { version_check (&@2, $2); }
313 char const *skeleton_user = $2;
314 if (mbschr (skeleton_user, '/'))
316 size_t dir_length = strlen (current_file);
317 char *skeleton_build;
318 while (dir_length && current_file[dir_length - 1] != '/')
320 while (dir_length && current_file[dir_length - 1] == '/')
323 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
326 memcpy (skeleton_build, current_file, dir_length);
327 skeleton_build[dir_length++] = '/';
329 strcpy (skeleton_build + dir_length, skeleton_user);
330 skeleton_user = uniqstr_new (skeleton_build);
331 free (skeleton_build);
333 skeleton_arg (skeleton_user, grammar_prio, @1);
335 | "%token-table" { token_table_flag = true; }
336 | "%verbose" { report_flag |= report_states; }
337 | "%yacc" { yacc_flag = true; }
338 | /*FIXME: Err? What is this horror doing here? */ ";"
342 precedence_declaration
346 grammar_start_symbol_set ($2, @2);
348 | "%destructor" "{...}" generic_symlist
351 code_props_symbol_action_init (&code, $2, @2);
352 code_props_translate_code (&code);
355 for (list = $3; list; list = list->next)
356 symbol_list_destructor_set (list, &code);
357 symbol_list_free ($3);
360 | "%printer" "{...}" generic_symlist
363 code_props_symbol_action_init (&code, $2, @2);
364 code_props_translate_code (&code);
367 for (list = $3; list; list = list->next)
368 symbol_list_printer_set (list, &code);
369 symbol_list_free ($3);
378 default_prec = false;
382 /* Do not invoke muscle_percent_code_grow here since it invokes
383 muscle_user_name_list_grow. */
384 muscle_code_grow ("percent_code()", $2, @2);
385 code_scanner_last_string_free ();
387 | "%code" ID braceless
389 muscle_percent_code_grow ($2, @2, $3, @3);
390 code_scanner_last_string_free ();
399 %token PERCENT_UNION "%union";
403 | ID { muscle_code_grow ("union_name", $1, @1); }
407 "%union" union_name braceless
410 muscle_code_grow ("stype", $3, @3);
411 code_scanner_last_string_free ();
419 "%nterm" { current_class = nterm_sym; } symbol_defs.1
421 current_class = unknown_sym;
424 | "%token" { current_class = token_sym; } symbol_defs.1
426 current_class = unknown_sym;
429 | "%type" TYPE symbols.1
433 for (list = $3; list; list = list->next)
434 symbol_type_set (list->content.sym, $2, @2);
435 symbol_list_free ($3);
439 precedence_declaration:
440 precedence_declarator type.opt symbols.prec
444 for (list = $3; list; list = list->next)
446 symbol_type_set (list->content.sym, current_type, @2);
447 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
449 symbol_list_free ($3);
454 precedence_declarator:
455 "%left" { $$ = left_assoc; }
456 | "%right" { $$ = right_assoc; }
457 | "%nonassoc" { $$ = non_assoc; }
461 /* Nothing. */ { current_type = NULL; }
462 | TYPE { current_type = $1; tag_seen = true; }
465 /* Just like symbols.1 but accept INT for the sake of POSIX. */
468 { $$ = symbol_list_sym_new ($1, @1); }
469 | symbols.prec symbol.prec
470 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
475 | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
478 /* One or more symbols to be %typed. */
481 { $$ = symbol_list_sym_new ($1, @1); }
483 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
487 generic_symlist_item { $$ = $1; }
488 | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
491 generic_symlist_item:
492 symbol { $$ = symbol_list_sym_new ($1, @1); }
493 | TYPE { $$ = symbol_list_type_new ($1, @1); }
494 | "<*>" { $$ = symbol_list_default_tagged_new (@1); }
495 | "<>" { $$ = symbol_list_default_tagless_new (@1); }
498 /* One token definition. */
507 symbol_class_set ($1, current_class, @1, true);
508 symbol_type_set ($1, current_type, @1);
512 symbol_class_set ($1, current_class, @1, true);
513 symbol_type_set ($1, current_type, @1);
514 symbol_user_token_number_set ($1, $2, @2);
518 symbol_class_set ($1, current_class, @1, true);
519 symbol_type_set ($1, current_type, @1);
520 symbol_make_alias ($1, $2, @$);
522 | id INT string_as_id
524 symbol_class_set ($1, current_class, @1, true);
525 symbol_type_set ($1, current_type, @1);
526 symbol_user_token_number_set ($1, $2, @2);
527 symbol_make_alias ($1, $3, @$);
531 /* One or more symbol definitions. */
534 | symbol_defs.1 symbol_def
538 /*------------------------------------------.
539 | The grammar section: between the two %%. |
540 `------------------------------------------*/
543 rules_or_grammar_declaration
544 | grammar rules_or_grammar_declaration
547 /* As a Bison extension, one can use the grammar declarations in the
548 body of the grammar. */
549 rules_or_grammar_declaration:
551 | grammar_declaration ";"
559 id_colon named_ref.opt { current_lhs ($1, @1, $2); } rhses.1
561 /* Free the current lhs. */
562 current_lhs (0, @1, 0);
567 rhs { grammar_current_rule_end (@1); }
568 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
574 { grammar_current_rule_begin (current_lhs_symbol, current_lhs_location,
575 current_lhs_named_ref); }
576 | rhs symbol named_ref.opt
577 { grammar_current_rule_symbol_append ($2, @2, $3); }
578 | rhs "{...}" named_ref.opt
579 { grammar_current_rule_action_append ($2, @2, $3); }
581 { grammar_current_rule_prec_set ($3, @3); }
583 { grammar_current_rule_dprec_set ($3, @3); }
585 { grammar_current_rule_merge_set ($3, @3); }
589 /* Nothing. */ { $$ = 0; }
591 BRACKETED_ID { $$ = named_ref_new($1, @1); }
595 /*----------------------------*
596 | variable and content.opt. |
597 *---------------------------*/
599 /* The STRING form of variable is deprecated and is not M4-friendly.
600 For example, M4 fails for `%define "[" "value"'. */
603 | STRING { $$ = uniqstr_new ($1); }
606 /* Some content or empty by default. */
608 /* Nothing. */ { $$ = ""; }
621 code_props plain_code;
622 $1[strlen ($1) - 1] = '\n';
623 code_props_plain_init (&plain_code, $1+1, @1);
624 code_props_translate_code (&plain_code);
625 gram_scanner_last_string_free ();
626 $$ = plain_code.code;
635 /* Identifiers are returned as uniqstr values by the scanner.
636 Depending on their use, we may need to make them genuine symbols. */
640 { $$ = symbol_from_uniqstr ($1, @1); }
643 $$ = symbol_get (char_name ($1), @1);
644 symbol_class_set ($$, token_sym, @1, false);
645 symbol_user_token_number_set ($$, $1, @1);
650 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
659 /* A string used as an ID: quote it. */
663 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
664 symbol_class_set ($$, token_sym, @1, false);
672 code_props plain_code;
673 code_props_plain_init (&plain_code, $2, @2);
674 code_props_translate_code (&plain_code);
675 gram_scanner_last_string_free ();
676 muscle_code_grow ("epilogue", plain_code.code, @2);
677 code_scanner_last_string_free ();
684 /* Return the location of the left-hand side of a rule whose
685 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
686 the right-hand side, and return an empty location equal to the end
687 boundary of RHS[0] if the right-hand side is empty. */
690 lloc_default (YYLTYPE const *rhs, int n)
695 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
696 The bug is fixed in 7.4.2m, but play it safe for now. */
697 loc.start = rhs[n].end;
698 loc.end = rhs[n].end;
700 /* Ignore empty nonterminals the start of the right-hand side.
701 Do not bother to ignore them at the end of the right-hand side,
702 since empty nonterminals have the same end as their predecessors. */
703 for (i = 1; i <= n; i++)
704 if (! equal_boundaries (rhs[i].start, rhs[i].end))
706 loc.start = rhs[i].start;
714 /* Add a lex-param or a parse-param (depending on TYPE) with
715 declaration DECL and location LOC. */
718 add_param (char const *type, char *decl, location loc)
720 static char const alphanum[26 + 26 + 1 + 10] =
721 "abcdefghijklmnopqrstuvwxyz"
722 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
725 char const *name_start = NULL;
728 /* Stop on last actual character. */
729 for (p = decl; p[1]; p++)
731 || ! memchr (alphanum, p[-1], sizeof alphanum))
732 && memchr (alphanum, p[0], sizeof alphanum - 10))
735 /* Strip the surrounding '{' and '}', and any blanks just inside
737 while (*--p == ' ' || *p == '\t')
740 while (*++decl == ' ' || *decl == '\t')
744 complain_at (loc, _("missing identifier in parameter declaration"));
747 char *name = xmemdup0 (name_start, strspn (name_start, alphanum));
748 muscle_pair_list_grow (type, decl, name);
752 gram_scanner_last_string_free ();
757 version_check (location const *loc, char const *version)
759 if (strverscmp (version, PACKAGE_VERSION) > 0)
761 complain_at (*loc, "require bison %s, but have %s",
762 version, PACKAGE_VERSION);
768 gram_error (location const *loc, char const *msg)
770 complain_at (*loc, "%s", msg);
774 token_name (int type)
776 return yytname[YYTRANSLATE (type)];
787 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
788 return quotearg_style (escape_quoting_style, buf);