]> git.saurik.com Git - bison.git/blame - src/parse-gram.y
* doc/bison.texinfo (Prologue, Rules, Actions, Union Decl, Action Decl):
[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
PE
31#include "muscle_tab.h"
32#include "output.h"
ca407bdf 33#include "quotearg.h"
e9955c83 34#include "reader.h"
3d2cbc26 35#include "symlist.h"
b50d2359 36#include "strverscmp.h"
e9955c83 37
b7295522
PE
38#define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
39static YYLTYPE lloc_default (YYLTYPE const *, int);
4cdb01db 40
b233d555
AD
41#define YY_LOCATION_PRINT(File, Loc) \
42 location_print (File, Loc)
43
b50d2359
AD
44static void version_check (location const *loc, char const *version);
45
6e649e65 46/* Request detailed syntax error messages, and pass them to GRAM_ERROR.
ad8a3efc 47 FIXME: depends on the undocumented availability of YYLLOC. */
e9955c83
AD
48#undef yyerror
49#define yyerror(Msg) \
fc5734fe 50 gram_error (&yylloc, Msg)
3d2cbc26 51static void gram_error (location const *, char const *);
e9955c83 52
e9ce5688 53static void add_param (char const *, char *, location);
1773ceee 54
04098407
PE
55static symbol_class current_class = unknown_sym;
56static uniqstr current_type = 0;
877519f8
PE
57static symbol *current_lhs;
58static location current_lhs_location;
04098407 59static int current_prec = 0;
e9955c83
AD
60%}
61
12ffdd28 62%debug
82b248ad 63%verbose
12ffdd28
PE
64%defines
65%locations
66%pure-parser
67%error-verbose
68%defines
69%name-prefix="gram_"
70
cd3684cf
AD
71%initial-action
72{
73 /* Bison's grammar can initial empty locations, hence a default
74 location is needed. */
75 @$.start.file = @$.end.file = current_file;
76 @$.start.line = @$.end.line = 1;
77 @$.start.column = @$.end.column = 0;
78}
e9955c83
AD
79
80/* Only NUMBERS have a value. */
81%union
82{
3d2cbc26
PE
83 symbol *symbol;
84 symbol_list *list;
e9955c83 85 int integer;
3d2cbc26
PE
86 char *chars;
87 assoc assoc;
88 uniqstr uniqstr;
e9955c83
AD
89};
90
f9a85a15 91/* Define the tokens together with their human representation. */
3d38c03a
AD
92%token GRAM_EOF 0 "end of file"
93%token STRING "string"
3d38c03a 94%token INT "integer"
e9955c83 95
9280d3ef
AD
96%token PERCENT_TOKEN "%token"
97%token PERCENT_NTERM "%nterm"
366eea36 98
9280d3ef 99%token PERCENT_TYPE "%type"
e9ce5688
PE
100%token PERCENT_DESTRUCTOR "%destructor {...}"
101%token PERCENT_PRINTER "%printer {...}"
366eea36 102
e9ce5688 103%token PERCENT_UNION "%union {...}"
366eea36 104
9280d3ef
AD
105%token PERCENT_LEFT "%left"
106%token PERCENT_RIGHT "%right"
107%token PERCENT_NONASSOC "%nonassoc"
04e60654 108
3d38c03a
AD
109%token PERCENT_PREC "%prec"
110%token PERCENT_DPREC "%dprec"
111%token PERCENT_MERGE "%merge"
e9955c83 112
e9955c83 113
ae7453f2
AD
114/*----------------------.
115| Global Declarations. |
116`----------------------*/
117
118%token
cd3684cf 119 PERCENT_DEBUG "%debug"
39a06c25 120 PERCENT_DEFAULT_PREC "%default-prec"
cd3684cf
AD
121 PERCENT_DEFINE "%define"
122 PERCENT_DEFINES "%defines"
123 PERCENT_ERROR_VERBOSE "%error-verbose"
124 PERCENT_EXPECT "%expect"
d6328241 125 PERCENT_EXPECT_RR "%expect-rr"
cd3684cf
AD
126 PERCENT_FILE_PREFIX "%file-prefix"
127 PERCENT_GLR_PARSER "%glr-parser"
128 PERCENT_INITIAL_ACTION "%initial-action {...}"
129 PERCENT_LEX_PARAM "%lex-param {...}"
130 PERCENT_LOCATIONS "%locations"
131 PERCENT_NAME_PREFIX "%name-prefix"
22fccf95 132 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
cd3684cf
AD
133 PERCENT_NO_LINES "%no-lines"
134 PERCENT_NONDETERMINISTIC_PARSER
135 "%nondeterministic-parser"
136 PERCENT_OUTPUT "%output"
137 PERCENT_PARSE_PARAM "%parse-param {...}"
138 PERCENT_PURE_PARSER "%pure-parser"
b50d2359 139 PERCENT_REQUIRE "%require"
cd3684cf
AD
140 PERCENT_SKELETON "%skeleton"
141 PERCENT_START "%start"
142 PERCENT_TOKEN_TABLE "%token-table"
143 PERCENT_VERBOSE "%verbose"
144 PERCENT_YACC "%yacc"
ae7453f2 145;
e9955c83 146
3d38c03a
AD
147%token TYPE "type"
148%token EQUAL "="
149%token SEMICOLON ";"
3d38c03a
AD
150%token PIPE "|"
151%token ID "identifier"
b7295522 152%token ID_COLON "identifier:"
e9955c83 153%token PERCENT_PERCENT "%%"
3d38c03a
AD
154%token PROLOGUE "%{...%}"
155%token EPILOGUE "epilogue"
156%token BRACED_CODE "{...}"
157
e9955c83 158
3d2cbc26 159%type <chars> STRING string_content
e9ce5688 160 "%destructor {...}"
cd3684cf 161 "%initial-action {...}"
e9ce5688
PE
162 "%lex-param {...}"
163 "%parse-param {...}"
164 "%printer {...}"
165 "%union {...}"
166 BRACED_CODE action
3d2cbc26 167 PROLOGUE EPILOGUE
82b248ad
AD
168%printer { fprintf (stderr, "\"%s\"", $$); }
169 STRING string_content
170%printer { fprintf (stderr, "{\n%s\n}", $$); }
171 "%destructor {...}"
172 "%initial-action {...}"
173 "%lex-param {...}"
174 "%parse-param {...}"
175 "%printer {...}"
176 "%union {...}"
177 BRACED_CODE action
178 PROLOGUE EPILOGUE
3d2cbc26 179%type <uniqstr> TYPE
82b248ad 180%printer { fprintf (stderr, "<%s>", $$); } TYPE
e9955c83 181%type <integer> INT
82b248ad
AD
182%printer { fprintf (stderr, "%d", $$); } INT
183%type <symbol> ID symbol string_as_id
2f4f028d 184%printer { fprintf (stderr, "%s", $$->tag); } ID symbol string_as_id
82b248ad
AD
185%type <symbol> ID_COLON
186%printer { fprintf (stderr, "%s:", $$->tag); } ID_COLON
2c569025 187%type <assoc> precedence_declarator
1e0bab92 188%type <list> symbols.1
e9955c83 189%%
2c569025 190
8efe435c 191input:
2c569025 192 declarations "%%" grammar epilogue.opt
e9955c83
AD
193;
194
2c569025
AD
195
196 /*------------------------------------.
197 | Declarations: before the first %%. |
198 `------------------------------------*/
199
200declarations:
e9955c83 201 /* Nothing */
b7295522 202| declarations declaration
e9955c83
AD
203;
204
2c569025
AD
205declaration:
206 grammar_declaration
207| PROLOGUE { prologue_augment ($1, @1); }
d0829076 208| "%debug" { debug_flag = true; }
c66dfadd
PE
209| "%define" string_content
210 {
211 static char one[] = "1";
212 muscle_insert ($2, one);
213 }
e9955c83 214| "%define" string_content string_content { muscle_insert ($2, $3); }
d0829076
PE
215| "%defines" { defines_flag = true; }
216| "%error-verbose" { error_verbose = true; }
d6328241 217| "%expect" INT { expected_sr_conflicts = $2; }
04098407 218| "%expect-rr" INT { expected_rr_conflicts = $2; }
e9955c83 219| "%file-prefix" "=" string_content { spec_file_prefix = $3; }
cd3684cf 220| "%glr-parser"
c66dfadd
PE
221 {
222 nondeterministic_parser = true;
223 glr_parser = true;
224 }
cd3684cf 225| "%initial-action {...}"
c66dfadd
PE
226 {
227 muscle_code_grow ("initial_action", $1, @1);
228 }
e9ce5688 229| "%lex-param {...}" { add_param ("lex_param", $1, @1); }
d0829076 230| "%locations" { locations_flag = true; }
e9955c83 231| "%name-prefix" "=" string_content { spec_name_prefix = $3; }
d0829076 232| "%no-lines" { no_lines_flag = true; }
04098407 233| "%nondeterministic-parser" { nondeterministic_parser = true; }
e9955c83 234| "%output" "=" string_content { spec_outfile = $3; }
cd3684cf 235| "%parse-param {...}" { add_param ("parse_param", $1, @1); }
916708d5 236| "%pure-parser" { pure_parser = true; }
b50d2359 237| "%require" string_content { version_check (&@2, $2); }
e9955c83 238| "%skeleton" string_content { skeleton = $2; }
d0829076 239| "%token-table" { token_table_flag = true; }
9dd5b378 240| "%verbose" { report_flag = report_states; }
d0829076 241| "%yacc" { yacc_flag = true; }
cd3684cf 242| /*FIXME: Err? What is this horror doing here? */ ";"
e9955c83
AD
243;
244
2c569025
AD
245grammar_declaration:
246 precedence_declaration
247| symbol_declaration
e9955c83
AD
248| "%start" symbol
249 {
8efe435c 250 grammar_start_symbol_set ($2, @2);
e9955c83 251 }
e9ce5688 252| "%union {...}"
2c569025 253 {
1221b78a
PE
254 char const *body = $1;
255
256 if (typed)
257 {
258 /* Concatenate the union bodies, turning the first one's
259 trailing '}' into '\n', and omitting the second one's '{'. */
260 char *code = muscle_find ("stype");
261 code[strlen (code) - 1] = '\n';
262 body++;
263 }
264
d0829076 265 typed = true;
1221b78a 266 muscle_code_grow ("stype", body, @1);
2c569025 267 }
e9ce5688 268| "%destructor {...}" symbols.1
9280d3ef 269 {
3d2cbc26 270 symbol_list *list;
e9ce5688
PE
271 for (list = $2; list; list = list->next)
272 symbol_destructor_set (list->sym, $1, @1);
273 symbol_list_free ($2);
9280d3ef 274 }
e9ce5688 275| "%printer {...}" symbols.1
366eea36 276 {
3d2cbc26 277 symbol_list *list;
e9ce5688
PE
278 for (list = $2; list; list = list->next)
279 symbol_printer_set (list->sym, $1, list->location);
280 symbol_list_free ($2);
366eea36 281 }
22fccf95 282| "%default-prec"
39a06c25 283 {
22fccf95
PE
284 default_prec = true;
285 }
286| "%no-default-prec"
287 {
288 default_prec = false;
39a06c25 289 }
2c569025
AD
290;
291
292symbol_declaration:
293 "%nterm" { current_class = nterm_sym; } symbol_defs.1
e9955c83
AD
294 {
295 current_class = unknown_sym;
296 current_type = NULL;
297 }
2c569025 298| "%token" { current_class = token_sym; } symbol_defs.1
e9955c83 299 {
2c569025 300 current_class = unknown_sym;
e9955c83
AD
301 current_type = NULL;
302 }
1e0bab92 303| "%type" TYPE symbols.1
e9955c83 304 {
3d2cbc26 305 symbol_list *list;
1e0bab92 306 for (list = $3; list; list = list->next)
1a31ed21 307 symbol_type_set (list->sym, $2, @2);
dafdc66f 308 symbol_list_free ($3);
e9955c83
AD
309 }
310;
311
2c569025 312precedence_declaration:
1e0bab92
AD
313 precedence_declarator type.opt symbols.1
314 {
3d2cbc26 315 symbol_list *list;
1e0bab92
AD
316 ++current_prec;
317 for (list = $3; list; list = list->next)
318 {
1a31ed21
AD
319 symbol_type_set (list->sym, current_type, @2);
320 symbol_precedence_set (list->sym, current_prec, $1, @1);
1e0bab92 321 }
dafdc66f 322 symbol_list_free ($3);
1e0bab92
AD
323 current_type = NULL;
324 }
e9955c83
AD
325;
326
2c569025 327precedence_declarator:
e9955c83
AD
328 "%left" { $$ = left_assoc; }
329| "%right" { $$ = right_assoc; }
330| "%nonassoc" { $$ = non_assoc; }
331;
332
333type.opt:
87fbb0bf 334 /* Nothing. */ { current_type = NULL; }
e9955c83
AD
335| TYPE { current_type = $1; }
336;
337
338/* One or more nonterminals to be %typed. */
e9955c83 339
1e0bab92
AD
340symbols.1:
341 symbol { $$ = symbol_list_new ($1, @1); }
342| symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
e9955c83
AD
343;
344
e9955c83
AD
345/* One token definition. */
346symbol_def:
347 TYPE
348 {
349 current_type = $1;
350 }
351| ID
352 {
073f9288 353 symbol_class_set ($1, current_class, @1, true);
1a31ed21 354 symbol_type_set ($1, current_type, @1);
e9955c83
AD
355 }
356| ID INT
357 {
073f9288 358 symbol_class_set ($1, current_class, @1, true);
1a31ed21 359 symbol_type_set ($1, current_type, @1);
e776192e 360 symbol_user_token_number_set ($1, $2, @2);
e9955c83
AD
361 }
362| ID string_as_id
363 {
073f9288 364 symbol_class_set ($1, current_class, @1, true);
1a31ed21 365 symbol_type_set ($1, current_type, @1);
a5d50994 366 symbol_make_alias ($1, $2, @$);
e9955c83
AD
367 }
368| ID INT string_as_id
369 {
073f9288 370 symbol_class_set ($1, current_class, @1, true);
1a31ed21 371 symbol_type_set ($1, current_type, @1);
e776192e 372 symbol_user_token_number_set ($1, $2, @2);
a5d50994 373 symbol_make_alias ($1, $3, @$);
e9955c83
AD
374 }
375;
376
377/* One or more symbol definitions. */
378symbol_defs.1:
379 symbol_def
e9955c83 380| symbol_defs.1 symbol_def
e9955c83
AD
381;
382
2c569025
AD
383
384 /*------------------------------------------.
385 | The grammar section: between the two %%. |
386 `------------------------------------------*/
387
388grammar:
1921f1d7
AD
389 rules_or_grammar_declaration
390| grammar rules_or_grammar_declaration
391;
392
393/* As a Bison extension, one can use the grammar declarations in the
b7295522 394 body of the grammar. */
1921f1d7 395rules_or_grammar_declaration:
e9955c83 396 rules
8d0a98bb 397| grammar_declaration ";"
b275314e
AD
398| error ";"
399 {
400 yyerrok;
401 }
e9955c83
AD
402;
403
404rules:
b7295522 405 ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
e9955c83
AD
406;
407
408rhses.1:
8f3596a6
AD
409 rhs { grammar_current_rule_end (@1); }
410| rhses.1 "|" rhs { grammar_current_rule_end (@3); }
8d0a98bb 411| rhses.1 ";"
e9955c83
AD
412;
413
414rhs:
415 /* Nothing. */
8f3596a6 416 { grammar_current_rule_begin (current_lhs, current_lhs_location); }
e9955c83 417| rhs symbol
8efe435c 418 { grammar_current_rule_symbol_append ($2, @2); }
e9955c83 419| rhs action
8efe435c 420 { grammar_current_rule_action_append ($2, @2); }
e9955c83 421| rhs "%prec" symbol
e776192e 422 { grammar_current_rule_prec_set ($3, @3); }
676385e2
PH
423| rhs "%dprec" INT
424 { grammar_current_rule_dprec_set ($3, @3); }
425| rhs "%merge" TYPE
426 { grammar_current_rule_merge_set ($3, @3); }
e9955c83
AD
427;
428
429symbol:
430 ID { $$ = $1; }
431| string_as_id { $$ = $1; }
e9955c83
AD
432;
433
434action:
435 BRACED_CODE
ca407bdf 436 { $$ = $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 {
cd3684cf 459 muscle_code_grow ("epilogue", $2, @2);
7ec2d4cd 460 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
545 scanner_last_string_free ();
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}