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