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