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