]> git.saurik.com Git - bison.git/blame - src/parse-gram.y
Fix portability problem on OpenBSD 4.7.
[bison.git] / src / parse-gram.y
CommitLineData
12ffdd28 1%{/* Bison Grammar Parser -*- C -*-
a737b216 2
7d424de1
PE
3 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software 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
ae7453f2
AD
121/*----------------------.
122| Global Declarations. |
123`----------------------*/
124
125%token
136a0f76 126 PERCENT_CODE "%code"
39a06c25 127 PERCENT_DEFAULT_PREC "%default-prec"
cd3684cf
AD
128 PERCENT_DEFINE "%define"
129 PERCENT_DEFINES "%defines"
31b850d2 130 PERCENT_ERROR_VERBOSE "%error-verbose"
cd3684cf 131 PERCENT_EXPECT "%expect"
a7706735 132 PERCENT_EXPECT_RR "%expect-rr"
ba061fa6 133 PERCENT_FLAG "%<flag>"
cd3684cf
AD
134 PERCENT_FILE_PREFIX "%file-prefix"
135 PERCENT_GLR_PARSER "%glr-parser"
e9071366 136 PERCENT_INITIAL_ACTION "%initial-action"
0e021770 137 PERCENT_LANGUAGE "%language"
cd3684cf 138 PERCENT_NAME_PREFIX "%name-prefix"
22fccf95 139 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
cd3684cf
AD
140 PERCENT_NO_LINES "%no-lines"
141 PERCENT_NONDETERMINISTIC_PARSER
a7706735 142 "%nondeterministic-parser"
cd3684cf 143 PERCENT_OUTPUT "%output"
a7706735 144 PERCENT_REQUIRE "%require"
cd3684cf
AD
145 PERCENT_SKELETON "%skeleton"
146 PERCENT_START "%start"
147 PERCENT_TOKEN_TABLE "%token-table"
148 PERCENT_VERBOSE "%verbose"
149 PERCENT_YACC "%yacc"
ae7453f2 150;
e9955c83 151
58d7a1a1 152%token BRACED_CODE "{...}"
ca2a6d15 153%token BRACED_PREDICATE "%?{...}"
b143f404 154%token BRACKETED_ID "[identifier]"
58d7a1a1
AD
155%token CHAR "char"
156%token EPILOGUE "epilogue"
3d38c03a 157%token EQUAL "="
3d38c03a 158%token ID "identifier"
b7295522 159%token ID_COLON "identifier:"
e9955c83 160%token PERCENT_PERCENT "%%"
58d7a1a1 161%token PIPE "|"
3d38c03a 162%token PROLOGUE "%{...%}"
58d7a1a1 163%token SEMICOLON ";"
cb823b6f
AD
164%token TAG "<tag>"
165%token TAG_ANY "<*>"
166%token TAG_NONE "<>"
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
ca2a6d15 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
b143f404
JD
180%type <uniqstr> BRACKETED_ID ID ID_COLON PERCENT_FLAG TAG variable
181%printer { fputs ($$, stderr); } <uniqstr>
182%printer { fprintf (stderr, "[%s]", $$); } BRACKETED_ID
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
b143f404 188%printer { fprintf (stderr, "%d", $$); } <integer>
58d7a1a1 189
b143f404
JD
190%type <symbol> id id_colon string_as_id symbol symbol.prec
191%printer { fprintf (stderr, "%s", $$->tag); } <symbol>
58d7a1a1
AD
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
b143f404 196%type <named_ref> named_ref.opt
a7706735
AD
197
198/*---------.
199| %param. |
200`---------*/
201%code requires
202{
203# ifndef PARAM_TYPE
204# define PARAM_TYPE
205 typedef enum
206 {
eaca4c11 207 param_none = 0,
a7706735
AD
208 param_lex = 1 << 0,
209 param_parse = 1 << 1,
210 param_both = param_lex | param_parse
211 } param_type;
212# endif
213};
214%code
215{
216 /** Add a lex-param and/or a parse-param.
217 *
218 * \param type where to push this formal argument.
219 * \param decl the formal argument. Destroyed.
220 * \param loc the location in the source.
221 */
222 static void add_param (param_type type, char *decl, location loc);
eaca4c11 223 static param_type current_param = param_none;
a7706735
AD
224};
225%union
226{
227 param_type param;
228}
229%token <param> PERCENT_PARAM "%param";
230%printer
231{
232 switch ($$)
233 {
234#define CASE(In, Out) \
eaca4c11 235 case param_ ## In: fputs ("%" #Out, stderr); break
a7706735
AD
236 CASE(lex, lex-param);
237 CASE(parse, parse-param);
238 CASE(both, param);
a7706735 239#undef CASE
2d399888 240 case param_none: aver (false); break;
eaca4c11 241 }
a7706735
AD
242} <param>;
243
eaca4c11
AD
244
245 /*==========\
246 | Grammar. |
247 \==========*/
e9955c83 248%%
2c569025 249
8efe435c 250input:
2ce4ed68 251 prologue_declarations "%%" grammar epilogue.opt
e9955c83
AD
252;
253
2c569025
AD
254
255 /*------------------------------------.
256 | Declarations: before the first %%. |
257 `------------------------------------*/
258
2ce4ed68 259prologue_declarations:
e9955c83 260 /* Nothing */
2ce4ed68 261| prologue_declarations prologue_declaration
e9955c83
AD
262;
263
2ce4ed68 264prologue_declaration:
2c569025 265 grammar_declaration
7c0c6181
JD
266| "%{...%}"
267 {
268 code_props plain_code;
269 code_props_plain_init (&plain_code, $1, @1);
270 code_props_translate_code (&plain_code);
271 gram_scanner_last_string_free ();
7ecec4dd
JD
272 muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
273 plain_code.code, @1);
7c0c6181
JD
274 code_scanner_last_string_free ();
275 }
ba061fa6 276| "%<flag>"
0ce61575 277 {
4920ae8b 278 muscle_percent_define_ensure ($1, @1, true);
0ce61575 279 }
16dc6a9e 280| "%define" variable content.opt
7eb8a0bc 281 {
de5ab940
JD
282 muscle_percent_define_insert ($2, @2, $3,
283 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
7eb8a0bc 284 }
2ce4ed68 285| "%defines" { defines_flag = true; }
02975b9a
JD
286| "%defines" STRING
287 {
288 defines_flag = true;
289 spec_defines_file = xstrdup ($2);
290 }
31b850d2
AD
291| "%error-verbose"
292 {
293 muscle_percent_define_insert ("parse.error", @1, "verbose",
294 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
295 }
2ce4ed68
AD
296| "%expect" INT { expected_sr_conflicts = $2; }
297| "%expect-rr" INT { expected_rr_conflicts = $2; }
02975b9a
JD
298| "%file-prefix" STRING { spec_file_prefix = $2; }
299| "%file-prefix" "=" STRING { spec_file_prefix = $3; } /* deprecated */
cd3684cf 300| "%glr-parser"
c66dfadd
PE
301 {
302 nondeterministic_parser = true;
303 glr_parser = true;
304 }
e9071366 305| "%initial-action" "{...}"
c66dfadd 306 {
7c0c6181
JD
307 code_props action;
308 code_props_symbol_action_init (&action, $2, @2);
309 code_props_translate_code (&action);
310 gram_scanner_last_string_free ();
311 muscle_code_grow ("initial_action", action.code, @2);
312 code_scanner_last_string_free ();
c66dfadd 313 }
51365192 314| "%language" STRING { language_argmatch ($2, grammar_prio, @1); }
02975b9a
JD
315| "%name-prefix" STRING { spec_name_prefix = $2; }
316| "%name-prefix" "=" STRING { spec_name_prefix = $3; } /* deprecated */
2ce4ed68
AD
317| "%no-lines" { no_lines_flag = true; }
318| "%nondeterministic-parser" { nondeterministic_parser = true; }
02975b9a
JD
319| "%output" STRING { spec_outfile = $2; }
320| "%output" "=" STRING { spec_outfile = $3; } /* deprecated */
eaca4c11 321| "%param" { current_param = $1; } params { current_param = param_none; }
2ce4ed68 322| "%require" STRING { version_check (&@2, $2); }
a7867f53
JD
323| "%skeleton" STRING
324 {
325 char const *skeleton_user = $2;
326 if (strchr (skeleton_user, '/'))
327 {
328 size_t dir_length = strlen (current_file);
329 char *skeleton_build;
330 while (dir_length && current_file[dir_length - 1] != '/')
331 --dir_length;
332 while (dir_length && current_file[dir_length - 1] == '/')
333 --dir_length;
334 skeleton_build =
335 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
336 if (dir_length > 0)
337 {
338 strncpy (skeleton_build, current_file, dir_length);
339 skeleton_build[dir_length++] = '/';
340 }
341 strcpy (skeleton_build + dir_length, skeleton_user);
342 skeleton_user = uniqstr_new (skeleton_build);
343 free (skeleton_build);
344 }
51365192 345 skeleton_arg (skeleton_user, grammar_prio, @1);
a7867f53 346 }
2ce4ed68 347| "%token-table" { token_table_flag = true; }
ef1b4273 348| "%verbose" { report_flag |= report_states; }
2ce4ed68 349| "%yacc" { yacc_flag = true; }
cd3684cf 350| /*FIXME: Err? What is this horror doing here? */ ";"
e9955c83
AD
351;
352
eaca4c11
AD
353params:
354 params "{...}" { add_param (current_param, $2, @2); }
355| "{...}" { add_param (current_param, $1, @1); }
356;
357
a7706735
AD
358
359/*----------------------.
360| grammar_declaration. |
361`----------------------*/
362
2c569025
AD
363grammar_declaration:
364 precedence_declaration
365| symbol_declaration
e9955c83
AD
366| "%start" symbol
367 {
8efe435c 368 grammar_start_symbol_set ($2, @2);
e9955c83 369 }
3be03b13 370| "%destructor" "{...}" generic_symlist
9280d3ef 371 {
3d2cbc26 372 symbol_list *list;
e9071366 373 for (list = $3; list; list = list->next)
7c0c6181 374 symbol_list_destructor_set (list, $2, @2);
e9071366 375 symbol_list_free ($3);
9280d3ef 376 }
3be03b13 377| "%printer" "{...}" generic_symlist
366eea36 378 {
3d2cbc26 379 symbol_list *list;
e9071366 380 for (list = $3; list; list = list->next)
7c0c6181 381 symbol_list_printer_set (list, $2, @2);
e9071366 382 symbol_list_free ($3);
366eea36 383 }
22fccf95 384| "%default-prec"
39a06c25 385 {
22fccf95
PE
386 default_prec = true;
387 }
388| "%no-default-prec"
389 {
390 default_prec = false;
39a06c25 391 }
8e0a5e9e
JD
392| "%code" braceless
393 {
9611cfa2
JD
394 /* Do not invoke muscle_percent_code_grow here since it invokes
395 muscle_user_name_list_grow. */
592d0b1e 396 muscle_code_grow ("percent_code()", $2, @2);
8e0a5e9e
JD
397 code_scanner_last_string_free ();
398 }
16dc6a9e 399| "%code" ID braceless
8e0a5e9e 400 {
9611cfa2 401 muscle_percent_code_grow ($2, @2, $3, @3);
8e0a5e9e 402 code_scanner_last_string_free ();
8e0a5e9e 403 }
2c569025
AD
404;
405
58d7a1a1 406
3c262606
AD
407/*---------.
408| %union. |
409`---------*/
58d7a1a1
AD
410
411%token PERCENT_UNION "%union";
412
413union_name:
414 /* Nothing. */ {}
415| ID { muscle_code_grow ("union_name", $1, @1); }
416;
417
418grammar_declaration:
7ecec4dd 419 "%union" union_name braceless
58d7a1a1 420 {
ddc8ede1 421 union_seen = true;
7ecec4dd
JD
422 muscle_code_grow ("stype", $3, @3);
423 code_scanner_last_string_free ();
58d7a1a1
AD
424 }
425;
426
427
428
429
2c569025
AD
430symbol_declaration:
431 "%nterm" { current_class = nterm_sym; } symbol_defs.1
e9955c83
AD
432 {
433 current_class = unknown_sym;
434 current_type = NULL;
435 }
2c569025 436| "%token" { current_class = token_sym; } symbol_defs.1
e9955c83 437 {
2c569025 438 current_class = unknown_sym;
e9955c83
AD
439 current_type = NULL;
440 }
cb823b6f 441| "%type" TAG symbols.1
e9955c83 442 {
3d2cbc26 443 symbol_list *list;
4f82b42a 444 tag_seen = true;
1e0bab92 445 for (list = $3; list; list = list->next)
3be03b13 446 symbol_type_set (list->content.sym, $2, @2);
dafdc66f 447 symbol_list_free ($3);
e9955c83
AD
448 }
449;
450
2c569025 451precedence_declaration:
cb823b6f 452 precedence_declarator tag.opt symbols.prec
1e0bab92 453 {
3d2cbc26 454 symbol_list *list;
1e0bab92
AD
455 ++current_prec;
456 for (list = $3; list; list = list->next)
457 {
3be03b13
JD
458 symbol_type_set (list->content.sym, current_type, @2);
459 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
1e0bab92 460 }
dafdc66f 461 symbol_list_free ($3);
1e0bab92
AD
462 current_type = NULL;
463 }
e9955c83
AD
464;
465
2c569025 466precedence_declarator:
d78f0ac9
AD
467 "%left" { $$ = left_assoc; }
468| "%right" { $$ = right_assoc; }
469| "%nonassoc" { $$ = non_assoc; }
470| "%precedence" { $$ = precedence_assoc; }
e9955c83
AD
471;
472
cb823b6f 473tag.opt:
87fbb0bf 474 /* Nothing. */ { current_type = NULL; }
cb823b6f 475| TAG { current_type = $1; tag_seen = true; }
e9955c83
AD
476;
477
ab7f29f8
JD
478/* Just like symbols.1 but accept INT for the sake of POSIX. */
479symbols.prec:
480 symbol.prec
481 { $$ = symbol_list_sym_new ($1, @1); }
482| symbols.prec symbol.prec
483 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
484;
485
486symbol.prec:
487 symbol { $$ = $1; }
488 | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
489 ;
490
3be03b13 491/* One or more symbols to be %typed. */
1e0bab92 492symbols.1:
3be03b13
JD
493 symbol
494 { $$ = symbol_list_sym_new ($1, @1); }
495| symbols.1 symbol
496 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
497;
498
499generic_symlist:
500 generic_symlist_item { $$ = $1; }
501| generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
502;
503
504generic_symlist_item:
cb823b6f
AD
505 symbol { $$ = symbol_list_sym_new ($1, @1); }
506| TAG { $$ = symbol_list_type_new ($1, @1); }
507| "<*>" { $$ = symbol_list_default_tagged_new (@1); }
508| "<>" { $$ = symbol_list_default_tagless_new (@1); }
e9955c83
AD
509;
510
e9955c83
AD
511/* One token definition. */
512symbol_def:
cb823b6f 513 TAG
e9955c83
AD
514 {
515 current_type = $1;
ddc8ede1 516 tag_seen = true;
e9955c83 517 }
58d7a1a1 518| id
e9955c83 519 {
073f9288 520 symbol_class_set ($1, current_class, @1, true);
1a31ed21 521 symbol_type_set ($1, current_type, @1);
e9955c83 522 }
58d7a1a1 523| id INT
e9955c83 524 {
073f9288 525 symbol_class_set ($1, current_class, @1, true);
1a31ed21 526 symbol_type_set ($1, current_type, @1);
e776192e 527 symbol_user_token_number_set ($1, $2, @2);
e9955c83 528 }
58d7a1a1 529| id string_as_id
e9955c83 530 {
073f9288 531 symbol_class_set ($1, current_class, @1, true);
1a31ed21 532 symbol_type_set ($1, current_type, @1);
a5d50994 533 symbol_make_alias ($1, $2, @$);
e9955c83 534 }
58d7a1a1 535| id INT string_as_id
e9955c83 536 {
073f9288 537 symbol_class_set ($1, current_class, @1, true);
1a31ed21 538 symbol_type_set ($1, current_type, @1);
e776192e 539 symbol_user_token_number_set ($1, $2, @2);
a5d50994 540 symbol_make_alias ($1, $3, @$);
e9955c83
AD
541 }
542;
543
544/* One or more symbol definitions. */
545symbol_defs.1:
546 symbol_def
e9955c83 547| symbol_defs.1 symbol_def
e9955c83
AD
548;
549
2c569025
AD
550
551 /*------------------------------------------.
552 | The grammar section: between the two %%. |
553 `------------------------------------------*/
554
555grammar:
1921f1d7
AD
556 rules_or_grammar_declaration
557| grammar rules_or_grammar_declaration
558;
559
560/* As a Bison extension, one can use the grammar declarations in the
b7295522 561 body of the grammar. */
1921f1d7 562rules_or_grammar_declaration:
e9955c83 563 rules
8d0a98bb 564| grammar_declaration ";"
b275314e
AD
565| error ";"
566 {
567 yyerrok;
568 }
e9955c83
AD
569;
570
571rules:
b9f1d9a4
AR
572 id_colon named_ref.opt { current_lhs = $1; current_lhs_location = @1;
573 current_lhs_named_ref = $2; } rhses.1
e9955c83
AD
574;
575
576rhses.1:
8f3596a6
AD
577 rhs { grammar_current_rule_end (@1); }
578| rhses.1 "|" rhs { grammar_current_rule_end (@3); }
8d0a98bb 579| rhses.1 ";"
e9955c83
AD
580;
581
582rhs:
583 /* Nothing. */
b9f1d9a4
AR
584 { grammar_current_rule_begin (current_lhs, current_lhs_location,
585 current_lhs_named_ref); }
586| rhs symbol named_ref.opt
587 { grammar_current_rule_symbol_append ($2, @2, $3); }
588| rhs "{...}" named_ref.opt
ca2a6d15
PH
589 { grammar_current_rule_action_append ($2, @2, $3, false); }
590| rhs "%?{...}"
591 { grammar_current_rule_action_append ($2, @2, NULL, true); }
e9955c83 592| rhs "%prec" symbol
e776192e 593 { grammar_current_rule_prec_set ($3, @3); }
676385e2
PH
594| rhs "%dprec" INT
595 { grammar_current_rule_dprec_set ($3, @3); }
cb823b6f 596| rhs "%merge" TAG
676385e2 597 { grammar_current_rule_merge_set ($3, @3); }
e9955c83
AD
598;
599
b9f1d9a4 600named_ref.opt:
872b52bc 601 /* Nothing. */ { $$ = 0; }
b9f1d9a4 602|
872b52bc 603 BRACKETED_ID { $$ = named_ref_new($1, @1); }
b9f1d9a4
AR
604;
605
3c262606
AD
606/*---------------------------.
607| variable and content.opt. |
608`---------------------------*/
16dc6a9e 609
c9aded4b
JD
610/* The STRING form of variable is deprecated and is not M4-friendly.
611 For example, M4 fails for `%define "[" "value"'. */
16dc6a9e
JD
612variable:
613 ID
4f646c37 614| STRING { $$ = uniqstr_new ($1); }
3c262606 615;
2ce4ed68 616
592d0b1e 617/* Some content or empty by default. */
2ce4ed68 618content.opt:
4f646c37 619 /* Nothing. */ { $$ = ""; }
cf499cff 620| ID { $$ = $1; }
6afc30cc 621| STRING
2ce4ed68
AD
622;
623
624
3c262606
AD
625/*------------.
626| braceless. |
627`------------*/
6afc30cc 628
2ce4ed68
AD
629braceless:
630 "{...}"
631 {
7c0c6181 632 code_props plain_code;
2ce4ed68 633 $1[strlen ($1) - 1] = '\n';
7c0c6181
JD
634 code_props_plain_init (&plain_code, $1+1, @1);
635 code_props_translate_code (&plain_code);
636 gram_scanner_last_string_free ();
637 $$ = plain_code.code;
2ce4ed68
AD
638 }
639;
640
641
3c262606
AD
642/*--------------.
643| Identifiers. |
644`--------------*/
58d7a1a1 645
33ad1a9c
PE
646/* Identifiers are returned as uniqstr values by the scanner.
647 Depending on their use, we may need to make them genuine symbols. */
58d7a1a1
AD
648
649id:
33ad1a9c 650 ID
203b9274 651 { $$ = symbol_from_uniqstr ($1, @1); }
33ad1a9c
PE
652| CHAR
653 {
654 $$ = symbol_get (char_name ($1), @1);
655 symbol_class_set ($$, token_sym, @1, false);
656 symbol_user_token_number_set ($$, $1, @1);
657 }
58d7a1a1
AD
658;
659
660id_colon:
203b9274 661 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
58d7a1a1
AD
662;
663
664
e9955c83 665symbol:
58d7a1a1
AD
666 id
667| string_as_id
e9955c83
AD
668;
669
ca407bdf 670/* A string used as an ID: quote it. */
e9955c83
AD
671string_as_id:
672 STRING
673 {
ca407bdf 674 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
db89d400 675 symbol_class_set ($$, token_sym, @1, false);
e9955c83
AD
676 }
677;
678
e9955c83
AD
679epilogue.opt:
680 /* Nothing. */
e9955c83
AD
681| "%%" EPILOGUE
682 {
7c0c6181
JD
683 code_props plain_code;
684 code_props_plain_init (&plain_code, $2, @2);
685 code_props_translate_code (&plain_code);
e9071366 686 gram_scanner_last_string_free ();
7c0c6181
JD
687 muscle_code_grow ("epilogue", plain_code.code, @2);
688 code_scanner_last_string_free ();
e9955c83
AD
689 }
690;
691
e9955c83 692%%
b7295522
PE
693
694
695/* Return the location of the left-hand side of a rule whose
696 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
697 the right-hand side, and return an empty location equal to the end
698 boundary of RHS[0] if the right-hand side is empty. */
699
700static YYLTYPE
701lloc_default (YYLTYPE const *rhs, int n)
702{
703 int i;
a737b216 704 YYLTYPE loc;
62cb8a99
PE
705
706 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
707 The bug is fixed in 7.4.2m, but play it safe for now. */
708 loc.start = rhs[n].end;
709 loc.end = rhs[n].end;
b7295522 710
5320ca4d
PE
711 /* Ignore empty nonterminals the start of the the right-hand side.
712 Do not bother to ignore them at the end of the right-hand side,
713 since empty nonterminals have the same end as their predecessors. */
b7295522
PE
714 for (i = 1; i <= n; i++)
715 if (! equal_boundaries (rhs[i].start, rhs[i].end))
716 {
a737b216 717 loc.start = rhs[i].start;
b7295522
PE
718 break;
719 }
720
a737b216 721 return loc;
b7295522
PE
722}
723
724
1773ceee 725static void
a7706735 726add_param (param_type type, char *decl, location loc)
1773ceee 727{
ead9e56e 728 static char const alphanum[26 + 26 + 1 + 10] =
1773ceee
PE
729 "abcdefghijklmnopqrstuvwxyz"
730 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ead9e56e
PE
731 "_"
732 "0123456789";
f71db70b 733
1773ceee 734 char const *name_start = NULL;
f71db70b
AD
735 {
736 char *p;
737 /* Stop on last actual character. */
738 for (p = decl; p[1]; p++)
739 if ((p == decl
740 || ! memchr (alphanum, p[-1], sizeof alphanum))
741 && memchr (alphanum, p[0], sizeof alphanum - 10))
742 name_start = p;
743
744 /* Strip the surrounding '{' and '}', and any blanks just inside
745 the braces. */
746 while (*--p == ' ' || *p == '\t')
747 continue;
748 p[1] = '\0';
749 while (*++decl == ' ' || *decl == '\t')
750 continue;
751 }
e9ce5688 752
1773ceee
PE
753 if (! name_start)
754 complain_at (loc, _("missing identifier in parameter declaration"));
755 else
756 {
757 char *name;
758 size_t name_len;
759
760 for (name_len = 1;
ead9e56e 761 memchr (alphanum, name_start[name_len], sizeof alphanum);
1773ceee
PE
762 name_len++)
763 continue;
764
765 name = xmalloc (name_len + 1);
766 memcpy (name, name_start, name_len);
767 name[name_len] = '\0';
a7706735
AD
768 if (type & param_lex)
769 muscle_pair_list_grow ("lex_param", decl, name);
770 if (type & param_parse)
771 muscle_pair_list_grow ("parse_param", decl, name);
1773ceee
PE
772 free (name);
773 }
774
e9071366 775 gram_scanner_last_string_free ();
1773ceee
PE
776}
777
2ce4ed68 778
b50d2359
AD
779static void
780version_check (location const *loc, char const *version)
781{
782 if (strverscmp (version, PACKAGE_VERSION) > 0)
9b8a5ce0
AD
783 {
784 complain_at (*loc, "require bison %s, but have %s",
785 version, PACKAGE_VERSION);
786 exit (63);
787 }
b50d2359
AD
788}
789
1fec91df 790static void
3d2cbc26 791gram_error (location const *loc, char const *msg)
e9955c83 792{
ad8a3efc 793 complain_at (*loc, "%s", msg);
e9955c83 794}
e9ce5688
PE
795
796char const *
797token_name (int type)
798{
fc01665e 799 return yytname[YYTRANSLATE (type)];
e9ce5688 800}
33ad1a9c
PE
801
802static char const *
803char_name (char c)
804{
805 if (c == '\'')
806 return "'\\''";
807 else
808 {
809 char buf[4];
810 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
811 return quotearg_style (escape_quoting_style, buf);
812 }
813}