1 %{/* Bison Grammar Parser -*- C -*-
3 Copyright (C) 2002-2010 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"
36 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
37 static YYLTYPE lloc_default (YYLTYPE const *, int);
39 #define YY_LOCATION_PRINT(File, Loc) \
40 location_print (File, Loc)
42 static void version_check (location const *loc, char const *version);
44 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
45 FIXME: depends on the undocumented availability of YYLLOC. */
47 #define yyerror(Msg) \
48 gram_error (&yylloc, Msg)
49 static void gram_error (location const *, char const *);
51 static char const *char_name (char);
53 /** Add a lex-param or a parse-param.
55 * \param type \a lex_param or \a parse_param
56 * \param decl the formal argument
57 * \param loc the location in the source.
59 static void add_param (char const *type, char *decl, location loc);
62 static symbol_class current_class = unknown_sym;
63 static uniqstr current_type = NULL;
64 static symbol *current_lhs;
65 static location current_lhs_location;
66 static named_ref *current_lhs_named_ref;
67 static int current_prec = 0;
69 #define YYTYPE_INT16 int_fast16_t
70 #define YYTYPE_INT8 int_fast8_t
71 #define YYTYPE_UINT16 uint_fast16_t
72 #define YYTYPE_UINT8 uint_fast8_t
86 /* Bison's grammar can initial empty locations, hence a default
87 location is needed. */
88 boundary_set (&@$.start, current_file, 1, 1);
89 boundary_set (&@$.end, current_file, 1, 1);
101 unsigned char character;
102 named_ref *named_ref;
105 /* Define the tokens together with their human representation. */
106 %token GRAM_EOF 0 "end of file"
107 %token STRING "string"
110 %token PERCENT_TOKEN "%token"
111 %token PERCENT_NTERM "%nterm"
113 %token PERCENT_TYPE "%type"
114 %token PERCENT_DESTRUCTOR "%destructor"
115 %token PERCENT_PRINTER "%printer"
117 %token PERCENT_LEFT "%left"
118 %token PERCENT_RIGHT "%right"
119 %token PERCENT_NONASSOC "%nonassoc"
121 %token PERCENT_PREC "%prec"
122 %token PERCENT_DPREC "%dprec"
123 %token PERCENT_MERGE "%merge"
126 /*----------------------.
127 | Global Declarations. |
128 `----------------------*/
132 PERCENT_DEBUG "%debug"
133 PERCENT_DEFAULT_PREC "%default-prec"
134 PERCENT_DEFINE "%define"
135 PERCENT_DEFINES "%defines"
136 PERCENT_ERROR_VERBOSE "%error-verbose"
137 PERCENT_EXPECT "%expect"
138 PERCENT_EXPECT_RR "%expect-rr"
139 PERCENT_FILE_PREFIX "%file-prefix"
140 PERCENT_GLR_PARSER "%glr-parser"
141 PERCENT_INITIAL_ACTION "%initial-action"
142 PERCENT_LANGUAGE "%language"
143 PERCENT_LEX_PARAM "%lex-param"
144 PERCENT_LOCATIONS "%locations"
145 PERCENT_NAME_PREFIX "%name-prefix"
146 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
147 PERCENT_NO_LINES "%no-lines"
148 PERCENT_NONDETERMINISTIC_PARSER
149 "%nondeterministic-parser"
150 PERCENT_OUTPUT "%output"
151 PERCENT_PARSE_PARAM "%parse-param"
152 PERCENT_PURE_PARSER "%pure-parser"
153 PERCENT_REQUIRE "%require"
154 PERCENT_SKELETON "%skeleton"
155 PERCENT_START "%start"
156 PERCENT_TOKEN_TABLE "%token-table"
157 PERCENT_VERBOSE "%verbose"
161 %token BRACED_CODE "{...}"
162 %token BRACKETED_ID "[identifier]"
164 %token EPILOGUE "epilogue"
166 %token ID "identifier"
167 %token ID_COLON "identifier:"
168 %token PERCENT_PERCENT "%%"
170 %token PROLOGUE "%{...%}"
173 %token TYPE_TAG_ANY "<*>"
174 %token TYPE_TAG_NONE "<>"
176 %type <character> CHAR
177 %printer { fputs (char_name ($$), stderr); } CHAR
179 /* braceless is not to be used for rule or symbol actions, as it
180 calls code_props_plain_init. */
181 %type <chars> STRING "%{...%}" EPILOGUE braceless content.opt
183 %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); }
185 %printer { fprintf (stderr, "{\n%s\n}", $$); }
186 braceless content.opt "{...}" "%{...%}" EPILOGUE
188 %type <uniqstr> BRACKETED_ID ID ID_COLON TYPE variable
189 %printer { fputs ($$, stderr); } <uniqstr>
190 %printer { fprintf (stderr, "[%s]", $$); } BRACKETED_ID
191 %printer { fprintf (stderr, "%s:", $$); } ID_COLON
192 %printer { fprintf (stderr, "<%s>", $$); } TYPE
195 %printer { fprintf (stderr, "%d", $$); } <integer>
197 %type <symbol> id id_colon string_as_id symbol symbol.prec
198 %printer { fprintf (stderr, "%s", $$->tag); } <symbol>
199 %printer { fprintf (stderr, "%s:", $$->tag); } id_colon
201 %type <assoc> precedence_declarator
202 %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item
203 %type <named_ref> named_ref.opt
208 prologue_declarations "%%" grammar epilogue.opt
212 /*------------------------------------.
213 | Declarations: before the first %%. |
214 `------------------------------------*/
216 prologue_declarations:
218 | prologue_declarations prologue_declaration
221 prologue_declaration:
225 code_props plain_code;
226 code_props_plain_init (&plain_code, $1, @1);
227 code_props_translate_code (&plain_code);
228 gram_scanner_last_string_free ();
229 muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
230 plain_code.code, @1);
231 code_scanner_last_string_free ();
233 | "%debug" { debug_flag = true; }
234 | "%define" variable content.opt
236 muscle_percent_define_insert ($2, @2, $3,
237 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
239 | "%defines" { defines_flag = true; }
243 spec_defines_file = xstrdup ($2);
245 | "%error-verbose" { error_verbose = true; }
246 | "%expect" INT { expected_sr_conflicts = $2; }
247 | "%expect-rr" INT { expected_rr_conflicts = $2; }
248 | "%file-prefix" STRING { spec_file_prefix = $2; }
249 | "%file-prefix" "=" STRING { spec_file_prefix = $3; } /* deprecated */
252 nondeterministic_parser = true;
255 | "%initial-action" "{...}"
258 code_props_symbol_action_init (&action, $2, @2);
259 code_props_translate_code (&action);
260 gram_scanner_last_string_free ();
261 muscle_code_grow ("initial_action", action.code, @2);
262 code_scanner_last_string_free ();
264 | "%language" STRING { language_argmatch ($2, grammar_prio, @1); }
265 | "%lex-param" "{...}" { add_param ("lex_param", $2, @2); }
266 | "%locations" { locations_flag = true; }
267 | "%name-prefix" STRING { spec_name_prefix = $2; }
268 | "%name-prefix" "=" STRING { spec_name_prefix = $3; } /* deprecated */
269 | "%no-lines" { no_lines_flag = true; }
270 | "%nondeterministic-parser" { nondeterministic_parser = true; }
271 | "%output" STRING { spec_outfile = $2; }
272 | "%output" "=" STRING { spec_outfile = $3; } /* deprecated */
273 | "%parse-param" "{...}" { add_param ("parse_param", $2, @2); }
276 /* %pure-parser is deprecated in favor of `%define api.pure', so use
277 `%define api.pure' in a backward-compatible manner here. First, don't
278 complain if %pure-parser is specified multiple times. */
279 if (!muscle_find_const ("percent_define(api.pure)"))
280 muscle_percent_define_insert ("api.pure", @1, "",
281 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
282 /* In all cases, use api.pure now so that the backend doesn't complain if
283 the skeleton ignores api.pure, but do warn now if there's a previous
284 conflicting definition from an actual %define. */
285 if (!muscle_percent_define_flag_if ("api.pure"))
286 muscle_percent_define_insert ("api.pure", @1, "",
287 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
289 | "%require" STRING { version_check (&@2, $2); }
292 char const *skeleton_user = $2;
293 if (strchr (skeleton_user, '/'))
295 size_t dir_length = strlen (current_file);
296 char *skeleton_build;
297 while (dir_length && current_file[dir_length - 1] != '/')
299 while (dir_length && current_file[dir_length - 1] == '/')
302 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
305 strncpy (skeleton_build, current_file, dir_length);
306 skeleton_build[dir_length++] = '/';
308 strcpy (skeleton_build + dir_length, skeleton_user);
309 skeleton_user = uniqstr_new (skeleton_build);
310 free (skeleton_build);
312 skeleton_arg (skeleton_user, grammar_prio, @1);
314 | "%token-table" { token_table_flag = true; }
315 | "%verbose" { report_flag |= report_states; }
316 | "%yacc" { yacc_flag = true; }
317 | /*FIXME: Err? What is this horror doing here? */ ";"
321 precedence_declaration
325 grammar_start_symbol_set ($2, @2);
327 | "%destructor" "{...}" generic_symlist
330 for (list = $3; list; list = list->next)
331 symbol_list_destructor_set (list, $2, @2);
332 symbol_list_free ($3);
334 | "%printer" "{...}" generic_symlist
337 for (list = $3; list; list = list->next)
338 symbol_list_printer_set (list, $2, @2);
339 symbol_list_free ($3);
347 default_prec = false;
351 /* Do not invoke muscle_percent_code_grow here since it invokes
352 muscle_user_name_list_grow. */
353 muscle_code_grow ("percent_code()", $2, @2);
354 code_scanner_last_string_free ();
356 | "%code" ID braceless
358 muscle_percent_code_grow ($2, @2, $3, @3);
359 code_scanner_last_string_free ();
368 %token PERCENT_UNION "%union";
372 | ID { muscle_code_grow ("union_name", $1, @1); }
376 "%union" union_name braceless
379 muscle_code_grow ("stype", $3, @3);
380 code_scanner_last_string_free ();
388 "%nterm" { current_class = nterm_sym; } symbol_defs.1
390 current_class = unknown_sym;
393 | "%token" { current_class = token_sym; } symbol_defs.1
395 current_class = unknown_sym;
398 | "%type" TYPE symbols.1
402 for (list = $3; list; list = list->next)
403 symbol_type_set (list->content.sym, $2, @2);
404 symbol_list_free ($3);
408 precedence_declaration:
409 precedence_declarator type.opt symbols.prec
413 for (list = $3; list; list = list->next)
415 symbol_type_set (list->content.sym, current_type, @2);
416 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
418 symbol_list_free ($3);
423 precedence_declarator:
424 "%left" { $$ = left_assoc; }
425 | "%right" { $$ = right_assoc; }
426 | "%nonassoc" { $$ = non_assoc; }
430 /* Nothing. */ { current_type = NULL; }
431 | TYPE { current_type = $1; tag_seen = true; }
434 /* Just like symbols.1 but accept INT for the sake of POSIX. */
437 { $$ = symbol_list_sym_new ($1, @1); }
438 | symbols.prec symbol.prec
439 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
444 | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
447 /* One or more symbols to be %typed. */
450 { $$ = symbol_list_sym_new ($1, @1); }
452 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
456 generic_symlist_item { $$ = $1; }
457 | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
460 generic_symlist_item:
461 symbol { $$ = symbol_list_sym_new ($1, @1); }
462 | TYPE { $$ = symbol_list_type_new ($1, @1); }
463 | "<*>" { $$ = symbol_list_default_tagged_new (@1); }
464 | "<>" { $$ = symbol_list_default_tagless_new (@1); }
467 /* One token definition. */
476 symbol_class_set ($1, current_class, @1, true);
477 symbol_type_set ($1, current_type, @1);
481 symbol_class_set ($1, current_class, @1, true);
482 symbol_type_set ($1, current_type, @1);
483 symbol_user_token_number_set ($1, $2, @2);
487 symbol_class_set ($1, current_class, @1, true);
488 symbol_type_set ($1, current_type, @1);
489 symbol_make_alias ($1, $2, @$);
491 | id INT string_as_id
493 symbol_class_set ($1, current_class, @1, true);
494 symbol_type_set ($1, current_type, @1);
495 symbol_user_token_number_set ($1, $2, @2);
496 symbol_make_alias ($1, $3, @$);
500 /* One or more symbol definitions. */
503 | symbol_defs.1 symbol_def
507 /*------------------------------------------.
508 | The grammar section: between the two %%. |
509 `------------------------------------------*/
512 rules_or_grammar_declaration
513 | grammar rules_or_grammar_declaration
516 /* As a Bison extension, one can use the grammar declarations in the
517 body of the grammar. */
518 rules_or_grammar_declaration:
520 | grammar_declaration ";"
528 id_colon named_ref.opt { current_lhs = $1; current_lhs_location = @1;
529 current_lhs_named_ref = $2; } rhses.1
533 rhs { grammar_current_rule_end (@1); }
534 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
540 { grammar_current_rule_begin (current_lhs, current_lhs_location,
541 current_lhs_named_ref); }
542 | rhs symbol named_ref.opt
543 { grammar_current_rule_symbol_append ($2, @2, $3); }
544 | rhs "{...}" named_ref.opt
545 { grammar_current_rule_action_append ($2, @2, $3); }
547 { grammar_current_rule_prec_set ($3, @3); }
549 { grammar_current_rule_dprec_set ($3, @3); }
551 { grammar_current_rule_merge_set ($3, @3); }
555 /* Nothing. */ { $$ = 0; }
557 BRACKETED_ID { $$ = named_ref_new($1, @1); }
561 /*----------------------------*
562 | variable and content.opt. |
563 *---------------------------*/
565 /* The STRING form of variable is deprecated and is not M4-friendly.
566 For example, M4 fails for `%define "[" "value"'. */
569 | STRING { $$ = uniqstr_new ($1); }
572 /* Some content or empty by default. */
574 /* Nothing. */ { $$ = ""; }
587 code_props plain_code;
588 $1[strlen ($1) - 1] = '\n';
589 code_props_plain_init (&plain_code, $1+1, @1);
590 code_props_translate_code (&plain_code);
591 gram_scanner_last_string_free ();
592 $$ = plain_code.code;
601 /* Identifiers are returned as uniqstr values by the scanner.
602 Depending on their use, we may need to make them genuine symbols. */
606 { $$ = symbol_from_uniqstr ($1, @1); }
609 $$ = symbol_get (char_name ($1), @1);
610 symbol_class_set ($$, token_sym, @1, false);
611 symbol_user_token_number_set ($$, $1, @1);
616 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
625 /* A string used as an ID: quote it. */
629 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
630 symbol_class_set ($$, token_sym, @1, false);
638 code_props plain_code;
639 code_props_plain_init (&plain_code, $2, @2);
640 code_props_translate_code (&plain_code);
641 gram_scanner_last_string_free ();
642 muscle_code_grow ("epilogue", plain_code.code, @2);
643 code_scanner_last_string_free ();
650 /* Return the location of the left-hand side of a rule whose
651 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
652 the right-hand side, and return an empty location equal to the end
653 boundary of RHS[0] if the right-hand side is empty. */
656 lloc_default (YYLTYPE const *rhs, int n)
661 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
662 The bug is fixed in 7.4.2m, but play it safe for now. */
663 loc.start = rhs[n].end;
664 loc.end = rhs[n].end;
666 /* Ignore empty nonterminals the start of the the right-hand side.
667 Do not bother to ignore them at the end of the right-hand side,
668 since empty nonterminals have the same end as their predecessors. */
669 for (i = 1; i <= n; i++)
670 if (! equal_boundaries (rhs[i].start, rhs[i].end))
672 loc.start = rhs[i].start;
680 /* Add a lex-param or a parse-param (depending on TYPE) with
681 declaration DECL and location LOC. */
684 add_param (char const *type, char *decl, location loc)
686 static char const alphanum[26 + 26 + 1 + 10] =
687 "abcdefghijklmnopqrstuvwxyz"
688 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
691 char const *name_start = NULL;
694 /* Stop on last actual character. */
695 for (p = decl; p[1]; p++)
697 || ! memchr (alphanum, p[-1], sizeof alphanum))
698 && memchr (alphanum, p[0], sizeof alphanum - 10))
701 /* Strip the surrounding '{' and '}', and any blanks just inside
703 while (*--p == ' ' || *p == '\t')
706 while (*++decl == ' ' || *decl == '\t')
710 complain_at (loc, _("missing identifier in parameter declaration"));
717 memchr (alphanum, name_start[name_len], sizeof alphanum);
721 name = xmalloc (name_len + 1);
722 memcpy (name, name_start, name_len);
723 name[name_len] = '\0';
724 muscle_pair_list_grow (type, decl, name);
728 gram_scanner_last_string_free ();
733 version_check (location const *loc, char const *version)
735 if (strverscmp (version, PACKAGE_VERSION) > 0)
737 complain_at (*loc, "require bison %s, but have %s",
738 version, PACKAGE_VERSION);
744 gram_error (location const *loc, char const *msg)
746 complain_at (*loc, "%s", msg);
750 token_name (int type)
752 return yytname[YYTRANSLATE (type)];
763 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
764 return quotearg_style (escape_quoting_style, buf);