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