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