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