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