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/>. */
25 #include "conflicts.h"
29 #include "muscle-tab.h"
30 #include "named-ref.h"
34 #include "scan-gram.h"
35 #include "scan-code.h"
38 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
39 static YYLTYPE lloc_default (YYLTYPE const *, int);
41 #define YY_LOCATION_PRINT(File, Loc) \
42 location_print (File, Loc)
44 static void version_check (location const *loc, char const *version);
46 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
47 FIXME: depends on the undocumented availability of YYLLOC. */
49 #define yyerror(Msg) \
50 gram_error (&yylloc, Msg)
51 static void gram_error (location const *, char const *);
53 static char const *char_name (char);
55 /** Add a lex-param or a parse-param.
57 * \param type \a lex_param or \a parse_param
58 * \param decl the formal argument
59 * \param loc the location in the source.
61 static void add_param (char const *type, char *decl, location loc);
64 static symbol_class current_class = unknown_sym;
65 static uniqstr current_type = NULL;
66 static symbol *current_lhs_symbol;
67 static location current_lhs_location;
68 static named_ref *current_lhs_named_ref;
69 static int current_prec = 0;
71 /** Set the new current left-hand side symbol, possibly common
72 * to several right-hand side parts of rule.
76 current_lhs(symbol *sym, location loc, named_ref *ref)
78 current_lhs_symbol = sym;
79 current_lhs_location = loc;
80 /* In order to simplify memory management, named references for lhs
81 are always assigned by deep copy into the current symbol_list
82 node. This is because a single named-ref in the grammar may
83 result in several uses when the user factors lhs between several
84 rules using "|". Therefore free the parser's original copy. */
85 free (current_lhs_named_ref);
86 current_lhs_named_ref = ref;
90 #define YYTYPE_INT16 int_fast16_t
91 #define YYTYPE_INT8 int_fast8_t
92 #define YYTYPE_UINT16 uint_fast16_t
93 #define YYTYPE_UINT8 uint_fast8_t
102 %define parse.lac full
108 /* Bison's grammar can initial empty locations, hence a default
109 location is needed. */
110 boundary_set (&@$.start, current_file, 1, 1);
111 boundary_set (&@$.end, current_file, 1, 1);
123 unsigned char character;
124 named_ref *named_ref;
127 /* Define the tokens together with their human representation. */
128 %token GRAM_EOF 0 "end of file"
129 %token STRING "string"
132 %token PERCENT_TOKEN "%token"
133 %token PERCENT_NTERM "%nterm"
135 %token PERCENT_TYPE "%type"
136 %token PERCENT_DESTRUCTOR "%destructor"
137 %token PERCENT_PRINTER "%printer"
139 %token PERCENT_LEFT "%left"
140 %token PERCENT_RIGHT "%right"
141 %token PERCENT_NONASSOC "%nonassoc"
143 %token PERCENT_PREC "%prec"
144 %token PERCENT_DPREC "%dprec"
145 %token PERCENT_MERGE "%merge"
148 /*----------------------.
149 | Global Declarations. |
150 `----------------------*/
154 PERCENT_DEBUG "%debug"
155 PERCENT_DEFAULT_PREC "%default-prec"
156 PERCENT_DEFINE "%define"
157 PERCENT_DEFINES "%defines"
158 PERCENT_ERROR_VERBOSE "%error-verbose"
159 PERCENT_EXPECT "%expect"
160 PERCENT_EXPECT_RR "%expect-rr"
161 PERCENT_FILE_PREFIX "%file-prefix"
162 PERCENT_GLR_PARSER "%glr-parser"
163 PERCENT_INITIAL_ACTION "%initial-action"
164 PERCENT_LANGUAGE "%language"
165 PERCENT_LEX_PARAM "%lex-param"
166 PERCENT_LOCATIONS "%locations"
167 PERCENT_NAME_PREFIX "%name-prefix"
168 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
169 PERCENT_NO_LINES "%no-lines"
170 PERCENT_NONDETERMINISTIC_PARSER
171 "%nondeterministic-parser"
172 PERCENT_OUTPUT "%output"
173 PERCENT_PARSE_PARAM "%parse-param"
174 PERCENT_PURE_PARSER "%pure-parser"
175 PERCENT_REQUIRE "%require"
176 PERCENT_SKELETON "%skeleton"
177 PERCENT_START "%start"
178 PERCENT_TOKEN_TABLE "%token-table"
179 PERCENT_VERBOSE "%verbose"
183 %token BRACED_CODE "{...}"
184 %token BRACKETED_ID "[identifier]"
186 %token EPILOGUE "epilogue"
188 %token ID "identifier"
189 %token ID_COLON "identifier:"
190 %token PERCENT_PERCENT "%%"
192 %token PROLOGUE "%{...%}"
195 %token TYPE_TAG_ANY "<*>"
196 %token TYPE_TAG_NONE "<>"
198 %type <character> CHAR
199 %printer { fputs (char_name ($$), stderr); } CHAR
201 /* braceless is not to be used for rule or symbol actions, as it
202 calls code_props_plain_init. */
203 %type <chars> STRING "%{...%}" EPILOGUE braceless content.opt
205 %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); }
207 %printer { fprintf (stderr, "{\n%s\n}", $$); }
208 braceless content.opt "{...}" "%{...%}" EPILOGUE
210 %type <uniqstr> BRACKETED_ID ID ID_COLON TYPE variable
211 %printer { fputs ($$, stderr); } <uniqstr>
212 %printer { fprintf (stderr, "[%s]", $$); } BRACKETED_ID
213 %printer { fprintf (stderr, "%s:", $$); } ID_COLON
214 %printer { fprintf (stderr, "<%s>", $$); } TYPE
217 %printer { fprintf (stderr, "%d", $$); } <integer>
219 %type <symbol> id id_colon string_as_id symbol symbol.prec
220 %printer { fprintf (stderr, "%s", $$->tag); } <symbol>
221 %printer { fprintf (stderr, "%s:", $$->tag); } id_colon
223 %type <assoc> precedence_declarator
224 %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item
225 %type <named_ref> named_ref.opt
230 prologue_declarations "%%" grammar epilogue.opt
234 /*------------------------------------.
235 | Declarations: before the first %%. |
236 `------------------------------------*/
238 prologue_declarations:
240 | prologue_declarations prologue_declaration
243 prologue_declaration:
247 code_props plain_code;
248 code_props_plain_init (&plain_code, $1, @1);
249 code_props_translate_code (&plain_code);
250 gram_scanner_last_string_free ();
251 muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
252 plain_code.code, @1);
253 code_scanner_last_string_free ();
255 | "%debug" { debug_flag = true; }
256 | "%define" variable content.opt
258 muscle_percent_define_insert ($2, @2, $3,
259 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
261 | "%defines" { defines_flag = true; }
265 spec_defines_file = xstrdup ($2);
267 | "%error-verbose" { error_verbose = true; }
268 | "%expect" INT { expected_sr_conflicts = $2; }
269 | "%expect-rr" INT { expected_rr_conflicts = $2; }
270 | "%file-prefix" STRING { spec_file_prefix = $2; }
271 | "%file-prefix" "=" STRING { spec_file_prefix = $3; } /* deprecated */
274 nondeterministic_parser = true;
277 | "%initial-action" "{...}"
280 code_props_symbol_action_init (&action, $2, @2);
281 code_props_translate_code (&action);
282 gram_scanner_last_string_free ();
283 muscle_code_grow ("initial_action", action.code, @2);
284 code_scanner_last_string_free ();
286 | "%language" STRING { language_argmatch ($2, grammar_prio, @1); }
287 | "%lex-param" "{...}" { add_param ("lex_param", $2, @2); }
288 | "%locations" { locations_flag = true; }
289 | "%name-prefix" STRING { spec_name_prefix = $2; }
290 | "%name-prefix" "=" STRING { spec_name_prefix = $3; } /* deprecated */
291 | "%no-lines" { no_lines_flag = true; }
292 | "%nondeterministic-parser" { nondeterministic_parser = true; }
293 | "%output" STRING { spec_outfile = $2; }
294 | "%output" "=" STRING { spec_outfile = $3; } /* deprecated */
295 | "%parse-param" "{...}" { add_param ("parse_param", $2, @2); }
298 /* %pure-parser is deprecated in favor of `%define api.pure', so use
299 `%define api.pure' in a backward-compatible manner here. First, don't
300 complain if %pure-parser is specified multiple times. */
301 if (!muscle_find_const ("percent_define(api.pure)"))
302 muscle_percent_define_insert ("api.pure", @1, "",
303 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
304 /* In all cases, use api.pure now so that the backend doesn't complain if
305 the skeleton ignores api.pure, but do warn now if there's a previous
306 conflicting definition from an actual %define. */
307 if (!muscle_percent_define_flag_if ("api.pure"))
308 muscle_percent_define_insert ("api.pure", @1, "",
309 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
311 | "%require" STRING { version_check (&@2, $2); }
314 char const *skeleton_user = $2;
315 if (mbschr (skeleton_user, '/'))
317 size_t dir_length = strlen (current_file);
318 char *skeleton_build;
319 while (dir_length && current_file[dir_length - 1] != '/')
321 while (dir_length && current_file[dir_length - 1] == '/')
324 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
327 memcpy (skeleton_build, current_file, dir_length);
328 skeleton_build[dir_length++] = '/';
330 strcpy (skeleton_build + dir_length, skeleton_user);
331 skeleton_user = uniqstr_new (skeleton_build);
332 free (skeleton_build);
334 skeleton_arg (skeleton_user, grammar_prio, @1);
336 | "%token-table" { token_table_flag = true; }
337 | "%verbose" { report_flag |= report_states; }
338 | "%yacc" { yacc_flag = true; }
339 | /*FIXME: Err? What is this horror doing here? */ ";"
343 precedence_declaration
347 grammar_start_symbol_set ($2, @2);
349 | "%destructor" "{...}" generic_symlist
352 code_props_symbol_action_init (&code, $2, @2);
353 code_props_translate_code (&code);
356 for (list = $3; list; list = list->next)
357 symbol_list_destructor_set (list, &code);
358 symbol_list_free ($3);
361 | "%printer" "{...}" generic_symlist
364 code_props_symbol_action_init (&code, $2, @2);
365 code_props_translate_code (&code);
368 for (list = $3; list; list = list->next)
369 symbol_list_printer_set (list, &code);
370 symbol_list_free ($3);
379 default_prec = false;
383 /* Do not invoke muscle_percent_code_grow here since it invokes
384 muscle_user_name_list_grow. */
385 muscle_code_grow ("percent_code()", $2, @2);
386 code_scanner_last_string_free ();
388 | "%code" ID braceless
390 muscle_percent_code_grow ($2, @2, $3, @3);
391 code_scanner_last_string_free ();
400 %token PERCENT_UNION "%union";
404 | ID { muscle_code_grow ("union_name", $1, @1); }
408 "%union" union_name braceless
411 muscle_code_grow ("stype", $3, @3);
412 code_scanner_last_string_free ();
420 "%nterm" { current_class = nterm_sym; } symbol_defs.1
422 current_class = unknown_sym;
425 | "%token" { current_class = token_sym; } symbol_defs.1
427 current_class = unknown_sym;
430 | "%type" TYPE symbols.1
434 for (list = $3; list; list = list->next)
435 symbol_type_set (list->content.sym, $2, @2);
436 symbol_list_free ($3);
440 precedence_declaration:
441 precedence_declarator type.opt symbols.prec
445 for (list = $3; list; list = list->next)
447 symbol_type_set (list->content.sym, current_type, @2);
448 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
450 symbol_list_free ($3);
455 precedence_declarator:
456 "%left" { $$ = left_assoc; }
457 | "%right" { $$ = right_assoc; }
458 | "%nonassoc" { $$ = non_assoc; }
462 /* Nothing. */ { current_type = NULL; }
463 | TYPE { current_type = $1; tag_seen = true; }
466 /* Just like symbols.1 but accept INT for the sake of POSIX. */
469 { $$ = symbol_list_sym_new ($1, @1); }
470 | symbols.prec symbol.prec
471 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
476 | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
479 /* One or more symbols to be %typed. */
482 { $$ = symbol_list_sym_new ($1, @1); }
484 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
488 generic_symlist_item { $$ = $1; }
489 | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
492 generic_symlist_item:
493 symbol { $$ = symbol_list_sym_new ($1, @1); }
494 | TYPE { $$ = symbol_list_type_new ($1, @1); }
495 | "<*>" { $$ = symbol_list_default_tagged_new (@1); }
496 | "<>" { $$ = symbol_list_default_tagless_new (@1); }
499 /* One token definition. */
508 symbol_class_set ($1, current_class, @1, true);
509 symbol_type_set ($1, current_type, @1);
513 symbol_class_set ($1, current_class, @1, true);
514 symbol_type_set ($1, current_type, @1);
515 symbol_user_token_number_set ($1, $2, @2);
519 symbol_class_set ($1, current_class, @1, true);
520 symbol_type_set ($1, current_type, @1);
521 symbol_make_alias ($1, $2, @$);
523 | id INT string_as_id
525 symbol_class_set ($1, current_class, @1, true);
526 symbol_type_set ($1, current_type, @1);
527 symbol_user_token_number_set ($1, $2, @2);
528 symbol_make_alias ($1, $3, @$);
532 /* One or more symbol definitions. */
535 | symbol_defs.1 symbol_def
539 /*------------------------------------------.
540 | The grammar section: between the two %%. |
541 `------------------------------------------*/
544 rules_or_grammar_declaration
545 | grammar rules_or_grammar_declaration
548 /* As a Bison extension, one can use the grammar declarations in the
549 body of the grammar. */
550 rules_or_grammar_declaration:
552 | grammar_declaration ";"
560 id_colon named_ref.opt { current_lhs ($1, @1, $2); } rhses.1
562 /* Free the current lhs. */
563 current_lhs (0, @1, 0);
568 rhs { grammar_current_rule_end (@1); }
569 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
575 { grammar_current_rule_begin (current_lhs_symbol, current_lhs_location,
576 current_lhs_named_ref); }
577 | rhs symbol named_ref.opt
578 { grammar_current_rule_symbol_append ($2, @2, $3); }
579 | rhs "{...}" named_ref.opt
580 { grammar_current_rule_action_append ($2, @2, $3); }
582 { grammar_current_rule_prec_set ($3, @3); }
584 { grammar_current_rule_dprec_set ($3, @3); }
586 { grammar_current_rule_merge_set ($3, @3); }
590 /* Nothing. */ { $$ = 0; }
592 BRACKETED_ID { $$ = named_ref_new($1, @1); }
596 /*----------------------------*
597 | variable and content.opt. |
598 *---------------------------*/
600 /* The STRING form of variable is deprecated and is not M4-friendly.
601 For example, M4 fails for `%define "[" "value"'. */
604 | STRING { $$ = uniqstr_new ($1); }
607 /* Some content or empty by default. */
609 /* Nothing. */ { $$ = ""; }
622 code_props plain_code;
623 $1[strlen ($1) - 1] = '\n';
624 code_props_plain_init (&plain_code, $1+1, @1);
625 code_props_translate_code (&plain_code);
626 gram_scanner_last_string_free ();
627 $$ = plain_code.code;
636 /* Identifiers are returned as uniqstr values by the scanner.
637 Depending on their use, we may need to make them genuine symbols. */
641 { $$ = symbol_from_uniqstr ($1, @1); }
644 $$ = symbol_get (char_name ($1), @1);
645 symbol_class_set ($$, token_sym, @1, false);
646 symbol_user_token_number_set ($$, $1, @1);
651 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
660 /* A string used as an ID: quote it. */
664 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
665 symbol_class_set ($$, token_sym, @1, false);
673 code_props plain_code;
674 code_props_plain_init (&plain_code, $2, @2);
675 code_props_translate_code (&plain_code);
676 gram_scanner_last_string_free ();
677 muscle_code_grow ("epilogue", plain_code.code, @2);
678 code_scanner_last_string_free ();
685 /* Return the location of the left-hand side of a rule whose
686 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
687 the right-hand side, and return an empty location equal to the end
688 boundary of RHS[0] if the right-hand side is empty. */
691 lloc_default (YYLTYPE const *rhs, int n)
696 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
697 The bug is fixed in 7.4.2m, but play it safe for now. */
698 loc.start = rhs[n].end;
699 loc.end = rhs[n].end;
701 /* Ignore empty nonterminals the start of the right-hand side.
702 Do not bother to ignore them at the end of the right-hand side,
703 since empty nonterminals have the same end as their predecessors. */
704 for (i = 1; i <= n; i++)
705 if (! equal_boundaries (rhs[i].start, rhs[i].end))
707 loc.start = rhs[i].start;
715 /* Add a lex-param or a parse-param (depending on TYPE) with
716 declaration DECL and location LOC. */
719 add_param (char const *type, char *decl, location loc)
721 static char const alphanum[26 + 26 + 1 + 10] =
722 "abcdefghijklmnopqrstuvwxyz"
723 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
726 char const *name_start = NULL;
729 /* Stop on last actual character. */
730 for (p = decl; p[1]; p++)
732 || ! memchr (alphanum, p[-1], sizeof alphanum))
733 && memchr (alphanum, p[0], sizeof alphanum - 10))
736 /* Strip the surrounding '{' and '}', and any blanks just inside
739 while (c_isspace ((unsigned char) *p))
743 while (c_isspace ((unsigned char) *decl))
747 complain_at (loc, _("missing identifier in parameter declaration"));
750 char *name = xmemdup0 (name_start, strspn (name_start, alphanum));
751 muscle_pair_list_grow (type, decl, name);
755 gram_scanner_last_string_free ();
760 version_check (location const *loc, char const *version)
762 if (strverscmp (version, PACKAGE_VERSION) > 0)
764 complain_at (*loc, "require bison %s, but have %s",
765 version, PACKAGE_VERSION);
771 gram_error (location const *loc, char const *msg)
773 complain_at (*loc, "%s", msg);
777 token_name (int type)
779 return yytname[YYTRANSLATE (type)];
790 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
791 return quotearg_style (escape_quoting_style, buf);