]>
Commit | Line | Data |
---|---|---|
12ffdd28 | 1 | %{/* Bison Grammar Parser -*- C -*- |
a737b216 | 2 | |
219c26ea | 3 | Copyright (C) 2002-2010 Free Software Foundation, Inc. |
e9955c83 AD |
4 | |
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
f16b0819 | 7 | This program is free software: you can redistribute it and/or modify |
e9955c83 | 8 | it under the terms of the GNU General Public License as published by |
f16b0819 | 9 | the Free Software Foundation, either version 3 of the License, or |
e9955c83 AD |
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 | |
f16b0819 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
e9955c83 | 19 | |
2cec9080 | 20 | #include <config.h> |
e9955c83 | 21 | #include "system.h" |
3d2cbc26 | 22 | |
b275314e | 23 | #include "complain.h" |
3d2cbc26 | 24 | #include "conflicts.h" |
e9955c83 AD |
25 | #include "files.h" |
26 | #include "getargs.h" | |
e9955c83 | 27 | #include "gram.h" |
3d2cbc26 | 28 | #include "muscle_tab.h" |
ca407bdf | 29 | #include "quotearg.h" |
e9955c83 | 30 | #include "reader.h" |
3d2cbc26 | 31 | #include "symlist.h" |
e9071366 AD |
32 | #include "scan-gram.h" |
33 | #include "scan-code.h" | |
e9955c83 | 34 | |
b7295522 PE |
35 | #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N) |
36 | static YYLTYPE lloc_default (YYLTYPE const *, int); | |
4cdb01db | 37 | |
b233d555 | 38 | #define YY_LOCATION_PRINT(File, Loc) \ |
f0064700 | 39 | location_print (File, Loc) |
b233d555 | 40 | |
b50d2359 AD |
41 | static void version_check (location const *loc, char const *version); |
42 | ||
6e649e65 | 43 | /* Request detailed syntax error messages, and pass them to GRAM_ERROR. |
ad8a3efc | 44 | FIXME: depends on the undocumented availability of YYLLOC. */ |
e9955c83 AD |
45 | #undef yyerror |
46 | #define yyerror(Msg) \ | |
f0064700 | 47 | gram_error (&yylloc, Msg) |
3d2cbc26 | 48 | static void gram_error (location const *, char const *); |
e9955c83 | 49 | |
33ad1a9c PE |
50 | static char const *char_name (char); |
51 | ||
2ce4ed68 AD |
52 | /** Add a lex-param or a parse-param. |
53 | * | |
54 | * \param type \a lex_param or \a parse_param | |
55 | * \param decl the formal argument | |
56 | * \param loc the location in the source. | |
57 | */ | |
58 | static void add_param (char const *type, char *decl, location loc); | |
59 | ||
1773ceee | 60 | |
04098407 | 61 | static symbol_class current_class = unknown_sym; |
ddc8ede1 | 62 | static uniqstr current_type = NULL; |
877519f8 PE |
63 | static symbol *current_lhs; |
64 | static location current_lhs_location; | |
04098407 | 65 | static int current_prec = 0; |
d42cf844 | 66 | |
cb48f191 PE |
67 | #define YYTYPE_INT16 int_fast16_t |
68 | #define YYTYPE_INT8 int_fast8_t | |
69 | #define YYTYPE_UINT16 uint_fast16_t | |
70 | #define YYTYPE_UINT8 uint_fast8_t | |
e9955c83 AD |
71 | %} |
72 | ||
12ffdd28 | 73 | %debug |
82b248ad | 74 | %verbose |
12ffdd28 PE |
75 | %defines |
76 | %locations | |
77 | %pure-parser | |
78 | %error-verbose | |
12ffdd28 | 79 | %name-prefix="gram_" |
219741d8 | 80 | %expect 0 |
12ffdd28 | 81 | |
cd3684cf AD |
82 | %initial-action |
83 | { | |
84 | /* Bison's grammar can initial empty locations, hence a default | |
85 | location is needed. */ | |
4a678af8 JD |
86 | boundary_set (&@$.start, current_file, 1, 1); |
87 | boundary_set (&@$.end, current_file, 1, 1); | |
cd3684cf | 88 | } |
e9955c83 | 89 | |
e9955c83 AD |
90 | %union |
91 | { | |
3d2cbc26 PE |
92 | symbol *symbol; |
93 | symbol_list *list; | |
e9955c83 | 94 | int integer; |
eb095650 PE |
95 | char const *chars; |
96 | char *code; | |
3d2cbc26 PE |
97 | assoc assoc; |
98 | uniqstr uniqstr; | |
58d7a1a1 | 99 | unsigned char character; |
e9955c83 AD |
100 | }; |
101 | ||
f9a85a15 | 102 | /* Define the tokens together with their human representation. */ |
3d38c03a AD |
103 | %token GRAM_EOF 0 "end of file" |
104 | %token STRING "string" | |
3d38c03a | 105 | %token INT "integer" |
e9955c83 | 106 | |
9280d3ef AD |
107 | %token PERCENT_TOKEN "%token" |
108 | %token PERCENT_NTERM "%nterm" | |
366eea36 | 109 | |
9280d3ef | 110 | %token PERCENT_TYPE "%type" |
e9071366 AD |
111 | %token PERCENT_DESTRUCTOR "%destructor" |
112 | %token PERCENT_PRINTER "%printer" | |
366eea36 | 113 | |
9280d3ef AD |
114 | %token PERCENT_LEFT "%left" |
115 | %token PERCENT_RIGHT "%right" | |
116 | %token PERCENT_NONASSOC "%nonassoc" | |
04e60654 | 117 | |
3d38c03a AD |
118 | %token PERCENT_PREC "%prec" |
119 | %token PERCENT_DPREC "%dprec" | |
120 | %token PERCENT_MERGE "%merge" | |
e9955c83 | 121 | |
e9955c83 | 122 | |
ae7453f2 AD |
123 | /*----------------------. |
124 | | Global Declarations. | | |
125 | `----------------------*/ | |
126 | ||
127 | %token | |
136a0f76 | 128 | PERCENT_CODE "%code" |
cd3684cf | 129 | PERCENT_DEBUG "%debug" |
39a06c25 | 130 | PERCENT_DEFAULT_PREC "%default-prec" |
cd3684cf AD |
131 | PERCENT_DEFINE "%define" |
132 | PERCENT_DEFINES "%defines" | |
133 | PERCENT_ERROR_VERBOSE "%error-verbose" | |
134 | PERCENT_EXPECT "%expect" | |
d6328241 | 135 | PERCENT_EXPECT_RR "%expect-rr" |
cd3684cf AD |
136 | PERCENT_FILE_PREFIX "%file-prefix" |
137 | PERCENT_GLR_PARSER "%glr-parser" | |
e9071366 | 138 | PERCENT_INITIAL_ACTION "%initial-action" |
0e021770 | 139 | PERCENT_LANGUAGE "%language" |
e9071366 | 140 | PERCENT_LEX_PARAM "%lex-param" |
cd3684cf AD |
141 | PERCENT_LOCATIONS "%locations" |
142 | PERCENT_NAME_PREFIX "%name-prefix" | |
22fccf95 | 143 | PERCENT_NO_DEFAULT_PREC "%no-default-prec" |
cd3684cf AD |
144 | PERCENT_NO_LINES "%no-lines" |
145 | PERCENT_NONDETERMINISTIC_PARSER | |
f0064700 | 146 | "%nondeterministic-parser" |
cd3684cf | 147 | PERCENT_OUTPUT "%output" |
e9071366 | 148 | PERCENT_PARSE_PARAM "%parse-param" |
cd3684cf | 149 | PERCENT_PURE_PARSER "%pure-parser" |
f0064700 | 150 | PERCENT_REQUIRE "%require" |
cd3684cf AD |
151 | PERCENT_SKELETON "%skeleton" |
152 | PERCENT_START "%start" | |
153 | PERCENT_TOKEN_TABLE "%token-table" | |
154 | PERCENT_VERBOSE "%verbose" | |
155 | PERCENT_YACC "%yacc" | |
ae7453f2 | 156 | ; |
e9955c83 | 157 | |
58d7a1a1 AD |
158 | %token BRACED_CODE "{...}" |
159 | %token CHAR "char" | |
160 | %token EPILOGUE "epilogue" | |
3d38c03a | 161 | %token EQUAL "=" |
3d38c03a | 162 | %token ID "identifier" |
b7295522 | 163 | %token ID_COLON "identifier:" |
e9955c83 | 164 | %token PERCENT_PERCENT "%%" |
58d7a1a1 | 165 | %token PIPE "|" |
3d38c03a | 166 | %token PROLOGUE "%{...%}" |
58d7a1a1 AD |
167 | %token SEMICOLON ";" |
168 | %token TYPE "type" | |
12e35840 | 169 | %token TYPE_TAG_ANY "<*>" |
3ebecc24 | 170 | %token TYPE_TAG_NONE "<>" |
58d7a1a1 AD |
171 | |
172 | %type <character> CHAR | |
33ad1a9c | 173 | %printer { fputs (char_name ($$), stderr); } CHAR |
3d38c03a | 174 | |
2ce4ed68 | 175 | /* braceless is not to be used for rule or symbol actions, as it |
7c0c6181 | 176 | calls code_props_plain_init. */ |
6afc30cc | 177 | %type <chars> STRING "%{...%}" EPILOGUE braceless content.opt |
eb095650 | 178 | %type <code> "{...}" |
33ad1a9c | 179 | %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); } |
eb095650 | 180 | STRING |
2ce4ed68 | 181 | %printer { fprintf (stderr, "{\n%s\n}", $$); } |
6afc30cc | 182 | braceless content.opt "{...}" "%{...%}" EPILOGUE |
58d7a1a1 | 183 | |
16dc6a9e | 184 | %type <uniqstr> TYPE ID ID_COLON variable |
82b248ad | 185 | %printer { fprintf (stderr, "<%s>", $$); } TYPE |
16dc6a9e | 186 | %printer { fputs ($$, stderr); } ID variable |
58d7a1a1 AD |
187 | %printer { fprintf (stderr, "%s:", $$); } ID_COLON |
188 | ||
e9955c83 | 189 | %type <integer> INT |
82b248ad | 190 | %printer { fprintf (stderr, "%d", $$); } INT |
58d7a1a1 | 191 | |
ab7f29f8 | 192 | %type <symbol> id id_colon symbol symbol.prec string_as_id |
58d7a1a1 AD |
193 | %printer { fprintf (stderr, "%s", $$->tag); } id symbol string_as_id |
194 | %printer { fprintf (stderr, "%s:", $$->tag); } id_colon | |
195 | ||
2c569025 | 196 | %type <assoc> precedence_declarator |
ab7f29f8 | 197 | %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item |
e9955c83 | 198 | %% |
2c569025 | 199 | |
8efe435c | 200 | input: |
2ce4ed68 | 201 | prologue_declarations "%%" grammar epilogue.opt |
e9955c83 AD |
202 | ; |
203 | ||
2c569025 AD |
204 | |
205 | /*------------------------------------. | |
206 | | Declarations: before the first %%. | | |
207 | `------------------------------------*/ | |
208 | ||
2ce4ed68 | 209 | prologue_declarations: |
e9955c83 | 210 | /* Nothing */ |
2ce4ed68 | 211 | | prologue_declarations prologue_declaration |
e9955c83 AD |
212 | ; |
213 | ||
2ce4ed68 | 214 | prologue_declaration: |
2c569025 | 215 | grammar_declaration |
7c0c6181 JD |
216 | | "%{...%}" |
217 | { | |
218 | code_props plain_code; | |
219 | code_props_plain_init (&plain_code, $1, @1); | |
220 | code_props_translate_code (&plain_code); | |
221 | gram_scanner_last_string_free (); | |
7ecec4dd JD |
222 | muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue", |
223 | plain_code.code, @1); | |
7c0c6181 JD |
224 | code_scanner_last_string_free (); |
225 | } | |
2ce4ed68 | 226 | | "%debug" { debug_flag = true; } |
16dc6a9e | 227 | | "%define" variable content.opt |
7eb8a0bc | 228 | { |
9611cfa2 | 229 | muscle_percent_define_insert ($2, @2, $3); |
7eb8a0bc | 230 | } |
2ce4ed68 | 231 | | "%defines" { defines_flag = true; } |
02975b9a JD |
232 | | "%defines" STRING |
233 | { | |
234 | defines_flag = true; | |
235 | spec_defines_file = xstrdup ($2); | |
236 | } | |
2ce4ed68 AD |
237 | | "%error-verbose" { error_verbose = true; } |
238 | | "%expect" INT { expected_sr_conflicts = $2; } | |
239 | | "%expect-rr" INT { expected_rr_conflicts = $2; } | |
02975b9a JD |
240 | | "%file-prefix" STRING { spec_file_prefix = $2; } |
241 | | "%file-prefix" "=" STRING { spec_file_prefix = $3; } /* deprecated */ | |
cd3684cf | 242 | | "%glr-parser" |
c66dfadd PE |
243 | { |
244 | nondeterministic_parser = true; | |
245 | glr_parser = true; | |
246 | } | |
e9071366 | 247 | | "%initial-action" "{...}" |
c66dfadd | 248 | { |
7c0c6181 JD |
249 | code_props action; |
250 | code_props_symbol_action_init (&action, $2, @2); | |
251 | code_props_translate_code (&action); | |
252 | gram_scanner_last_string_free (); | |
253 | muscle_code_grow ("initial_action", action.code, @2); | |
254 | code_scanner_last_string_free (); | |
c66dfadd | 255 | } |
4b1ebc49 | 256 | | "%language" STRING { language_argmatch ($2, grammar_prio, &@1); } |
2ce4ed68 AD |
257 | | "%lex-param" "{...}" { add_param ("lex_param", $2, @2); } |
258 | | "%locations" { locations_flag = true; } | |
02975b9a JD |
259 | | "%name-prefix" STRING { spec_name_prefix = $2; } |
260 | | "%name-prefix" "=" STRING { spec_name_prefix = $3; } /* deprecated */ | |
2ce4ed68 AD |
261 | | "%no-lines" { no_lines_flag = true; } |
262 | | "%nondeterministic-parser" { nondeterministic_parser = true; } | |
02975b9a JD |
263 | | "%output" STRING { spec_outfile = $2; } |
264 | | "%output" "=" STRING { spec_outfile = $3; } /* deprecated */ | |
2ce4ed68 | 265 | | "%parse-param" "{...}" { add_param ("parse_param", $2, @2); } |
d9df47b6 JD |
266 | | "%pure-parser" |
267 | { | |
268 | /* %pure-parser is deprecated in favor of `%define api.pure', so use | |
269 | `%define api.pure' in a backward-compatible manner here. First, don't | |
270 | complain if %pure-parser is specified multiple times. */ | |
271 | if (!muscle_find_const ("percent_define(api.pure)")) | |
272 | muscle_percent_define_insert ("api.pure", @1, ""); | |
273 | /* In all cases, use api.pure now so that the backend doesn't complain if | |
274 | the skeleton ignores api.pure, but do warn now if there's a previous | |
275 | conflicting definition from an actual %define. */ | |
276 | if (!muscle_percent_define_flag_if ("api.pure")) | |
277 | muscle_percent_define_insert ("api.pure", @1, ""); | |
278 | } | |
2ce4ed68 | 279 | | "%require" STRING { version_check (&@2, $2); } |
a7867f53 JD |
280 | | "%skeleton" STRING |
281 | { | |
282 | char const *skeleton_user = $2; | |
283 | if (strchr (skeleton_user, '/')) | |
284 | { | |
285 | size_t dir_length = strlen (current_file); | |
286 | char *skeleton_build; | |
287 | while (dir_length && current_file[dir_length - 1] != '/') | |
288 | --dir_length; | |
289 | while (dir_length && current_file[dir_length - 1] == '/') | |
290 | --dir_length; | |
291 | skeleton_build = | |
292 | xmalloc (dir_length + 1 + strlen (skeleton_user) + 1); | |
293 | if (dir_length > 0) | |
294 | { | |
295 | strncpy (skeleton_build, current_file, dir_length); | |
296 | skeleton_build[dir_length++] = '/'; | |
297 | } | |
298 | strcpy (skeleton_build + dir_length, skeleton_user); | |
299 | skeleton_user = uniqstr_new (skeleton_build); | |
300 | free (skeleton_build); | |
301 | } | |
4b1ebc49 | 302 | skeleton_arg (skeleton_user, grammar_prio, &@1); |
a7867f53 | 303 | } |
2ce4ed68 | 304 | | "%token-table" { token_table_flag = true; } |
ef1b4273 | 305 | | "%verbose" { report_flag |= report_states; } |
2ce4ed68 | 306 | | "%yacc" { yacc_flag = true; } |
cd3684cf | 307 | | /*FIXME: Err? What is this horror doing here? */ ";" |
e9955c83 AD |
308 | ; |
309 | ||
2c569025 AD |
310 | grammar_declaration: |
311 | precedence_declaration | |
312 | | symbol_declaration | |
e9955c83 AD |
313 | | "%start" symbol |
314 | { | |
8efe435c | 315 | grammar_start_symbol_set ($2, @2); |
e9955c83 | 316 | } |
3be03b13 | 317 | | "%destructor" "{...}" generic_symlist |
9280d3ef | 318 | { |
3d2cbc26 | 319 | symbol_list *list; |
e9071366 | 320 | for (list = $3; list; list = list->next) |
7c0c6181 | 321 | symbol_list_destructor_set (list, $2, @2); |
e9071366 | 322 | symbol_list_free ($3); |
9280d3ef | 323 | } |
3be03b13 | 324 | | "%printer" "{...}" generic_symlist |
366eea36 | 325 | { |
3d2cbc26 | 326 | symbol_list *list; |
e9071366 | 327 | for (list = $3; list; list = list->next) |
7c0c6181 | 328 | symbol_list_printer_set (list, $2, @2); |
e9071366 | 329 | symbol_list_free ($3); |
366eea36 | 330 | } |
22fccf95 | 331 | | "%default-prec" |
39a06c25 | 332 | { |
22fccf95 PE |
333 | default_prec = true; |
334 | } | |
335 | | "%no-default-prec" | |
336 | { | |
337 | default_prec = false; | |
39a06c25 | 338 | } |
8e0a5e9e JD |
339 | | "%code" braceless |
340 | { | |
9611cfa2 JD |
341 | /* Do not invoke muscle_percent_code_grow here since it invokes |
342 | muscle_user_name_list_grow. */ | |
592d0b1e | 343 | muscle_code_grow ("percent_code()", $2, @2); |
8e0a5e9e JD |
344 | code_scanner_last_string_free (); |
345 | } | |
16dc6a9e | 346 | | "%code" ID braceless |
8e0a5e9e | 347 | { |
9611cfa2 | 348 | muscle_percent_code_grow ($2, @2, $3, @3); |
8e0a5e9e | 349 | code_scanner_last_string_free (); |
8e0a5e9e | 350 | } |
2c569025 AD |
351 | ; |
352 | ||
58d7a1a1 AD |
353 | |
354 | /*----------* | |
355 | | %union. | | |
356 | *----------*/ | |
357 | ||
358 | %token PERCENT_UNION "%union"; | |
359 | ||
360 | union_name: | |
361 | /* Nothing. */ {} | |
362 | | ID { muscle_code_grow ("union_name", $1, @1); } | |
363 | ; | |
364 | ||
365 | grammar_declaration: | |
7ecec4dd | 366 | "%union" union_name braceless |
58d7a1a1 | 367 | { |
ddc8ede1 | 368 | union_seen = true; |
7ecec4dd JD |
369 | muscle_code_grow ("stype", $3, @3); |
370 | code_scanner_last_string_free (); | |
58d7a1a1 AD |
371 | } |
372 | ; | |
373 | ||
374 | ||
375 | ||
376 | ||
2c569025 AD |
377 | symbol_declaration: |
378 | "%nterm" { current_class = nterm_sym; } symbol_defs.1 | |
e9955c83 AD |
379 | { |
380 | current_class = unknown_sym; | |
381 | current_type = NULL; | |
382 | } | |
2c569025 | 383 | | "%token" { current_class = token_sym; } symbol_defs.1 |
e9955c83 | 384 | { |
2c569025 | 385 | current_class = unknown_sym; |
e9955c83 AD |
386 | current_type = NULL; |
387 | } | |
1e0bab92 | 388 | | "%type" TYPE symbols.1 |
e9955c83 | 389 | { |
3d2cbc26 | 390 | symbol_list *list; |
4f82b42a | 391 | tag_seen = true; |
1e0bab92 | 392 | for (list = $3; list; list = list->next) |
3be03b13 | 393 | symbol_type_set (list->content.sym, $2, @2); |
dafdc66f | 394 | symbol_list_free ($3); |
e9955c83 AD |
395 | } |
396 | ; | |
397 | ||
2c569025 | 398 | precedence_declaration: |
ab7f29f8 | 399 | precedence_declarator type.opt symbols.prec |
1e0bab92 | 400 | { |
3d2cbc26 | 401 | symbol_list *list; |
1e0bab92 AD |
402 | ++current_prec; |
403 | for (list = $3; list; list = list->next) | |
404 | { | |
3be03b13 JD |
405 | symbol_type_set (list->content.sym, current_type, @2); |
406 | symbol_precedence_set (list->content.sym, current_prec, $1, @1); | |
1e0bab92 | 407 | } |
dafdc66f | 408 | symbol_list_free ($3); |
1e0bab92 AD |
409 | current_type = NULL; |
410 | } | |
e9955c83 AD |
411 | ; |
412 | ||
2c569025 | 413 | precedence_declarator: |
e9955c83 AD |
414 | "%left" { $$ = left_assoc; } |
415 | | "%right" { $$ = right_assoc; } | |
416 | | "%nonassoc" { $$ = non_assoc; } | |
417 | ; | |
418 | ||
419 | type.opt: | |
87fbb0bf | 420 | /* Nothing. */ { current_type = NULL; } |
ddc8ede1 | 421 | | TYPE { current_type = $1; tag_seen = true; } |
e9955c83 AD |
422 | ; |
423 | ||
ab7f29f8 JD |
424 | /* Just like symbols.1 but accept INT for the sake of POSIX. */ |
425 | symbols.prec: | |
426 | symbol.prec | |
427 | { $$ = symbol_list_sym_new ($1, @1); } | |
428 | | symbols.prec symbol.prec | |
429 | { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); } | |
430 | ; | |
431 | ||
432 | symbol.prec: | |
433 | symbol { $$ = $1; } | |
434 | | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); } | |
435 | ; | |
436 | ||
3be03b13 | 437 | /* One or more symbols to be %typed. */ |
1e0bab92 | 438 | symbols.1: |
3be03b13 JD |
439 | symbol |
440 | { $$ = symbol_list_sym_new ($1, @1); } | |
441 | | symbols.1 symbol | |
442 | { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); } | |
443 | ; | |
444 | ||
445 | generic_symlist: | |
446 | generic_symlist_item { $$ = $1; } | |
447 | | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); } | |
448 | ; | |
449 | ||
450 | generic_symlist_item: | |
451 | symbol { $$ = symbol_list_sym_new ($1, @1); } | |
452 | | TYPE { $$ = symbol_list_type_new ($1, @1); } | |
12e35840 | 453 | | "<*>" { $$ = symbol_list_default_tagged_new (@1); } |
3ebecc24 | 454 | | "<>" { $$ = symbol_list_default_tagless_new (@1); } |
e9955c83 AD |
455 | ; |
456 | ||
e9955c83 AD |
457 | /* One token definition. */ |
458 | symbol_def: | |
459 | TYPE | |
460 | { | |
461 | current_type = $1; | |
ddc8ede1 | 462 | tag_seen = true; |
e9955c83 | 463 | } |
58d7a1a1 | 464 | | id |
e9955c83 | 465 | { |
073f9288 | 466 | symbol_class_set ($1, current_class, @1, true); |
1a31ed21 | 467 | symbol_type_set ($1, current_type, @1); |
e9955c83 | 468 | } |
58d7a1a1 | 469 | | id INT |
e9955c83 | 470 | { |
073f9288 | 471 | symbol_class_set ($1, current_class, @1, true); |
1a31ed21 | 472 | symbol_type_set ($1, current_type, @1); |
e776192e | 473 | symbol_user_token_number_set ($1, $2, @2); |
e9955c83 | 474 | } |
58d7a1a1 | 475 | | id string_as_id |
e9955c83 | 476 | { |
073f9288 | 477 | symbol_class_set ($1, current_class, @1, true); |
1a31ed21 | 478 | symbol_type_set ($1, current_type, @1); |
a5d50994 | 479 | symbol_make_alias ($1, $2, @$); |
e9955c83 | 480 | } |
58d7a1a1 | 481 | | id INT string_as_id |
e9955c83 | 482 | { |
073f9288 | 483 | symbol_class_set ($1, current_class, @1, true); |
1a31ed21 | 484 | symbol_type_set ($1, current_type, @1); |
e776192e | 485 | symbol_user_token_number_set ($1, $2, @2); |
a5d50994 | 486 | symbol_make_alias ($1, $3, @$); |
e9955c83 AD |
487 | } |
488 | ; | |
489 | ||
490 | /* One or more symbol definitions. */ | |
491 | symbol_defs.1: | |
492 | symbol_def | |
e9955c83 | 493 | | symbol_defs.1 symbol_def |
e9955c83 AD |
494 | ; |
495 | ||
2c569025 AD |
496 | |
497 | /*------------------------------------------. | |
498 | | The grammar section: between the two %%. | | |
499 | `------------------------------------------*/ | |
500 | ||
501 | grammar: | |
1921f1d7 AD |
502 | rules_or_grammar_declaration |
503 | | grammar rules_or_grammar_declaration | |
504 | ; | |
505 | ||
506 | /* As a Bison extension, one can use the grammar declarations in the | |
b7295522 | 507 | body of the grammar. */ |
1921f1d7 | 508 | rules_or_grammar_declaration: |
e9955c83 | 509 | rules |
8d0a98bb | 510 | | grammar_declaration ";" |
b275314e AD |
511 | | error ";" |
512 | { | |
513 | yyerrok; | |
514 | } | |
e9955c83 AD |
515 | ; |
516 | ||
517 | rules: | |
58d7a1a1 | 518 | id_colon { current_lhs = $1; current_lhs_location = @1; } rhses.1 |
e9955c83 AD |
519 | ; |
520 | ||
521 | rhses.1: | |
8f3596a6 AD |
522 | rhs { grammar_current_rule_end (@1); } |
523 | | rhses.1 "|" rhs { grammar_current_rule_end (@3); } | |
8d0a98bb | 524 | | rhses.1 ";" |
e9955c83 AD |
525 | ; |
526 | ||
527 | rhs: | |
528 | /* Nothing. */ | |
8f3596a6 | 529 | { grammar_current_rule_begin (current_lhs, current_lhs_location); } |
e9955c83 | 530 | | rhs symbol |
8efe435c | 531 | { grammar_current_rule_symbol_append ($2, @2); } |
e9071366 | 532 | | rhs "{...}" |
381ecb06 | 533 | { grammar_current_rule_action_append ($2, @2); } |
e9955c83 | 534 | | rhs "%prec" symbol |
e776192e | 535 | { grammar_current_rule_prec_set ($3, @3); } |
676385e2 PH |
536 | | rhs "%dprec" INT |
537 | { grammar_current_rule_dprec_set ($3, @3); } | |
538 | | rhs "%merge" TYPE | |
539 | { grammar_current_rule_merge_set ($3, @3); } | |
e9955c83 AD |
540 | ; |
541 | ||
58d7a1a1 | 542 | |
16dc6a9e JD |
543 | /*----------------------------* |
544 | | variable and content.opt. | | |
545 | *---------------------------*/ | |
546 | ||
547 | variable: | |
548 | ID | |
549 | | STRING { $$ = uniqstr_new ($1); } /* deprecated and not M4-friendly */ | |
550 | ; | |
2ce4ed68 | 551 | |
592d0b1e | 552 | /* Some content or empty by default. */ |
2ce4ed68 AD |
553 | content.opt: |
554 | /* Nothing. */ | |
555 | { | |
592d0b1e | 556 | $$ = ""; |
2ce4ed68 | 557 | } |
6afc30cc | 558 | | STRING |
2ce4ed68 AD |
559 | ; |
560 | ||
561 | ||
6afc30cc JD |
562 | /*-------------* |
563 | | braceless. | | |
564 | *-------------*/ | |
565 | ||
2ce4ed68 AD |
566 | braceless: |
567 | "{...}" | |
568 | { | |
7c0c6181 | 569 | code_props plain_code; |
2ce4ed68 | 570 | $1[strlen ($1) - 1] = '\n'; |
7c0c6181 JD |
571 | code_props_plain_init (&plain_code, $1+1, @1); |
572 | code_props_translate_code (&plain_code); | |
573 | gram_scanner_last_string_free (); | |
574 | $$ = plain_code.code; | |
2ce4ed68 AD |
575 | } |
576 | ; | |
577 | ||
578 | ||
58d7a1a1 AD |
579 | /*---------------* |
580 | | Identifiers. | | |
581 | *---------------*/ | |
582 | ||
33ad1a9c PE |
583 | /* Identifiers are returned as uniqstr values by the scanner. |
584 | Depending on their use, we may need to make them genuine symbols. */ | |
58d7a1a1 AD |
585 | |
586 | id: | |
33ad1a9c | 587 | ID |
203b9274 | 588 | { $$ = symbol_from_uniqstr ($1, @1); } |
33ad1a9c PE |
589 | | CHAR |
590 | { | |
591 | $$ = symbol_get (char_name ($1), @1); | |
592 | symbol_class_set ($$, token_sym, @1, false); | |
593 | symbol_user_token_number_set ($$, $1, @1); | |
594 | } | |
58d7a1a1 AD |
595 | ; |
596 | ||
597 | id_colon: | |
203b9274 | 598 | ID_COLON { $$ = symbol_from_uniqstr ($1, @1); } |
58d7a1a1 AD |
599 | ; |
600 | ||
601 | ||
e9955c83 | 602 | symbol: |
58d7a1a1 AD |
603 | id |
604 | | string_as_id | |
e9955c83 AD |
605 | ; |
606 | ||
ca407bdf | 607 | /* A string used as an ID: quote it. */ |
e9955c83 AD |
608 | string_as_id: |
609 | STRING | |
610 | { | |
ca407bdf | 611 | $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1); |
db89d400 | 612 | symbol_class_set ($$, token_sym, @1, false); |
e9955c83 AD |
613 | } |
614 | ; | |
615 | ||
e9955c83 AD |
616 | epilogue.opt: |
617 | /* Nothing. */ | |
e9955c83 AD |
618 | | "%%" EPILOGUE |
619 | { | |
7c0c6181 JD |
620 | code_props plain_code; |
621 | code_props_plain_init (&plain_code, $2, @2); | |
622 | code_props_translate_code (&plain_code); | |
e9071366 | 623 | gram_scanner_last_string_free (); |
7c0c6181 JD |
624 | muscle_code_grow ("epilogue", plain_code.code, @2); |
625 | code_scanner_last_string_free (); | |
e9955c83 AD |
626 | } |
627 | ; | |
628 | ||
e9955c83 | 629 | %% |
b7295522 PE |
630 | |
631 | ||
632 | /* Return the location of the left-hand side of a rule whose | |
633 | right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in | |
634 | the right-hand side, and return an empty location equal to the end | |
635 | boundary of RHS[0] if the right-hand side is empty. */ | |
636 | ||
637 | static YYLTYPE | |
638 | lloc_default (YYLTYPE const *rhs, int n) | |
639 | { | |
640 | int i; | |
a737b216 | 641 | YYLTYPE loc; |
62cb8a99 PE |
642 | |
643 | /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;". | |
644 | The bug is fixed in 7.4.2m, but play it safe for now. */ | |
645 | loc.start = rhs[n].end; | |
646 | loc.end = rhs[n].end; | |
b7295522 | 647 | |
5320ca4d PE |
648 | /* Ignore empty nonterminals the start of the the right-hand side. |
649 | Do not bother to ignore them at the end of the right-hand side, | |
650 | since empty nonterminals have the same end as their predecessors. */ | |
b7295522 PE |
651 | for (i = 1; i <= n; i++) |
652 | if (! equal_boundaries (rhs[i].start, rhs[i].end)) | |
653 | { | |
a737b216 | 654 | loc.start = rhs[i].start; |
b7295522 PE |
655 | break; |
656 | } | |
657 | ||
a737b216 | 658 | return loc; |
b7295522 PE |
659 | } |
660 | ||
661 | ||
662 | /* Add a lex-param or a parse-param (depending on TYPE) with | |
663 | declaration DECL and location LOC. */ | |
664 | ||
1773ceee | 665 | static void |
e9ce5688 | 666 | add_param (char const *type, char *decl, location loc) |
1773ceee | 667 | { |
ead9e56e | 668 | static char const alphanum[26 + 26 + 1 + 10] = |
1773ceee PE |
669 | "abcdefghijklmnopqrstuvwxyz" |
670 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
ead9e56e PE |
671 | "_" |
672 | "0123456789"; | |
1773ceee | 673 | char const *name_start = NULL; |
e9ce5688 | 674 | char *p; |
1773ceee | 675 | |
e503aa60 AD |
676 | /* Stop on last actual character. */ |
677 | for (p = decl; p[1]; p++) | |
ead9e56e PE |
678 | if ((p == decl |
679 | || ! memchr (alphanum, p[-1], sizeof alphanum)) | |
680 | && memchr (alphanum, p[0], sizeof alphanum - 10)) | |
1773ceee PE |
681 | name_start = p; |
682 | ||
ead9e56e PE |
683 | /* Strip the surrounding '{' and '}', and any blanks just inside |
684 | the braces. */ | |
685 | while (*--p == ' ' || *p == '\t') | |
686 | continue; | |
e503aa60 | 687 | p[1] = '\0'; |
ead9e56e PE |
688 | while (*++decl == ' ' || *decl == '\t') |
689 | continue; | |
e9ce5688 | 690 | |
1773ceee PE |
691 | if (! name_start) |
692 | complain_at (loc, _("missing identifier in parameter declaration")); | |
693 | else | |
694 | { | |
695 | char *name; | |
696 | size_t name_len; | |
697 | ||
698 | for (name_len = 1; | |
ead9e56e | 699 | memchr (alphanum, name_start[name_len], sizeof alphanum); |
1773ceee PE |
700 | name_len++) |
701 | continue; | |
702 | ||
703 | name = xmalloc (name_len + 1); | |
704 | memcpy (name, name_start, name_len); | |
705 | name[name_len] = '\0'; | |
706 | muscle_pair_list_grow (type, decl, name); | |
707 | free (name); | |
708 | } | |
709 | ||
e9071366 | 710 | gram_scanner_last_string_free (); |
1773ceee PE |
711 | } |
712 | ||
2ce4ed68 | 713 | |
b50d2359 AD |
714 | static void |
715 | version_check (location const *loc, char const *version) | |
716 | { | |
717 | if (strverscmp (version, PACKAGE_VERSION) > 0) | |
9b8a5ce0 AD |
718 | { |
719 | complain_at (*loc, "require bison %s, but have %s", | |
720 | version, PACKAGE_VERSION); | |
721 | exit (63); | |
722 | } | |
b50d2359 AD |
723 | } |
724 | ||
1fec91df | 725 | static void |
3d2cbc26 | 726 | gram_error (location const *loc, char const *msg) |
e9955c83 | 727 | { |
ad8a3efc | 728 | complain_at (*loc, "%s", msg); |
e9955c83 | 729 | } |
e9ce5688 PE |
730 | |
731 | char const * | |
732 | token_name (int type) | |
733 | { | |
fc01665e | 734 | return yytname[YYTRANSLATE (type)]; |
e9ce5688 | 735 | } |
33ad1a9c PE |
736 | |
737 | static char const * | |
738 | char_name (char c) | |
739 | { | |
740 | if (c == '\'') | |
741 | return "'\\''"; | |
742 | else | |
743 | { | |
744 | char buf[4]; | |
745 | buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0'; | |
746 | return quotearg_style (escape_quoting_style, buf); | |
747 | } | |
748 | } |