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