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