]> git.saurik.com Git - bison.git/blame - src/parse-gram.y
gnulib: update
[bison.git] / src / parse-gram.y
CommitLineData
3f21a394 1/* Bison Grammar Parser -*- C -*-
a737b216 2
3209eb1c 3 Copyright (C) 2002-2015 Free Software Foundation, Inc.
e9955c83
AD
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
f16b0819 7 This program is free software: you can redistribute it and/or modify
e9955c83 8 it under the terms of the GNU General Public License as published by
f16b0819 9 the Free Software Foundation, either version 3 of the License, or
e9955c83
AD
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
f16b0819 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
e9955c83 19
3f21a394
AD
20%code requires
21{
22 #include "symlist.h"
23 #include "symtab.h"
24}
e9955c83 25
89ff1b5e
AD
26%code top
27{
28 /* On column 0 to please syntax-check. */
29#include <config.h>
30}
31
a7706735
AD
32%code
33{
3f21a394
AD
34 #include "system.h"
35
36 #include "c-ctype.h"
37 #include "complain.h"
38 #include "conflicts.h"
39 #include "files.h"
40 #include "getargs.h"
41 #include "gram.h"
42 #include "named-ref.h"
43 #include "quotearg.h"
44 #include "reader.h"
45 #include "scan-gram.h"
46 #include "scan-code.h"
47 #include "xmemdup0.h"
48
eaca4c11 49 static int current_prec = 0;
a7706735
AD
50 static location current_lhs_location;
51 static named_ref *current_lhs_named_ref;
a4d1bf6a 52 static symbol *current_lhs_symbol;
eaca4c11
AD
53 static symbol_class current_class = unknown_sym;
54 static uniqstr current_type = NULL;
a7706735 55
a4d1bf6a
AD
56 /** Set the new current left-hand side symbol, possibly common
57 * to several right-hand side parts of rule.
58 */
827aca04
AD
59 static void current_lhs (symbol *sym, location loc, named_ref *ref);
60
61 #define YYLLOC_DEFAULT(Current, Rhs, N) \
62 (Current) = lloc_default (Rhs, N)
63 static YYLTYPE lloc_default (YYLTYPE const *, int);
64
65 #define YY_LOCATION_PRINT(File, Loc) \
66 location_print (Loc, File)
67
985d7177
AD
68 /* Strip initial '{' and final '}' (must be first and last characters).
69 Return the result. */
70 static char *strip_braces (char *code);
71
72 /* Convert CODE by calling code_props_plain_init if PLAIN, otherwise
73 code_props_symbol_action_init. Call
74 gram_scanner_last_string_free to release the latest string from
75 the scanner (should be CODE). */
76 static char const *translate_code (char *code, location loc, bool plain);
77
78 /* Convert CODE by calling code_props_plain_init after having
79 stripped the first and last characters (expected to be '{', and
80 '}'). Call gram_scanner_last_string_free to release the latest
81 string from the scanner (should be CODE). */
82 static char const *translate_code_braceless (char *code, location loc);
83
827aca04
AD
84 static void version_check (location const *loc, char const *version);
85
86 static void gram_error (location const *, char const *);
87
88 /* A string that describes a char (e.g., 'a' -> "'a'"). */
89 static char const *char_name (char);
a4d1bf6a 90
a7706735
AD
91 #define YYTYPE_INT16 int_fast16_t
92 #define YYTYPE_INT8 int_fast8_t
93 #define YYTYPE_UINT16 uint_fast16_t
94 #define YYTYPE_UINT8 uint_fast8_t
95}
96
aba47f56 97%define api.prefix {gram_}
2c056d69 98%define api.pure full
d0a30438 99%define locations
5320fffd 100%define parse.error verbose
107844a3 101%define parse.lac full
5320fffd 102%define parse.trace
12ffdd28 103%defines
219741d8 104%expect 0
e73ac5a0 105%verbose
12ffdd28 106
cd3684cf
AD
107%initial-action
108{
109 /* Bison's grammar can initial empty locations, hence a default
110 location is needed. */
4a678af8
JD
111 boundary_set (&@$.start, current_file, 1, 1);
112 boundary_set (&@$.end, current_file, 1, 1);
cd3684cf 113}
e9955c83 114
f9a85a15 115/* Define the tokens together with their human representation. */
3d38c03a
AD
116%token GRAM_EOF 0 "end of file"
117%token STRING "string"
e9955c83 118
9280d3ef
AD
119%token PERCENT_TOKEN "%token"
120%token PERCENT_NTERM "%nterm"
366eea36 121
9280d3ef 122%token PERCENT_TYPE "%type"
e9071366
AD
123%token PERCENT_DESTRUCTOR "%destructor"
124%token PERCENT_PRINTER "%printer"
366eea36 125
9280d3ef
AD
126%token PERCENT_LEFT "%left"
127%token PERCENT_RIGHT "%right"
128%token PERCENT_NONASSOC "%nonassoc"
d78f0ac9 129%token PERCENT_PRECEDENCE "%precedence"
04e60654 130
3d38c03a
AD
131%token PERCENT_PREC "%prec"
132%token PERCENT_DPREC "%dprec"
133%token PERCENT_MERGE "%merge"
e9955c83 134
ae7453f2
AD
135/*----------------------.
136| Global Declarations. |
137`----------------------*/
138
139%token
136a0f76 140 PERCENT_CODE "%code"
39a06c25 141 PERCENT_DEFAULT_PREC "%default-prec"
cd3684cf
AD
142 PERCENT_DEFINE "%define"
143 PERCENT_DEFINES "%defines"
31b850d2 144 PERCENT_ERROR_VERBOSE "%error-verbose"
cd3684cf 145 PERCENT_EXPECT "%expect"
a7706735 146 PERCENT_EXPECT_RR "%expect-rr"
ba061fa6 147 PERCENT_FLAG "%<flag>"
cd3684cf
AD
148 PERCENT_FILE_PREFIX "%file-prefix"
149 PERCENT_GLR_PARSER "%glr-parser"
e9071366 150 PERCENT_INITIAL_ACTION "%initial-action"
0e021770 151 PERCENT_LANGUAGE "%language"
cd3684cf 152 PERCENT_NAME_PREFIX "%name-prefix"
22fccf95 153 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
cd3684cf
AD
154 PERCENT_NO_LINES "%no-lines"
155 PERCENT_NONDETERMINISTIC_PARSER
a7706735 156 "%nondeterministic-parser"
cd3684cf 157 PERCENT_OUTPUT "%output"
a7706735 158 PERCENT_REQUIRE "%require"
cd3684cf
AD
159 PERCENT_SKELETON "%skeleton"
160 PERCENT_START "%start"
161 PERCENT_TOKEN_TABLE "%token-table"
162 PERCENT_VERBOSE "%verbose"
163 PERCENT_YACC "%yacc"
ae7453f2 164;
e9955c83 165
58d7a1a1 166%token BRACED_CODE "{...}"
ca2a6d15 167%token BRACED_PREDICATE "%?{...}"
b143f404 168%token BRACKETED_ID "[identifier]"
58d7a1a1
AD
169%token CHAR "char"
170%token EPILOGUE "epilogue"
3d38c03a 171%token EQUAL "="
3d38c03a 172%token ID "identifier"
b7295522 173%token ID_COLON "identifier:"
e9955c83 174%token PERCENT_PERCENT "%%"
58d7a1a1 175%token PIPE "|"
3d38c03a 176%token PROLOGUE "%{...%}"
58d7a1a1 177%token SEMICOLON ";"
cb823b6f
AD
178%token TAG "<tag>"
179%token TAG_ANY "<*>"
180%token TAG_NONE "<>"
58d7a1a1 181
a17c70f8 182%union {unsigned char character;}
58d7a1a1 183%type <character> CHAR
585a791e 184%printer { fputs (char_name ($$), yyo); } CHAR
3d38c03a 185
985d7177
AD
186%union {char *code;};
187%type <code> "{...}" "%?{...}" "%{...%}" EPILOGUE STRING
188%printer { fputs (quotearg_style (c_quoting_style, $$), yyo); } STRING
189%printer { fprintf (yyo, "{\n%s\n}", $$); } <code>
58d7a1a1 190
a17c70f8 191%union {uniqstr uniqstr;}
a82cbb63 192%type <uniqstr> BRACKETED_ID ID ID_COLON PERCENT_FLAG TAG tag variable
585a791e
AD
193%printer { fputs ($$, yyo); } <uniqstr>
194%printer { fprintf (yyo, "[%s]", $$); } BRACKETED_ID
195%printer { fprintf (yyo, "%s:", $$); } ID_COLON
196%printer { fprintf (yyo, "%%%s", $$); } PERCENT_FLAG
197%printer { fprintf (yyo, "<%s>", $$); } TAG tag
58d7a1a1 198
a17c70f8
AD
199%union {int integer;};
200%token <integer> INT "integer"
585a791e 201%printer { fprintf (yyo, "%d", $$); } <integer>
58d7a1a1 202
a17c70f8 203%union {symbol *symbol;}
b143f404 204%type <symbol> id id_colon string_as_id symbol symbol.prec
585a791e
AD
205%printer { fprintf (yyo, "%s", $$->tag); } <symbol>
206%printer { fprintf (yyo, "%s:", $$->tag); } id_colon
58d7a1a1 207
a17c70f8 208%union {assoc assoc;};
2c569025 209%type <assoc> precedence_declarator
a17c70f8
AD
210
211%union {symbol_list *list;}
ab7f29f8 212%type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item
a17c70f8
AD
213
214%union {named_ref *named_ref;}
b143f404 215%type <named_ref> named_ref.opt
a7706735
AD
216
217/*---------.
218| %param. |
219`---------*/
220%code requires
221{
a7706735
AD
222 typedef enum
223 {
eaca4c11 224 param_none = 0,
a7706735
AD
225 param_lex = 1 << 0,
226 param_parse = 1 << 1,
227 param_both = param_lex | param_parse
228 } param_type;
a7706735
AD
229};
230%code
231{
232 /** Add a lex-param and/or a parse-param.
233 *
234 * \param type where to push this formal argument.
235 * \param decl the formal argument. Destroyed.
236 * \param loc the location in the source.
237 */
238 static void add_param (param_type type, char *decl, location loc);
eaca4c11 239 static param_type current_param = param_none;
a7706735 240};
dac72a91 241%union {param_type param;}
a7706735
AD
242%token <param> PERCENT_PARAM "%param";
243%printer
244{
245 switch ($$)
246 {
247#define CASE(In, Out) \
dac72a91 248 case param_ ## In: fputs ("%" #Out, yyo); break
f50fa145
AD
249 CASE (lex, lex-param);
250 CASE (parse, parse-param);
251 CASE (both, param);
a7706735 252#undef CASE
2d399888 253 case param_none: aver (false); break;
eaca4c11 254 }
a7706735
AD
255} <param>;
256
eaca4c11
AD
257
258 /*==========\
259 | Grammar. |
260 \==========*/
e9955c83 261%%
2c569025 262
8efe435c 263input:
2ce4ed68 264 prologue_declarations "%%" grammar epilogue.opt
e9955c83
AD
265;
266
2c569025 267
e9690142
JD
268 /*------------------------------------.
269 | Declarations: before the first %%. |
270 `------------------------------------*/
2c569025 271
2ce4ed68 272prologue_declarations:
fd003416 273 %empty
2ce4ed68 274| prologue_declarations prologue_declaration
e9955c83
AD
275;
276
2ce4ed68 277prologue_declaration:
2c569025 278 grammar_declaration
7c0c6181
JD
279| "%{...%}"
280 {
7ecec4dd 281 muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
985d7177 282 translate_code ($1, @1, true), @1);
7c0c6181
JD
283 code_scanner_last_string_free ();
284 }
ba061fa6 285| "%<flag>"
0ce61575 286 {
4920ae8b 287 muscle_percent_define_ensure ($1, @1, true);
0ce61575 288 }
14bfd2e9 289| "%define" variable value
7eb8a0bc 290 {
14bfd2e9 291 muscle_percent_define_insert ($2, @2, $3.kind, $3.chars,
de5ab940 292 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
7eb8a0bc 293 }
2ce4ed68 294| "%defines" { defines_flag = true; }
02975b9a
JD
295| "%defines" STRING
296 {
297 defines_flag = true;
298 spec_defines_file = xstrdup ($2);
299 }
31b850d2
AD
300| "%error-verbose"
301 {
14bfd2e9
AD
302 muscle_percent_define_insert ("parse.error", @1, muscle_keyword,
303 "verbose",
31b850d2
AD
304 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
305 }
2ce4ed68 306| "%expect" INT { expected_sr_conflicts = $2; }
e9690142 307| "%expect-rr" INT { expected_rr_conflicts = $2; }
02975b9a 308| "%file-prefix" STRING { spec_file_prefix = $2; }
cd3684cf 309| "%glr-parser"
c66dfadd
PE
310 {
311 nondeterministic_parser = true;
312 glr_parser = true;
313 }
e9071366 314| "%initial-action" "{...}"
c66dfadd 315 {
985d7177 316 muscle_code_grow ("initial_action", translate_code ($2, @2, false), @2);
7c0c6181 317 code_scanner_last_string_free ();
c66dfadd 318 }
e9690142 319| "%language" STRING { language_argmatch ($2, grammar_prio, @1); }
02975b9a 320| "%name-prefix" STRING { spec_name_prefix = $2; }
2ce4ed68 321| "%no-lines" { no_lines_flag = true; }
e9690142 322| "%nondeterministic-parser" { nondeterministic_parser = true; }
02975b9a 323| "%output" STRING { spec_outfile = $2; }
eaca4c11 324| "%param" { current_param = $1; } params { current_param = param_none; }
2ce4ed68 325| "%require" STRING { version_check (&@2, $2); }
a7867f53
JD
326| "%skeleton" STRING
327 {
328 char const *skeleton_user = $2;
84526bf3 329 if (strchr (skeleton_user, '/'))
a7867f53
JD
330 {
331 size_t dir_length = strlen (current_file);
332 char *skeleton_build;
333 while (dir_length && current_file[dir_length - 1] != '/')
334 --dir_length;
335 while (dir_length && current_file[dir_length - 1] == '/')
336 --dir_length;
337 skeleton_build =
338 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
339 if (dir_length > 0)
340 {
bb3b912b 341 memcpy (skeleton_build, current_file, dir_length);
a7867f53
JD
342 skeleton_build[dir_length++] = '/';
343 }
344 strcpy (skeleton_build + dir_length, skeleton_user);
345 skeleton_user = uniqstr_new (skeleton_build);
346 free (skeleton_build);
347 }
51365192 348 skeleton_arg (skeleton_user, grammar_prio, @1);
a7867f53 349 }
2ce4ed68 350| "%token-table" { token_table_flag = true; }
ef1b4273 351| "%verbose" { report_flag |= report_states; }
2ce4ed68 352| "%yacc" { yacc_flag = true; }
cd3684cf 353| /*FIXME: Err? What is this horror doing here? */ ";"
e9955c83
AD
354;
355
eaca4c11
AD
356params:
357 params "{...}" { add_param (current_param, $2, @2); }
358| "{...}" { add_param (current_param, $1, @1); }
359;
360
a7706735
AD
361
362/*----------------------.
363| grammar_declaration. |
364`----------------------*/
365
2c569025
AD
366grammar_declaration:
367 precedence_declaration
368| symbol_declaration
e9955c83
AD
369| "%start" symbol
370 {
8efe435c 371 grammar_start_symbol_set ($2, @2);
e9955c83 372 }
49196047 373| code_props_type "{...}" generic_symlist
9280d3ef 374 {
1c292035
AD
375 code_props code;
376 code_props_symbol_action_init (&code, $2, @2);
377 code_props_translate_code (&code);
378 {
379 symbol_list *list;
380 for (list = $3; list; list = list->next)
4323e0da 381 symbol_list_code_props_set (list, $1, &code);
1c292035
AD
382 symbol_list_free ($3);
383 }
9280d3ef 384 }
22fccf95 385| "%default-prec"
39a06c25 386 {
22fccf95
PE
387 default_prec = true;
388 }
389| "%no-default-prec"
390 {
391 default_prec = false;
39a06c25 392 }
985d7177 393| "%code" "{...}"
8e0a5e9e 394 {
9611cfa2
JD
395 /* Do not invoke muscle_percent_code_grow here since it invokes
396 muscle_user_name_list_grow. */
985d7177
AD
397 muscle_code_grow ("percent_code()",
398 translate_code_braceless ($2, @2), @2);
8e0a5e9e
JD
399 code_scanner_last_string_free ();
400 }
985d7177 401| "%code" ID "{...}"
8e0a5e9e 402 {
985d7177 403 muscle_percent_code_grow ($2, @2, translate_code_braceless ($3, @3), @3);
8e0a5e9e 404 code_scanner_last_string_free ();
8e0a5e9e 405 }
2c569025
AD
406;
407
49196047
AD
408%type <code_type> code_props_type;
409%union {code_props_type code_type;};
585a791e 410%printer { fprintf (yyo, "%s", code_props_type_string ($$)); } <code_type>;
49196047
AD
411code_props_type:
412 "%destructor" { $$ = destructor; }
413| "%printer" { $$ = printer; }
414;
58d7a1a1 415
3c262606
AD
416/*---------.
417| %union. |
418`---------*/
58d7a1a1
AD
419
420%token PERCENT_UNION "%union";
421
422union_name:
fd003416 423 %empty {}
827bc59c
AD
424| ID { muscle_percent_define_insert ("api.value.union.name",
425 @1, muscle_keyword, $1,
426 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE); }
58d7a1a1
AD
427;
428
429grammar_declaration:
985d7177 430 "%union" union_name "{...}"
58d7a1a1 431 {
ddc8ede1 432 union_seen = true;
985d7177 433 muscle_code_grow ("union_members", translate_code_braceless ($3, @3), @3);
7ecec4dd 434 code_scanner_last_string_free ();
58d7a1a1
AD
435 }
436;
437
438
439
440
2c569025
AD
441symbol_declaration:
442 "%nterm" { current_class = nterm_sym; } symbol_defs.1
e9955c83
AD
443 {
444 current_class = unknown_sym;
445 current_type = NULL;
446 }
2c569025 447| "%token" { current_class = token_sym; } symbol_defs.1
e9955c83 448 {
2c569025 449 current_class = unknown_sym;
e9955c83
AD
450 current_type = NULL;
451 }
cb823b6f 452| "%type" TAG symbols.1
e9955c83 453 {
3d2cbc26 454 symbol_list *list;
4f82b42a 455 tag_seen = true;
1e0bab92 456 for (list = $3; list; list = list->next)
e9690142 457 symbol_type_set (list->content.sym, $2, @2);
dafdc66f 458 symbol_list_free ($3);
e9955c83
AD
459 }
460;
461
2c569025 462precedence_declaration:
cb823b6f 463 precedence_declarator tag.opt symbols.prec
1e0bab92 464 {
3d2cbc26 465 symbol_list *list;
1e0bab92
AD
466 ++current_prec;
467 for (list = $3; list; list = list->next)
e9690142
JD
468 {
469 symbol_type_set (list->content.sym, current_type, @2);
470 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
471 }
dafdc66f 472 symbol_list_free ($3);
1e0bab92
AD
473 current_type = NULL;
474 }
e9955c83
AD
475;
476
2c569025 477precedence_declarator:
d78f0ac9
AD
478 "%left" { $$ = left_assoc; }
479| "%right" { $$ = right_assoc; }
480| "%nonassoc" { $$ = non_assoc; }
481| "%precedence" { $$ = precedence_assoc; }
e9955c83
AD
482;
483
cb823b6f 484tag.opt:
fd003416
AD
485 %empty { current_type = NULL; }
486| TAG { current_type = $1; tag_seen = true; }
e9955c83
AD
487;
488
ab7f29f8
JD
489/* Just like symbols.1 but accept INT for the sake of POSIX. */
490symbols.prec:
491 symbol.prec
492 { $$ = symbol_list_sym_new ($1, @1); }
493| symbols.prec symbol.prec
93561c21 494 { $$ = symbol_list_append ($1, symbol_list_sym_new ($2, @2)); }
ab7f29f8
JD
495;
496
497symbol.prec:
5202b6ac
VT
498 symbol
499 {
500 $$ = $1;
501 symbol_class_set ($1, token_sym, @1, false);
502 }
503| symbol INT
504 {
505 $$ = $1;
506 symbol_user_token_number_set ($1, $2, @2);
507 symbol_class_set ($1, token_sym, @1, false);
508 }
0560fa24 509;
ab7f29f8 510
3be03b13 511/* One or more symbols to be %typed. */
1e0bab92 512symbols.1:
3be03b13
JD
513 symbol
514 { $$ = symbol_list_sym_new ($1, @1); }
515| symbols.1 symbol
93561c21 516 { $$ = symbol_list_append ($1, symbol_list_sym_new ($2, @2)); }
3be03b13
JD
517;
518
519generic_symlist:
520 generic_symlist_item { $$ = $1; }
93561c21 521| generic_symlist generic_symlist_item { $$ = symbol_list_append ($1, $2); }
3be03b13
JD
522;
523
524generic_symlist_item:
cb823b6f 525 symbol { $$ = symbol_list_sym_new ($1, @1); }
a82cbb63
AD
526| tag { $$ = symbol_list_type_new ($1, @1); }
527;
528
529tag:
530 TAG
531| "<*>" { $$ = uniqstr_new ("*"); }
532| "<>" { $$ = uniqstr_new (""); }
e9955c83
AD
533;
534
e9955c83
AD
535/* One token definition. */
536symbol_def:
cb823b6f 537 TAG
c1392807
AD
538 {
539 current_type = $1;
540 tag_seen = true;
541 }
58d7a1a1 542| id
c1392807
AD
543 {
544 symbol_class_set ($1, current_class, @1, true);
545 symbol_type_set ($1, current_type, @1);
546 }
58d7a1a1 547| id INT
e9955c83 548 {
073f9288 549 symbol_class_set ($1, current_class, @1, true);
1a31ed21 550 symbol_type_set ($1, current_type, @1);
e776192e 551 symbol_user_token_number_set ($1, $2, @2);
e9955c83 552 }
58d7a1a1 553| id string_as_id
e9955c83 554 {
073f9288 555 symbol_class_set ($1, current_class, @1, true);
1a31ed21 556 symbol_type_set ($1, current_type, @1);
a5d50994 557 symbol_make_alias ($1, $2, @$);
e9955c83 558 }
58d7a1a1 559| id INT string_as_id
e9955c83 560 {
073f9288 561 symbol_class_set ($1, current_class, @1, true);
1a31ed21 562 symbol_type_set ($1, current_type, @1);
e776192e 563 symbol_user_token_number_set ($1, $2, @2);
a5d50994 564 symbol_make_alias ($1, $3, @$);
e9955c83
AD
565 }
566;
567
568/* One or more symbol definitions. */
569symbol_defs.1:
570 symbol_def
e9955c83 571| symbol_defs.1 symbol_def
e9955c83
AD
572;
573
2c569025 574
e9690142
JD
575 /*------------------------------------------.
576 | The grammar section: between the two %%. |
577 `------------------------------------------*/
2c569025
AD
578
579grammar:
1921f1d7
AD
580 rules_or_grammar_declaration
581| grammar rules_or_grammar_declaration
582;
583
584/* As a Bison extension, one can use the grammar declarations in the
b7295522 585 body of the grammar. */
1921f1d7 586rules_or_grammar_declaration:
e9955c83 587 rules
8d0a98bb 588| grammar_declaration ";"
b275314e
AD
589| error ";"
590 {
591 yyerrok;
592 }
e9955c83
AD
593;
594
595rules:
a4d1bf6a
AD
596 id_colon named_ref.opt { current_lhs ($1, @1, $2); } rhses.1
597 {
598 /* Free the current lhs. */
599 current_lhs (0, @1, 0);
600 }
e9955c83
AD
601;
602
603rhses.1:
8f3596a6
AD
604 rhs { grammar_current_rule_end (@1); }
605| rhses.1 "|" rhs { grammar_current_rule_end (@3); }
8d0a98bb 606| rhses.1 ";"
e9955c83
AD
607;
608
ae2b48f5 609%token PERCENT_EMPTY "%empty";
e9955c83 610rhs:
fd003416 611 %empty
a4d1bf6a 612 { grammar_current_rule_begin (current_lhs_symbol, current_lhs_location,
e9690142 613 current_lhs_named_ref); }
b9f1d9a4
AR
614| rhs symbol named_ref.opt
615 { grammar_current_rule_symbol_append ($2, @2, $3); }
616| rhs "{...}" named_ref.opt
ca2a6d15 617 { grammar_current_rule_action_append ($2, @2, $3, false); }
59420cd7 618| rhs "%?{...}"
ca2a6d15 619 { grammar_current_rule_action_append ($2, @2, NULL, true); }
ae2b48f5
AD
620| rhs "%empty"
621 { grammar_current_rule_empty_set (@2); }
e9955c83 622| rhs "%prec" symbol
e776192e 623 { grammar_current_rule_prec_set ($3, @3); }
676385e2
PH
624| rhs "%dprec" INT
625 { grammar_current_rule_dprec_set ($3, @3); }
cb823b6f 626| rhs "%merge" TAG
676385e2 627 { grammar_current_rule_merge_set ($3, @3); }
e9955c83
AD
628;
629
b9f1d9a4 630named_ref.opt:
fd003416 631 %empty { $$ = 0; }
c0ef22ab 632| BRACKETED_ID { $$ = named_ref_new ($1, @1); }
b9f1d9a4
AR
633;
634
14bfd2e9
AD
635/*---------------------.
636| variable and value. |
637`---------------------*/
16dc6a9e 638
c9aded4b 639/* The STRING form of variable is deprecated and is not M4-friendly.
45eebca4 640 For example, M4 fails for '%define "[" "value"'. */
16dc6a9e
JD
641variable:
642 ID
4f646c37 643| STRING { $$ = uniqstr_new ($1); }
3c262606 644;
2ce4ed68 645
592d0b1e 646/* Some content or empty by default. */
14bfd2e9
AD
647%code requires {#include "muscle-tab.h"};
648%union
649{
650 struct
651 {
652 char const *chars;
653 muscle_kind kind;
654 } value;
655};
656%type <value> value;
657%printer
658{
659 switch ($$.kind)
660 {
661 case muscle_code: fprintf (yyo, "{%s}", $$.chars); break;
662 case muscle_keyword: fprintf (yyo, "%s", $$.chars); break;
663 case muscle_string: fprintf (yyo, "\"%s\"", $$.chars); break;
664 }
665} <value>;
666
667value:
985d7177
AD
668 %empty { $$.kind = muscle_keyword; $$.chars = ""; }
669| ID { $$.kind = muscle_keyword; $$.chars = $1; }
670| STRING { $$.kind = muscle_string; $$.chars = $1; }
9402b623 671| "{...}" { $$.kind = muscle_code; $$.chars = strip_braces ($1); }
2ce4ed68
AD
672;
673
674
3c262606
AD
675/*--------------.
676| Identifiers. |
677`--------------*/
58d7a1a1 678
33ad1a9c
PE
679/* Identifiers are returned as uniqstr values by the scanner.
680 Depending on their use, we may need to make them genuine symbols. */
58d7a1a1
AD
681
682id:
33ad1a9c 683 ID
203b9274 684 { $$ = symbol_from_uniqstr ($1, @1); }
33ad1a9c
PE
685| CHAR
686 {
687 $$ = symbol_get (char_name ($1), @1);
688 symbol_class_set ($$, token_sym, @1, false);
689 symbol_user_token_number_set ($$, $1, @1);
690 }
58d7a1a1
AD
691;
692
693id_colon:
203b9274 694 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
58d7a1a1
AD
695;
696
697
e9955c83 698symbol:
58d7a1a1
AD
699 id
700| string_as_id
e9955c83
AD
701;
702
ca407bdf 703/* A string used as an ID: quote it. */
e9955c83
AD
704string_as_id:
705 STRING
706 {
ca407bdf 707 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
db89d400 708 symbol_class_set ($$, token_sym, @1, false);
e9955c83
AD
709 }
710;
711
e9955c83 712epilogue.opt:
fd003416 713 %empty
e9955c83
AD
714| "%%" EPILOGUE
715 {
985d7177 716 muscle_code_grow ("epilogue", translate_code ($2, @2, true), @2);
7c0c6181 717 code_scanner_last_string_free ();
e9955c83
AD
718 }
719;
720
e9955c83 721%%
b7295522 722
b7295522
PE
723/* Return the location of the left-hand side of a rule whose
724 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
725 the right-hand side, and return an empty location equal to the end
726 boundary of RHS[0] if the right-hand side is empty. */
727
728static YYLTYPE
729lloc_default (YYLTYPE const *rhs, int n)
730{
731 int i;
a737b216 732 YYLTYPE loc;
62cb8a99
PE
733
734 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
735 The bug is fixed in 7.4.2m, but play it safe for now. */
736 loc.start = rhs[n].end;
737 loc.end = rhs[n].end;
b7295522 738
59420cd7 739 /* Ignore empty nonterminals the start of the right-hand side.
5320ca4d
PE
740 Do not bother to ignore them at the end of the right-hand side,
741 since empty nonterminals have the same end as their predecessors. */
b7295522
PE
742 for (i = 1; i <= n; i++)
743 if (! equal_boundaries (rhs[i].start, rhs[i].end))
744 {
e9690142
JD
745 loc.start = rhs[i].start;
746 break;
b7295522
PE
747 }
748
a737b216 749 return loc;
b7295522
PE
750}
751
985d7177
AD
752static
753char *strip_braces (char *code)
754{
c0ef22ab 755 code[strlen (code) - 1] = 0;
985d7177
AD
756 return code + 1;
757}
758
759static
760char const *
761translate_code (char *code, location loc, bool plain)
762{
763 code_props plain_code;
764 if (plain)
765 code_props_plain_init (&plain_code, code, loc);
766 else
767 code_props_symbol_action_init (&plain_code, code, loc);
768 code_props_translate_code (&plain_code);
769 gram_scanner_last_string_free ();
770 return plain_code.code;
771}
772
773static
774char const *
775translate_code_braceless (char *code, location loc)
776{
777 return translate_code (strip_braces (code), loc, true);
778}
b7295522 779
1773ceee 780static void
a7706735 781add_param (param_type type, char *decl, location loc)
1773ceee 782{
ead9e56e 783 static char const alphanum[26 + 26 + 1 + 10] =
1773ceee
PE
784 "abcdefghijklmnopqrstuvwxyz"
785 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ead9e56e
PE
786 "_"
787 "0123456789";
f71db70b 788
1773ceee 789 char const *name_start = NULL;
f71db70b
AD
790 {
791 char *p;
792 /* Stop on last actual character. */
793 for (p = decl; p[1]; p++)
794 if ((p == decl
795 || ! memchr (alphanum, p[-1], sizeof alphanum))
796 && memchr (alphanum, p[0], sizeof alphanum - 10))
797 name_start = p;
798
799 /* Strip the surrounding '{' and '}', and any blanks just inside
800 the braces. */
c9d546b2 801 --p;
6b5a7489 802 while (c_isspace ((unsigned char) *p))
c8554191 803 --p;
f71db70b 804 p[1] = '\0';
c9d546b2 805 ++decl;
6b5a7489 806 while (c_isspace ((unsigned char) *decl))
c8554191 807 ++decl;
f71db70b 808 }
e9ce5688 809
1773ceee 810 if (! name_start)
bb8e56ff 811 complain (&loc, complaint, _("missing identifier in parameter declaration"));
1773ceee
PE
812 else
813 {
60457f30 814 char *name = xmemdup0 (name_start, strspn (name_start, alphanum));
a7706735
AD
815 if (type & param_lex)
816 muscle_pair_list_grow ("lex_param", decl, name);
817 if (type & param_parse)
818 muscle_pair_list_grow ("parse_param", decl, name);
1773ceee
PE
819 free (name);
820 }
821
e9071366 822 gram_scanner_last_string_free ();
1773ceee
PE
823}
824
2ce4ed68 825
b50d2359
AD
826static void
827version_check (location const *loc, char const *version)
828{
829 if (strverscmp (version, PACKAGE_VERSION) > 0)
9b8a5ce0 830 {
bb8e56ff
TR
831 complain (loc, complaint, "require bison %s, but have %s",
832 version, PACKAGE_VERSION);
459a57a9 833 exit (EX_MISMATCH);
9b8a5ce0 834 }
b50d2359
AD
835}
836
1fec91df 837static void
3d2cbc26 838gram_error (location const *loc, char const *msg)
e9955c83 839{
bb8e56ff 840 complain (loc, complaint, "%s", msg);
e9955c83 841}
e9ce5688
PE
842
843char const *
844token_name (int type)
845{
fc01665e 846 return yytname[YYTRANSLATE (type)];
e9ce5688 847}
33ad1a9c
PE
848
849static char const *
850char_name (char c)
851{
852 if (c == '\'')
853 return "'\\''";
854 else
855 {
856 char buf[4];
857 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
858 return quotearg_style (escape_quoting_style, buf);
859 }
860}
827aca04
AD
861
862static
863void
864current_lhs (symbol *sym, location loc, named_ref *ref)
865{
866 current_lhs_symbol = sym;
867 current_lhs_location = loc;
868 /* In order to simplify memory management, named references for lhs
869 are always assigned by deep copy into the current symbol_list
870 node. This is because a single named-ref in the grammar may
871 result in several uses when the user factors lhs between several
872 rules using "|". Therefore free the parser's original copy. */
873 free (current_lhs_named_ref);
874 current_lhs_named_ref = ref;
875}