]> git.saurik.com Git - bison.git/blob - src/parse-gram.y
(Yacc Library): New node. Regenerate top menu.
[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
66 #define YYPRINT(File, Type, Value) \
67 print_token_value (File, Type, &Value)
68 static void print_token_value (FILE *file, int type, YYSTYPE const *value);
69
70 static void add_param (char const *, char const *, location_t);
71
72 symbol_class current_class = unknown_sym;
73 struniq_t current_type = 0;
74 symbol_t *current_lhs;
75 location_t current_lhs_location;
76 assoc_t current_assoc;
77 int current_prec = 0;
78 braced_code_t current_braced_code = action_braced_code;
79 %}
80
81
82 /* Only NUMBERS have a value. */
83 %union
84 {
85 symbol_t *symbol;
86 symbol_list_t *list;
87 int integer;
88 char *string;
89 assoc_t assoc;
90 struniq_t struniq;
91 };
92
93 /* Define the tokens together with their human representation. */
94 %token GRAM_EOF 0 "end of file"
95 %token STRING "string"
96 %token INT "integer"
97
98 %token PERCENT_TOKEN "%token"
99 %token PERCENT_NTERM "%nterm"
100
101 %token PERCENT_TYPE "%type"
102 %token PERCENT_DESTRUCTOR "%destructor"
103 %token PERCENT_PRINTER "%printer"
104
105 %token PERCENT_UNION "%union"
106
107 %token PERCENT_LEFT "%left"
108 %token PERCENT_RIGHT "%right"
109 %token PERCENT_NONASSOC "%nonassoc"
110
111 %token PERCENT_PREC "%prec"
112 %token PERCENT_DPREC "%dprec"
113 %token PERCENT_MERGE "%merge"
114
115
116 /*----------------------.
117 | Global Declarations. |
118 `----------------------*/
119
120 %token
121 PERCENT_DEBUG "%debug"
122 PERCENT_DEFINE "%define"
123 PERCENT_DEFINES "%defines"
124 PERCENT_ERROR_VERBOSE "%error-verbose"
125 PERCENT_EXPECT "%expect"
126 PERCENT_FILE_PREFIX "%file-prefix"
127 PERCENT_GLR_PARSER "%glr-parser"
128 PERCENT_LEX_PARAM "%lex-param"
129 PERCENT_LOCATIONS "%locations"
130 PERCENT_NAME_PREFIX "%name-prefix"
131 PERCENT_NO_LINES "%no-lines"
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 COLON ":"
146 %token PIPE "|"
147 %token ID "identifier"
148 %token PERCENT_PERCENT "%%"
149 %token PROLOGUE "%{...%}"
150 %token EPILOGUE "epilogue"
151 %token BRACED_CODE "{...}"
152
153
154 %type <string> STRING string_content
155 BRACED_CODE code_content action
156 PROLOGUE EPILOGUE
157 %type <struniq> TYPE
158 %type <integer> INT
159 %type <symbol> ID symbol string_as_id
160 %type <assoc> precedence_declarator
161 %type <list> symbols.1
162 %%
163
164 input:
165 declarations "%%" grammar epilogue.opt
166 ;
167
168
169 /*------------------------------------.
170 | Declarations: before the first %%. |
171 `------------------------------------*/
172
173 declarations:
174 /* Nothing */
175 | declarations declaration semi_colon.opt
176 ;
177
178 declaration:
179 grammar_declaration
180 | PROLOGUE { prologue_augment ($1, @1); }
181 | "%debug" { debug_flag = 1; }
182 | "%define" string_content string_content { muscle_insert ($2, $3); }
183 | "%defines" { defines_flag = 1; }
184 | "%error-verbose" { error_verbose = 1; }
185 | "%expect" INT { expected_conflicts = $2; }
186 | "%file-prefix" "=" string_content { spec_file_prefix = $3; }
187 | "%glr-parser" { glr_parser = 1; }
188 | "%lex-param" code_content { add_param ("lex_param", $2, @2); }
189 | "%locations" { locations_flag = 1; }
190 | "%name-prefix" "=" string_content { spec_name_prefix = $3; }
191 | "%no-lines" { no_lines_flag = 1; }
192 | "%output" "=" string_content { spec_outfile = $3; }
193 | "%parse-param" code_content { add_param ("parse_param", $2, @2); }
194 | "%pure-parser" { pure_parser = 1; }
195 | "%skeleton" string_content { skeleton = $2; }
196 | "%token-table" { token_table_flag = 1; }
197 | "%verbose" { report_flag = 1; }
198 | "%yacc" { yacc_flag = 1; }
199 ;
200
201 grammar_declaration:
202 precedence_declaration
203 | symbol_declaration
204 | "%start" symbol
205 {
206 grammar_start_symbol_set ($2, @2);
207 }
208 | "%union" BRACED_CODE
209 {
210 typed = 1;
211 MUSCLE_INSERT_INT ("stype_line", @2.first_line);
212 muscle_insert ("stype", $2);
213 }
214 | "%destructor"
215 { current_braced_code = destructor_braced_code; }
216 BRACED_CODE symbols.1
217 {
218 symbol_list_t *list;
219 for (list = $4; list; list = list->next)
220 symbol_destructor_set (list->sym, $3, @3);
221 symbol_list_free ($4);
222 current_braced_code = action_braced_code;
223 }
224 | "%printer"
225 { current_braced_code = printer_braced_code; }
226 BRACED_CODE symbols.1
227 {
228 symbol_list_t *list;
229 for (list = $4; list; list = list->next)
230 symbol_printer_set (list->sym, $3, list->location);
231 symbol_list_free ($4);
232 current_braced_code = action_braced_code;
233 }
234 ;
235
236 symbol_declaration:
237 "%nterm" { current_class = nterm_sym; } symbol_defs.1
238 {
239 current_class = unknown_sym;
240 current_type = NULL;
241 }
242 | "%token" { current_class = token_sym; } symbol_defs.1
243 {
244 current_class = unknown_sym;
245 current_type = NULL;
246 }
247 | "%type" TYPE symbols.1
248 {
249 symbol_list_t *list;
250 for (list = $3; list; list = list->next)
251 symbol_type_set (list->sym, $2, @2);
252 symbol_list_free ($3);
253 }
254 ;
255
256 precedence_declaration:
257 precedence_declarator type.opt symbols.1
258 {
259 symbol_list_t *list;
260 ++current_prec;
261 for (list = $3; list; list = list->next)
262 {
263 symbol_type_set (list->sym, current_type, @2);
264 symbol_precedence_set (list->sym, current_prec, $1, @1);
265 }
266 symbol_list_free ($3);
267 current_type = NULL;
268 }
269 ;
270
271 precedence_declarator:
272 "%left" { $$ = left_assoc; }
273 | "%right" { $$ = right_assoc; }
274 | "%nonassoc" { $$ = non_assoc; }
275 ;
276
277 type.opt:
278 /* Nothing. */ { current_type = NULL; }
279 | TYPE { current_type = $1; }
280 ;
281
282 /* One or more nonterminals to be %typed. */
283
284 symbols.1:
285 symbol { $$ = symbol_list_new ($1, @1); }
286 | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); }
287 ;
288
289 /* One token definition. */
290 symbol_def:
291 TYPE
292 {
293 current_type = $1;
294 }
295 | ID
296 {
297 symbol_class_set ($1, current_class, @1);
298 symbol_type_set ($1, current_type, @1);
299 }
300 | ID INT
301 {
302 symbol_class_set ($1, current_class, @1);
303 symbol_type_set ($1, current_type, @1);
304 symbol_user_token_number_set ($1, $2, @2);
305 }
306 | ID string_as_id
307 {
308 symbol_class_set ($1, current_class, @1);
309 symbol_type_set ($1, current_type, @1);
310 symbol_make_alias ($1, $2, @$);
311 }
312 | ID INT string_as_id
313 {
314 symbol_class_set ($1, current_class, @1);
315 symbol_type_set ($1, current_type, @1);
316 symbol_user_token_number_set ($1, $2, @2);
317 symbol_make_alias ($1, $3, @$);
318 }
319 ;
320
321 /* One or more symbol definitions. */
322 symbol_defs.1:
323 symbol_def
324 {;}
325 | symbol_defs.1 symbol_def
326 {;}
327 ;
328
329
330 /*------------------------------------------.
331 | The grammar section: between the two %%. |
332 `------------------------------------------*/
333
334 grammar:
335 rules_or_grammar_declaration
336 | grammar rules_or_grammar_declaration
337 ;
338
339 /* As a Bison extension, one can use the grammar declarations in the
340 body of the grammar. But to remain LALR(1), they must be ended
341 with a semi-colon. */
342 rules_or_grammar_declaration:
343 rules
344 | grammar_declaration ";"
345 {
346 if (yacc_flag)
347 complain_at (@$, _("POSIX forbids declarations in the grammar"));
348 }
349 | error ";"
350 {
351 yyerrok;
352 }
353 ;
354
355 rules:
356 ID ":" { current_lhs = $1; current_lhs_location = @1; } rhses.1 ";"
357 {;}
358 ;
359
360 rhses.1:
361 rhs { grammar_rule_end (@1); }
362 | rhses.1 "|" rhs { grammar_rule_end (@3); }
363 ;
364
365 rhs:
366 /* Nothing. */
367 { grammar_rule_begin (current_lhs, current_lhs_location); }
368 | rhs symbol
369 { grammar_current_rule_symbol_append ($2, @2); }
370 | rhs action
371 { grammar_current_rule_action_append ($2, @2); }
372 | rhs "%prec" symbol
373 { grammar_current_rule_prec_set ($3, @3); }
374 | rhs "%dprec" INT
375 { grammar_current_rule_dprec_set ($3, @3); }
376 | rhs "%merge" TYPE
377 { grammar_current_rule_merge_set ($3, @3); }
378 ;
379
380 symbol:
381 ID { $$ = $1; }
382 | string_as_id { $$ = $1; }
383 ;
384
385 action:
386 BRACED_CODE
387 { $$ = $1; }
388 ;
389
390 /* A string used as an ID: we have to keep the quotes. */
391 string_as_id:
392 STRING
393 {
394 $$ = symbol_get ($1, @1);
395 symbol_class_set ($$, token_sym, @1);
396 }
397 ;
398
399 /* A string used for its contents. Strip the quotes. */
400 string_content:
401 STRING
402 {
403 $$ = $1 + 1;
404 $$[strlen ($$) - 1] = '\0';
405 };
406
407
408 /* A BRACED_CODE used for its contents. Strip the braces. */
409 code_content:
410 BRACED_CODE
411 {
412 $$ = $1 + 1;
413 $$[strlen ($$) - 1] = '\0';
414 };
415
416
417 epilogue.opt:
418 /* Nothing. */
419 | "%%" EPILOGUE
420 {
421 epilogue_augment ($2, @2);
422 scanner_last_string_free ();
423 }
424 ;
425
426 semi_colon.opt:
427 /* Nothing. */
428 | ";"
429 ;
430 %%
431 static void
432 add_param (char const *type, char const *decl, location_t loc)
433 {
434 static char const alphanum[] =
435 "0123456789"
436 "abcdefghijklmnopqrstuvwxyz"
437 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
438 "_";
439 char const *alpha = alphanum + 10;
440 char const *name_start = NULL;
441 char const *p;
442
443 for (p = decl; *p; p++)
444 if ((p == decl || ! strchr (alphanum, p[-1])) && strchr (alpha, p[0]))
445 name_start = p;
446
447 if (! name_start)
448 complain_at (loc, _("missing identifier in parameter declaration"));
449 else
450 {
451 char *name;
452 size_t name_len;
453
454 for (name_len = 1;
455 name_start[name_len] && strchr (alphanum, name_start[name_len]);
456 name_len++)
457 continue;
458
459 name = xmalloc (name_len + 1);
460 memcpy (name, name_start, name_len);
461 name[name_len] = '\0';
462 muscle_pair_list_grow (type, decl, name);
463 free (name);
464 }
465
466 scanner_last_string_free ();
467 }
468
469 /*------------------------------------------------------------------.
470 | When debugging the parser, display tokens' locations and values. |
471 `------------------------------------------------------------------*/
472
473 static void
474 print_token_value (FILE *file, int type, YYSTYPE const *value)
475 {
476 fputc (' ', file);
477 switch (type)
478 {
479 case ID:
480 fprintf (file, " = %s", value->symbol->tag);
481 break;
482
483 case INT:
484 fprintf (file, " = %d", value->integer);
485 break;
486
487 case STRING:
488 fprintf (file, " = \"%s\"", value->string);
489 break;
490
491 case TYPE:
492 fprintf (file, " = <%s>", value->struniq);
493 break;
494
495 case BRACED_CODE:
496 case PROLOGUE:
497 case EPILOGUE:
498 fprintf (file, " = {{ %s }}", value->string);
499 break;
500
501 default:
502 fprintf (file, "unknown token type");
503 break;
504 }
505 }
506
507 void
508 gram_error (location_t const *loc, char const *msg)
509 {
510 complain_at (*loc, "%s", msg);
511 }