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