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