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