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