1 %{/* Bison Grammar Parser -*- C -*-
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "conflicts.h"
29 #include "muscle-tab.h"
30 #include "named-ref.h"
34 #include "scan-gram.h"
35 #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);
57 static int current_prec = 0;
58 static location current_lhs_location;
59 static named_ref *current_lhs_named_ref;
60 static symbol *current_lhs;
61 static symbol_class current_class = unknown_sym;
62 static uniqstr current_type = NULL;
64 #define YYTYPE_INT16 int_fast16_t
65 #define YYTYPE_INT8 int_fast8_t
66 #define YYTYPE_UINT16 uint_fast16_t
67 #define YYTYPE_UINT8 uint_fast8_t
75 %define parse.error "verbose"
76 %define parse.lac full
82 /* Bison's grammar can initial empty locations, hence a default
83 location is needed. */
84 boundary_set (&@$.start, current_file, 1, 1);
85 boundary_set (&@$.end, current_file, 1, 1);
98 unsigned char character;
101 /* Define the tokens together with their human representation. */
102 %token GRAM_EOF 0 "end of file"
103 %token STRING "string"
106 %token PERCENT_TOKEN "%token"
107 %token PERCENT_NTERM "%nterm"
109 %token PERCENT_TYPE "%type"
110 %token PERCENT_DESTRUCTOR "%destructor"
111 %token PERCENT_PRINTER "%printer"
113 %token PERCENT_LEFT "%left"
114 %token PERCENT_RIGHT "%right"
115 %token PERCENT_NONASSOC "%nonassoc"
116 %token PERCENT_PRECEDENCE "%precedence"
118 %token PERCENT_PREC "%prec"
119 %token PERCENT_DPREC "%dprec"
120 %token PERCENT_MERGE "%merge"
122 /*----------------------.
123 | Global Declarations. |
124 `----------------------*/
128 PERCENT_DEFAULT_PREC "%default-prec"
129 PERCENT_DEFINE "%define"
130 PERCENT_DEFINES "%defines"
131 PERCENT_ERROR_VERBOSE "%error-verbose"
132 PERCENT_EXPECT "%expect"
133 PERCENT_EXPECT_RR "%expect-rr"
134 PERCENT_FLAG "%<flag>"
135 PERCENT_FILE_PREFIX "%file-prefix"
136 PERCENT_GLR_PARSER "%glr-parser"
137 PERCENT_INITIAL_ACTION "%initial-action"
138 PERCENT_LANGUAGE "%language"
139 PERCENT_NAME_PREFIX "%name-prefix"
140 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
141 PERCENT_NO_LINES "%no-lines"
142 PERCENT_NONDETERMINISTIC_PARSER
143 "%nondeterministic-parser"
144 PERCENT_OUTPUT "%output"
145 PERCENT_REQUIRE "%require"
146 PERCENT_SKELETON "%skeleton"
147 PERCENT_START "%start"
148 PERCENT_TOKEN_TABLE "%token-table"
149 PERCENT_VERBOSE "%verbose"
153 %token BRACED_CODE "{...}"
154 %token BRACED_PREDICATE "%?{...}"
155 %token BRACKETED_ID "[identifier]"
157 %token EPILOGUE "epilogue"
159 %token ID "identifier"
160 %token ID_COLON "identifier:"
161 %token PERCENT_PERCENT "%%"
163 %token PROLOGUE "%{...%}"
169 %type <character> CHAR
170 %printer { fputs (char_name ($$), stderr); } CHAR
172 /* braceless is not to be used for rule or symbol actions, as it
173 calls code_props_plain_init. */
174 %type <chars> STRING "%{...%}" EPILOGUE braceless content.opt
175 %type <code> "{...}" "%?{...}"
176 %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); }
178 %printer { fprintf (stderr, "{\n%s\n}", $$); }
179 braceless content.opt "{...}" "%{...%}" EPILOGUE
181 %type <uniqstr> BRACKETED_ID ID ID_COLON PERCENT_FLAG TAG variable
182 %printer { fputs ($$, stderr); } <uniqstr>
183 %printer { fprintf (stderr, "[%s]", $$); } BRACKETED_ID
184 %printer { fprintf (stderr, "%s:", $$); } ID_COLON
185 %printer { fprintf (stderr, "%%%s", $$); } PERCENT_FLAG
186 %printer { fprintf (stderr, "<%s>", $$); } TAG
189 %printer { fprintf (stderr, "%d", $$); } <integer>
191 %type <symbol> id id_colon string_as_id symbol symbol.prec
192 %printer { fprintf (stderr, "%s", $$->tag); } <symbol>
193 %printer { fprintf (stderr, "%s:", $$->tag); } id_colon
195 %type <assoc> precedence_declarator
196 %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item
197 %type <named_ref> named_ref.opt
210 param_parse = 1 << 1,
211 param_both = param_lex | param_parse
217 /** Add a lex-param and/or a parse-param.
219 * \param type where to push this formal argument.
220 * \param decl the formal argument. Destroyed.
221 * \param loc the location in the source.
223 static void add_param (param_type type, char *decl, location loc);
224 static param_type current_param = param_none;
230 %token <param> PERCENT_PARAM "%param";
235 #define CASE(In, Out) \
236 case param_ ## In: fputs ("%" #Out, stderr); 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 content.opt
283 muscle_percent_define_insert ($2, @2, $3,
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, "verbose",
295 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
297 | "%expect" INT { expected_sr_conflicts = $2; }
298 | "%expect-rr" INT { expected_rr_conflicts = $2; }
299 | "%file-prefix" STRING { spec_file_prefix = $2; }
300 | "%file-prefix" "=" STRING { spec_file_prefix = $3; } /* deprecated */
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 | "%name-prefix" "=" STRING { spec_name_prefix = $3; } /* deprecated */
318 | "%no-lines" { no_lines_flag = true; }
319 | "%nondeterministic-parser" { nondeterministic_parser = true; }
320 | "%output" STRING { spec_outfile = $2; }
321 | "%output" "=" STRING { spec_outfile = $3; } /* deprecated */
322 | "%param" { current_param = $1; } params { current_param = param_none; }
323 | "%require" STRING { version_check (&@2, $2); }
326 char const *skeleton_user = $2;
327 if (strchr (skeleton_user, '/'))
329 size_t dir_length = strlen (current_file);
330 char *skeleton_build;
331 while (dir_length && current_file[dir_length - 1] != '/')
333 while (dir_length && current_file[dir_length - 1] == '/')
336 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
339 strncpy (skeleton_build, current_file, dir_length);
340 skeleton_build[dir_length++] = '/';
342 strcpy (skeleton_build + dir_length, skeleton_user);
343 skeleton_user = uniqstr_new (skeleton_build);
344 free (skeleton_build);
346 skeleton_arg (skeleton_user, grammar_prio, @1);
348 | "%token-table" { token_table_flag = true; }
349 | "%verbose" { report_flag |= report_states; }
350 | "%yacc" { yacc_flag = true; }
351 | /*FIXME: Err? What is this horror doing here? */ ";"
355 params "{...}" { add_param (current_param, $2, @2); }
356 | "{...}" { add_param (current_param, $1, @1); }
360 /*----------------------.
361 | grammar_declaration. |
362 `----------------------*/
365 precedence_declaration
369 grammar_start_symbol_set ($2, @2);
371 | "%destructor" "{...}" generic_symlist
374 for (list = $3; list; list = list->next)
375 symbol_list_destructor_set (list, $2, @2);
376 symbol_list_free ($3);
378 | "%printer" "{...}" generic_symlist
381 for (list = $3; list; list = list->next)
382 symbol_list_printer_set (list, $2, @2);
383 symbol_list_free ($3);
391 default_prec = false;
395 /* Do not invoke muscle_percent_code_grow here since it invokes
396 muscle_user_name_list_grow. */
397 muscle_code_grow ("percent_code()", $2, @2);
398 code_scanner_last_string_free ();
400 | "%code" ID braceless
402 muscle_percent_code_grow ($2, @2, $3, @3);
403 code_scanner_last_string_free ();
412 %token PERCENT_UNION "%union";
416 | ID { muscle_code_grow ("union_name", $1, @1); }
420 "%union" union_name braceless
423 muscle_code_grow ("stype", $3, @3);
424 code_scanner_last_string_free ();
432 "%nterm" { current_class = nterm_sym; } symbol_defs.1
434 current_class = unknown_sym;
437 | "%token" { current_class = token_sym; } symbol_defs.1
439 current_class = unknown_sym;
442 | "%type" TAG symbols.1
446 for (list = $3; list; list = list->next)
447 symbol_type_set (list->content.sym, $2, @2);
448 symbol_list_free ($3);
452 precedence_declaration:
453 precedence_declarator tag.opt symbols.prec
457 for (list = $3; list; list = list->next)
459 symbol_type_set (list->content.sym, current_type, @2);
460 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
462 symbol_list_free ($3);
467 precedence_declarator:
468 "%left" { $$ = left_assoc; }
469 | "%right" { $$ = right_assoc; }
470 | "%nonassoc" { $$ = non_assoc; }
471 | "%precedence" { $$ = precedence_assoc; }
475 /* Nothing. */ { current_type = NULL; }
476 | TAG { current_type = $1; tag_seen = true; }
479 /* Just like symbols.1 but accept INT for the sake of POSIX. */
482 { $$ = symbol_list_sym_new ($1, @1); }
483 | symbols.prec symbol.prec
484 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
489 | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
492 /* One or more symbols to be %typed. */
495 { $$ = symbol_list_sym_new ($1, @1); }
497 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
501 generic_symlist_item { $$ = $1; }
502 | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
505 generic_symlist_item:
506 symbol { $$ = symbol_list_sym_new ($1, @1); }
507 | TAG { $$ = symbol_list_type_new ($1, @1); }
508 | "<*>" { $$ = symbol_list_default_tagged_new (@1); }
509 | "<>" { $$ = symbol_list_default_tagless_new (@1); }
512 /* One token definition. */
521 symbol_class_set ($1, current_class, @1, true);
522 symbol_type_set ($1, current_type, @1);
526 symbol_class_set ($1, current_class, @1, true);
527 symbol_type_set ($1, current_type, @1);
528 symbol_user_token_number_set ($1, $2, @2);
532 symbol_class_set ($1, current_class, @1, true);
533 symbol_type_set ($1, current_type, @1);
534 symbol_make_alias ($1, $2, @$);
536 | id INT string_as_id
538 symbol_class_set ($1, current_class, @1, true);
539 symbol_type_set ($1, current_type, @1);
540 symbol_user_token_number_set ($1, $2, @2);
541 symbol_make_alias ($1, $3, @$);
545 /* One or more symbol definitions. */
548 | symbol_defs.1 symbol_def
552 /*------------------------------------------.
553 | The grammar section: between the two %%. |
554 `------------------------------------------*/
557 rules_or_grammar_declaration
558 | grammar rules_or_grammar_declaration
561 /* As a Bison extension, one can use the grammar declarations in the
562 body of the grammar. */
563 rules_or_grammar_declaration:
565 | grammar_declaration ";"
573 id_colon named_ref.opt { current_lhs = $1; current_lhs_location = @1;
574 current_lhs_named_ref = $2; } rhses.1
578 rhs { grammar_current_rule_end (@1); }
579 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
585 { grammar_current_rule_begin (current_lhs, current_lhs_location,
586 current_lhs_named_ref); }
587 | rhs symbol named_ref.opt
588 { grammar_current_rule_symbol_append ($2, @2, $3); }
589 | rhs "{...}" named_ref.opt
590 { grammar_current_rule_action_append ($2, @2, $3, false); }
592 { grammar_current_rule_action_append ($2, @2, NULL, true); }
594 { grammar_current_rule_prec_set ($3, @3); }
596 { grammar_current_rule_dprec_set ($3, @3); }
598 { grammar_current_rule_merge_set ($3, @3); }
602 /* Nothing. */ { $$ = 0; }
604 BRACKETED_ID { $$ = named_ref_new($1, @1); }
607 /*---------------------------.
608 | variable and content.opt. |
609 `---------------------------*/
611 /* The STRING form of variable is deprecated and is not M4-friendly.
612 For example, M4 fails for `%define "[" "value"'. */
615 | STRING { $$ = uniqstr_new ($1); }
618 /* Some content or empty by default. */
620 /* Nothing. */ { $$ = ""; }
633 code_props plain_code;
634 $1[strlen ($1) - 1] = '\n';
635 code_props_plain_init (&plain_code, $1+1, @1);
636 code_props_translate_code (&plain_code);
637 gram_scanner_last_string_free ();
638 $$ = plain_code.code;
647 /* Identifiers are returned as uniqstr values by the scanner.
648 Depending on their use, we may need to make them genuine symbols. */
652 { $$ = symbol_from_uniqstr ($1, @1); }
655 $$ = symbol_get (char_name ($1), @1);
656 symbol_class_set ($$, token_sym, @1, false);
657 symbol_user_token_number_set ($$, $1, @1);
662 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
671 /* A string used as an ID: quote it. */
675 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
676 symbol_class_set ($$, token_sym, @1, false);
684 code_props plain_code;
685 code_props_plain_init (&plain_code, $2, @2);
686 code_props_translate_code (&plain_code);
687 gram_scanner_last_string_free ();
688 muscle_code_grow ("epilogue", plain_code.code, @2);
689 code_scanner_last_string_free ();
696 /* Return the location of the left-hand side of a rule whose
697 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
698 the right-hand side, and return an empty location equal to the end
699 boundary of RHS[0] if the right-hand side is empty. */
702 lloc_default (YYLTYPE const *rhs, int n)
707 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
708 The bug is fixed in 7.4.2m, but play it safe for now. */
709 loc.start = rhs[n].end;
710 loc.end = rhs[n].end;
712 /* Ignore empty nonterminals the start of the the right-hand side.
713 Do not bother to ignore them at the end of the right-hand side,
714 since empty nonterminals have the same end as their predecessors. */
715 for (i = 1; i <= n; i++)
716 if (! equal_boundaries (rhs[i].start, rhs[i].end))
718 loc.start = rhs[i].start;
727 add_param (param_type type, char *decl, location loc)
729 static char const alphanum[26 + 26 + 1 + 10] =
730 "abcdefghijklmnopqrstuvwxyz"
731 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
735 char const *name_start = NULL;
738 /* Stop on last actual character. */
739 for (p = decl; p[1]; p++)
741 || ! memchr (alphanum, p[-1], sizeof alphanum))
742 && memchr (alphanum, p[0], sizeof alphanum - 10))
745 /* Strip the surrounding '{' and '}', and any blanks just inside
747 while (*--p == ' ' || *p == '\t')
750 while (*++decl == ' ' || *decl == '\t')
755 complain_at (loc, _("missing identifier in parameter declaration"));
762 memchr (alphanum, name_start[name_len], sizeof alphanum);
766 name = xmalloc (name_len + 1);
767 memcpy (name, name_start, name_len);
768 name[name_len] = '\0';
769 if (type & param_lex)
770 muscle_pair_list_grow ("lex_param", decl, name);
771 if (type & param_parse)
772 muscle_pair_list_grow ("parse_param", decl, name);
776 gram_scanner_last_string_free ();
781 version_check (location const *loc, char const *version)
783 if (strverscmp (version, PACKAGE_VERSION) > 0)
785 complain_at (*loc, "require bison %s, but have %s",
786 version, PACKAGE_VERSION);
792 gram_error (location const *loc, char const *msg)
794 complain_at (*loc, "%s", msg);
798 token_name (int type)
800 return yytname[YYTRANSLATE (type)];
811 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
812 return quotearg_style (escape_quoting_style, buf);