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 for (list = $3; list; list = list->next)
352 symbol_list_destructor_set (list, $2, @2);
353 symbol_list_free ($3);
355 | "%printer" "{...}" generic_symlist
358 for (list = $3; list; list = list->next)
359 symbol_list_printer_set (list, $2, @2);
360 symbol_list_free ($3);
368 default_prec = false;
372 /* Do not invoke muscle_percent_code_grow here since it invokes
373 muscle_user_name_list_grow. */
374 muscle_code_grow ("percent_code()", $2, @2);
375 code_scanner_last_string_free ();
377 | "%code" ID braceless
379 muscle_percent_code_grow ($2, @2, $3, @3);
380 code_scanner_last_string_free ();
389 %token PERCENT_UNION "%union";
393 | ID { muscle_code_grow ("union_name", $1, @1); }
397 "%union" union_name braceless
400 muscle_code_grow ("stype", $3, @3);
401 code_scanner_last_string_free ();
409 "%nterm" { current_class = nterm_sym; } symbol_defs.1
411 current_class = unknown_sym;
414 | "%token" { current_class = token_sym; } symbol_defs.1
416 current_class = unknown_sym;
419 | "%type" TYPE symbols.1
423 for (list = $3; list; list = list->next)
424 symbol_type_set (list->content.sym, $2, @2);
425 symbol_list_free ($3);
429 precedence_declaration:
430 precedence_declarator type.opt symbols.prec
434 for (list = $3; list; list = list->next)
436 symbol_type_set (list->content.sym, current_type, @2);
437 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
439 symbol_list_free ($3);
444 precedence_declarator:
445 "%left" { $$ = left_assoc; }
446 | "%right" { $$ = right_assoc; }
447 | "%nonassoc" { $$ = non_assoc; }
451 /* Nothing. */ { current_type = NULL; }
452 | TYPE { current_type = $1; tag_seen = true; }
455 /* Just like symbols.1 but accept INT for the sake of POSIX. */
458 { $$ = symbol_list_sym_new ($1, @1); }
459 | symbols.prec symbol.prec
460 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
465 | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
468 /* One or more symbols to be %typed. */
471 { $$ = symbol_list_sym_new ($1, @1); }
473 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
477 generic_symlist_item { $$ = $1; }
478 | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
481 generic_symlist_item:
482 symbol { $$ = symbol_list_sym_new ($1, @1); }
483 | TYPE { $$ = symbol_list_type_new ($1, @1); }
484 | "<*>" { $$ = symbol_list_default_tagged_new (@1); }
485 | "<>" { $$ = symbol_list_default_tagless_new (@1); }
488 /* One token definition. */
497 symbol_class_set ($1, current_class, @1, true);
498 symbol_type_set ($1, current_type, @1);
502 symbol_class_set ($1, current_class, @1, true);
503 symbol_type_set ($1, current_type, @1);
504 symbol_user_token_number_set ($1, $2, @2);
508 symbol_class_set ($1, current_class, @1, true);
509 symbol_type_set ($1, current_type, @1);
510 symbol_make_alias ($1, $2, @$);
512 | id INT string_as_id
514 symbol_class_set ($1, current_class, @1, true);
515 symbol_type_set ($1, current_type, @1);
516 symbol_user_token_number_set ($1, $2, @2);
517 symbol_make_alias ($1, $3, @$);
521 /* One or more symbol definitions. */
524 | symbol_defs.1 symbol_def
528 /*------------------------------------------.
529 | The grammar section: between the two %%. |
530 `------------------------------------------*/
533 rules_or_grammar_declaration
534 | grammar rules_or_grammar_declaration
537 /* As a Bison extension, one can use the grammar declarations in the
538 body of the grammar. */
539 rules_or_grammar_declaration:
541 | grammar_declaration ";"
549 id_colon named_ref.opt { current_lhs ($1, @1, $2); } rhses.1
551 /* Free the current lhs. */
552 current_lhs (0, @1, 0);
557 rhs { grammar_current_rule_end (@1); }
558 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
564 { grammar_current_rule_begin (current_lhs_symbol, current_lhs_location,
565 current_lhs_named_ref); }
566 | rhs symbol named_ref.opt
567 { grammar_current_rule_symbol_append ($2, @2, $3); }
568 | rhs "{...}" named_ref.opt
569 { grammar_current_rule_action_append ($2, @2, $3); }
571 { grammar_current_rule_prec_set ($3, @3); }
573 { grammar_current_rule_dprec_set ($3, @3); }
575 { grammar_current_rule_merge_set ($3, @3); }
579 /* Nothing. */ { $$ = 0; }
581 BRACKETED_ID { $$ = named_ref_new($1, @1); }
585 /*----------------------------*
586 | variable and content.opt. |
587 *---------------------------*/
589 /* The STRING form of variable is deprecated and is not M4-friendly.
590 For example, M4 fails for `%define "[" "value"'. */
593 | STRING { $$ = uniqstr_new ($1); }
596 /* Some content or empty by default. */
598 /* Nothing. */ { $$ = ""; }
611 code_props plain_code;
612 $1[strlen ($1) - 1] = '\n';
613 code_props_plain_init (&plain_code, $1+1, @1);
614 code_props_translate_code (&plain_code);
615 gram_scanner_last_string_free ();
616 $$ = plain_code.code;
625 /* Identifiers are returned as uniqstr values by the scanner.
626 Depending on their use, we may need to make them genuine symbols. */
630 { $$ = symbol_from_uniqstr ($1, @1); }
633 $$ = symbol_get (char_name ($1), @1);
634 symbol_class_set ($$, token_sym, @1, false);
635 symbol_user_token_number_set ($$, $1, @1);
640 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
649 /* A string used as an ID: quote it. */
653 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
654 symbol_class_set ($$, token_sym, @1, false);
662 code_props plain_code;
663 code_props_plain_init (&plain_code, $2, @2);
664 code_props_translate_code (&plain_code);
665 gram_scanner_last_string_free ();
666 muscle_code_grow ("epilogue", plain_code.code, @2);
667 code_scanner_last_string_free ();
674 /* Return the location of the left-hand side of a rule whose
675 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
676 the right-hand side, and return an empty location equal to the end
677 boundary of RHS[0] if the right-hand side is empty. */
680 lloc_default (YYLTYPE const *rhs, int n)
685 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
686 The bug is fixed in 7.4.2m, but play it safe for now. */
687 loc.start = rhs[n].end;
688 loc.end = rhs[n].end;
690 /* Ignore empty nonterminals the start of the right-hand side.
691 Do not bother to ignore them at the end of the right-hand side,
692 since empty nonterminals have the same end as their predecessors. */
693 for (i = 1; i <= n; i++)
694 if (! equal_boundaries (rhs[i].start, rhs[i].end))
696 loc.start = rhs[i].start;
704 /* Add a lex-param or a parse-param (depending on TYPE) with
705 declaration DECL and location LOC. */
708 add_param (char const *type, char *decl, location loc)
710 static char const alphanum[26 + 26 + 1 + 10] =
711 "abcdefghijklmnopqrstuvwxyz"
712 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
715 char const *name_start = NULL;
718 /* Stop on last actual character. */
719 for (p = decl; p[1]; p++)
721 || ! memchr (alphanum, p[-1], sizeof alphanum))
722 && memchr (alphanum, p[0], sizeof alphanum - 10))
725 /* Strip the surrounding '{' and '}', and any blanks just inside
727 while (*--p == ' ' || *p == '\t')
730 while (*++decl == ' ' || *decl == '\t')
734 complain_at (loc, _("missing identifier in parameter declaration"));
737 char *name = xmemdup0 (name_start, strspn (name_start, alphanum));
738 muscle_pair_list_grow (type, decl, name);
742 gram_scanner_last_string_free ();
747 version_check (location const *loc, char const *version)
749 if (strverscmp (version, PACKAGE_VERSION) > 0)
751 complain_at (*loc, "require bison %s, but have %s",
752 version, PACKAGE_VERSION);
758 gram_error (location const *loc, char const *msg)
760 complain_at (*loc, "%s", msg);
764 token_name (int type)
766 return yytname[YYTRANSLATE (type)];
777 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
778 return quotearg_style (escape_quoting_style, buf);