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