]> git.saurik.com Git - bison.git/blame - src/parse-gram.y
YYFAIL: warn about uses and remove from lalr1.java.
[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 153%token BRACED_CODE "{...}"
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
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
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
589 { grammar_current_rule_action_append ($2, @2, $3); }
e9955c83 590| rhs "%prec" symbol
e776192e 591 { grammar_current_rule_prec_set ($3, @3); }
676385e2
PH
592| rhs "%dprec" INT
593 { grammar_current_rule_dprec_set ($3, @3); }
cb823b6f 594| rhs "%merge" TAG
676385e2 595 { grammar_current_rule_merge_set ($3, @3); }
e9955c83
AD
596;
597
b9f1d9a4 598named_ref.opt:
872b52bc 599 /* Nothing. */ { $$ = 0; }
b9f1d9a4 600|
872b52bc 601 BRACKETED_ID { $$ = named_ref_new($1, @1); }
b9f1d9a4
AR
602;
603
58d7a1a1 604
3c262606
AD
605/*---------------------------.
606| variable and content.opt. |
607`---------------------------*/
16dc6a9e 608
c9aded4b
JD
609/* The STRING form of variable is deprecated and is not M4-friendly.
610 For example, M4 fails for `%define "[" "value"'. */
16dc6a9e
JD
611variable:
612 ID
4f646c37 613| STRING { $$ = uniqstr_new ($1); }
3c262606 614;
2ce4ed68 615
592d0b1e 616/* Some content or empty by default. */
2ce4ed68 617content.opt:
4f646c37 618 /* Nothing. */ { $$ = ""; }
cf499cff 619| ID { $$ = $1; }
6afc30cc 620| STRING
2ce4ed68
AD
621;
622
623
3c262606
AD
624/*------------.
625| braceless. |
626`------------*/
6afc30cc 627
2ce4ed68
AD
628braceless:
629 "{...}"
630 {
7c0c6181 631 code_props plain_code;
2ce4ed68 632 $1[strlen ($1) - 1] = '\n';
7c0c6181
JD
633 code_props_plain_init (&plain_code, $1+1, @1);
634 code_props_translate_code (&plain_code);
635 gram_scanner_last_string_free ();
636 $$ = plain_code.code;
2ce4ed68
AD
637 }
638;
639
640
3c262606
AD
641/*--------------.
642| Identifiers. |
643`--------------*/
58d7a1a1 644
33ad1a9c
PE
645/* Identifiers are returned as uniqstr values by the scanner.
646 Depending on their use, we may need to make them genuine symbols. */
58d7a1a1
AD
647
648id:
33ad1a9c 649 ID
203b9274 650 { $$ = symbol_from_uniqstr ($1, @1); }
33ad1a9c
PE
651| CHAR
652 {
653 $$ = symbol_get (char_name ($1), @1);
654 symbol_class_set ($$, token_sym, @1, false);
655 symbol_user_token_number_set ($$, $1, @1);
656 }
58d7a1a1
AD
657;
658
659id_colon:
203b9274 660 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
58d7a1a1
AD
661;
662
663
e9955c83 664symbol:
58d7a1a1
AD
665 id
666| string_as_id
e9955c83
AD
667;
668
ca407bdf 669/* A string used as an ID: quote it. */
e9955c83
AD
670string_as_id:
671 STRING
672 {
ca407bdf 673 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
db89d400 674 symbol_class_set ($$, token_sym, @1, false);
e9955c83
AD
675 }
676;
677
e9955c83
AD
678epilogue.opt:
679 /* Nothing. */
e9955c83
AD
680| "%%" EPILOGUE
681 {
7c0c6181
JD
682 code_props plain_code;
683 code_props_plain_init (&plain_code, $2, @2);
684 code_props_translate_code (&plain_code);
e9071366 685 gram_scanner_last_string_free ();
7c0c6181
JD
686 muscle_code_grow ("epilogue", plain_code.code, @2);
687 code_scanner_last_string_free ();
e9955c83
AD
688 }
689;
690
e9955c83 691%%
b7295522
PE
692
693
694/* Return the location of the left-hand side of a rule whose
695 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
696 the right-hand side, and return an empty location equal to the end
697 boundary of RHS[0] if the right-hand side is empty. */
698
699static YYLTYPE
700lloc_default (YYLTYPE const *rhs, int n)
701{
702 int i;
a737b216 703 YYLTYPE loc;
62cb8a99
PE
704
705 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
706 The bug is fixed in 7.4.2m, but play it safe for now. */
707 loc.start = rhs[n].end;
708 loc.end = rhs[n].end;
b7295522 709
5320ca4d
PE
710 /* Ignore empty nonterminals the start of the the right-hand side.
711 Do not bother to ignore them at the end of the right-hand side,
712 since empty nonterminals have the same end as their predecessors. */
b7295522
PE
713 for (i = 1; i <= n; i++)
714 if (! equal_boundaries (rhs[i].start, rhs[i].end))
715 {
a737b216 716 loc.start = rhs[i].start;
b7295522
PE
717 break;
718 }
719
a737b216 720 return loc;
b7295522
PE
721}
722
723
1773ceee 724static void
a7706735 725add_param (param_type type, char *decl, location loc)
1773ceee 726{
ead9e56e 727 static char const alphanum[26 + 26 + 1 + 10] =
1773ceee
PE
728 "abcdefghijklmnopqrstuvwxyz"
729 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ead9e56e
PE
730 "_"
731 "0123456789";
f71db70b 732
1773ceee 733 char const *name_start = NULL;
f71db70b
AD
734 {
735 char *p;
736 /* Stop on last actual character. */
737 for (p = decl; p[1]; p++)
738 if ((p == decl
739 || ! memchr (alphanum, p[-1], sizeof alphanum))
740 && memchr (alphanum, p[0], sizeof alphanum - 10))
741 name_start = p;
742
743 /* Strip the surrounding '{' and '}', and any blanks just inside
744 the braces. */
745 while (*--p == ' ' || *p == '\t')
746 continue;
747 p[1] = '\0';
748 while (*++decl == ' ' || *decl == '\t')
749 continue;
750 }
e9ce5688 751
1773ceee
PE
752 if (! name_start)
753 complain_at (loc, _("missing identifier in parameter declaration"));
754 else
755 {
756 char *name;
757 size_t name_len;
758
759 for (name_len = 1;
ead9e56e 760 memchr (alphanum, name_start[name_len], sizeof alphanum);
1773ceee
PE
761 name_len++)
762 continue;
763
764 name = xmalloc (name_len + 1);
765 memcpy (name, name_start, name_len);
766 name[name_len] = '\0';
a7706735
AD
767 if (type & param_lex)
768 muscle_pair_list_grow ("lex_param", decl, name);
769 if (type & param_parse)
770 muscle_pair_list_grow ("parse_param", decl, name);
1773ceee
PE
771 free (name);
772 }
773
e9071366 774 gram_scanner_last_string_free ();
1773ceee
PE
775}
776
2ce4ed68 777
b50d2359
AD
778static void
779version_check (location const *loc, char const *version)
780{
781 if (strverscmp (version, PACKAGE_VERSION) > 0)
9b8a5ce0
AD
782 {
783 complain_at (*loc, "require bison %s, but have %s",
784 version, PACKAGE_VERSION);
785 exit (63);
786 }
b50d2359
AD
787}
788
1fec91df 789static void
3d2cbc26 790gram_error (location const *loc, char const *msg)
e9955c83 791{
ad8a3efc 792 complain_at (*loc, "%s", msg);
e9955c83 793}
e9ce5688
PE
794
795char const *
796token_name (int type)
797{
fc01665e 798 return yytname[YYTRANSLATE (type)];
e9ce5688 799}
33ad1a9c
PE
800
801static char const *
802char_name (char c)
803{
804 if (c == '\'')
805 return "'\\''";
806 else
807 {
808 char buf[4];
809 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
810 return quotearg_style (escape_quoting_style, buf);
811 }
812}