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