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