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"
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);
56 static int current_prec = 0;
57 static location current_lhs_location;
58 static named_ref *current_lhs_named_ref;
59 static symbol *current_lhs_symbol;
60 static symbol_class current_class = unknown_sym;
61 static uniqstr current_type = NULL;
63 /** Set the new current left-hand side symbol, possibly common
64 * to several right-hand side parts of rule.
68 current_lhs(symbol *sym, location loc, named_ref *ref)
70 current_lhs_symbol = sym;
71 current_lhs_location = loc;
72 /* In order to simplify memory management, named references for lhs
73 are always assigned by deep copy into the current symbol_list
74 node. This is because a single named-ref in the grammar may
75 result in several uses when the user factors lhs between several
76 rules using "|". Therefore free the parser's original copy. */
77 free (current_lhs_named_ref);
78 current_lhs_named_ref = ref;
81 #define YYTYPE_INT16 int_fast16_t
82 #define YYTYPE_INT8 int_fast8_t
83 #define YYTYPE_UINT16 uint_fast16_t
84 #define YYTYPE_UINT8 uint_fast8_t
92 %define parse.error "verbose"
93 %define parse.lac full
99 /* Bison's grammar can initial empty locations, hence a default
100 location is needed. */
101 boundary_set (&@$.start, current_file, 1, 1);
102 boundary_set (&@$.end, current_file, 1, 1);
111 named_ref *named_ref;
115 unsigned char character;
118 /* Define the tokens together with their human representation. */
119 %token GRAM_EOF 0 "end of file"
120 %token STRING "string"
123 %token PERCENT_TOKEN "%token"
124 %token PERCENT_NTERM "%nterm"
126 %token PERCENT_TYPE "%type"
127 %token PERCENT_DESTRUCTOR "%destructor"
128 %token PERCENT_PRINTER "%printer"
130 %token PERCENT_LEFT "%left"
131 %token PERCENT_RIGHT "%right"
132 %token PERCENT_NONASSOC "%nonassoc"
133 %token PERCENT_PRECEDENCE "%precedence"
135 %token PERCENT_PREC "%prec"
136 %token PERCENT_DPREC "%dprec"
137 %token PERCENT_MERGE "%merge"
139 /*----------------------.
140 | Global Declarations. |
141 `----------------------*/
145 PERCENT_DEFAULT_PREC "%default-prec"
146 PERCENT_DEFINE "%define"
147 PERCENT_DEFINES "%defines"
148 PERCENT_ERROR_VERBOSE "%error-verbose"
149 PERCENT_EXPECT "%expect"
150 PERCENT_EXPECT_RR "%expect-rr"
151 PERCENT_FLAG "%<flag>"
152 PERCENT_FILE_PREFIX "%file-prefix"
153 PERCENT_GLR_PARSER "%glr-parser"
154 PERCENT_INITIAL_ACTION "%initial-action"
155 PERCENT_LANGUAGE "%language"
156 PERCENT_NAME_PREFIX "%name-prefix"
157 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
158 PERCENT_NO_LINES "%no-lines"
159 PERCENT_NONDETERMINISTIC_PARSER
160 "%nondeterministic-parser"
161 PERCENT_OUTPUT "%output"
162 PERCENT_REQUIRE "%require"
163 PERCENT_SKELETON "%skeleton"
164 PERCENT_START "%start"
165 PERCENT_TOKEN_TABLE "%token-table"
166 PERCENT_VERBOSE "%verbose"
170 %token BRACED_CODE "{...}"
171 %token BRACED_PREDICATE "%?{...}"
172 %token BRACKETED_ID "[identifier]"
174 %token EPILOGUE "epilogue"
176 %token ID "identifier"
177 %token ID_COLON "identifier:"
178 %token PERCENT_PERCENT "%%"
180 %token PROLOGUE "%{...%}"
186 %type <character> CHAR
187 %printer { fputs (char_name ($$), stderr); } CHAR
189 /* braceless is not to be used for rule or symbol actions, as it
190 calls code_props_plain_init. */
191 %type <chars> STRING "%{...%}" EPILOGUE braceless content.opt
192 %type <code> "{...}" "%?{...}"
193 %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); }
195 %printer { fprintf (stderr, "{\n%s\n}", $$); }
196 braceless content.opt "{...}" "%{...%}" EPILOGUE
198 %type <uniqstr> BRACKETED_ID ID ID_COLON PERCENT_FLAG TAG variable
199 %printer { fputs ($$, stderr); } <uniqstr>
200 %printer { fprintf (stderr, "[%s]", $$); } BRACKETED_ID
201 %printer { fprintf (stderr, "%s:", $$); } ID_COLON
202 %printer { fprintf (stderr, "%%%s", $$); } PERCENT_FLAG
203 %printer { fprintf (stderr, "<%s>", $$); } TAG
206 %printer { fprintf (stderr, "%d", $$); } <integer>
208 %type <symbol> id id_colon string_as_id symbol symbol.prec
209 %printer { fprintf (stderr, "%s", $$->tag); } <symbol>
210 %printer { fprintf (stderr, "%s:", $$->tag); } id_colon
212 %type <assoc> precedence_declarator
213 %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item
214 %type <named_ref> named_ref.opt
227 param_parse = 1 << 1,
228 param_both = param_lex | param_parse
234 /** Add a lex-param and/or a parse-param.
236 * \param type where to push this formal argument.
237 * \param decl the formal argument. Destroyed.
238 * \param loc the location in the source.
240 static void add_param (param_type type, char *decl, location loc);
241 static param_type current_param = param_none;
247 %token <param> PERCENT_PARAM "%param";
252 #define CASE(In, Out) \
253 case param_ ## In: fputs ("%" #Out, stderr); break
254 CASE(lex, lex-param);
255 CASE(parse, parse-param);
258 case param_none: aver (false); break;
269 prologue_declarations "%%" grammar epilogue.opt
273 /*------------------------------------.
274 | Declarations: before the first %%. |
275 `------------------------------------*/
277 prologue_declarations:
279 | prologue_declarations prologue_declaration
282 prologue_declaration:
286 code_props plain_code;
287 code_props_plain_init (&plain_code, $1, @1);
288 code_props_translate_code (&plain_code);
289 gram_scanner_last_string_free ();
290 muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
291 plain_code.code, @1);
292 code_scanner_last_string_free ();
296 muscle_percent_define_ensure ($1, @1, true);
298 | "%define" variable content.opt
300 muscle_percent_define_insert ($2, @2, $3,
301 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
303 | "%defines" { defines_flag = true; }
307 spec_defines_file = xstrdup ($2);
311 muscle_percent_define_insert ("parse.error", @1, "verbose",
312 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
314 | "%expect" INT { expected_sr_conflicts = $2; }
315 | "%expect-rr" INT { expected_rr_conflicts = $2; }
316 | "%file-prefix" STRING { spec_file_prefix = $2; }
317 | "%file-prefix" "=" STRING { spec_file_prefix = $3; } /* deprecated */
320 nondeterministic_parser = true;
323 | "%initial-action" "{...}"
326 code_props_symbol_action_init (&action, $2, @2);
327 code_props_translate_code (&action);
328 gram_scanner_last_string_free ();
329 muscle_code_grow ("initial_action", action.code, @2);
330 code_scanner_last_string_free ();
332 | "%language" STRING { language_argmatch ($2, grammar_prio, @1); }
333 | "%name-prefix" STRING { spec_name_prefix = $2; }
334 | "%name-prefix" "=" STRING { spec_name_prefix = $3; } /* deprecated */
335 | "%no-lines" { no_lines_flag = true; }
336 | "%nondeterministic-parser" { nondeterministic_parser = true; }
337 | "%output" STRING { spec_outfile = $2; }
338 | "%output" "=" STRING { spec_outfile = $3; } /* deprecated */
339 | "%param" { current_param = $1; } params { current_param = param_none; }
340 | "%require" STRING { version_check (&@2, $2); }
343 char const *skeleton_user = $2;
344 if (mbschr (skeleton_user, '/'))
346 size_t dir_length = strlen (current_file);
347 char *skeleton_build;
348 while (dir_length && current_file[dir_length - 1] != '/')
350 while (dir_length && current_file[dir_length - 1] == '/')
353 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
356 strncpy (skeleton_build, current_file, dir_length);
357 skeleton_build[dir_length++] = '/';
359 strcpy (skeleton_build + dir_length, skeleton_user);
360 skeleton_user = uniqstr_new (skeleton_build);
361 free (skeleton_build);
363 skeleton_arg (skeleton_user, grammar_prio, @1);
365 | "%token-table" { token_table_flag = true; }
366 | "%verbose" { report_flag |= report_states; }
367 | "%yacc" { yacc_flag = true; }
368 | /*FIXME: Err? What is this horror doing here? */ ";"
372 params "{...}" { add_param (current_param, $2, @2); }
373 | "{...}" { add_param (current_param, $1, @1); }
377 /*----------------------.
378 | grammar_declaration. |
379 `----------------------*/
382 precedence_declaration
386 grammar_start_symbol_set ($2, @2);
388 | "%destructor" "{...}" generic_symlist
391 for (list = $3; list; list = list->next)
392 symbol_list_destructor_set (list, $2, @2);
393 symbol_list_free ($3);
395 | "%printer" "{...}" generic_symlist
398 for (list = $3; list; list = list->next)
399 symbol_list_printer_set (list, $2, @2);
400 symbol_list_free ($3);
408 default_prec = false;
412 /* Do not invoke muscle_percent_code_grow here since it invokes
413 muscle_user_name_list_grow. */
414 muscle_code_grow ("percent_code()", $2, @2);
415 code_scanner_last_string_free ();
417 | "%code" ID braceless
419 muscle_percent_code_grow ($2, @2, $3, @3);
420 code_scanner_last_string_free ();
429 %token PERCENT_UNION "%union";
433 | ID { muscle_code_grow ("union_name", $1, @1); }
437 "%union" union_name braceless
440 muscle_code_grow ("stype", $3, @3);
441 code_scanner_last_string_free ();
449 "%nterm" { current_class = nterm_sym; } symbol_defs.1
451 current_class = unknown_sym;
454 | "%token" { current_class = token_sym; } symbol_defs.1
456 current_class = unknown_sym;
459 | "%type" TAG symbols.1
463 for (list = $3; list; list = list->next)
464 symbol_type_set (list->content.sym, $2, @2);
465 symbol_list_free ($3);
469 precedence_declaration:
470 precedence_declarator tag.opt symbols.prec
474 for (list = $3; list; list = list->next)
476 symbol_type_set (list->content.sym, current_type, @2);
477 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
479 symbol_list_free ($3);
484 precedence_declarator:
485 "%left" { $$ = left_assoc; }
486 | "%right" { $$ = right_assoc; }
487 | "%nonassoc" { $$ = non_assoc; }
488 | "%precedence" { $$ = precedence_assoc; }
492 /* Nothing. */ { current_type = NULL; }
493 | TAG { current_type = $1; tag_seen = true; }
496 /* Just like symbols.1 but accept INT for the sake of POSIX. */
499 { $$ = symbol_list_sym_new ($1, @1); }
500 | symbols.prec symbol.prec
501 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
506 | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
509 /* One or more symbols to be %typed. */
512 { $$ = symbol_list_sym_new ($1, @1); }
514 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
518 generic_symlist_item { $$ = $1; }
519 | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
522 generic_symlist_item:
523 symbol { $$ = symbol_list_sym_new ($1, @1); }
524 | TAG { $$ = symbol_list_type_new ($1, @1); }
525 | "<*>" { $$ = symbol_list_default_tagged_new (@1); }
526 | "<>" { $$ = symbol_list_default_tagless_new (@1); }
529 /* One token definition. */
538 symbol_class_set ($1, current_class, @1, true);
539 symbol_type_set ($1, current_type, @1);
543 symbol_class_set ($1, current_class, @1, true);
544 symbol_type_set ($1, current_type, @1);
545 symbol_user_token_number_set ($1, $2, @2);
549 symbol_class_set ($1, current_class, @1, true);
550 symbol_type_set ($1, current_type, @1);
551 symbol_make_alias ($1, $2, @$);
553 | id INT string_as_id
555 symbol_class_set ($1, current_class, @1, true);
556 symbol_type_set ($1, current_type, @1);
557 symbol_user_token_number_set ($1, $2, @2);
558 symbol_make_alias ($1, $3, @$);
562 /* One or more symbol definitions. */
565 | symbol_defs.1 symbol_def
569 /*------------------------------------------.
570 | The grammar section: between the two %%. |
571 `------------------------------------------*/
574 rules_or_grammar_declaration
575 | grammar rules_or_grammar_declaration
578 /* As a Bison extension, one can use the grammar declarations in the
579 body of the grammar. */
580 rules_or_grammar_declaration:
582 | grammar_declaration ";"
590 id_colon named_ref.opt { current_lhs ($1, @1, $2); } rhses.1
592 /* Free the current lhs. */
593 current_lhs (0, @1, 0);
598 rhs { grammar_current_rule_end (@1); }
599 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
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_prec_set ($3, @3); }
616 { grammar_current_rule_dprec_set ($3, @3); }
618 { grammar_current_rule_merge_set ($3, @3); }
622 /* Nothing. */ { $$ = 0; }
624 BRACKETED_ID { $$ = named_ref_new($1, @1); }
627 /*---------------------------.
628 | variable and content.opt. |
629 `---------------------------*/
631 /* The STRING form of variable is deprecated and is not M4-friendly.
632 For example, M4 fails for `%define "[" "value"'. */
635 | STRING { $$ = uniqstr_new ($1); }
638 /* Some content or empty by default. */
640 /* Nothing. */ { $$ = ""; }
653 code_props plain_code;
654 $1[strlen ($1) - 1] = '\n';
655 code_props_plain_init (&plain_code, $1+1, @1);
656 code_props_translate_code (&plain_code);
657 gram_scanner_last_string_free ();
658 $$ = plain_code.code;
667 /* Identifiers are returned as uniqstr values by the scanner.
668 Depending on their use, we may need to make them genuine symbols. */
672 { $$ = symbol_from_uniqstr ($1, @1); }
675 $$ = symbol_get (char_name ($1), @1);
676 symbol_class_set ($$, token_sym, @1, false);
677 symbol_user_token_number_set ($$, $1, @1);
682 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
691 /* A string used as an ID: quote it. */
695 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
696 symbol_class_set ($$, token_sym, @1, false);
704 code_props plain_code;
705 code_props_plain_init (&plain_code, $2, @2);
706 code_props_translate_code (&plain_code);
707 gram_scanner_last_string_free ();
708 muscle_code_grow ("epilogue", plain_code.code, @2);
709 code_scanner_last_string_free ();
716 /* Return the location of the left-hand side of a rule whose
717 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
718 the right-hand side, and return an empty location equal to the end
719 boundary of RHS[0] if the right-hand side is empty. */
722 lloc_default (YYLTYPE const *rhs, int n)
727 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
728 The bug is fixed in 7.4.2m, but play it safe for now. */
729 loc.start = rhs[n].end;
730 loc.end = rhs[n].end;
732 /* Ignore empty nonterminals the start of the right-hand side.
733 Do not bother to ignore them at the end of the right-hand side,
734 since empty nonterminals have the same end as their predecessors. */
735 for (i = 1; i <= n; i++)
736 if (! equal_boundaries (rhs[i].start, rhs[i].end))
738 loc.start = rhs[i].start;
747 add_param (param_type type, char *decl, location loc)
749 static char const alphanum[26 + 26 + 1 + 10] =
750 "abcdefghijklmnopqrstuvwxyz"
751 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
755 char const *name_start = NULL;
758 /* Stop on last actual character. */
759 for (p = decl; p[1]; p++)
761 || ! memchr (alphanum, p[-1], sizeof alphanum))
762 && memchr (alphanum, p[0], sizeof alphanum - 10))
765 /* Strip the surrounding '{' and '}', and any blanks just inside
767 while (*--p == ' ' || *p == '\t')
770 while (*++decl == ' ' || *decl == '\t')
775 complain_at (loc, _("missing identifier in parameter declaration"));
782 memchr (alphanum, name_start[name_len], sizeof alphanum);
786 name = xmalloc (name_len + 1);
787 memcpy (name, name_start, name_len);
788 name[name_len] = '\0';
789 if (type & param_lex)
790 muscle_pair_list_grow ("lex_param", decl, name);
791 if (type & param_parse)
792 muscle_pair_list_grow ("parse_param", decl, name);
796 gram_scanner_last_string_free ();
801 version_check (location const *loc, char const *version)
803 if (strverscmp (version, PACKAGE_VERSION) > 0)
805 complain_at (*loc, "require bison %s, but have %s",
806 version, PACKAGE_VERSION);
812 gram_error (location const *loc, char const *msg)
814 complain_at (*loc, "%s", msg);
818 token_name (int type)
820 return yytname[YYTRANSLATE (type)];
831 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
832 return quotearg_style (escape_quoting_style, buf);