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