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