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