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