]> git.saurik.com Git - bison.git/blame - src/parse-gram.y
Alexandre Duret-Lutz <adl@gnu.org>
[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
04098407
PE
51static symbol_class current_class = unknown_sym;
52static uniqstr current_type = 0;
3d2cbc26
PE
53symbol *current_lhs;
54location current_lhs_location;
55assoc current_assoc;
04098407 56static int current_prec = 0;
e9955c83
AD
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; }
fb9712a9 205| "%define" string_content { muscle_insert ($2, "1"); }
e9955c83 206| "%define" string_content string_content { muscle_insert ($2, $3); }
d0829076
PE
207| "%defines" { defines_flag = true; }
208| "%error-verbose" { error_verbose = true; }
d6328241 209| "%expect" INT { expected_sr_conflicts = $2; }
04098407 210| "%expect-rr" INT { expected_rr_conflicts = $2; }
e9955c83 211| "%file-prefix" "=" string_content { spec_file_prefix = $3; }
cd3684cf
AD
212| "%glr-parser"
213 {
214 nondeterministic_parser = true;
215 glr_parser = true;
216 }
217| "%initial-action {...}"
218 {
219 muscle_code_grow ("initial_action", $1, @1);
220 }
e9ce5688 221| "%lex-param {...}" { add_param ("lex_param", $1, @1); }
d0829076 222| "%locations" { locations_flag = true; }
e9955c83 223| "%name-prefix" "=" string_content { spec_name_prefix = $3; }
d0829076 224| "%no-lines" { no_lines_flag = true; }
04098407 225| "%nondeterministic-parser" { nondeterministic_parser = true; }
e9955c83 226| "%output" "=" string_content { spec_outfile = $3; }
cd3684cf 227| "%parse-param {...}" { add_param ("parse_param", $1, @1); }
916708d5 228| "%pure-parser" { pure_parser = true; }
e9955c83 229| "%skeleton" string_content { skeleton = $2; }
d0829076 230| "%token-table" { token_table_flag = true; }
9dd5b378 231| "%verbose" { report_flag = report_states; }
d0829076 232| "%yacc" { yacc_flag = true; }
cd3684cf 233| /*FIXME: Err? What is this horror doing here? */ ";"
e9955c83
AD
234;
235
2c569025
AD
236grammar_declaration:
237 precedence_declaration
238| symbol_declaration
e9955c83
AD
239| "%start" symbol
240 {
8efe435c 241 grammar_start_symbol_set ($2, @2);
e9955c83 242 }
e9ce5688 243| "%union {...}"
2c569025 244 {
d0829076 245 typed = true;
e9ce5688
PE
246 MUSCLE_INSERT_INT ("stype_line", @1.start.line);
247 muscle_insert ("stype", $1);
2c569025 248 }
e9ce5688 249| "%destructor {...}" symbols.1
9280d3ef 250 {
3d2cbc26 251 symbol_list *list;
e9ce5688
PE
252 for (list = $2; list; list = list->next)
253 symbol_destructor_set (list->sym, $1, @1);
254 symbol_list_free ($2);
9280d3ef 255 }
e9ce5688 256| "%printer {...}" symbols.1
366eea36 257 {
3d2cbc26 258 symbol_list *list;
e9ce5688
PE
259 for (list = $2; list; list = list->next)
260 symbol_printer_set (list->sym, $1, list->location);
261 symbol_list_free ($2);
366eea36 262 }
22fccf95 263| "%default-prec"
39a06c25 264 {
22fccf95
PE
265 default_prec = true;
266 }
267| "%no-default-prec"
268 {
269 default_prec = false;
39a06c25 270 }
2c569025
AD
271;
272
273symbol_declaration:
274 "%nterm" { current_class = nterm_sym; } symbol_defs.1
e9955c83
AD
275 {
276 current_class = unknown_sym;
277 current_type = NULL;
278 }
2c569025 279| "%token" { current_class = token_sym; } symbol_defs.1
e9955c83 280 {
2c569025 281 current_class = unknown_sym;
e9955c83
AD
282 current_type = NULL;
283 }
1e0bab92 284| "%type" TYPE symbols.1
e9955c83 285 {
3d2cbc26 286 symbol_list *list;
1e0bab92 287 for (list = $3; list; list = list->next)
1a31ed21 288 symbol_type_set (list->sym, $2, @2);
dafdc66f 289 symbol_list_free ($3);
e9955c83
AD
290 }
291;
292
2c569025 293precedence_declaration:
1e0bab92
AD
294 precedence_declarator type.opt symbols.1
295 {
3d2cbc26 296 symbol_list *list;
1e0bab92
AD
297 ++current_prec;
298 for (list = $3; list; list = list->next)
299 {
1a31ed21
AD
300 symbol_type_set (list->sym, current_type, @2);
301 symbol_precedence_set (list->sym, current_prec, $1, @1);
1e0bab92 302 }
dafdc66f 303 symbol_list_free ($3);
1e0bab92
AD
304 current_type = NULL;
305 }
e9955c83
AD
306;
307
2c569025 308precedence_declarator:
e9955c83
AD
309 "%left" { $$ = left_assoc; }
310| "%right" { $$ = right_assoc; }
311| "%nonassoc" { $$ = non_assoc; }
312;
313
314type.opt:
87fbb0bf 315 /* Nothing. */ { current_type = NULL; }
e9955c83
AD
316| TYPE { current_type = $1; }
317;
318
319/* One or more nonterminals to be %typed. */
e9955c83 320
1e0bab92
AD
321symbols.1:
322 symbol { $$ = symbol_list_new ($1, @1); }
323| symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
e9955c83
AD
324;
325
e9955c83
AD
326/* One token definition. */
327symbol_def:
328 TYPE
329 {
330 current_type = $1;
331 }
332| ID
333 {
e776192e 334 symbol_class_set ($1, current_class, @1);
1a31ed21 335 symbol_type_set ($1, current_type, @1);
e9955c83
AD
336 }
337| ID INT
338 {
e776192e 339 symbol_class_set ($1, current_class, @1);
1a31ed21 340 symbol_type_set ($1, current_type, @1);
e776192e 341 symbol_user_token_number_set ($1, $2, @2);
e9955c83
AD
342 }
343| ID string_as_id
344 {
e776192e 345 symbol_class_set ($1, current_class, @1);
1a31ed21 346 symbol_type_set ($1, current_type, @1);
a5d50994 347 symbol_make_alias ($1, $2, @$);
e9955c83
AD
348 }
349| ID INT string_as_id
350 {
e776192e 351 symbol_class_set ($1, current_class, @1);
1a31ed21 352 symbol_type_set ($1, current_type, @1);
e776192e 353 symbol_user_token_number_set ($1, $2, @2);
a5d50994 354 symbol_make_alias ($1, $3, @$);
e9955c83
AD
355 }
356;
357
358/* One or more symbol definitions. */
359symbol_defs.1:
360 symbol_def
e9955c83 361| symbol_defs.1 symbol_def
e9955c83
AD
362;
363
2c569025
AD
364
365 /*------------------------------------------.
366 | The grammar section: between the two %%. |
367 `------------------------------------------*/
368
369grammar:
1921f1d7
AD
370 rules_or_grammar_declaration
371| grammar rules_or_grammar_declaration
372;
373
374/* As a Bison extension, one can use the grammar declarations in the
b7295522 375 body of the grammar. */
1921f1d7 376rules_or_grammar_declaration:
e9955c83 377 rules
8d0a98bb 378| grammar_declaration ";"
b275314e
AD
379 {
380 if (yacc_flag)
381 complain_at (@$, _("POSIX forbids declarations in the grammar"));
382 }
383| error ";"
384 {
385 yyerrok;
386 }
e9955c83
AD
387;
388
389rules:
b7295522 390 ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
e9955c83
AD
391;
392
393rhses.1:
8efe435c
AD
394 rhs { grammar_rule_end (@1); }
395| rhses.1 "|" rhs { grammar_rule_end (@3); }
8d0a98bb 396| rhses.1 ";"
e9955c83
AD
397;
398
399rhs:
400 /* Nothing. */
8efe435c 401 { grammar_rule_begin (current_lhs, current_lhs_location); }
e9955c83 402| rhs symbol
8efe435c 403 { grammar_current_rule_symbol_append ($2, @2); }
e9955c83 404| rhs action
8efe435c 405 { grammar_current_rule_action_append ($2, @2); }
e9955c83 406| rhs "%prec" symbol
e776192e 407 { grammar_current_rule_prec_set ($3, @3); }
676385e2
PH
408| rhs "%dprec" INT
409 { grammar_current_rule_dprec_set ($3, @3); }
410| rhs "%merge" TYPE
411 { grammar_current_rule_merge_set ($3, @3); }
e9955c83
AD
412;
413
414symbol:
415 ID { $$ = $1; }
416| string_as_id { $$ = $1; }
e9955c83
AD
417;
418
419action:
420 BRACED_CODE
ca407bdf 421 { $$ = $1; }
e9955c83
AD
422;
423
ca407bdf 424/* A string used as an ID: quote it. */
e9955c83
AD
425string_as_id:
426 STRING
427 {
ca407bdf 428 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
e776192e 429 symbol_class_set ($$, token_sym, @1);
e9955c83
AD
430 }
431;
432
ca407bdf 433/* A string used for its contents. Don't quote it. */
e9955c83
AD
434string_content:
435 STRING
ca407bdf
PE
436 { $$ = $1; }
437;
e9955c83
AD
438
439
440epilogue.opt:
441 /* Nothing. */
e9955c83
AD
442| "%%" EPILOGUE
443 {
cd3684cf 444 muscle_code_grow ("epilogue", $2, @2);
7ec2d4cd 445 scanner_last_string_free ();
e9955c83
AD
446 }
447;
448
e9955c83 449%%
b7295522
PE
450
451
452/* Return the location of the left-hand side of a rule whose
453 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
454 the right-hand side, and return an empty location equal to the end
455 boundary of RHS[0] if the right-hand side is empty. */
456
457static YYLTYPE
458lloc_default (YYLTYPE const *rhs, int n)
459{
460 int i;
a737b216 461 YYLTYPE loc;
62cb8a99
PE
462
463 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
464 The bug is fixed in 7.4.2m, but play it safe for now. */
465 loc.start = rhs[n].end;
466 loc.end = rhs[n].end;
b7295522 467
5320ca4d
PE
468 /* Ignore empty nonterminals the start of the the right-hand side.
469 Do not bother to ignore them at the end of the right-hand side,
470 since empty nonterminals have the same end as their predecessors. */
b7295522
PE
471 for (i = 1; i <= n; i++)
472 if (! equal_boundaries (rhs[i].start, rhs[i].end))
473 {
a737b216 474 loc.start = rhs[i].start;
b7295522
PE
475 break;
476 }
477
a737b216 478 return loc;
b7295522
PE
479}
480
481
482/* Add a lex-param or a parse-param (depending on TYPE) with
483 declaration DECL and location LOC. */
484
1773ceee 485static void
e9ce5688 486add_param (char const *type, char *decl, location loc)
1773ceee 487{
ead9e56e 488 static char const alphanum[26 + 26 + 1 + 10] =
1773ceee
PE
489 "abcdefghijklmnopqrstuvwxyz"
490 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ead9e56e
PE
491 "_"
492 "0123456789";
1773ceee 493 char const *name_start = NULL;
e9ce5688 494 char *p;
1773ceee 495
e503aa60
AD
496 /* Stop on last actual character. */
497 for (p = decl; p[1]; p++)
ead9e56e
PE
498 if ((p == decl
499 || ! memchr (alphanum, p[-1], sizeof alphanum))
500 && memchr (alphanum, p[0], sizeof alphanum - 10))
1773ceee
PE
501 name_start = p;
502
ead9e56e
PE
503 /* Strip the surrounding '{' and '}', and any blanks just inside
504 the braces. */
505 while (*--p == ' ' || *p == '\t')
506 continue;
e503aa60 507 p[1] = '\0';
ead9e56e
PE
508 while (*++decl == ' ' || *decl == '\t')
509 continue;
e9ce5688 510
1773ceee
PE
511 if (! name_start)
512 complain_at (loc, _("missing identifier in parameter declaration"));
513 else
514 {
515 char *name;
516 size_t name_len;
517
518 for (name_len = 1;
ead9e56e 519 memchr (alphanum, name_start[name_len], sizeof alphanum);
1773ceee
PE
520 name_len++)
521 continue;
522
523 name = xmalloc (name_len + 1);
524 memcpy (name, name_start, name_len);
525 name[name_len] = '\0';
526 muscle_pair_list_grow (type, decl, name);
527 free (name);
528 }
529
530 scanner_last_string_free ();
531}
532
1fec91df 533static void
3d2cbc26 534gram_error (location const *loc, char const *msg)
e9955c83 535{
ad8a3efc 536 complain_at (*loc, "%s", msg);
e9955c83 537}
e9ce5688
PE
538
539char const *
540token_name (int type)
541{
fc01665e 542 return yytname[YYTRANSLATE (type)];
e9ce5688 543}