]>
Commit | Line | Data |
---|---|---|
12ffdd28 | 1 | %{/* Bison Grammar Parser -*- C -*- |
a737b216 | 2 | |
073f9288 | 3 | Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. |
e9955c83 AD |
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 | |
0fb669f9 PE |
19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
20 | 02110-1301 USA | |
e9955c83 AD |
21 | */ |
22 | ||
2cec9080 | 23 | #include <config.h> |
e9955c83 | 24 | #include "system.h" |
3d2cbc26 | 25 | |
b275314e | 26 | #include "complain.h" |
3d2cbc26 | 27 | #include "conflicts.h" |
e9955c83 AD |
28 | #include "files.h" |
29 | #include "getargs.h" | |
e9955c83 | 30 | #include "gram.h" |
3d2cbc26 | 31 | #include "muscle_tab.h" |
ca407bdf | 32 | #include "quotearg.h" |
e9955c83 | 33 | #include "reader.h" |
3d2cbc26 | 34 | #include "symlist.h" |
e9071366 AD |
35 | #include "scan-gram.h" |
36 | #include "scan-code.h" | |
b50d2359 | 37 | #include "strverscmp.h" |
e9955c83 | 38 | |
b7295522 PE |
39 | #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N) |
40 | static YYLTYPE lloc_default (YYLTYPE const *, int); | |
4cdb01db | 41 | |
b233d555 | 42 | #define YY_LOCATION_PRINT(File, Loc) \ |
f0064700 | 43 | location_print (File, Loc) |
b233d555 | 44 | |
b50d2359 AD |
45 | static void version_check (location const *loc, char const *version); |
46 | ||
6e649e65 | 47 | /* Request detailed syntax error messages, and pass them to GRAM_ERROR. |
ad8a3efc | 48 | FIXME: depends on the undocumented availability of YYLLOC. */ |
e9955c83 AD |
49 | #undef yyerror |
50 | #define yyerror(Msg) \ | |
f0064700 | 51 | gram_error (&yylloc, Msg) |
3d2cbc26 | 52 | static void gram_error (location const *, char const *); |
e9955c83 | 53 | |
33ad1a9c PE |
54 | static char const *char_name (char); |
55 | ||
e9ce5688 | 56 | static void add_param (char const *, char *, location); |
1773ceee | 57 | |
04098407 PE |
58 | static symbol_class current_class = unknown_sym; |
59 | static uniqstr current_type = 0; | |
877519f8 PE |
60 | static symbol *current_lhs; |
61 | static location current_lhs_location; | |
04098407 | 62 | static int current_prec = 0; |
d42cf844 PE |
63 | |
64 | #ifdef UINT_FAST8_MAX | |
65 | # define YYTYPE_UINT8 uint_fast8_t | |
66 | #endif | |
67 | #ifdef INT_FAST8_MAX | |
68 | # define YYTYPE_INT8 int_fast8_t | |
69 | #endif | |
70 | #ifdef UINT_FAST16_MAX | |
71 | # define YYTYPE_UINT16 uint_fast16_t | |
72 | #endif | |
73 | #ifdef INT_FAST16_MAX | |
74 | # define YYTYPE_INT16 int_fast16_t | |
75 | #endif | |
e9955c83 AD |
76 | %} |
77 | ||
12ffdd28 | 78 | %debug |
82b248ad | 79 | %verbose |
12ffdd28 PE |
80 | %defines |
81 | %locations | |
82 | %pure-parser | |
83 | %error-verbose | |
84 | %defines | |
85 | %name-prefix="gram_" | |
86 | ||
cd3684cf AD |
87 | %initial-action |
88 | { | |
89 | /* Bison's grammar can initial empty locations, hence a default | |
90 | location is needed. */ | |
e9071366 AD |
91 | boundary_set (&@$.start, current_file, 1, 0); |
92 | boundary_set (&@$.end, current_file, 1, 0); | |
cd3684cf | 93 | } |
e9955c83 AD |
94 | |
95 | /* Only NUMBERS have a value. */ | |
96 | %union | |
97 | { | |
3d2cbc26 PE |
98 | symbol *symbol; |
99 | symbol_list *list; | |
e9955c83 | 100 | int integer; |
3d2cbc26 PE |
101 | char *chars; |
102 | assoc assoc; | |
103 | uniqstr uniqstr; | |
58d7a1a1 | 104 | unsigned char character; |
e9955c83 AD |
105 | }; |
106 | ||
f9a85a15 | 107 | /* Define the tokens together with their human representation. */ |
3d38c03a AD |
108 | %token GRAM_EOF 0 "end of file" |
109 | %token STRING "string" | |
3d38c03a | 110 | %token INT "integer" |
e9955c83 | 111 | |
9280d3ef AD |
112 | %token PERCENT_TOKEN "%token" |
113 | %token PERCENT_NTERM "%nterm" | |
366eea36 | 114 | |
9280d3ef | 115 | %token PERCENT_TYPE "%type" |
e9071366 AD |
116 | %token PERCENT_DESTRUCTOR "%destructor" |
117 | %token PERCENT_PRINTER "%printer" | |
366eea36 | 118 | |
9280d3ef AD |
119 | %token PERCENT_LEFT "%left" |
120 | %token PERCENT_RIGHT "%right" | |
121 | %token PERCENT_NONASSOC "%nonassoc" | |
04e60654 | 122 | |
3d38c03a AD |
123 | %token PERCENT_PREC "%prec" |
124 | %token PERCENT_DPREC "%dprec" | |
125 | %token PERCENT_MERGE "%merge" | |
e9955c83 | 126 | |
e9955c83 | 127 | |
ae7453f2 AD |
128 | /*----------------------. |
129 | | Global Declarations. | | |
130 | `----------------------*/ | |
131 | ||
132 | %token | |
34f98f46 JD |
133 | PERCENT_AFTER_HEADER "%after-header" |
134 | PERCENT_BEFORE_HEADER "%before-header" | |
cd3684cf | 135 | PERCENT_DEBUG "%debug" |
39a06c25 | 136 | PERCENT_DEFAULT_PREC "%default-prec" |
cd3684cf AD |
137 | PERCENT_DEFINE "%define" |
138 | PERCENT_DEFINES "%defines" | |
34f98f46 | 139 | PERCENT_END_HEADER "%end-header" |
cd3684cf AD |
140 | PERCENT_ERROR_VERBOSE "%error-verbose" |
141 | PERCENT_EXPECT "%expect" | |
d6328241 | 142 | PERCENT_EXPECT_RR "%expect-rr" |
cd3684cf AD |
143 | PERCENT_FILE_PREFIX "%file-prefix" |
144 | PERCENT_GLR_PARSER "%glr-parser" | |
e9071366 AD |
145 | PERCENT_INITIAL_ACTION "%initial-action" |
146 | PERCENT_LEX_PARAM "%lex-param" | |
cd3684cf AD |
147 | PERCENT_LOCATIONS "%locations" |
148 | PERCENT_NAME_PREFIX "%name-prefix" | |
22fccf95 | 149 | PERCENT_NO_DEFAULT_PREC "%no-default-prec" |
cd3684cf AD |
150 | PERCENT_NO_LINES "%no-lines" |
151 | PERCENT_NONDETERMINISTIC_PARSER | |
f0064700 | 152 | "%nondeterministic-parser" |
cd3684cf | 153 | PERCENT_OUTPUT "%output" |
e9071366 | 154 | PERCENT_PARSE_PARAM "%parse-param" |
cd3684cf | 155 | PERCENT_PURE_PARSER "%pure-parser" |
f0064700 | 156 | PERCENT_REQUIRE "%require" |
cd3684cf AD |
157 | PERCENT_SKELETON "%skeleton" |
158 | PERCENT_START "%start" | |
34f98f46 | 159 | PERCENT_START_HEADER "%start-header" |
cd3684cf AD |
160 | PERCENT_TOKEN_TABLE "%token-table" |
161 | PERCENT_VERBOSE "%verbose" | |
162 | PERCENT_YACC "%yacc" | |
ae7453f2 | 163 | ; |
e9955c83 | 164 | |
58d7a1a1 AD |
165 | %token BRACED_CODE "{...}" |
166 | %token CHAR "char" | |
167 | %token EPILOGUE "epilogue" | |
3d38c03a | 168 | %token EQUAL "=" |
3d38c03a | 169 | %token ID "identifier" |
b7295522 | 170 | %token ID_COLON "identifier:" |
e9955c83 | 171 | %token PERCENT_PERCENT "%%" |
58d7a1a1 | 172 | %token PIPE "|" |
3d38c03a | 173 | %token PROLOGUE "%{...%}" |
58d7a1a1 AD |
174 | %token SEMICOLON ";" |
175 | %token TYPE "type" | |
176 | ||
177 | %type <character> CHAR | |
33ad1a9c | 178 | %printer { fputs (char_name ($$), stderr); } CHAR |
3d38c03a | 179 | |
58d7a1a1 | 180 | %type <chars> STRING string_content "{...}" PROLOGUE EPILOGUE |
33ad1a9c PE |
181 | %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); } |
182 | STRING string_content | |
58d7a1a1 AD |
183 | %printer { fprintf (stderr, "{\n%s\n}", $$); } "{...}" PROLOGUE EPILOGUE |
184 | ||
185 | %type <uniqstr> TYPE ID ID_COLON | |
82b248ad | 186 | %printer { fprintf (stderr, "<%s>", $$); } TYPE |
33ad1a9c | 187 | %printer { fputs ($$, stderr); } ID |
58d7a1a1 AD |
188 | %printer { fprintf (stderr, "%s:", $$); } ID_COLON |
189 | ||
e9955c83 | 190 | %type <integer> INT |
82b248ad | 191 | %printer { fprintf (stderr, "%d", $$); } INT |
58d7a1a1 AD |
192 | |
193 | %type <symbol> id id_colon symbol string_as_id | |
194 | %printer { fprintf (stderr, "%s", $$->tag); } id symbol string_as_id | |
195 | %printer { fprintf (stderr, "%s:", $$->tag); } id_colon | |
196 | ||
2c569025 | 197 | %type <assoc> precedence_declarator |
1e0bab92 | 198 | %type <list> symbols.1 |
e9955c83 | 199 | %% |
2c569025 | 200 | |
8efe435c | 201 | input: |
2c569025 | 202 | declarations "%%" grammar epilogue.opt |
e9955c83 AD |
203 | ; |
204 | ||
2c569025 AD |
205 | |
206 | /*------------------------------------. | |
207 | | Declarations: before the first %%. | | |
208 | `------------------------------------*/ | |
209 | ||
210 | declarations: | |
e9955c83 | 211 | /* Nothing */ |
b7295522 | 212 | | declarations declaration |
e9955c83 AD |
213 | ; |
214 | ||
2c569025 AD |
215 | declaration: |
216 | grammar_declaration | |
34f98f46 JD |
217 | | PROLOGUE |
218 | { | |
219 | prologue_augment (translate_code ($1, @1), @1, typed); | |
220 | } | |
221 | | "%after-header" "{...}" | |
9bc0dd67 | 222 | { |
9bc0dd67 JD |
223 | /* Remove the '{', and replace the '}' with '\n'. */ |
224 | $2[strlen ($2) - 1] = '\n'; | |
34f98f46 | 225 | prologue_augment (translate_code ($2+1, @2), @2, true); |
9bc0dd67 | 226 | } |
34f98f46 | 227 | | "%before-header" "{...}" |
9bc0dd67 | 228 | { |
9bc0dd67 JD |
229 | /* Remove the '{', and replace the '}' with '\n'. */ |
230 | $2[strlen ($2) - 1] = '\n'; | |
34f98f46 | 231 | prologue_augment (translate_code ($2+1, @2), @2, false); |
9bc0dd67 | 232 | } |
d0829076 | 233 | | "%debug" { debug_flag = true; } |
c66dfadd PE |
234 | | "%define" string_content |
235 | { | |
236 | static char one[] = "1"; | |
237 | muscle_insert ($2, one); | |
238 | } | |
e9955c83 | 239 | | "%define" string_content string_content { muscle_insert ($2, $3); } |
d0829076 | 240 | | "%defines" { defines_flag = true; } |
34f98f46 JD |
241 | | "%end-header" "{...}" |
242 | { | |
243 | /* Remove the '{', and replace the '}' with '\n'. */ | |
244 | $2[strlen ($2) - 1] = '\n'; | |
245 | muscle_code_grow ("end_header", translate_code ($2+1, @2), @2); | |
246 | } | |
d0829076 | 247 | | "%error-verbose" { error_verbose = true; } |
d6328241 | 248 | | "%expect" INT { expected_sr_conflicts = $2; } |
04098407 | 249 | | "%expect-rr" INT { expected_rr_conflicts = $2; } |
e9955c83 | 250 | | "%file-prefix" "=" string_content { spec_file_prefix = $3; } |
cd3684cf | 251 | | "%glr-parser" |
c66dfadd PE |
252 | { |
253 | nondeterministic_parser = true; | |
254 | glr_parser = true; | |
255 | } | |
e9071366 | 256 | | "%initial-action" "{...}" |
c66dfadd | 257 | { |
e9071366 | 258 | muscle_code_grow ("initial_action", translate_symbol_action ($2, @2), @2); |
c66dfadd | 259 | } |
e9071366 | 260 | | "%lex-param" "{...}" { add_param ("lex_param", $2, @2); } |
d0829076 | 261 | | "%locations" { locations_flag = true; } |
e9955c83 | 262 | | "%name-prefix" "=" string_content { spec_name_prefix = $3; } |
d0829076 | 263 | | "%no-lines" { no_lines_flag = true; } |
04098407 | 264 | | "%nondeterministic-parser" { nondeterministic_parser = true; } |
e9955c83 | 265 | | "%output" "=" string_content { spec_outfile = $3; } |
e9071366 | 266 | | "%parse-param" "{...}" { add_param ("parse_param", $2, @2); } |
916708d5 | 267 | | "%pure-parser" { pure_parser = true; } |
b50d2359 | 268 | | "%require" string_content { version_check (&@2, $2); } |
e9955c83 | 269 | | "%skeleton" string_content { skeleton = $2; } |
34f98f46 JD |
270 | | "%start-header" "{...}" |
271 | { | |
272 | /* Remove the '{', and replace the '}' with '\n'. */ | |
273 | $2[strlen ($2) - 1] = '\n'; | |
274 | muscle_code_grow ("start_header", translate_code ($2+1, @2), @2); | |
275 | } | |
d0829076 | 276 | | "%token-table" { token_table_flag = true; } |
9dd5b378 | 277 | | "%verbose" { report_flag = report_states; } |
d0829076 | 278 | | "%yacc" { yacc_flag = true; } |
cd3684cf | 279 | | /*FIXME: Err? What is this horror doing here? */ ";" |
e9955c83 AD |
280 | ; |
281 | ||
2c569025 AD |
282 | grammar_declaration: |
283 | precedence_declaration | |
284 | | symbol_declaration | |
e9955c83 AD |
285 | | "%start" symbol |
286 | { | |
8efe435c | 287 | grammar_start_symbol_set ($2, @2); |
e9955c83 | 288 | } |
e9071366 | 289 | | "%destructor" "{...}" symbols.1 |
9280d3ef | 290 | { |
3d2cbc26 | 291 | symbol_list *list; |
e9071366 AD |
292 | const char *action = translate_symbol_action ($2, @2); |
293 | for (list = $3; list; list = list->next) | |
294 | symbol_destructor_set (list->sym, action, @2); | |
295 | symbol_list_free ($3); | |
9280d3ef | 296 | } |
e9071366 | 297 | | "%printer" "{...}" symbols.1 |
366eea36 | 298 | { |
3d2cbc26 | 299 | symbol_list *list; |
e9071366 AD |
300 | const char *action = translate_symbol_action ($2, @2); |
301 | for (list = $3; list; list = list->next) | |
302 | symbol_printer_set (list->sym, action, @2); | |
303 | symbol_list_free ($3); | |
366eea36 | 304 | } |
22fccf95 | 305 | | "%default-prec" |
39a06c25 | 306 | { |
22fccf95 PE |
307 | default_prec = true; |
308 | } | |
309 | | "%no-default-prec" | |
310 | { | |
311 | default_prec = false; | |
39a06c25 | 312 | } |
2c569025 AD |
313 | ; |
314 | ||
58d7a1a1 AD |
315 | |
316 | /*----------* | |
317 | | %union. | | |
318 | *----------*/ | |
319 | ||
320 | %token PERCENT_UNION "%union"; | |
321 | ||
322 | union_name: | |
323 | /* Nothing. */ {} | |
324 | | ID { muscle_code_grow ("union_name", $1, @1); } | |
325 | ; | |
326 | ||
327 | grammar_declaration: | |
328 | "%union" union_name "{...}" | |
329 | { | |
330 | char const *body = $3; | |
331 | ||
332 | if (typed) | |
333 | { | |
334 | /* Concatenate the union bodies, turning the first one's | |
335 | trailing '}' into '\n', and omitting the second one's '{'. */ | |
336 | char *code = muscle_find ("stype"); | |
337 | code[strlen (code) - 1] = '\n'; | |
338 | body++; | |
339 | } | |
340 | ||
341 | typed = true; | |
342 | muscle_code_grow ("stype", body, @3); | |
343 | } | |
344 | ; | |
345 | ||
346 | ||
347 | ||
348 | ||
2c569025 AD |
349 | symbol_declaration: |
350 | "%nterm" { current_class = nterm_sym; } symbol_defs.1 | |
e9955c83 AD |
351 | { |
352 | current_class = unknown_sym; | |
353 | current_type = NULL; | |
354 | } | |
2c569025 | 355 | | "%token" { current_class = token_sym; } symbol_defs.1 |
e9955c83 | 356 | { |
2c569025 | 357 | current_class = unknown_sym; |
e9955c83 AD |
358 | current_type = NULL; |
359 | } | |
1e0bab92 | 360 | | "%type" TYPE symbols.1 |
e9955c83 | 361 | { |
3d2cbc26 | 362 | symbol_list *list; |
1e0bab92 | 363 | for (list = $3; list; list = list->next) |
1a31ed21 | 364 | symbol_type_set (list->sym, $2, @2); |
dafdc66f | 365 | symbol_list_free ($3); |
e9955c83 AD |
366 | } |
367 | ; | |
368 | ||
2c569025 | 369 | precedence_declaration: |
1e0bab92 AD |
370 | precedence_declarator type.opt symbols.1 |
371 | { | |
3d2cbc26 | 372 | symbol_list *list; |
1e0bab92 AD |
373 | ++current_prec; |
374 | for (list = $3; list; list = list->next) | |
375 | { | |
1a31ed21 AD |
376 | symbol_type_set (list->sym, current_type, @2); |
377 | symbol_precedence_set (list->sym, current_prec, $1, @1); | |
1e0bab92 | 378 | } |
dafdc66f | 379 | symbol_list_free ($3); |
1e0bab92 AD |
380 | current_type = NULL; |
381 | } | |
e9955c83 AD |
382 | ; |
383 | ||
2c569025 | 384 | precedence_declarator: |
e9955c83 AD |
385 | "%left" { $$ = left_assoc; } |
386 | | "%right" { $$ = right_assoc; } | |
387 | | "%nonassoc" { $$ = non_assoc; } | |
388 | ; | |
389 | ||
390 | type.opt: | |
87fbb0bf | 391 | /* Nothing. */ { current_type = NULL; } |
e9955c83 AD |
392 | | TYPE { current_type = $1; } |
393 | ; | |
394 | ||
395 | /* One or more nonterminals to be %typed. */ | |
1e0bab92 AD |
396 | symbols.1: |
397 | symbol { $$ = symbol_list_new ($1, @1); } | |
398 | | symbols.1 symbol { $$ = symbol_list_prepend ($1, $2, @2); } | |
e9955c83 AD |
399 | ; |
400 | ||
e9955c83 AD |
401 | /* One token definition. */ |
402 | symbol_def: | |
403 | TYPE | |
404 | { | |
405 | current_type = $1; | |
406 | } | |
58d7a1a1 | 407 | | id |
e9955c83 | 408 | { |
073f9288 | 409 | symbol_class_set ($1, current_class, @1, true); |
1a31ed21 | 410 | symbol_type_set ($1, current_type, @1); |
e9955c83 | 411 | } |
58d7a1a1 | 412 | | id INT |
e9955c83 | 413 | { |
073f9288 | 414 | symbol_class_set ($1, current_class, @1, true); |
1a31ed21 | 415 | symbol_type_set ($1, current_type, @1); |
e776192e | 416 | symbol_user_token_number_set ($1, $2, @2); |
e9955c83 | 417 | } |
58d7a1a1 | 418 | | id string_as_id |
e9955c83 | 419 | { |
073f9288 | 420 | symbol_class_set ($1, current_class, @1, true); |
1a31ed21 | 421 | symbol_type_set ($1, current_type, @1); |
a5d50994 | 422 | symbol_make_alias ($1, $2, @$); |
e9955c83 | 423 | } |
58d7a1a1 | 424 | | id INT string_as_id |
e9955c83 | 425 | { |
073f9288 | 426 | symbol_class_set ($1, current_class, @1, true); |
1a31ed21 | 427 | symbol_type_set ($1, current_type, @1); |
e776192e | 428 | symbol_user_token_number_set ($1, $2, @2); |
a5d50994 | 429 | symbol_make_alias ($1, $3, @$); |
e9955c83 AD |
430 | } |
431 | ; | |
432 | ||
433 | /* One or more symbol definitions. */ | |
434 | symbol_defs.1: | |
435 | symbol_def | |
e9955c83 | 436 | | symbol_defs.1 symbol_def |
e9955c83 AD |
437 | ; |
438 | ||
2c569025 AD |
439 | |
440 | /*------------------------------------------. | |
441 | | The grammar section: between the two %%. | | |
442 | `------------------------------------------*/ | |
443 | ||
444 | grammar: | |
1921f1d7 AD |
445 | rules_or_grammar_declaration |
446 | | grammar rules_or_grammar_declaration | |
447 | ; | |
448 | ||
449 | /* As a Bison extension, one can use the grammar declarations in the | |
b7295522 | 450 | body of the grammar. */ |
1921f1d7 | 451 | rules_or_grammar_declaration: |
e9955c83 | 452 | rules |
8d0a98bb | 453 | | grammar_declaration ";" |
b275314e AD |
454 | | error ";" |
455 | { | |
456 | yyerrok; | |
457 | } | |
e9955c83 AD |
458 | ; |
459 | ||
460 | rules: | |
58d7a1a1 | 461 | id_colon { current_lhs = $1; current_lhs_location = @1; } rhses.1 |
e9955c83 AD |
462 | ; |
463 | ||
464 | rhses.1: | |
8f3596a6 AD |
465 | rhs { grammar_current_rule_end (@1); } |
466 | | rhses.1 "|" rhs { grammar_current_rule_end (@3); } | |
8d0a98bb | 467 | | rhses.1 ";" |
e9955c83 AD |
468 | ; |
469 | ||
470 | rhs: | |
471 | /* Nothing. */ | |
8f3596a6 | 472 | { grammar_current_rule_begin (current_lhs, current_lhs_location); } |
e9955c83 | 473 | | rhs symbol |
8efe435c | 474 | { grammar_current_rule_symbol_append ($2, @2); } |
e9071366 AD |
475 | | rhs "{...}" |
476 | { grammar_current_rule_action_append (gram_last_string, | |
477 | gram_last_braced_code_loc); } | |
e9955c83 | 478 | | rhs "%prec" symbol |
e776192e | 479 | { grammar_current_rule_prec_set ($3, @3); } |
676385e2 PH |
480 | | rhs "%dprec" INT |
481 | { grammar_current_rule_dprec_set ($3, @3); } | |
482 | | rhs "%merge" TYPE | |
483 | { grammar_current_rule_merge_set ($3, @3); } | |
e9955c83 AD |
484 | ; |
485 | ||
58d7a1a1 AD |
486 | |
487 | /*---------------* | |
488 | | Identifiers. | | |
489 | *---------------*/ | |
490 | ||
33ad1a9c PE |
491 | /* Identifiers are returned as uniqstr values by the scanner. |
492 | Depending on their use, we may need to make them genuine symbols. */ | |
58d7a1a1 AD |
493 | |
494 | id: | |
33ad1a9c | 495 | ID |
203b9274 | 496 | { $$ = symbol_from_uniqstr ($1, @1); } |
33ad1a9c PE |
497 | | CHAR |
498 | { | |
499 | $$ = symbol_get (char_name ($1), @1); | |
500 | symbol_class_set ($$, token_sym, @1, false); | |
501 | symbol_user_token_number_set ($$, $1, @1); | |
502 | } | |
58d7a1a1 AD |
503 | ; |
504 | ||
505 | id_colon: | |
203b9274 | 506 | ID_COLON { $$ = symbol_from_uniqstr ($1, @1); } |
58d7a1a1 AD |
507 | ; |
508 | ||
509 | ||
e9955c83 | 510 | symbol: |
58d7a1a1 AD |
511 | id |
512 | | string_as_id | |
e9955c83 AD |
513 | ; |
514 | ||
ca407bdf | 515 | /* A string used as an ID: quote it. */ |
e9955c83 AD |
516 | string_as_id: |
517 | STRING | |
518 | { | |
ca407bdf | 519 | $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1); |
073f9288 | 520 | symbol_class_set ($$, token_sym, @1, false); |
e9955c83 AD |
521 | } |
522 | ; | |
523 | ||
ca407bdf | 524 | /* A string used for its contents. Don't quote it. */ |
e9955c83 AD |
525 | string_content: |
526 | STRING | |
ca407bdf PE |
527 | { $$ = $1; } |
528 | ; | |
e9955c83 AD |
529 | |
530 | ||
531 | epilogue.opt: | |
532 | /* Nothing. */ | |
e9955c83 AD |
533 | | "%%" EPILOGUE |
534 | { | |
e9071366 AD |
535 | muscle_code_grow ("epilogue", translate_code ($2, @2), @2); |
536 | gram_scanner_last_string_free (); | |
e9955c83 AD |
537 | } |
538 | ; | |
539 | ||
e9955c83 | 540 | %% |
b7295522 PE |
541 | |
542 | ||
543 | /* Return the location of the left-hand side of a rule whose | |
544 | right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in | |
545 | the right-hand side, and return an empty location equal to the end | |
546 | boundary of RHS[0] if the right-hand side is empty. */ | |
547 | ||
548 | static YYLTYPE | |
549 | lloc_default (YYLTYPE const *rhs, int n) | |
550 | { | |
551 | int i; | |
a737b216 | 552 | YYLTYPE loc; |
62cb8a99 PE |
553 | |
554 | /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;". | |
555 | The bug is fixed in 7.4.2m, but play it safe for now. */ | |
556 | loc.start = rhs[n].end; | |
557 | loc.end = rhs[n].end; | |
b7295522 | 558 | |
5320ca4d PE |
559 | /* Ignore empty nonterminals the start of the the right-hand side. |
560 | Do not bother to ignore them at the end of the right-hand side, | |
561 | since empty nonterminals have the same end as their predecessors. */ | |
b7295522 PE |
562 | for (i = 1; i <= n; i++) |
563 | if (! equal_boundaries (rhs[i].start, rhs[i].end)) | |
564 | { | |
a737b216 | 565 | loc.start = rhs[i].start; |
b7295522 PE |
566 | break; |
567 | } | |
568 | ||
a737b216 | 569 | return loc; |
b7295522 PE |
570 | } |
571 | ||
572 | ||
573 | /* Add a lex-param or a parse-param (depending on TYPE) with | |
574 | declaration DECL and location LOC. */ | |
575 | ||
1773ceee | 576 | static void |
e9ce5688 | 577 | add_param (char const *type, char *decl, location loc) |
1773ceee | 578 | { |
ead9e56e | 579 | static char const alphanum[26 + 26 + 1 + 10] = |
1773ceee PE |
580 | "abcdefghijklmnopqrstuvwxyz" |
581 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
ead9e56e PE |
582 | "_" |
583 | "0123456789"; | |
1773ceee | 584 | char const *name_start = NULL; |
e9ce5688 | 585 | char *p; |
1773ceee | 586 | |
e503aa60 AD |
587 | /* Stop on last actual character. */ |
588 | for (p = decl; p[1]; p++) | |
ead9e56e PE |
589 | if ((p == decl |
590 | || ! memchr (alphanum, p[-1], sizeof alphanum)) | |
591 | && memchr (alphanum, p[0], sizeof alphanum - 10)) | |
1773ceee PE |
592 | name_start = p; |
593 | ||
ead9e56e PE |
594 | /* Strip the surrounding '{' and '}', and any blanks just inside |
595 | the braces. */ | |
596 | while (*--p == ' ' || *p == '\t') | |
597 | continue; | |
e503aa60 | 598 | p[1] = '\0'; |
ead9e56e PE |
599 | while (*++decl == ' ' || *decl == '\t') |
600 | continue; | |
e9ce5688 | 601 | |
1773ceee PE |
602 | if (! name_start) |
603 | complain_at (loc, _("missing identifier in parameter declaration")); | |
604 | else | |
605 | { | |
606 | char *name; | |
607 | size_t name_len; | |
608 | ||
609 | for (name_len = 1; | |
ead9e56e | 610 | memchr (alphanum, name_start[name_len], sizeof alphanum); |
1773ceee PE |
611 | name_len++) |
612 | continue; | |
613 | ||
614 | name = xmalloc (name_len + 1); | |
615 | memcpy (name, name_start, name_len); | |
616 | name[name_len] = '\0'; | |
617 | muscle_pair_list_grow (type, decl, name); | |
618 | free (name); | |
619 | } | |
620 | ||
e9071366 | 621 | gram_scanner_last_string_free (); |
1773ceee PE |
622 | } |
623 | ||
b50d2359 AD |
624 | static void |
625 | version_check (location const *loc, char const *version) | |
626 | { | |
627 | if (strverscmp (version, PACKAGE_VERSION) > 0) | |
9b8a5ce0 AD |
628 | { |
629 | complain_at (*loc, "require bison %s, but have %s", | |
630 | version, PACKAGE_VERSION); | |
631 | exit (63); | |
632 | } | |
b50d2359 AD |
633 | } |
634 | ||
1fec91df | 635 | static void |
3d2cbc26 | 636 | gram_error (location const *loc, char const *msg) |
e9955c83 | 637 | { |
ad8a3efc | 638 | complain_at (*loc, "%s", msg); |
e9955c83 | 639 | } |
e9ce5688 PE |
640 | |
641 | char const * | |
642 | token_name (int type) | |
643 | { | |
fc01665e | 644 | return yytname[YYTRANSLATE (type)]; |
e9ce5688 | 645 | } |
33ad1a9c PE |
646 | |
647 | static char const * | |
648 | char_name (char c) | |
649 | { | |
650 | if (c == '\'') | |
651 | return "'\\''"; | |
652 | else | |
653 | { | |
654 | char buf[4]; | |
655 | buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0'; | |
656 | return quotearg_style (escape_quoting_style, buf); | |
657 | } | |
658 | } |