]> git.saurik.com Git - bison.git/blame - src/parse-gram.y
* src/Makefile.am (BUILT_SOURCES): Fix the trailing backslash.
[bison.git] / src / parse-gram.y
CommitLineData
12ffdd28 1%{/* Bison Grammar Parser -*- C -*-
a737b216 2
073f9288 3 Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
e9955c83
AD
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
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
18 along with this program; if not, write to the Free Software
0fb669f9
PE
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA
e9955c83
AD
21*/
22
2cec9080 23#include <config.h>
e9955c83 24#include "system.h"
3d2cbc26 25
b275314e 26#include "complain.h"
3d2cbc26 27#include "conflicts.h"
e9955c83
AD
28#include "files.h"
29#include "getargs.h"
e9955c83 30#include "gram.h"
3d2cbc26 31#include "muscle_tab.h"
ca407bdf 32#include "quotearg.h"
e9955c83 33#include "reader.h"
3d2cbc26 34#include "symlist.h"
e9071366
AD
35#include "scan-gram.h"
36#include "scan-code.h"
b50d2359 37#include "strverscmp.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) \
f0064700 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) \
f0064700 51 gram_error (&yylloc, Msg)
3d2cbc26 52static void gram_error (location const *, char const *);
e9955c83 53
e9ce5688 54static void add_param (char const *, char *, location);
1773ceee 55
04098407
PE
56static symbol_class current_class = unknown_sym;
57static uniqstr current_type = 0;
877519f8
PE
58static symbol *current_lhs;
59static location current_lhs_location;
04098407 60static int current_prec = 0;
d42cf844
PE
61
62#ifdef UINT_FAST8_MAX
63# define YYTYPE_UINT8 uint_fast8_t
64#endif
65#ifdef INT_FAST8_MAX
66# define YYTYPE_INT8 int_fast8_t
67#endif
68#ifdef UINT_FAST16_MAX
69# define YYTYPE_UINT16 uint_fast16_t
70#endif
71#ifdef INT_FAST16_MAX
72# define YYTYPE_INT16 int_fast16_t
73#endif
e9955c83
AD
74%}
75
12ffdd28 76%debug
82b248ad 77%verbose
12ffdd28
PE
78%defines
79%locations
80%pure-parser
81%error-verbose
82%defines
83%name-prefix="gram_"
84
cd3684cf
AD
85%initial-action
86{
87 /* Bison's grammar can initial empty locations, hence a default
88 location is needed. */
e9071366
AD
89 boundary_set (&@$.start, current_file, 1, 0);
90 boundary_set (&@$.end, current_file, 1, 0);
cd3684cf 91}
e9955c83
AD
92
93/* Only NUMBERS have a value. */
94%union
95{
3d2cbc26
PE
96 symbol *symbol;
97 symbol_list *list;
e9955c83 98 int integer;
3d2cbc26
PE
99 char *chars;
100 assoc assoc;
101 uniqstr uniqstr;
e9955c83
AD
102};
103
f9a85a15 104/* Define the tokens together with their human representation. */
3d38c03a
AD
105%token GRAM_EOF 0 "end of file"
106%token STRING "string"
3d38c03a 107%token INT "integer"
e9955c83 108
9280d3ef
AD
109%token PERCENT_TOKEN "%token"
110%token PERCENT_NTERM "%nterm"
366eea36 111
9280d3ef 112%token PERCENT_TYPE "%type"
e9071366
AD
113%token PERCENT_DESTRUCTOR "%destructor"
114%token PERCENT_PRINTER "%printer"
366eea36 115
e9ce5688 116%token PERCENT_UNION "%union {...}"
366eea36 117
9280d3ef
AD
118%token PERCENT_LEFT "%left"
119%token PERCENT_RIGHT "%right"
120%token PERCENT_NONASSOC "%nonassoc"
04e60654 121
3d38c03a
AD
122%token PERCENT_PREC "%prec"
123%token PERCENT_DPREC "%dprec"
124%token PERCENT_MERGE "%merge"
e9955c83 125
e9955c83 126
ae7453f2
AD
127/*----------------------.
128| Global Declarations. |
129`----------------------*/
130
131%token
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
AD
141 PERCENT_INITIAL_ACTION "%initial-action"
142 PERCENT_LEX_PARAM "%lex-param"
cd3684cf
AD
143 PERCENT_LOCATIONS "%locations"
144 PERCENT_NAME_PREFIX "%name-prefix"
22fccf95 145 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
cd3684cf
AD
146 PERCENT_NO_LINES "%no-lines"
147 PERCENT_NONDETERMINISTIC_PARSER
f0064700 148 "%nondeterministic-parser"
cd3684cf 149 PERCENT_OUTPUT "%output"
e9071366 150 PERCENT_PARSE_PARAM "%parse-param"
cd3684cf 151 PERCENT_PURE_PARSER "%pure-parser"
f0064700 152 PERCENT_REQUIRE "%require"
cd3684cf
AD
153 PERCENT_SKELETON "%skeleton"
154 PERCENT_START "%start"
155 PERCENT_TOKEN_TABLE "%token-table"
156 PERCENT_VERBOSE "%verbose"
157 PERCENT_YACC "%yacc"
ae7453f2 158;
e9955c83 159
3d38c03a
AD
160%token TYPE "type"
161%token EQUAL "="
162%token SEMICOLON ";"
3d38c03a
AD
163%token PIPE "|"
164%token ID "identifier"
b7295522 165%token ID_COLON "identifier:"
e9955c83 166%token PERCENT_PERCENT "%%"
3d38c03a
AD
167%token PROLOGUE "%{...%}"
168%token EPILOGUE "epilogue"
169%token BRACED_CODE "{...}"
170
3d2cbc26 171%type <chars> STRING string_content
e9071366 172 "{...}"
e9ce5688 173 "%union {...}"
3d2cbc26 174 PROLOGUE EPILOGUE
82b248ad 175%printer { fprintf (stderr, "\"%s\"", $$); }
f0064700 176 STRING string_content
82b248ad 177%printer { fprintf (stderr, "{\n%s\n}", $$); }
e9071366 178 "{...}"
82b248ad 179 "%union {...}"
82b248ad 180 PROLOGUE EPILOGUE
3d2cbc26 181%type <uniqstr> TYPE
82b248ad 182%printer { fprintf (stderr, "<%s>", $$); } TYPE
e9955c83 183%type <integer> INT
82b248ad
AD
184%printer { fprintf (stderr, "%d", $$); } INT
185%type <symbol> ID symbol string_as_id
2f4f028d 186%printer { fprintf (stderr, "%s", $$->tag); } ID symbol string_as_id
82b248ad
AD
187%type <symbol> ID_COLON
188%printer { fprintf (stderr, "%s:", $$->tag); } ID_COLON
2c569025 189%type <assoc> precedence_declarator
1e0bab92 190%type <list> symbols.1
e9955c83 191%%
2c569025 192
8efe435c 193input:
2c569025 194 declarations "%%" grammar epilogue.opt
e9955c83
AD
195;
196
2c569025
AD
197
198 /*------------------------------------.
199 | Declarations: before the first %%. |
200 `------------------------------------*/
201
202declarations:
e9955c83 203 /* Nothing */
b7295522 204| declarations declaration
e9955c83
AD
205;
206
2c569025
AD
207declaration:
208 grammar_declaration
e9071366
AD
209| PROLOGUE { prologue_augment (translate_code ($1, @1),
210 @1); }
d0829076 211| "%debug" { debug_flag = true; }
c66dfadd
PE
212| "%define" string_content
213 {
214 static char one[] = "1";
215 muscle_insert ($2, one);
216 }
e9955c83 217| "%define" string_content string_content { muscle_insert ($2, $3); }
d0829076
PE
218| "%defines" { defines_flag = true; }
219| "%error-verbose" { error_verbose = true; }
d6328241 220| "%expect" INT { expected_sr_conflicts = $2; }
04098407 221| "%expect-rr" INT { expected_rr_conflicts = $2; }
e9955c83 222| "%file-prefix" "=" string_content { spec_file_prefix = $3; }
cd3684cf 223| "%glr-parser"
c66dfadd
PE
224 {
225 nondeterministic_parser = true;
226 glr_parser = true;
227 }
e9071366 228| "%initial-action" "{...}"
c66dfadd 229 {
e9071366 230 muscle_code_grow ("initial_action", translate_symbol_action ($2, @2), @2);
c66dfadd 231 }
e9071366 232| "%lex-param" "{...}" { add_param ("lex_param", $2, @2); }
d0829076 233| "%locations" { locations_flag = true; }
e9955c83 234| "%name-prefix" "=" string_content { spec_name_prefix = $3; }
d0829076 235| "%no-lines" { no_lines_flag = true; }
04098407 236| "%nondeterministic-parser" { nondeterministic_parser = true; }
e9955c83 237| "%output" "=" string_content { spec_outfile = $3; }
e9071366 238| "%parse-param" "{...}" { add_param ("parse_param", $2, @2); }
916708d5 239| "%pure-parser" { pure_parser = true; }
b50d2359 240| "%require" string_content { version_check (&@2, $2); }
e9955c83 241| "%skeleton" string_content { skeleton = $2; }
d0829076 242| "%token-table" { token_table_flag = true; }
9dd5b378 243| "%verbose" { report_flag = report_states; }
d0829076 244| "%yacc" { yacc_flag = true; }
cd3684cf 245| /*FIXME: Err? What is this horror doing here? */ ";"
e9955c83
AD
246;
247
2c569025
AD
248grammar_declaration:
249 precedence_declaration
250| symbol_declaration
e9955c83
AD
251| "%start" symbol
252 {
8efe435c 253 grammar_start_symbol_set ($2, @2);
e9955c83 254 }
e9ce5688 255| "%union {...}"
2c569025 256 {
1221b78a
PE
257 char const *body = $1;
258
259 if (typed)
260 {
261 /* Concatenate the union bodies, turning the first one's
262 trailing '}' into '\n', and omitting the second one's '{'. */
263 char *code = muscle_find ("stype");
264 code[strlen (code) - 1] = '\n';
265 body++;
266 }
267
d0829076 268 typed = true;
1221b78a 269 muscle_code_grow ("stype", body, @1);
2c569025 270 }
e9071366 271| "%destructor" "{...}" symbols.1
9280d3ef 272 {
3d2cbc26 273 symbol_list *list;
e9071366
AD
274 const char *action = translate_symbol_action ($2, @2);
275 for (list = $3; list; list = list->next)
276 symbol_destructor_set (list->sym, action, @2);
277 symbol_list_free ($3);
9280d3ef 278 }
e9071366 279| "%printer" "{...}" symbols.1
366eea36 280 {
3d2cbc26 281 symbol_list *list;
e9071366
AD
282 const char *action = translate_symbol_action ($2, @2);
283 for (list = $3; list; list = list->next)
284 symbol_printer_set (list->sym, action, @2);
285 symbol_list_free ($3);
366eea36 286 }
22fccf95 287| "%default-prec"
39a06c25 288 {
22fccf95
PE
289 default_prec = true;
290 }
291| "%no-default-prec"
292 {
293 default_prec = false;
39a06c25 294 }
2c569025
AD
295;
296
297symbol_declaration:
298 "%nterm" { current_class = nterm_sym; } symbol_defs.1
e9955c83
AD
299 {
300 current_class = unknown_sym;
301 current_type = NULL;
302 }
2c569025 303| "%token" { current_class = token_sym; } symbol_defs.1
e9955c83 304 {
2c569025 305 current_class = unknown_sym;
e9955c83
AD
306 current_type = NULL;
307 }
1e0bab92 308| "%type" TYPE symbols.1
e9955c83 309 {
3d2cbc26 310 symbol_list *list;
1e0bab92 311 for (list = $3; list; list = list->next)
1a31ed21 312 symbol_type_set (list->sym, $2, @2);
dafdc66f 313 symbol_list_free ($3);
e9955c83
AD
314 }
315;
316
2c569025 317precedence_declaration:
1e0bab92
AD
318 precedence_declarator type.opt symbols.1
319 {
3d2cbc26 320 symbol_list *list;
1e0bab92
AD
321 ++current_prec;
322 for (list = $3; list; list = list->next)
323 {
1a31ed21
AD
324 symbol_type_set (list->sym, current_type, @2);
325 symbol_precedence_set (list->sym, current_prec, $1, @1);
1e0bab92 326 }
dafdc66f 327 symbol_list_free ($3);
1e0bab92
AD
328 current_type = NULL;
329 }
e9955c83
AD
330;
331
2c569025 332precedence_declarator:
e9955c83
AD
333 "%left" { $$ = left_assoc; }
334| "%right" { $$ = right_assoc; }
335| "%nonassoc" { $$ = non_assoc; }
336;
337
338type.opt:
87fbb0bf 339 /* Nothing. */ { current_type = NULL; }
e9955c83
AD
340| TYPE { current_type = $1; }
341;
342
343/* One or more nonterminals to be %typed. */
1e0bab92
AD
344symbols.1:
345 symbol { $$ = symbol_list_new ($1, @1); }
346| symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
e9955c83
AD
347;
348
e9955c83
AD
349/* One token definition. */
350symbol_def:
351 TYPE
352 {
353 current_type = $1;
354 }
355| ID
356 {
073f9288 357 symbol_class_set ($1, current_class, @1, true);
1a31ed21 358 symbol_type_set ($1, current_type, @1);
e9955c83
AD
359 }
360| ID INT
361 {
073f9288 362 symbol_class_set ($1, current_class, @1, true);
1a31ed21 363 symbol_type_set ($1, current_type, @1);
e776192e 364 symbol_user_token_number_set ($1, $2, @2);
e9955c83
AD
365 }
366| ID string_as_id
367 {
073f9288 368 symbol_class_set ($1, current_class, @1, true);
1a31ed21 369 symbol_type_set ($1, current_type, @1);
a5d50994 370 symbol_make_alias ($1, $2, @$);
e9955c83
AD
371 }
372| ID INT string_as_id
373 {
073f9288 374 symbol_class_set ($1, current_class, @1, true);
1a31ed21 375 symbol_type_set ($1, current_type, @1);
e776192e 376 symbol_user_token_number_set ($1, $2, @2);
a5d50994 377 symbol_make_alias ($1, $3, @$);
e9955c83
AD
378 }
379;
380
381/* One or more symbol definitions. */
382symbol_defs.1:
383 symbol_def
e9955c83 384| symbol_defs.1 symbol_def
e9955c83
AD
385;
386
2c569025
AD
387
388 /*------------------------------------------.
389 | The grammar section: between the two %%. |
390 `------------------------------------------*/
391
392grammar:
1921f1d7
AD
393 rules_or_grammar_declaration
394| grammar rules_or_grammar_declaration
395;
396
397/* As a Bison extension, one can use the grammar declarations in the
b7295522 398 body of the grammar. */
1921f1d7 399rules_or_grammar_declaration:
e9955c83 400 rules
8d0a98bb 401| grammar_declaration ";"
b275314e
AD
402| error ";"
403 {
404 yyerrok;
405 }
e9955c83
AD
406;
407
408rules:
b7295522 409 ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
e9955c83
AD
410;
411
412rhses.1:
8f3596a6
AD
413 rhs { grammar_current_rule_end (@1); }
414| rhses.1 "|" rhs { grammar_current_rule_end (@3); }
8d0a98bb 415| rhses.1 ";"
e9955c83
AD
416;
417
418rhs:
419 /* Nothing. */
8f3596a6 420 { grammar_current_rule_begin (current_lhs, current_lhs_location); }
e9955c83 421| rhs symbol
8efe435c 422 { grammar_current_rule_symbol_append ($2, @2); }
e9071366
AD
423| rhs "{...}"
424 { grammar_current_rule_action_append (gram_last_string,
425 gram_last_braced_code_loc); }
e9955c83 426| rhs "%prec" symbol
e776192e 427 { grammar_current_rule_prec_set ($3, @3); }
676385e2
PH
428| rhs "%dprec" INT
429 { grammar_current_rule_dprec_set ($3, @3); }
430| rhs "%merge" TYPE
431 { grammar_current_rule_merge_set ($3, @3); }
e9955c83
AD
432;
433
434symbol:
435 ID { $$ = $1; }
436| string_as_id { $$ = $1; }
e9955c83
AD
437;
438
ca407bdf 439/* A string used as an ID: quote it. */
e9955c83
AD
440string_as_id:
441 STRING
442 {
ca407bdf 443 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
073f9288 444 symbol_class_set ($$, token_sym, @1, false);
e9955c83
AD
445 }
446;
447
ca407bdf 448/* A string used for its contents. Don't quote it. */
e9955c83
AD
449string_content:
450 STRING
ca407bdf
PE
451 { $$ = $1; }
452;
e9955c83
AD
453
454
455epilogue.opt:
456 /* Nothing. */
e9955c83
AD
457| "%%" EPILOGUE
458 {
e9071366
AD
459 muscle_code_grow ("epilogue", translate_code ($2, @2), @2);
460 gram_scanner_last_string_free ();
e9955c83
AD
461 }
462;
463
e9955c83 464%%
b7295522
PE
465
466
467/* Return the location of the left-hand side of a rule whose
468 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
469 the right-hand side, and return an empty location equal to the end
470 boundary of RHS[0] if the right-hand side is empty. */
471
472static YYLTYPE
473lloc_default (YYLTYPE const *rhs, int n)
474{
475 int i;
a737b216 476 YYLTYPE loc;
62cb8a99
PE
477
478 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
479 The bug is fixed in 7.4.2m, but play it safe for now. */
480 loc.start = rhs[n].end;
481 loc.end = rhs[n].end;
b7295522 482
5320ca4d
PE
483 /* Ignore empty nonterminals the start of the the right-hand side.
484 Do not bother to ignore them at the end of the right-hand side,
485 since empty nonterminals have the same end as their predecessors. */
b7295522
PE
486 for (i = 1; i <= n; i++)
487 if (! equal_boundaries (rhs[i].start, rhs[i].end))
488 {
a737b216 489 loc.start = rhs[i].start;
b7295522
PE
490 break;
491 }
492
a737b216 493 return loc;
b7295522
PE
494}
495
496
497/* Add a lex-param or a parse-param (depending on TYPE) with
498 declaration DECL and location LOC. */
499
1773ceee 500static void
e9ce5688 501add_param (char const *type, char *decl, location loc)
1773ceee 502{
ead9e56e 503 static char const alphanum[26 + 26 + 1 + 10] =
1773ceee
PE
504 "abcdefghijklmnopqrstuvwxyz"
505 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ead9e56e
PE
506 "_"
507 "0123456789";
1773ceee 508 char const *name_start = NULL;
e9ce5688 509 char *p;
1773ceee 510
e503aa60
AD
511 /* Stop on last actual character. */
512 for (p = decl; p[1]; p++)
ead9e56e
PE
513 if ((p == decl
514 || ! memchr (alphanum, p[-1], sizeof alphanum))
515 && memchr (alphanum, p[0], sizeof alphanum - 10))
1773ceee
PE
516 name_start = p;
517
ead9e56e
PE
518 /* Strip the surrounding '{' and '}', and any blanks just inside
519 the braces. */
520 while (*--p == ' ' || *p == '\t')
521 continue;
e503aa60 522 p[1] = '\0';
ead9e56e
PE
523 while (*++decl == ' ' || *decl == '\t')
524 continue;
e9ce5688 525
1773ceee
PE
526 if (! name_start)
527 complain_at (loc, _("missing identifier in parameter declaration"));
528 else
529 {
530 char *name;
531 size_t name_len;
532
533 for (name_len = 1;
ead9e56e 534 memchr (alphanum, name_start[name_len], sizeof alphanum);
1773ceee
PE
535 name_len++)
536 continue;
537
538 name = xmalloc (name_len + 1);
539 memcpy (name, name_start, name_len);
540 name[name_len] = '\0';
541 muscle_pair_list_grow (type, decl, name);
542 free (name);
543 }
544
e9071366 545 gram_scanner_last_string_free ();
1773ceee
PE
546}
547
b50d2359
AD
548static void
549version_check (location const *loc, char const *version)
550{
551 if (strverscmp (version, PACKAGE_VERSION) > 0)
9b8a5ce0
AD
552 {
553 complain_at (*loc, "require bison %s, but have %s",
554 version, PACKAGE_VERSION);
555 exit (63);
556 }
b50d2359
AD
557}
558
1fec91df 559static void
3d2cbc26 560gram_error (location const *loc, char const *msg)
e9955c83 561{
ad8a3efc 562 complain_at (*loc, "%s", msg);
e9955c83 563}
e9ce5688
PE
564
565char const *
566token_name (int type)
567{
fc01665e 568 return yytname[YYTRANSLATE (type)];
e9ce5688 569}