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