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