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"
32 #include "scan-gram.h"
33 #include "scan-code.h"
35 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
36 static YYLTYPE lloc_default (YYLTYPE const *, int);
38 #define YY_LOCATION_PRINT(File, Loc) \
39 location_print (File, Loc)
41 static void version_check (location const *loc, char const *version);
43 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
44 FIXME: depends on the undocumented availability of YYLLOC. */
46 #define yyerror(Msg) \
47 gram_error (&yylloc, Msg)
48 static void gram_error (location const *, char const *);
50 static char const *char_name (char);
52 /** Add a lex-param or a parse-param.
54 * \param type \a lex_param or \a parse_param
55 * \param decl the formal argument
56 * \param loc the location in the source.
58 static void add_param (char const *type, char *decl, location loc);
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;
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
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);
99 unsigned char character;
102 /* Define the tokens together with their human representation. */
103 %token GRAM_EOF 0 "end of file"
104 %token STRING "string"
107 %token PERCENT_TOKEN "%token"
108 %token PERCENT_NTERM "%nterm"
110 %token PERCENT_TYPE "%type"
111 %token PERCENT_DESTRUCTOR "%destructor"
112 %token PERCENT_PRINTER "%printer"
114 %token PERCENT_LEFT "%left"
115 %token PERCENT_RIGHT "%right"
116 %token PERCENT_NONASSOC "%nonassoc"
118 %token PERCENT_PREC "%prec"
119 %token PERCENT_DPREC "%dprec"
120 %token PERCENT_MERGE "%merge"
123 /*----------------------.
124 | Global Declarations. |
125 `----------------------*/
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"
158 %token BRACED_CODE "{...}"
160 %token EPILOGUE "epilogue"
162 %token ID "identifier"
163 %token ID_COLON "identifier:"
164 %token PERCENT_PERCENT "%%"
166 %token PROLOGUE "%{...%}"
169 %token TYPE_TAG_ANY "<*>"
170 %token TYPE_TAG_NONE "<>"
172 %type <character> CHAR
173 %printer { fputs (char_name ($$), stderr); } CHAR
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
179 %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); }
181 %printer { fprintf (stderr, "{\n%s\n}", $$); }
182 braceless content.opt "{...}" "%{...%}" EPILOGUE
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
190 %printer { fprintf (stderr, "%d", $$); } INT
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
196 %type <assoc> precedence_declarator
197 %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item
201 prologue_declarations "%%" grammar epilogue.opt
205 /*------------------------------------.
206 | Declarations: before the first %%. |
207 `------------------------------------*/
209 prologue_declarations:
211 | prologue_declarations prologue_declaration
214 prologue_declaration:
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 ();
226 | "%debug" { debug_flag = true; }
227 | "%define" variable content.opt
229 muscle_percent_define_insert ($2, @2, $3);
231 | "%defines" { defines_flag = true; }
235 spec_defines_file = xstrdup ($2);
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 */
244 nondeterministic_parser = true;
247 | "%initial-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 ();
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); }
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, "");
279 | "%require" STRING { version_check (&@2, $2); }
282 char const *skeleton_user = $2;
283 if (strchr (skeleton_user, '/'))
285 size_t dir_length = strlen (current_file);
286 char *skeleton_build;
287 while (dir_length && current_file[dir_length - 1] != '/')
289 while (dir_length && current_file[dir_length - 1] == '/')
292 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
295 strncpy (skeleton_build, current_file, dir_length);
296 skeleton_build[dir_length++] = '/';
298 strcpy (skeleton_build + dir_length, skeleton_user);
299 skeleton_user = uniqstr_new (skeleton_build);
300 free (skeleton_build);
302 skeleton_arg (skeleton_user, grammar_prio, &@1);
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? */ ";"
311 precedence_declaration
315 grammar_start_symbol_set ($2, @2);
317 | "%destructor" "{...}" generic_symlist
320 for (list = $3; list; list = list->next)
321 symbol_list_destructor_set (list, $2, @2);
322 symbol_list_free ($3);
324 | "%printer" "{...}" generic_symlist
327 for (list = $3; list; list = list->next)
328 symbol_list_printer_set (list, $2, @2);
329 symbol_list_free ($3);
337 default_prec = false;
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 ();
346 | "%code" ID braceless
348 muscle_percent_code_grow ($2, @2, $3, @3);
349 code_scanner_last_string_free ();
358 %token PERCENT_UNION "%union";
362 | ID { muscle_code_grow ("union_name", $1, @1); }
366 "%union" union_name braceless
369 muscle_code_grow ("stype", $3, @3);
370 code_scanner_last_string_free ();
378 "%nterm" { current_class = nterm_sym; } symbol_defs.1
380 current_class = unknown_sym;
383 | "%token" { current_class = token_sym; } symbol_defs.1
385 current_class = unknown_sym;
388 | "%type" TYPE symbols.1
392 for (list = $3; list; list = list->next)
393 symbol_type_set (list->content.sym, $2, @2);
394 symbol_list_free ($3);
398 precedence_declaration:
399 precedence_declarator type.opt symbols.prec
403 for (list = $3; list; list = list->next)
405 symbol_type_set (list->content.sym, current_type, @2);
406 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
408 symbol_list_free ($3);
413 precedence_declarator:
414 "%left" { $$ = left_assoc; }
415 | "%right" { $$ = right_assoc; }
416 | "%nonassoc" { $$ = non_assoc; }
420 /* Nothing. */ { current_type = NULL; }
421 | TYPE { current_type = $1; tag_seen = true; }
424 /* Just like symbols.1 but accept INT for the sake of POSIX. */
427 { $$ = symbol_list_sym_new ($1, @1); }
428 | symbols.prec symbol.prec
429 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
434 | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
437 /* One or more symbols to be %typed. */
440 { $$ = symbol_list_sym_new ($1, @1); }
442 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
446 generic_symlist_item { $$ = $1; }
447 | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
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); }
457 /* One token definition. */
466 symbol_class_set ($1, current_class, @1, true);
467 symbol_type_set ($1, current_type, @1);
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);
477 symbol_class_set ($1, current_class, @1, true);
478 symbol_type_set ($1, current_type, @1);
479 symbol_make_alias ($1, $2, @$);
481 | id INT string_as_id
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, @$);
490 /* One or more symbol definitions. */
493 | symbol_defs.1 symbol_def
497 /*------------------------------------------.
498 | The grammar section: between the two %%. |
499 `------------------------------------------*/
502 rules_or_grammar_declaration
503 | grammar rules_or_grammar_declaration
506 /* As a Bison extension, one can use the grammar declarations in the
507 body of the grammar. */
508 rules_or_grammar_declaration:
510 | grammar_declaration ";"
518 id_colon { current_lhs = $1; current_lhs_location = @1; } rhses.1
522 rhs { grammar_current_rule_end (@1); }
523 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
529 { grammar_current_rule_begin (current_lhs, current_lhs_location); }
531 { grammar_current_rule_symbol_append ($2, @2); }
533 { grammar_current_rule_action_append ($2, @2); }
535 { grammar_current_rule_prec_set ($3, @3); }
537 { grammar_current_rule_dprec_set ($3, @3); }
539 { grammar_current_rule_merge_set ($3, @3); }
543 /*----------------------------*
544 | variable and content.opt. |
545 *---------------------------*/
549 | STRING { $$ = uniqstr_new ($1); } /* deprecated and not M4-friendly */
552 /* Some content or empty by default. */
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;
583 /* Identifiers are returned as uniqstr values by the scanner.
584 Depending on their use, we may need to make them genuine symbols. */
588 { $$ = symbol_from_uniqstr ($1, @1); }
591 $$ = symbol_get (char_name ($1), @1);
592 symbol_class_set ($$, token_sym, @1, false);
593 symbol_user_token_number_set ($$, $1, @1);
598 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
607 /* A string used as an ID: quote it. */
611 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
612 symbol_class_set ($$, token_sym, @1, false);
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 ();
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. */
638 lloc_default (YYLTYPE const *rhs, int n)
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;
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))
654 loc.start = rhs[i].start;
662 /* Add a lex-param or a parse-param (depending on TYPE) with
663 declaration DECL and location LOC. */
666 add_param (char const *type, char *decl, location loc)
668 static char const alphanum[26 + 26 + 1 + 10] =
669 "abcdefghijklmnopqrstuvwxyz"
670 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
673 char const *name_start = NULL;
676 /* Stop on last actual character. */
677 for (p = decl; p[1]; p++)
679 || ! memchr (alphanum, p[-1], sizeof alphanum))
680 && memchr (alphanum, p[0], sizeof alphanum - 10))
683 /* Strip the surrounding '{' and '}', and any blanks just inside
685 while (*--p == ' ' || *p == '\t')
688 while (*++decl == ' ' || *decl == '\t')
692 complain_at (loc, _("missing identifier in parameter declaration"));
699 memchr (alphanum, name_start[name_len], sizeof alphanum);
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);
710 gram_scanner_last_string_free ();
715 version_check (location const *loc, char const *version)
717 if (strverscmp (version, PACKAGE_VERSION) > 0)
719 complain_at (*loc, "require bison %s, but have %s",
720 version, PACKAGE_VERSION);
726 gram_error (location const *loc, char const *msg)
728 complain_at (*loc, "%s", msg);
732 token_name (int type)
734 return yytname[YYTRANSLATE (type)];
745 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
746 return quotearg_style (escape_quoting_style, buf);