]> git.saurik.com Git - bison.git/blob - src/parse-gram.y
dcdd9f52e530bb7e5d33191123c5f273d60a7ccf
[bison.git] / src / parse-gram.y
1 /* Bison Grammar Parser -*- C -*-
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA
20 */
21
22
23 %debug
24 %defines
25 %locations
26 %pure-parser
27 // %error-verbose
28 %defines
29 %name-prefix="gram_"
30
31 %{
32 #include "system.h"
33 #include "complain.h"
34 #include "muscle_tab.h"
35 #include "files.h"
36 #include "getargs.h"
37 #include "output.h"
38 #include "symlist.h"
39 #include "gram.h"
40 #include "reader.h"
41 #include "conflicts.h"
42
43 /* Produce verbose syntax errors. */
44 #define YYERROR_VERBOSE 1
45 #define YYLLOC_DEFAULT(Current, Rhs, N) \
46 do { \
47 if (N) \
48 { \
49 Current.first_column = Rhs[1].first_column; \
50 Current.first_line = Rhs[1].first_line; \
51 Current.last_column = Rhs[N].last_column; \
52 Current.last_line = Rhs[N].last_line; \
53 } \
54 else \
55 { \
56 Current = Rhs[0]; \
57 } \
58 } while (0)
59
60 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
61 FIXME: depends on the undocumented availability of YYLLOC. */
62 #undef yyerror
63 #define yyerror(Msg) \
64 gram_error (&yylloc, Msg)
65 static void gram_error (location_t const *, char const *);
66
67 #define YYPRINT(File, Type, Value) \
68 print_token_value (File, Type, &Value)
69 static void print_token_value (FILE *, int, YYSTYPE const *);
70
71 static void add_param (char const *, char const *, location_t);
72
73 symbol_class current_class = unknown_sym;
74 struniq_t current_type = 0;
75 symbol_t *current_lhs;
76 location_t current_lhs_location;
77 assoc_t current_assoc;
78 int current_prec = 0;
79 braced_code_t current_braced_code = action_braced_code;
80 %}
81
82
83 /* Only NUMBERS have a value. */
84 %union
85 {
86 symbol_t *symbol;
87 symbol_list_t *list;
88 int integer;
89 char *string;
90 assoc_t assoc;
91 struniq_t struniq;
92 };
93
94 /* Define the tokens together with their human representation. */
95 %token GRAM_EOF 0 "end of file"
96 %token STRING "string"
97 %token INT "integer"
98
99 %token PERCENT_TOKEN "%token"
100 %token PERCENT_NTERM "%nterm"
101
102 %token PERCENT_TYPE "%type"
103 %token PERCENT_DESTRUCTOR "%destructor"
104 %token PERCENT_PRINTER "%printer"
105
106 %token PERCENT_UNION "%union"
107
108 %token PERCENT_LEFT "%left"
109 %token PERCENT_RIGHT "%right"
110 %token PERCENT_NONASSOC "%nonassoc"
111
112 %token PERCENT_PREC "%prec"
113 %token PERCENT_DPREC "%dprec"
114 %token PERCENT_MERGE "%merge"
115
116
117 /*----------------------.
118 | Global Declarations. |
119 `----------------------*/
120
121 %token
122 PERCENT_DEBUG "%debug"
123 PERCENT_DEFINE "%define"
124 PERCENT_DEFINES "%defines"
125 PERCENT_ERROR_VERBOSE "%error-verbose"
126 PERCENT_EXPECT "%expect"
127 PERCENT_FILE_PREFIX "%file-prefix"
128 PERCENT_GLR_PARSER "%glr-parser"
129 PERCENT_LEX_PARAM "%lex-param"
130 PERCENT_LOCATIONS "%locations"
131 PERCENT_NAME_PREFIX "%name-prefix"
132 PERCENT_NO_LINES "%no-lines"
133 PERCENT_OUTPUT "%output"
134 PERCENT_PARSE_PARAM "%parse-param"
135 PERCENT_PURE_PARSER "%pure-parser"
136 PERCENT_SKELETON "%skeleton"
137 PERCENT_START "%start"
138 PERCENT_TOKEN_TABLE "%token-table"
139 PERCENT_VERBOSE "%verbose"
140 PERCENT_YACC "%yacc"
141 ;
142
143 %token TYPE "type"
144 %token EQUAL "="
145 %token SEMICOLON ";"
146 %token COLON ":"
147 %token PIPE "|"
148 %token ID "identifier"
149 %token PERCENT_PERCENT "%%"
150 %token PROLOGUE "%{...%}"
151 %token EPILOGUE "epilogue"
152 %token BRACED_CODE "{...}"
153
154
155 %type <string> STRING string_content
156 BRACED_CODE code_content action
157 PROLOGUE EPILOGUE
158 %type <struniq> TYPE
159 %type <integer> INT
160 %type <symbol> ID symbol string_as_id
161 %type <assoc> precedence_declarator
162 %type <list> symbols.1
163 %%
164
165 input:
166 declarations "%%" grammar epilogue.opt
167 ;
168
169
170 /*------------------------------------.
171 | Declarations: before the first %%. |
172 `------------------------------------*/
173
174 declarations:
175 /* Nothing */
176 | declarations declaration semi_colon.opt
177 ;
178
179 declaration:
180 grammar_declaration
181 | PROLOGUE { prologue_augment ($1, @1); }
182 | "%debug" { debug_flag = 1; }
183 | "%define" string_content string_content { muscle_insert ($2, $3); }
184 | "%defines" { defines_flag = 1; }
185 | "%error-verbose" { error_verbose = 1; }
186 | "%expect" INT { expected_conflicts = $2; }
187 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
188 | "%glr-parser" { glr_parser = 1; }
189 | "%lex-param" code_content { add_param ("lex_param", $2, @2); }
190 | "%locations" { locations_flag = 1; }
191 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
192 | "%no-lines" { no_lines_flag = 1; }
193 | "%output" "=" string_content { spec_outfile = $3; }
194 | "%parse-param" code_content { add_param ("parse_param", $2, @2); }
195 | "%pure-parser" { pure_parser = 1; }
196 | "%skeleton" string_content { skeleton = $2; }
197 | "%token-table" { token_table_flag = 1; }
198 | "%verbose" { report_flag = 1; }
199 | "%yacc" { yacc_flag = 1; }
200 ;
201
202 grammar_declaration:
203 precedence_declaration
204 | symbol_declaration
205 | "%start" symbol
206 {
207 grammar_start_symbol_set ($2, @2);
208 }
209 | "%union" BRACED_CODE
210 {
211 typed = 1;
212 MUSCLE_INSERT_INT ("stype_line", @2.first_line);
213 muscle_insert ("stype", $2);
214 }
215 | "%destructor"
216 { current_braced_code = destructor_braced_code; }
217 BRACED_CODE symbols.1
218 {
219 symbol_list_t *list;
220 for (list = $4; list; list = list->next)
221 symbol_destructor_set (list->sym, $3, @3);
222 symbol_list_free ($4);
223 current_braced_code = action_braced_code;
224 }
225 | "%printer"
226 { current_braced_code = printer_braced_code; }
227 BRACED_CODE symbols.1
228 {
229 symbol_list_t *list;
230 for (list = $4; list; list = list->next)
231 symbol_printer_set (list->sym, $3, list->location);
232 symbol_list_free ($4);
233 current_braced_code = action_braced_code;
234 }
235 ;
236
237 symbol_declaration:
238 "%nterm" { current_class = nterm_sym; } symbol_defs.1
239 {
240 current_class = unknown_sym;
241 current_type = NULL;
242 }
243 | "%token" { current_class = token_sym; } symbol_defs.1
244 {
245 current_class = unknown_sym;
246 current_type = NULL;
247 }
248 | "%type" TYPE symbols.1
249 {
250 symbol_list_t *list;
251 for (list = $3; list; list = list->next)
252 symbol_type_set (list->sym, $2, @2);
253 symbol_list_free ($3);
254 }
255 ;
256
257 precedence_declaration:
258 precedence_declarator type.opt symbols.1
259 {
260 symbol_list_t *list;
261 ++current_prec;
262 for (list = $3; list; list = list->next)
263 {
264 symbol_type_set (list->sym, current_type, @2);
265 symbol_precedence_set (list->sym, current_prec, $1, @1);
266 }
267 symbol_list_free ($3);
268 current_type = NULL;
269 }
270 ;
271
272 precedence_declarator:
273 "%left" { $$ = left_assoc; }
274 | "%right" { $$ = right_assoc; }
275 | "%nonassoc" { $$ = non_assoc; }
276 ;
277
278 type.opt:
279 /* Nothing. */ { current_type = NULL; }
280 | TYPE { current_type = $1; }
281 ;
282
283 /* One or more nonterminals to be %typed. */
284
285 symbols.1:
286 symbol { $$ = symbol_list_new ($1, @1); }
287 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
288 ;
289
290 /* One token definition. */
291 symbol_def:
292 TYPE
293 {
294 current_type = $1;
295 }
296 | ID
297 {
298 symbol_class_set ($1, current_class, @1);
299 symbol_type_set ($1, current_type, @1);
300 }
301 | ID INT
302 {
303 symbol_class_set ($1, current_class, @1);
304 symbol_type_set ($1, current_type, @1);
305 symbol_user_token_number_set ($1, $2, @2);
306 }
307 | ID string_as_id
308 {
309 symbol_class_set ($1, current_class, @1);
310 symbol_type_set ($1, current_type, @1);
311 symbol_make_alias ($1, $2, @$);
312 }
313 | ID INT string_as_id
314 {
315 symbol_class_set ($1, current_class, @1);
316 symbol_type_set ($1, current_type, @1);
317 symbol_user_token_number_set ($1, $2, @2);
318 symbol_make_alias ($1, $3, @$);
319 }
320 ;
321
322 /* One or more symbol definitions. */
323 symbol_defs.1:
324 symbol_def
325 {;}
326 | symbol_defs.1 symbol_def
327 {;}
328 ;
329
330
331 /*------------------------------------------.
332 | The grammar section: between the two %%. |
333 `------------------------------------------*/
334
335 grammar:
336 rules_or_grammar_declaration
337 | grammar rules_or_grammar_declaration
338 ;
339
340 /* As a Bison extension, one can use the grammar declarations in the
341 body of the grammar. But to remain LALR(1), they must be ended
342 with a semi-colon. */
343 rules_or_grammar_declaration:
344 rules
345 | grammar_declaration ";"
346 {
347 if (yacc_flag)
348 complain_at (@$, _("POSIX forbids declarations in the grammar"));
349 }
350 | error ";"
351 {
352 yyerrok;
353 }
354 ;
355
356 rules:
357 ID ":" { current_lhs = $1; current_lhs_location = @1; } rhses.1 ";"
358 {;}
359 ;
360
361 rhses.1:
362 rhs { grammar_rule_end (@1); }
363 | rhses.1 "|" rhs { grammar_rule_end (@3); }
364 ;
365
366 rhs:
367 /* Nothing. */
368 { grammar_rule_begin (current_lhs, current_lhs_location); }
369 | rhs symbol
370 { grammar_current_rule_symbol_append ($2, @2); }
371 | rhs action
372 { grammar_current_rule_action_append ($2, @2); }
373 | rhs "%prec" symbol
374 { grammar_current_rule_prec_set ($3, @3); }
375 | rhs "%dprec" INT
376 { grammar_current_rule_dprec_set ($3, @3); }
377 | rhs "%merge" TYPE
378 { grammar_current_rule_merge_set ($3, @3); }
379 ;
380
381 symbol:
382 ID { $$ = $1; }
383 | string_as_id { $$ = $1; }
384 ;
385
386 action:
387 BRACED_CODE
388 { $$ = $1; }
389 ;
390
391 /* A string used as an ID: we have to keep the quotes. */
392 string_as_id:
393 STRING
394 {
395 $$ = symbol_get ($1, @1);
396 symbol_class_set ($$, token_sym, @1);
397 }
398 ;
399
400 /* A string used for its contents. Strip the quotes. */
401 string_content:
402 STRING
403 {
404 $$ = $1 + 1;
405 $$[strlen ($$) - 1] = '\0';
406 };
407
408
409 /* A BRACED_CODE used for its contents. Strip the braces. */
410 code_content:
411 BRACED_CODE
412 {
413 $$ = $1 + 1;
414 $$[strlen ($$) - 1] = '\0';
415 };
416
417
418 epilogue.opt:
419 /* Nothing. */
420 | "%%" EPILOGUE
421 {
422 epilogue_augment ($2, @2);
423 scanner_last_string_free ();
424 }
425 ;
426
427 semi_colon.opt:
428 /* Nothing. */
429 | ";"
430 ;
431 %%
432 static void
433 add_param (char const *type, char const *decl, location_t loc)
434 {
435 static char const alphanum[] =
436 "0123456789"
437 "abcdefghijklmnopqrstuvwxyz"
438 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
439 "_";
440 char const *alpha = alphanum + 10;
441 char const *name_start = NULL;
442 char const *p;
443
444 for (p = decl; *p; p++)
445 if ((p == decl || ! strchr (alphanum, p[-1])) && strchr (alpha, p[0]))
446 name_start = p;
447
448 if (! name_start)
449 complain_at (loc, _("missing identifier in parameter declaration"));
450 else
451 {
452 char *name;
453 size_t name_len;
454
455 for (name_len = 1;
456 name_start[name_len] && strchr (alphanum, name_start[name_len]);
457 name_len++)
458 continue;
459
460 name = xmalloc (name_len + 1);
461 memcpy (name, name_start, name_len);
462 name[name_len] = '\0';
463 muscle_pair_list_grow (type, decl, name);
464 free (name);
465 }
466
467 scanner_last_string_free ();
468 }
469
470 /*------------------------------------------------------------------.
471 | When debugging the parser, display tokens' locations and values. |
472 `------------------------------------------------------------------*/
473
474 static void
475 print_token_value (FILE *file, int type, YYSTYPE const *value)
476 {
477 fputc (' ', file);
478 switch (type)
479 {
480 case ID:
481 fprintf (file, " = %s", value->symbol->tag);
482 break;
483
484 case INT:
485 fprintf (file, " = %d", value->integer);
486 break;
487
488 case STRING:
489 fprintf (file, " = \"%s\"", value->string);
490 break;
491
492 case TYPE:
493 fprintf (file, " = <%s>", value->struniq);
494 break;
495
496 case BRACED_CODE:
497 case PROLOGUE:
498 case EPILOGUE:
499 fprintf (file, " = {{ %s }}", value->string);
500 break;
501
502 default:
503 fprintf (file, "unknown token type");
504 break;
505 }
506 }
507
508 static void
509 gram_error (location_t const *loc, char const *msg)
510 {
511 complain_at (*loc, "%s", msg);
512 }