1 /* Bison Grammar Parser -*- C -*-
3 Copyright (C) 2002-2013 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/>. */
33 #include "conflicts.h"
37 #include "named-ref.h"
40 #include "scan-gram.h"
41 #include "scan-code.h"
44 static int current_prec = 0;
45 static location current_lhs_location;
46 static named_ref *current_lhs_named_ref;
47 static symbol *current_lhs_symbol;
48 static symbol_class current_class = unknown_sym;
49 static uniqstr current_type = NULL;
51 /** Set the new current left-hand side symbol, possibly common
52 * to several right-hand side parts of rule.
54 static void current_lhs (symbol *sym, location loc, named_ref *ref);
56 #define YYLLOC_DEFAULT(Current, Rhs, N) \
57 (Current) = lloc_default (Rhs, N)
58 static YYLTYPE lloc_default (YYLTYPE const *, int);
60 #define YY_LOCATION_PRINT(File, Loc) \
61 location_print (Loc, File)
63 static void version_check (location const *loc, char const *version);
65 static void gram_error (location const *, char const *);
67 /* A string that describes a char (e.g., 'a' -> "'a'"). */
68 static char const *char_name (char);
70 #define YYTYPE_INT16 int_fast16_t
71 #define YYTYPE_INT8 int_fast8_t
72 #define YYTYPE_UINT16 uint_fast16_t
73 #define YYTYPE_UINT8 uint_fast8_t
76 %define api.prefix {gram_}
79 %define parse.error verbose
80 %define parse.lac full
88 /* Bison's grammar can initial empty locations, hence a default
89 location is needed. */
90 boundary_set (&@$.start, current_file, 1, 1);
91 boundary_set (&@$.end, current_file, 1, 1);
94 /* Define the tokens together with their human representation. */
95 %token GRAM_EOF 0 "end of file"
96 %token STRING "string"
98 %token PERCENT_TOKEN "%token"
99 %token PERCENT_NTERM "%nterm"
101 %token PERCENT_TYPE "%type"
102 %token PERCENT_DESTRUCTOR "%destructor"
103 %token PERCENT_PRINTER "%printer"
105 %token PERCENT_LEFT "%left"
106 %token PERCENT_RIGHT "%right"
107 %token PERCENT_NONASSOC "%nonassoc"
108 %token PERCENT_PRECEDENCE "%precedence"
110 %token PERCENT_PREC "%prec"
111 %token PERCENT_DPREC "%dprec"
112 %token PERCENT_MERGE "%merge"
114 /*----------------------.
115 | Global Declarations. |
116 `----------------------*/
120 PERCENT_DEFAULT_PREC "%default-prec"
121 PERCENT_DEFINE "%define"
122 PERCENT_DEFINES "%defines"
123 PERCENT_ERROR_VERBOSE "%error-verbose"
124 PERCENT_EXPECT "%expect"
125 PERCENT_EXPECT_RR "%expect-rr"
126 PERCENT_FLAG "%<flag>"
127 PERCENT_FILE_PREFIX "%file-prefix"
128 PERCENT_GLR_PARSER "%glr-parser"
129 PERCENT_INITIAL_ACTION "%initial-action"
130 PERCENT_LANGUAGE "%language"
131 PERCENT_NAME_PREFIX "%name-prefix"
132 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
133 PERCENT_NO_LINES "%no-lines"
134 PERCENT_NONDETERMINISTIC_PARSER
135 "%nondeterministic-parser"
136 PERCENT_OUTPUT "%output"
137 PERCENT_REQUIRE "%require"
138 PERCENT_SKELETON "%skeleton"
139 PERCENT_START "%start"
140 PERCENT_TOKEN_TABLE "%token-table"
141 PERCENT_VERBOSE "%verbose"
145 %token BRACED_CODE "{...}"
146 %token BRACED_PREDICATE "%?{...}"
147 %token BRACKETED_ID "[identifier]"
149 %token EPILOGUE "epilogue"
151 %token ID "identifier"
152 %token ID_COLON "identifier:"
153 %token PERCENT_PERCENT "%%"
155 %token PROLOGUE "%{...%}"
161 %union {unsigned char character;}
162 %type <character> CHAR
163 %printer { fputs (char_name ($$), yyo); } CHAR
165 /* braceless is not to be used for rule or symbol actions, as it
166 calls code_props_plain_init. */
172 %type <chars> STRING "%{...%}" EPILOGUE braceless
173 %type <code> "{...}" "%?{...}"
174 %printer { fputs (quotearg_style (c_quoting_style, $$), yyo); }
176 %printer { fprintf (yyo, "{\n%s\n}", $$); }
177 braceless "{...}" "%{...%}" EPILOGUE
179 %union {uniqstr uniqstr;}
180 %type <uniqstr> BRACKETED_ID ID ID_COLON PERCENT_FLAG TAG tag variable
181 %printer { fputs ($$, yyo); } <uniqstr>
182 %printer { fprintf (yyo, "[%s]", $$); } BRACKETED_ID
183 %printer { fprintf (yyo, "%s:", $$); } ID_COLON
184 %printer { fprintf (yyo, "%%%s", $$); } PERCENT_FLAG
185 %printer { fprintf (yyo, "<%s>", $$); } TAG tag
187 %union {int integer;};
188 %token <integer> INT "integer"
189 %printer { fprintf (yyo, "%d", $$); } <integer>
191 %union {symbol *symbol;}
192 %type <symbol> id id_colon string_as_id symbol symbol.prec
193 %printer { fprintf (yyo, "%s", $$->tag); } <symbol>
194 %printer { fprintf (yyo, "%s:", $$->tag); } id_colon
196 %union {assoc assoc;};
197 %type <assoc> precedence_declarator
199 %union {symbol_list *list;}
200 %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item
202 %union {named_ref *named_ref;}
203 %type <named_ref> named_ref.opt
214 param_parse = 1 << 1,
215 param_both = param_lex | param_parse
220 /** Add a lex-param and/or a parse-param.
222 * \param type where to push this formal argument.
223 * \param decl the formal argument. Destroyed.
224 * \param loc the location in the source.
226 static void add_param (param_type type, char *decl, location loc);
227 static param_type current_param = param_none;
229 %union {param_type param;}
230 %token <param> PERCENT_PARAM "%param";
235 #define CASE(In, Out) \
236 case param_ ## In: fputs ("%" #Out, yyo); break
237 CASE (lex, lex-param);
238 CASE (parse, parse-param);
241 case param_none: aver (false); break;
252 prologue_declarations "%%" grammar epilogue.opt
256 /*------------------------------------.
257 | Declarations: before the first %%. |
258 `------------------------------------*/
260 prologue_declarations:
262 | prologue_declarations prologue_declaration
265 prologue_declaration:
269 code_props plain_code;
270 code_props_plain_init (&plain_code, $1, @1);
271 code_props_translate_code (&plain_code);
272 gram_scanner_last_string_free ();
273 muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
274 plain_code.code, @1);
275 code_scanner_last_string_free ();
279 muscle_percent_define_ensure ($1, @1, true);
281 | "%define" variable value
283 muscle_percent_define_insert ($2, @2, $3.kind, $3.chars,
284 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
286 | "%defines" { defines_flag = true; }
290 spec_defines_file = xstrdup ($2);
294 muscle_percent_define_insert ("parse.error", @1, muscle_keyword,
296 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
298 | "%expect" INT { expected_sr_conflicts = $2; }
299 | "%expect-rr" INT { expected_rr_conflicts = $2; }
300 | "%file-prefix" STRING { spec_file_prefix = $2; }
303 nondeterministic_parser = true;
306 | "%initial-action" "{...}"
309 code_props_symbol_action_init (&action, $2, @2);
310 code_props_translate_code (&action);
311 gram_scanner_last_string_free ();
312 muscle_code_grow ("initial_action", action.code, @2);
313 code_scanner_last_string_free ();
315 | "%language" STRING { language_argmatch ($2, grammar_prio, @1); }
316 | "%name-prefix" STRING { spec_name_prefix = $2; }
317 | "%no-lines" { no_lines_flag = true; }
318 | "%nondeterministic-parser" { nondeterministic_parser = true; }
319 | "%output" STRING { spec_outfile = $2; }
320 | "%param" { current_param = $1; } params { current_param = param_none; }
321 | "%require" STRING { version_check (&@2, $2); }
324 char const *skeleton_user = $2;
325 if (strchr (skeleton_user, '/'))
327 size_t dir_length = strlen (current_file);
328 char *skeleton_build;
329 while (dir_length && current_file[dir_length - 1] != '/')
331 while (dir_length && current_file[dir_length - 1] == '/')
334 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
337 memcpy (skeleton_build, current_file, dir_length);
338 skeleton_build[dir_length++] = '/';
340 strcpy (skeleton_build + dir_length, skeleton_user);
341 skeleton_user = uniqstr_new (skeleton_build);
342 free (skeleton_build);
344 skeleton_arg (skeleton_user, grammar_prio, @1);
346 | "%token-table" { token_table_flag = true; }
347 | "%verbose" { report_flag |= report_states; }
348 | "%yacc" { yacc_flag = true; }
349 | /*FIXME: Err? What is this horror doing here? */ ";"
353 params "{...}" { add_param (current_param, $2, @2); }
354 | "{...}" { add_param (current_param, $1, @1); }
358 /*----------------------.
359 | grammar_declaration. |
360 `----------------------*/
363 precedence_declaration
367 grammar_start_symbol_set ($2, @2);
369 | code_props_type "{...}" generic_symlist
372 code_props_symbol_action_init (&code, $2, @2);
373 code_props_translate_code (&code);
376 for (list = $3; list; list = list->next)
377 symbol_list_code_props_set (list, $1, &code);
378 symbol_list_free ($3);
387 default_prec = false;
391 /* Do not invoke muscle_percent_code_grow here since it invokes
392 muscle_user_name_list_grow. */
393 muscle_code_grow ("percent_code()", $2, @2);
394 code_scanner_last_string_free ();
396 | "%code" ID braceless
398 muscle_percent_code_grow ($2, @2, $3, @3);
399 code_scanner_last_string_free ();
403 %type <code_type> code_props_type;
404 %union {code_props_type code_type;};
405 %printer { fprintf (yyo, "%s", code_props_type_string ($$)); } <code_type>;
407 "%destructor" { $$ = destructor; }
408 | "%printer" { $$ = printer; }
415 %token PERCENT_UNION "%union";
419 | ID { muscle_code_grow ("union_name", $1, @1); }
423 "%union" union_name braceless
426 muscle_code_grow ("union_members", $3, @3);
427 code_scanner_last_string_free ();
435 "%nterm" { current_class = nterm_sym; } symbol_defs.1
437 current_class = unknown_sym;
440 | "%token" { current_class = token_sym; } symbol_defs.1
442 current_class = unknown_sym;
445 | "%type" TAG symbols.1
449 for (list = $3; list; list = list->next)
450 symbol_type_set (list->content.sym, $2, @2);
451 symbol_list_free ($3);
455 precedence_declaration:
456 precedence_declarator tag.opt symbols.prec
460 for (list = $3; list; list = list->next)
462 symbol_type_set (list->content.sym, current_type, @2);
463 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
465 symbol_list_free ($3);
470 precedence_declarator:
471 "%left" { $$ = left_assoc; }
472 | "%right" { $$ = right_assoc; }
473 | "%nonassoc" { $$ = non_assoc; }
474 | "%precedence" { $$ = precedence_assoc; }
478 %empty { current_type = NULL; }
479 | TAG { current_type = $1; tag_seen = true; }
482 /* Just like symbols.1 but accept INT for the sake of POSIX. */
485 { $$ = symbol_list_sym_new ($1, @1); }
486 | symbols.prec symbol.prec
487 { $$ = symbol_list_append ($1, symbol_list_sym_new ($2, @2)); }
494 symbol_class_set ($1, token_sym, @1, false);
499 symbol_user_token_number_set ($1, $2, @2);
500 symbol_class_set ($1, token_sym, @1, false);
504 /* One or more symbols to be %typed. */
507 { $$ = symbol_list_sym_new ($1, @1); }
509 { $$ = symbol_list_append ($1, symbol_list_sym_new ($2, @2)); }
513 generic_symlist_item { $$ = $1; }
514 | generic_symlist generic_symlist_item { $$ = symbol_list_append ($1, $2); }
517 generic_symlist_item:
518 symbol { $$ = symbol_list_sym_new ($1, @1); }
519 | tag { $$ = symbol_list_type_new ($1, @1); }
524 | "<*>" { $$ = uniqstr_new ("*"); }
525 | "<>" { $$ = uniqstr_new (""); }
528 /* One token definition. */
537 symbol_class_set ($1, current_class, @1, true);
538 symbol_type_set ($1, current_type, @1);
542 symbol_class_set ($1, current_class, @1, true);
543 symbol_type_set ($1, current_type, @1);
544 symbol_user_token_number_set ($1, $2, @2);
548 symbol_class_set ($1, current_class, @1, true);
549 symbol_type_set ($1, current_type, @1);
550 symbol_make_alias ($1, $2, @$);
552 | id INT string_as_id
554 symbol_class_set ($1, current_class, @1, true);
555 symbol_type_set ($1, current_type, @1);
556 symbol_user_token_number_set ($1, $2, @2);
557 symbol_make_alias ($1, $3, @$);
561 /* One or more symbol definitions. */
564 | symbol_defs.1 symbol_def
568 /*------------------------------------------.
569 | The grammar section: between the two %%. |
570 `------------------------------------------*/
573 rules_or_grammar_declaration
574 | grammar rules_or_grammar_declaration
577 /* As a Bison extension, one can use the grammar declarations in the
578 body of the grammar. */
579 rules_or_grammar_declaration:
581 | grammar_declaration ";"
589 id_colon named_ref.opt { current_lhs ($1, @1, $2); } rhses.1
591 /* Free the current lhs. */
592 current_lhs (0, @1, 0);
597 rhs { grammar_current_rule_end (@1); }
598 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
602 %token PERCENT_EMPTY "%empty";
605 { grammar_current_rule_begin (current_lhs_symbol, current_lhs_location,
606 current_lhs_named_ref); }
607 | rhs symbol named_ref.opt
608 { grammar_current_rule_symbol_append ($2, @2, $3); }
609 | rhs "{...}" named_ref.opt
610 { grammar_current_rule_action_append ($2, @2, $3, false); }
612 { grammar_current_rule_action_append ($2, @2, NULL, true); }
614 { grammar_current_rule_empty_set (@2); }
616 { grammar_current_rule_prec_set ($3, @3); }
618 { grammar_current_rule_dprec_set ($3, @3); }
620 { grammar_current_rule_merge_set ($3, @3); }
625 | BRACKETED_ID { $$ = named_ref_new($1, @1); }
628 /*---------------------.
629 | variable and value. |
630 `---------------------*/
632 /* The STRING form of variable is deprecated and is not M4-friendly.
633 For example, M4 fails for '%define "[" "value"'. */
636 | STRING { $$ = uniqstr_new ($1); }
639 /* Some content or empty by default. */
640 %code requires {#include "muscle-tab.h"};
654 case muscle_code: fprintf (yyo, "{%s}", $$.chars); break;
655 case muscle_keyword: fprintf (yyo, "%s", $$.chars); break;
656 case muscle_string: fprintf (yyo, "\"%s\"", $$.chars); break;
661 %empty { $$.kind = muscle_keyword; $$.chars = ""; }
662 | ID { $$.kind = muscle_keyword; $$.chars = $1; }
663 | STRING { $$.kind = muscle_string; $$.chars = $1; }
664 | braceless { $$.kind = muscle_code; $$.chars = $1; }
675 code_props plain_code;
676 $1[strlen ($1) - 1] = '\0';
677 code_props_plain_init (&plain_code, $1+1, @1);
678 code_props_translate_code (&plain_code);
679 gram_scanner_last_string_free ();
680 $$ = plain_code.code;
689 /* Identifiers are returned as uniqstr values by the scanner.
690 Depending on their use, we may need to make them genuine symbols. */
694 { $$ = symbol_from_uniqstr ($1, @1); }
697 $$ = symbol_get (char_name ($1), @1);
698 symbol_class_set ($$, token_sym, @1, false);
699 symbol_user_token_number_set ($$, $1, @1);
704 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
713 /* A string used as an ID: quote it. */
717 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
718 symbol_class_set ($$, token_sym, @1, false);
726 code_props plain_code;
727 code_props_plain_init (&plain_code, $2, @2);
728 code_props_translate_code (&plain_code);
729 gram_scanner_last_string_free ();
730 muscle_code_grow ("epilogue", plain_code.code, @2);
731 code_scanner_last_string_free ();
737 /* Return the location of the left-hand side of a rule whose
738 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
739 the right-hand side, and return an empty location equal to the end
740 boundary of RHS[0] if the right-hand side is empty. */
743 lloc_default (YYLTYPE const *rhs, int n)
748 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
749 The bug is fixed in 7.4.2m, but play it safe for now. */
750 loc.start = rhs[n].end;
751 loc.end = rhs[n].end;
753 /* Ignore empty nonterminals the start of the right-hand side.
754 Do not bother to ignore them at the end of the right-hand side,
755 since empty nonterminals have the same end as their predecessors. */
756 for (i = 1; i <= n; i++)
757 if (! equal_boundaries (rhs[i].start, rhs[i].end))
759 loc.start = rhs[i].start;
768 add_param (param_type type, char *decl, location loc)
770 static char const alphanum[26 + 26 + 1 + 10] =
771 "abcdefghijklmnopqrstuvwxyz"
772 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
776 char const *name_start = NULL;
779 /* Stop on last actual character. */
780 for (p = decl; p[1]; p++)
782 || ! memchr (alphanum, p[-1], sizeof alphanum))
783 && memchr (alphanum, p[0], sizeof alphanum - 10))
786 /* Strip the surrounding '{' and '}', and any blanks just inside
789 while (c_isspace ((unsigned char) *p))
793 while (c_isspace ((unsigned char) *decl))
798 complain (&loc, complaint, _("missing identifier in parameter declaration"));
801 char *name = xmemdup0 (name_start, strspn (name_start, alphanum));
802 if (type & param_lex)
803 muscle_pair_list_grow ("lex_param", decl, name);
804 if (type & param_parse)
805 muscle_pair_list_grow ("parse_param", decl, name);
809 gram_scanner_last_string_free ();
814 version_check (location const *loc, char const *version)
816 if (strverscmp (version, PACKAGE_VERSION) > 0)
818 complain (loc, complaint, "require bison %s, but have %s",
819 version, PACKAGE_VERSION);
825 gram_error (location const *loc, char const *msg)
827 complain (loc, complaint, "%s", msg);
831 token_name (int type)
833 return yytname[YYTRANSLATE (type)];
844 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
845 return quotearg_style (escape_quoting_style, buf);
851 current_lhs (symbol *sym, location loc, named_ref *ref)
853 current_lhs_symbol = sym;
854 current_lhs_location = loc;
855 /* In order to simplify memory management, named references for lhs
856 are always assigned by deep copy into the current symbol_list
857 node. This is because a single named-ref in the grammar may
858 result in several uses when the user factors lhs between several
859 rules using "|". Therefore free the parser's original copy. */
860 free (current_lhs_named_ref);
861 current_lhs_named_ref = ref;