]> git.saurik.com Git - bison.git/blame - src/parse-gram.y
remove useless include.
[bison.git] / src / parse-gram.y
CommitLineData
12ffdd28 1%{/* Bison Grammar Parser -*- C -*-
a737b216 2
1462fcee 3 Copyright (C) 2002-2010 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"
23ec25b7 28#include "muscle-tab.h"
d5e8574b 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) \
f0064700 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) \
f0064700 48 gram_error (&yylloc, Msg)
3d2cbc26 49static void gram_error (location const *, char const *);
e9955c83 50
33ad1a9c
PE
51static char const *char_name (char);
52
2ce4ed68
AD
53/** Add a lex-param or a parse-param.
54 *
55 * \param type \a lex_param or \a parse_param
56 * \param decl the formal argument
57 * \param loc the location in the source.
58 */
59static void add_param (char const *type, char *decl, location loc);
60
1773ceee 61
04098407 62static symbol_class current_class = unknown_sym;
ddc8ede1 63static uniqstr current_type = NULL;
877519f8
PE
64static symbol *current_lhs;
65static location current_lhs_location;
7685e2f7 66static named_ref *current_lhs_named_ref;
04098407 67static int current_prec = 0;
d42cf844 68
cb48f191
PE
69#define YYTYPE_INT16 int_fast16_t
70#define YYTYPE_INT8 int_fast8_t
71#define YYTYPE_UINT16 uint_fast16_t
72#define YYTYPE_UINT8 uint_fast8_t
e9955c83
AD
73%}
74
12ffdd28 75%debug
82b248ad 76%verbose
12ffdd28
PE
77%defines
78%locations
79%pure-parser
80%error-verbose
12ffdd28 81%name-prefix="gram_"
219741d8 82%expect 0
12ffdd28 83
cd3684cf
AD
84%initial-action
85{
86 /* Bison's grammar can initial empty locations, hence a default
87 location is needed. */
4a678af8
JD
88 boundary_set (&@$.start, current_file, 1, 1);
89 boundary_set (&@$.end, current_file, 1, 1);
cd3684cf 90}
e9955c83 91
e9955c83
AD
92%union
93{
3d2cbc26
PE
94 symbol *symbol;
95 symbol_list *list;
e9955c83 96 int integer;
eb095650
PE
97 char const *chars;
98 char *code;
3d2cbc26
PE
99 assoc assoc;
100 uniqstr uniqstr;
58d7a1a1 101 unsigned char character;
7685e2f7 102 named_ref *named_ref;
e9955c83
AD
103};
104
f9a85a15 105/* Define the tokens together with their human representation. */
3d38c03a
AD
106%token GRAM_EOF 0 "end of file"
107%token STRING "string"
3d38c03a 108%token INT "integer"
e9955c83 109
9280d3ef
AD
110%token PERCENT_TOKEN "%token"
111%token PERCENT_NTERM "%nterm"
366eea36 112
9280d3ef 113%token PERCENT_TYPE "%type"
e9071366
AD
114%token PERCENT_DESTRUCTOR "%destructor"
115%token PERCENT_PRINTER "%printer"
366eea36 116
9280d3ef
AD
117%token PERCENT_LEFT "%left"
118%token PERCENT_RIGHT "%right"
119%token PERCENT_NONASSOC "%nonassoc"
04e60654 120
3d38c03a
AD
121%token PERCENT_PREC "%prec"
122%token PERCENT_DPREC "%dprec"
123%token PERCENT_MERGE "%merge"
e9955c83 124
e9955c83 125
ae7453f2
AD
126/*----------------------.
127| Global Declarations. |
128`----------------------*/
129
130%token
136a0f76 131 PERCENT_CODE "%code"
cd3684cf 132 PERCENT_DEBUG "%debug"
39a06c25 133 PERCENT_DEFAULT_PREC "%default-prec"
cd3684cf
AD
134 PERCENT_DEFINE "%define"
135 PERCENT_DEFINES "%defines"
136 PERCENT_ERROR_VERBOSE "%error-verbose"
137 PERCENT_EXPECT "%expect"
d6328241 138 PERCENT_EXPECT_RR "%expect-rr"
cd3684cf
AD
139 PERCENT_FILE_PREFIX "%file-prefix"
140 PERCENT_GLR_PARSER "%glr-parser"
e9071366 141 PERCENT_INITIAL_ACTION "%initial-action"
0e021770 142 PERCENT_LANGUAGE "%language"
e9071366 143 PERCENT_LEX_PARAM "%lex-param"
cd3684cf
AD
144 PERCENT_LOCATIONS "%locations"
145 PERCENT_NAME_PREFIX "%name-prefix"
22fccf95 146 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
cd3684cf
AD
147 PERCENT_NO_LINES "%no-lines"
148 PERCENT_NONDETERMINISTIC_PARSER
f0064700 149 "%nondeterministic-parser"
cd3684cf 150 PERCENT_OUTPUT "%output"
e9071366 151 PERCENT_PARSE_PARAM "%parse-param"
cd3684cf 152 PERCENT_PURE_PARSER "%pure-parser"
f0064700 153 PERCENT_REQUIRE "%require"
cd3684cf
AD
154 PERCENT_SKELETON "%skeleton"
155 PERCENT_START "%start"
156 PERCENT_TOKEN_TABLE "%token-table"
157 PERCENT_VERBOSE "%verbose"
158 PERCENT_YACC "%yacc"
ae7453f2 159;
e9955c83 160
58d7a1a1 161%token BRACED_CODE "{...}"
42ec0ae1 162%token BRACKETED_ID "[identifier]"
58d7a1a1
AD
163%token CHAR "char"
164%token EPILOGUE "epilogue"
3d38c03a 165%token EQUAL "="
3d38c03a 166%token ID "identifier"
b7295522 167%token ID_COLON "identifier:"
e9955c83 168%token PERCENT_PERCENT "%%"
58d7a1a1 169%token PIPE "|"
3d38c03a 170%token PROLOGUE "%{...%}"
58d7a1a1
AD
171%token SEMICOLON ";"
172%token TYPE "type"
12e35840 173%token TYPE_TAG_ANY "<*>"
3ebecc24 174%token TYPE_TAG_NONE "<>"
58d7a1a1
AD
175
176%type <character> CHAR
33ad1a9c 177%printer { fputs (char_name ($$), stderr); } CHAR
3d38c03a 178
2ce4ed68 179/* braceless is not to be used for rule or symbol actions, as it
7c0c6181 180 calls code_props_plain_init. */
6afc30cc 181%type <chars> STRING "%{...%}" EPILOGUE braceless content.opt
eb095650 182%type <code> "{...}"
33ad1a9c 183%printer { fputs (quotearg_style (c_quoting_style, $$), stderr); }
eb095650 184 STRING
2ce4ed68 185%printer { fprintf (stderr, "{\n%s\n}", $$); }
6afc30cc 186 braceless content.opt "{...}" "%{...%}" EPILOGUE
58d7a1a1 187
42ec0ae1
JD
188%type <uniqstr> BRACKETED_ID ID ID_COLON TYPE variable
189%printer { fputs ($$, stderr); } <uniqstr>
190%printer { fprintf (stderr, "[%s]", $$); } BRACKETED_ID
58d7a1a1 191%printer { fprintf (stderr, "%s:", $$); } ID_COLON
42ec0ae1 192%printer { fprintf (stderr, "<%s>", $$); } TYPE
58d7a1a1 193
e9955c83 194%type <integer> INT
42ec0ae1 195%printer { fprintf (stderr, "%d", $$); } <integer>
58d7a1a1 196
42ec0ae1
JD
197%type <symbol> id id_colon string_as_id symbol symbol.prec
198%printer { fprintf (stderr, "%s", $$->tag); } <symbol>
58d7a1a1
AD
199%printer { fprintf (stderr, "%s:", $$->tag); } id_colon
200
2c569025 201%type <assoc> precedence_declarator
ab7f29f8 202%type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item
42ec0ae1
JD
203%type <named_ref> named_ref.opt
204
e9955c83 205%%
2c569025 206
8efe435c 207input:
2ce4ed68 208 prologue_declarations "%%" grammar epilogue.opt
e9955c83
AD
209;
210
2c569025
AD
211
212 /*------------------------------------.
213 | Declarations: before the first %%. |
214 `------------------------------------*/
215
2ce4ed68 216prologue_declarations:
e9955c83 217 /* Nothing */
2ce4ed68 218| prologue_declarations prologue_declaration
e9955c83
AD
219;
220
2ce4ed68 221prologue_declaration:
2c569025 222 grammar_declaration
7c0c6181
JD
223| "%{...%}"
224 {
225 code_props plain_code;
226 code_props_plain_init (&plain_code, $1, @1);
227 code_props_translate_code (&plain_code);
228 gram_scanner_last_string_free ();
7ecec4dd
JD
229 muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
230 plain_code.code, @1);
7c0c6181
JD
231 code_scanner_last_string_free ();
232 }
2ce4ed68 233| "%debug" { debug_flag = true; }
16dc6a9e 234| "%define" variable content.opt
7eb8a0bc 235 {
34d41938
JD
236 muscle_percent_define_insert ($2, @2, $3,
237 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
7eb8a0bc 238 }
2ce4ed68 239| "%defines" { defines_flag = true; }
02975b9a
JD
240| "%defines" STRING
241 {
242 defines_flag = true;
243 spec_defines_file = xstrdup ($2);
244 }
2ce4ed68
AD
245| "%error-verbose" { error_verbose = true; }
246| "%expect" INT { expected_sr_conflicts = $2; }
247| "%expect-rr" INT { expected_rr_conflicts = $2; }
02975b9a
JD
248| "%file-prefix" STRING { spec_file_prefix = $2; }
249| "%file-prefix" "=" STRING { spec_file_prefix = $3; } /* deprecated */
cd3684cf 250| "%glr-parser"
c66dfadd
PE
251 {
252 nondeterministic_parser = true;
253 glr_parser = true;
254 }
e9071366 255| "%initial-action" "{...}"
c66dfadd 256 {
7c0c6181
JD
257 code_props action;
258 code_props_symbol_action_init (&action, $2, @2);
259 code_props_translate_code (&action);
260 gram_scanner_last_string_free ();
261 muscle_code_grow ("initial_action", action.code, @2);
262 code_scanner_last_string_free ();
c66dfadd 263 }
e186a284 264| "%language" STRING { language_argmatch ($2, grammar_prio, @1); }
2ce4ed68
AD
265| "%lex-param" "{...}" { add_param ("lex_param", $2, @2); }
266| "%locations" { locations_flag = true; }
02975b9a
JD
267| "%name-prefix" STRING { spec_name_prefix = $2; }
268| "%name-prefix" "=" STRING { spec_name_prefix = $3; } /* deprecated */
2ce4ed68
AD
269| "%no-lines" { no_lines_flag = true; }
270| "%nondeterministic-parser" { nondeterministic_parser = true; }
02975b9a
JD
271| "%output" STRING { spec_outfile = $2; }
272| "%output" "=" STRING { spec_outfile = $3; } /* deprecated */
2ce4ed68 273| "%parse-param" "{...}" { add_param ("parse_param", $2, @2); }
d9df47b6
JD
274| "%pure-parser"
275 {
276 /* %pure-parser is deprecated in favor of `%define api.pure', so use
277 `%define api.pure' in a backward-compatible manner here. First, don't
278 complain if %pure-parser is specified multiple times. */
279 if (!muscle_find_const ("percent_define(api.pure)"))
34d41938
JD
280 muscle_percent_define_insert ("api.pure", @1, "",
281 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
d9df47b6
JD
282 /* In all cases, use api.pure now so that the backend doesn't complain if
283 the skeleton ignores api.pure, but do warn now if there's a previous
284 conflicting definition from an actual %define. */
285 if (!muscle_percent_define_flag_if ("api.pure"))
34d41938
JD
286 muscle_percent_define_insert ("api.pure", @1, "",
287 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
d9df47b6 288 }
2ce4ed68 289| "%require" STRING { version_check (&@2, $2); }
a7867f53
JD
290| "%skeleton" STRING
291 {
292 char const *skeleton_user = $2;
293 if (strchr (skeleton_user, '/'))
294 {
295 size_t dir_length = strlen (current_file);
296 char *skeleton_build;
297 while (dir_length && current_file[dir_length - 1] != '/')
298 --dir_length;
299 while (dir_length && current_file[dir_length - 1] == '/')
300 --dir_length;
301 skeleton_build =
302 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
303 if (dir_length > 0)
304 {
305 strncpy (skeleton_build, current_file, dir_length);
306 skeleton_build[dir_length++] = '/';
307 }
308 strcpy (skeleton_build + dir_length, skeleton_user);
309 skeleton_user = uniqstr_new (skeleton_build);
310 free (skeleton_build);
311 }
e186a284 312 skeleton_arg (skeleton_user, grammar_prio, @1);
a7867f53 313 }
2ce4ed68 314| "%token-table" { token_table_flag = true; }
ef1b4273 315| "%verbose" { report_flag |= report_states; }
2ce4ed68 316| "%yacc" { yacc_flag = true; }
cd3684cf 317| /*FIXME: Err? What is this horror doing here? */ ";"
e9955c83
AD
318;
319
2c569025
AD
320grammar_declaration:
321 precedence_declaration
322| symbol_declaration
e9955c83
AD
323| "%start" symbol
324 {
8efe435c 325 grammar_start_symbol_set ($2, @2);
e9955c83 326 }
3be03b13 327| "%destructor" "{...}" generic_symlist
9280d3ef 328 {
3d2cbc26 329 symbol_list *list;
e9071366 330 for (list = $3; list; list = list->next)
7c0c6181 331 symbol_list_destructor_set (list, $2, @2);
e9071366 332 symbol_list_free ($3);
9280d3ef 333 }
3be03b13 334| "%printer" "{...}" generic_symlist
366eea36 335 {
3d2cbc26 336 symbol_list *list;
e9071366 337 for (list = $3; list; list = list->next)
7c0c6181 338 symbol_list_printer_set (list, $2, @2);
e9071366 339 symbol_list_free ($3);
366eea36 340 }
22fccf95 341| "%default-prec"
39a06c25 342 {
22fccf95
PE
343 default_prec = true;
344 }
345| "%no-default-prec"
346 {
347 default_prec = false;
39a06c25 348 }
8e0a5e9e
JD
349| "%code" braceless
350 {
9611cfa2
JD
351 /* Do not invoke muscle_percent_code_grow here since it invokes
352 muscle_user_name_list_grow. */
592d0b1e 353 muscle_code_grow ("percent_code()", $2, @2);
8e0a5e9e
JD
354 code_scanner_last_string_free ();
355 }
16dc6a9e 356| "%code" ID braceless
8e0a5e9e 357 {
9611cfa2 358 muscle_percent_code_grow ($2, @2, $3, @3);
8e0a5e9e 359 code_scanner_last_string_free ();
8e0a5e9e 360 }
2c569025
AD
361;
362
58d7a1a1
AD
363
364/*----------*
365 | %union. |
366 *----------*/
367
368%token PERCENT_UNION "%union";
369
370union_name:
371 /* Nothing. */ {}
372| ID { muscle_code_grow ("union_name", $1, @1); }
373;
374
375grammar_declaration:
7ecec4dd 376 "%union" union_name braceless
58d7a1a1 377 {
ddc8ede1 378 union_seen = true;
7ecec4dd
JD
379 muscle_code_grow ("stype", $3, @3);
380 code_scanner_last_string_free ();
58d7a1a1
AD
381 }
382;
383
384
385
386
2c569025
AD
387symbol_declaration:
388 "%nterm" { current_class = nterm_sym; } symbol_defs.1
e9955c83
AD
389 {
390 current_class = unknown_sym;
391 current_type = NULL;
392 }
2c569025 393| "%token" { current_class = token_sym; } symbol_defs.1
e9955c83 394 {
2c569025 395 current_class = unknown_sym;
e9955c83
AD
396 current_type = NULL;
397 }
1e0bab92 398| "%type" TYPE symbols.1
e9955c83 399 {
3d2cbc26 400 symbol_list *list;
4f82b42a 401 tag_seen = true;
1e0bab92 402 for (list = $3; list; list = list->next)
3be03b13 403 symbol_type_set (list->content.sym, $2, @2);
dafdc66f 404 symbol_list_free ($3);
e9955c83
AD
405 }
406;
407
2c569025 408precedence_declaration:
ab7f29f8 409 precedence_declarator type.opt symbols.prec
1e0bab92 410 {
3d2cbc26 411 symbol_list *list;
1e0bab92
AD
412 ++current_prec;
413 for (list = $3; list; list = list->next)
414 {
3be03b13
JD
415 symbol_type_set (list->content.sym, current_type, @2);
416 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
1e0bab92 417 }
dafdc66f 418 symbol_list_free ($3);
1e0bab92
AD
419 current_type = NULL;
420 }
e9955c83
AD
421;
422
2c569025 423precedence_declarator:
e9955c83
AD
424 "%left" { $$ = left_assoc; }
425| "%right" { $$ = right_assoc; }
426| "%nonassoc" { $$ = non_assoc; }
427;
428
429type.opt:
87fbb0bf 430 /* Nothing. */ { current_type = NULL; }
ddc8ede1 431| TYPE { current_type = $1; tag_seen = true; }
e9955c83
AD
432;
433
ab7f29f8
JD
434/* Just like symbols.1 but accept INT for the sake of POSIX. */
435symbols.prec:
436 symbol.prec
437 { $$ = symbol_list_sym_new ($1, @1); }
438| symbols.prec symbol.prec
439 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
440;
441
442symbol.prec:
443 symbol { $$ = $1; }
444 | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
445 ;
446
3be03b13 447/* One or more symbols to be %typed. */
1e0bab92 448symbols.1:
3be03b13
JD
449 symbol
450 { $$ = symbol_list_sym_new ($1, @1); }
451| symbols.1 symbol
452 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
453;
454
455generic_symlist:
456 generic_symlist_item { $$ = $1; }
457| generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
458;
459
460generic_symlist_item:
461 symbol { $$ = symbol_list_sym_new ($1, @1); }
462| TYPE { $$ = symbol_list_type_new ($1, @1); }
12e35840 463| "<*>" { $$ = symbol_list_default_tagged_new (@1); }
3ebecc24 464| "<>" { $$ = symbol_list_default_tagless_new (@1); }
e9955c83
AD
465;
466
e9955c83
AD
467/* One token definition. */
468symbol_def:
469 TYPE
470 {
471 current_type = $1;
ddc8ede1 472 tag_seen = true;
e9955c83 473 }
58d7a1a1 474| id
e9955c83 475 {
073f9288 476 symbol_class_set ($1, current_class, @1, true);
1a31ed21 477 symbol_type_set ($1, current_type, @1);
e9955c83 478 }
58d7a1a1 479| id INT
e9955c83 480 {
073f9288 481 symbol_class_set ($1, current_class, @1, true);
1a31ed21 482 symbol_type_set ($1, current_type, @1);
e776192e 483 symbol_user_token_number_set ($1, $2, @2);
e9955c83 484 }
58d7a1a1 485| id string_as_id
e9955c83 486 {
073f9288 487 symbol_class_set ($1, current_class, @1, true);
1a31ed21 488 symbol_type_set ($1, current_type, @1);
a5d50994 489 symbol_make_alias ($1, $2, @$);
e9955c83 490 }
58d7a1a1 491| id INT string_as_id
e9955c83 492 {
073f9288 493 symbol_class_set ($1, current_class, @1, true);
1a31ed21 494 symbol_type_set ($1, current_type, @1);
e776192e 495 symbol_user_token_number_set ($1, $2, @2);
a5d50994 496 symbol_make_alias ($1, $3, @$);
e9955c83
AD
497 }
498;
499
500/* One or more symbol definitions. */
501symbol_defs.1:
502 symbol_def
e9955c83 503| symbol_defs.1 symbol_def
e9955c83
AD
504;
505
2c569025
AD
506
507 /*------------------------------------------.
508 | The grammar section: between the two %%. |
509 `------------------------------------------*/
510
511grammar:
1921f1d7
AD
512 rules_or_grammar_declaration
513| grammar rules_or_grammar_declaration
514;
515
516/* As a Bison extension, one can use the grammar declarations in the
b7295522 517 body of the grammar. */
1921f1d7 518rules_or_grammar_declaration:
e9955c83 519 rules
8d0a98bb 520| grammar_declaration ";"
b275314e
AD
521| error ";"
522 {
523 yyerrok;
524 }
e9955c83
AD
525;
526
527rules:
7685e2f7
AR
528 id_colon named_ref.opt { current_lhs = $1; current_lhs_location = @1;
529 current_lhs_named_ref = $2; } rhses.1
e9955c83
AD
530;
531
532rhses.1:
8f3596a6
AD
533 rhs { grammar_current_rule_end (@1); }
534| rhses.1 "|" rhs { grammar_current_rule_end (@3); }
8d0a98bb 535| rhses.1 ";"
e9955c83
AD
536;
537
538rhs:
539 /* Nothing. */
7685e2f7
AR
540 { grammar_current_rule_begin (current_lhs, current_lhs_location,
541 current_lhs_named_ref); }
542| rhs symbol named_ref.opt
543 { grammar_current_rule_symbol_append ($2, @2, $3); }
544| rhs "{...}" named_ref.opt
545 { grammar_current_rule_action_append ($2, @2, $3); }
e9955c83 546| rhs "%prec" symbol
e776192e 547 { grammar_current_rule_prec_set ($3, @3); }
676385e2
PH
548| rhs "%dprec" INT
549 { grammar_current_rule_dprec_set ($3, @3); }
550| rhs "%merge" TYPE
551 { grammar_current_rule_merge_set ($3, @3); }
e9955c83
AD
552;
553
7685e2f7 554named_ref.opt:
d5e8574b 555 /* Nothing. */ { $$ = 0; }
7685e2f7 556|
d5e8574b 557 BRACKETED_ID { $$ = named_ref_new($1, @1); }
7685e2f7
AR
558;
559
58d7a1a1 560
16dc6a9e
JD
561/*----------------------------*
562 | variable and content.opt. |
563 *---------------------------*/
564
ae618dcc
JD
565/* The STRING form of variable is deprecated and is not M4-friendly.
566 For example, M4 fails for `%define "[" "value"'. */
16dc6a9e
JD
567variable:
568 ID
663ce7bb
AD
569| STRING { $$ = uniqstr_new ($1); }
570;
2ce4ed68 571
592d0b1e 572/* Some content or empty by default. */
2ce4ed68 573content.opt:
663ce7bb 574 /* Nothing. */ { $$ = ""; }
f37495f6 575| ID { $$ = $1; }
6afc30cc 576| STRING
2ce4ed68
AD
577;
578
579
6afc30cc
JD
580/*-------------*
581 | braceless. |
582 *-------------*/
583
2ce4ed68
AD
584braceless:
585 "{...}"
586 {
7c0c6181 587 code_props plain_code;
2ce4ed68 588 $1[strlen ($1) - 1] = '\n';
7c0c6181
JD
589 code_props_plain_init (&plain_code, $1+1, @1);
590 code_props_translate_code (&plain_code);
591 gram_scanner_last_string_free ();
592 $$ = plain_code.code;
2ce4ed68
AD
593 }
594;
595
596
58d7a1a1
AD
597/*---------------*
598 | Identifiers. |
599 *---------------*/
600
33ad1a9c
PE
601/* Identifiers are returned as uniqstr values by the scanner.
602 Depending on their use, we may need to make them genuine symbols. */
58d7a1a1
AD
603
604id:
33ad1a9c 605 ID
203b9274 606 { $$ = symbol_from_uniqstr ($1, @1); }
33ad1a9c
PE
607| CHAR
608 {
609 $$ = symbol_get (char_name ($1), @1);
610 symbol_class_set ($$, token_sym, @1, false);
611 symbol_user_token_number_set ($$, $1, @1);
612 }
58d7a1a1
AD
613;
614
615id_colon:
203b9274 616 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
58d7a1a1
AD
617;
618
619
e9955c83 620symbol:
58d7a1a1
AD
621 id
622| string_as_id
e9955c83
AD
623;
624
ca407bdf 625/* A string used as an ID: quote it. */
e9955c83
AD
626string_as_id:
627 STRING
628 {
ca407bdf 629 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
db89d400 630 symbol_class_set ($$, token_sym, @1, false);
e9955c83
AD
631 }
632;
633
e9955c83
AD
634epilogue.opt:
635 /* Nothing. */
e9955c83
AD
636| "%%" EPILOGUE
637 {
7c0c6181
JD
638 code_props plain_code;
639 code_props_plain_init (&plain_code, $2, @2);
640 code_props_translate_code (&plain_code);
e9071366 641 gram_scanner_last_string_free ();
7c0c6181
JD
642 muscle_code_grow ("epilogue", plain_code.code, @2);
643 code_scanner_last_string_free ();
e9955c83
AD
644 }
645;
646
e9955c83 647%%
b7295522
PE
648
649
650/* Return the location of the left-hand side of a rule whose
651 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
652 the right-hand side, and return an empty location equal to the end
653 boundary of RHS[0] if the right-hand side is empty. */
654
655static YYLTYPE
656lloc_default (YYLTYPE const *rhs, int n)
657{
658 int i;
a737b216 659 YYLTYPE loc;
62cb8a99
PE
660
661 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
662 The bug is fixed in 7.4.2m, but play it safe for now. */
663 loc.start = rhs[n].end;
664 loc.end = rhs[n].end;
b7295522 665
5320ca4d
PE
666 /* Ignore empty nonterminals the start of the the right-hand side.
667 Do not bother to ignore them at the end of the right-hand side,
668 since empty nonterminals have the same end as their predecessors. */
b7295522
PE
669 for (i = 1; i <= n; i++)
670 if (! equal_boundaries (rhs[i].start, rhs[i].end))
671 {
a737b216 672 loc.start = rhs[i].start;
b7295522
PE
673 break;
674 }
675
a737b216 676 return loc;
b7295522
PE
677}
678
679
680/* Add a lex-param or a parse-param (depending on TYPE) with
681 declaration DECL and location LOC. */
682
1773ceee 683static void
e9ce5688 684add_param (char const *type, char *decl, location loc)
1773ceee 685{
ead9e56e 686 static char const alphanum[26 + 26 + 1 + 10] =
1773ceee
PE
687 "abcdefghijklmnopqrstuvwxyz"
688 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ead9e56e
PE
689 "_"
690 "0123456789";
1773ceee 691 char const *name_start = NULL;
e9ce5688 692 char *p;
1773ceee 693
e503aa60
AD
694 /* Stop on last actual character. */
695 for (p = decl; p[1]; p++)
ead9e56e
PE
696 if ((p == decl
697 || ! memchr (alphanum, p[-1], sizeof alphanum))
698 && memchr (alphanum, p[0], sizeof alphanum - 10))
1773ceee
PE
699 name_start = p;
700
ead9e56e
PE
701 /* Strip the surrounding '{' and '}', and any blanks just inside
702 the braces. */
703 while (*--p == ' ' || *p == '\t')
704 continue;
e503aa60 705 p[1] = '\0';
ead9e56e
PE
706 while (*++decl == ' ' || *decl == '\t')
707 continue;
e9ce5688 708
1773ceee
PE
709 if (! name_start)
710 complain_at (loc, _("missing identifier in parameter declaration"));
711 else
712 {
713 char *name;
714 size_t name_len;
715
716 for (name_len = 1;
ead9e56e 717 memchr (alphanum, name_start[name_len], sizeof alphanum);
1773ceee
PE
718 name_len++)
719 continue;
720
721 name = xmalloc (name_len + 1);
722 memcpy (name, name_start, name_len);
723 name[name_len] = '\0';
724 muscle_pair_list_grow (type, decl, name);
725 free (name);
726 }
727
e9071366 728 gram_scanner_last_string_free ();
1773ceee
PE
729}
730
2ce4ed68 731
b50d2359
AD
732static void
733version_check (location const *loc, char const *version)
734{
735 if (strverscmp (version, PACKAGE_VERSION) > 0)
9b8a5ce0
AD
736 {
737 complain_at (*loc, "require bison %s, but have %s",
738 version, PACKAGE_VERSION);
739 exit (63);
740 }
b50d2359
AD
741}
742
1fec91df 743static void
3d2cbc26 744gram_error (location const *loc, char const *msg)
e9955c83 745{
ad8a3efc 746 complain_at (*loc, "%s", msg);
e9955c83 747}
e9ce5688
PE
748
749char const *
750token_name (int type)
751{
fc01665e 752 return yytname[YYTRANSLATE (type)];
e9ce5688 753}
33ad1a9c
PE
754
755static char const *
756char_name (char c)
757{
758 if (c == '\'')
759 return "'\\''";
760 else
761 {
762 char buf[4];
763 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
764 return quotearg_style (escape_quoting_style, buf);
765 }
766}