1 /* Bison Grammar Parser -*- C -*-
3 Copyright (C) 2002-2015, 2018-2020 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 /* Initialize unquote. */
29 void parser_init
(void);
30 /* Deallocate storage for unquote. */
31 void parser_free
(void);
36 /* On column 0 to please syntax-check. */
48 #include <vasnprintf.h>
52 #include "conflicts.h"
56 #include "named-ref.h"
58 #include "scan-code.h"
59 #include "scan-gram.h"
61 /* Pretend to be at least that version, to check features published
62 in that version while developping it. */
63 static const char* api_version
= "3.7";
65 static int current_prec
= 0;
66 static location current_lhs_loc
;
67 static named_ref
*current_lhs_named_ref
;
68 static symbol
*current_lhs_symbol
;
69 static symbol_class current_class
= unknown_sym
;
71 /** Set the new current left-hand side symbol, possibly common
72 * to several right-hand side parts of rule.
74 static void current_lhs
(symbol
*sym
, location loc
, named_ref
*ref
);
76 #define YYLLOC_DEFAULT(Current, Rhs, N) \
77 (Current
) = lloc_default
(Rhs
, N
)
78 static YYLTYPE lloc_default
(YYLTYPE const *, int);
80 #define YY_LOCATION_PRINT(File, Loc) \
81 location_print
(Loc
, File
)
83 /* Strip initial '{' and final '}' (must be first and last characters).
85 static char *strip_braces
(char *code
);
87 /* Convert CODE by calling code_props_plain_init if PLAIN, otherwise
88 code_props_symbol_action_init. Calls
89 gram_scanner_last_string_free to release the latest string from
90 the scanner (should be CODE). */
91 static char const *translate_code
(char *code
, location loc
, bool plain
);
93 /* Convert CODE by calling code_props_plain_init after having
94 stripped the first and last characters (expected to be '{', and
95 '}'). Calls gram_scanner_last_string_free to release the latest
96 string from the scanner (should be CODE). */
97 static char const *translate_code_braceless
(char *code
, location loc
);
99 /* Handle a %defines directive. */
100 static void handle_defines
(char const *value
);
102 /* Handle a %error-verbose directive. */
103 static void handle_error_verbose
(location
const *loc
, char const *directive
);
105 /* Handle a %file-prefix directive. */
106 static void handle_file_prefix
(location
const *loc
,
107 location
const *dir_loc
,
108 char const *directive
, char const *value
);
110 /* Handle a %language directive. */
111 static void handle_language
(location
const *loc
, char const *lang
);
113 /* Handle a %name-prefix directive. */
114 static void handle_name_prefix
(location
const *loc
,
115 char const *directive
, char const *value
);
117 /* Handle a %pure-parser directive. */
118 static void handle_pure_parser
(location
const *loc
, char const *directive
);
120 /* Handle a %require directive. */
121 static void handle_require
(location
const *loc
, char const *version
);
123 /* Handle a %skeleton directive. */
124 static void handle_skeleton
(location
const *loc
, char const *skel
);
126 /* Handle a %yacc directive. */
127 static void handle_yacc
(location
const *loc
);
129 /* Implementation of yyerror. */
130 static void gram_error
(location
const *, char const *);
132 /* A string that describes a char (e.g., 'a' -> "'a'"). */
133 static char const *char_name
(char);
135 /* Add style to semantic values in traces. */
136 static void tron
(FILE *yyo
);
137 static void troff
(FILE *yyo
);
139 /* Interpret a quoted string (such as `"Hello, \"World\"\n\""`).
140 Manages the memory of the result. */
141 static char *unquote
(const char *str
);
143 /* Discard the latest unquoted string. */
144 static void unquote_free
(char *last_string
);
147 %define api.header.include
{"parse-gram.h"}
148 %define api.prefix
{gram_
}
149 %define api.pure full
150 %define api.token.raw
151 %define api.value.type
union
153 %define parse.
error custom
154 %define parse.lac full
162 /* Bison's grammar can initial empty locations, hence a default
163 location is needed. */
164 boundary_set
(&@$.start
, grammar_file
, 1, 1, 1);
165 boundary_set
(&@$.end
, grammar_file
, 1, 1, 1);
170 TSTRING _
("translatable string")
172 PERCENT_TOKEN
"%token"
173 PERCENT_NTERM
"%nterm"
176 PERCENT_DESTRUCTOR
"%destructor"
177 PERCENT_PRINTER
"%printer"
180 PERCENT_RIGHT
"%right"
181 PERCENT_NONASSOC
"%nonassoc"
182 PERCENT_PRECEDENCE
"%precedence"
185 PERCENT_DPREC
"%dprec"
186 PERCENT_MERGE
"%merge"
189 PERCENT_DEFAULT_PREC
"%default-prec"
190 PERCENT_DEFINE
"%define"
191 PERCENT_DEFINES
"%defines"
192 PERCENT_ERROR_VERBOSE
"%error-verbose"
193 PERCENT_EXPECT
"%expect"
194 PERCENT_EXPECT_RR
"%expect-rr"
195 PERCENT_FLAG
"%<flag>"
196 PERCENT_FILE_PREFIX
"%file-prefix"
197 PERCENT_GLR_PARSER
"%glr-parser"
198 PERCENT_INITIAL_ACTION
"%initial-action"
199 PERCENT_LANGUAGE
"%language"
200 PERCENT_NAME_PREFIX
"%name-prefix"
201 PERCENT_NO_DEFAULT_PREC
"%no-default-prec"
202 PERCENT_NO_LINES
"%no-lines"
203 PERCENT_NONDETERMINISTIC_PARSER
204 "%nondeterministic-parser"
205 PERCENT_OUTPUT
"%output"
206 PERCENT_PURE_PARSER
"%pure-parser"
207 PERCENT_REQUIRE
"%require"
208 PERCENT_SKELETON
"%skeleton"
209 PERCENT_START
"%start"
210 PERCENT_TOKEN_TABLE
"%token-table"
211 PERCENT_VERBOSE
"%verbose"
215 BRACED_PREDICATE
"%?{...}"
216 BRACKETED_ID _
("[identifier]")
217 CHAR _
("character literal")
219 EPILOGUE _
("epilogue")
222 ID_COLON _
("identifier:")
231 /* Experimental feature, don't rely on it. */
232 %code pre
-printer
{tron
(yyo
);}
233 %code post
-printer
{troff
(yyo
);}
235 %type
<unsigned char> CHAR
236 %printer
{ fputs
(char_name
($$
), yyo
); } <unsigned char>
238 %type
<char*> "{...}" "%?{...}" "%{...%}" EPILOGUE STRING TSTRING
239 %printer
{ fputs
($$
, yyo
); } <char*>
242 BRACKETED_ID ID ID_COLON
243 PERCENT_ERROR_VERBOSE PERCENT_FILE_PREFIX PERCENT_FLAG PERCENT_NAME_PREFIX
245 TAG tag tag.opt variable
246 %printer
{ fputs
($$
, yyo
); } <uniqstr
>
247 %printer
{ fprintf
(yyo
, "[%s]", $$
); } BRACKETED_ID
248 %printer
{ fprintf
(yyo
, "%s:", $$
); } ID_COLON
249 %printer
{ fprintf
(yyo
, "%%%s", $$
); } PERCENT_FLAG
250 %printer
{ fprintf
(yyo
, "<%s>", $$
); } TAG tag
252 %token
<int> INT _
("integer literal")
253 %printer
{ fprintf
(yyo
, "%d", $$
); } <int>
255 %type
<symbol
*> id id_colon string_as_id symbol token_decl token_decl_for_prec
256 %printer
{ fprintf
(yyo
, "%s", $$ ? $$
->tag
: "<NULL>"); } <symbol
*>
257 %printer
{ fprintf
(yyo
, "%s:", $$
->tag
); } id_colon
259 %type
<assoc
> precedence_declarator
261 %destructor
{ symbol_list_free
($$
); } <symbol_list
*>
262 %printer
{ symbol_list_syms_print
($$
, yyo
); } <symbol_list
*>
264 %type
<named_ref
*> named_ref.opt
275 param_parse
= 1 << 1,
276 param_both
= param_lex | param_parse
281 /** Add a lex-param and/or a parse-param.
283 * \param type where to push this formal argument.
284 * \param decl the formal argument. Destroyed.
285 * \param loc the location in the source.
287 static void add_param
(param_type type
, char *decl
, location loc
);
288 static param_type current_param
= param_none
;
290 %token
<param_type
> PERCENT_PARAM
"%param";
295 #define CASE(In, Out) \
296 case param_
## In: fputs ("%" #Out, yyo); break
297 CASE
(lex
, lex
-param
);
298 CASE
(parse
, parse
-param
);
301 case param_none
: aver
(false
); break
;
312 prologue_declarations
"%%" grammar epilogue.opt
316 /*------------------------------------.
317 | Declarations: before the first %%. |
318 `------------------------------------*/
320 prologue_declarations:
322 | prologue_declarations prologue_declaration
325 prologue_declaration:
329 muscle_code_grow
(union_seen ?
"post_prologue" : "pre_prologue",
330 translate_code
($1, @
1, true
), @
1);
331 code_scanner_last_string_free
();
335 muscle_percent_define_ensure
($1, @
1, true
);
337 |
"%define" variable value
339 muscle_percent_define_insert
($2, @$
, $3.kind
, $3.chars
,
340 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
342 |
"%defines" { defines_flag
= true
; }
343 |
"%defines" STRING
{ handle_defines
($2); }
344 |
"%error-verbose" { handle_error_verbose
(&@$
, $1); }
345 |
"%expect" INT
{ expected_sr_conflicts
= $2; }
346 |
"%expect-rr" INT
{ expected_rr_conflicts
= $2; }
347 |
"%file-prefix" STRING
{ handle_file_prefix
(&@$
, &@
1, $1, $2); }
350 nondeterministic_parser
= true
;
353 |
"%initial-action" "{...}"
355 muscle_code_grow
("initial_action", translate_code
($2, @
2, false
), @
2);
356 code_scanner_last_string_free
();
358 |
"%language" STRING
{ handle_language
(&@
1, $2); }
359 |
"%name-prefix" STRING
{ handle_name_prefix
(&@$
, $1, $2); }
360 |
"%no-lines" { no_lines_flag
= true
; }
361 |
"%nondeterministic-parser" { nondeterministic_parser
= true
; }
362 |
"%output" STRING
{ spec_outfile
= unquote
($2); gram_scanner_last_string_free
(); }
363 |
"%param" { current_param
= $1; } params
{ current_param
= param_none
; }
364 |
"%pure-parser" { handle_pure_parser
(&@$
, $1); }
365 |
"%require" STRING
{ handle_require
(&@
2, $2); }
366 |
"%skeleton" STRING
{ handle_skeleton
(&@
2, $2); }
367 |
"%token-table" { token_table_flag
= true
; }
368 |
"%verbose" { report_flag |
= report_states
; }
369 |
"%yacc" { handle_yacc
(&@$
); }
370 |
error ";" { current_class
= unknown_sym
; yyerrok; }
371 |
/*FIXME: Err? What is this horror doing here? */ ";"
375 params
"{...}" { add_param
(current_param
, $2, @
2); }
376 |
"{...}" { add_param
(current_param
, $1, @
1); }
380 /*----------------------.
381 | grammar_declaration. |
382 `----------------------*/
388 grammar_start_symbol_set
($2, @
2);
390 | code_props_type
"{...}" generic_symlist
393 code_props_symbol_action_init
(&code
, $2, @
2);
394 code_props_translate_code
(&code
);
396 for
(symbol_list
*list
= $3; list
; list
= list
->next
)
397 symbol_list_code_props_set
(list
, $1, &code
);
398 symbol_list_free
($3);
407 default_prec
= false
;
411 /* Do not invoke muscle_percent_code_grow here since it invokes
412 muscle_user_name_list_grow. */
413 muscle_code_grow
("percent_code()",
414 translate_code_braceless
($2, @
2), @
2);
415 code_scanner_last_string_free
();
419 muscle_percent_code_grow
($2, @
2, translate_code_braceless
($3, @
3), @
3);
420 code_scanner_last_string_free
();
424 %type
<code_props_type
> code_props_type
;
425 %printer
{ fprintf
(yyo
, "%s", code_props_type_string
($$
)); } <code_props_type
>;
427 "%destructor" { $$
= destructor
; }
428 |
"%printer" { $$
= printer
; }
435 %token PERCENT_UNION
"%union";
439 | ID
{ muscle_percent_define_insert
("api.value.union.name",
440 @
1, muscle_keyword
, $1,
441 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
); }
445 "%union" union_name
"{...}"
448 muscle_code_grow
("union_members", translate_code_braceless
($3, @
3), @
3);
449 code_scanner_last_string_free
();
454 %type
<symbol_list
*> nterm_decls symbol_decls symbol_decl
.1
455 token_decls token_decls_for_prec
456 token_decl
.1 token_decl_for_prec
.1;
458 "%nterm" { current_class
= nterm_sym
; } nterm_decls
[syms
]
460 current_class
= unknown_sym
;
461 symbol_list_free
($syms);
463 |
"%token" { current_class
= token_sym
; } token_decls
[syms
]
465 current_class
= unknown_sym
;
466 symbol_list_free
($syms);
468 |
"%type" symbol_decls
[syms
]
470 symbol_list_free
($syms);
472 | precedence_declarator token_decls_for_prec
[syms
]
475 for
(symbol_list
*list
= $syms; list
; list
= list
->next
)
476 symbol_precedence_set
(list
->content.sym
, current_prec
, $1, @
1);
477 symbol_list_free
($syms);
481 precedence_declarator:
482 "%left" { $$
= left_assoc
; }
483 |
"%right" { $$
= right_assoc
; }
484 |
"%nonassoc" { $$
= non_assoc
; }
485 |
"%precedence" { $$
= precedence_assoc
; }
489 %empty
{ $$
= NULL
; }
493 %type
<symbol_list
*> generic_symlist generic_symlist_item
;
496 | generic_symlist generic_symlist_item
{ $$
= symbol_list_append
($1, $2); }
499 generic_symlist_item:
500 symbol
{ $$
= symbol_list_sym_new
($1, @
1); }
501 | tag
{ $$
= symbol_list_type_new
($1, @
1); }
506 |
"<*>" { $$
= uniqstr_new
("*"); }
507 |
"<>" { $$
= uniqstr_new
(""); }
510 /*-----------------------.
511 | nterm_decls (%nterm). |
512 `-----------------------*/
514 // A non empty list of possibly tagged symbols for %nterm.
516 // Can easily be defined like symbol_decls but restricted to ID, but
517 // using token_decls allows to reudce the number of rules, and also to
518 // make nicer error messages on "%nterm 'a'" or '%nterm FOO "foo"'.
523 /*-----------------------------------.
524 | token_decls (%token, and %nterm). |
525 `-----------------------------------*/
527 // A non empty list of possibly tagged symbols for %token or %nterm.
533 | TAG token_decl
.1[syms
]
535 $$
= symbol_list_type_set
($syms, $TAG, @TAG
);
537 | token_decls TAG token_decl
.1[syms
]
539 $$
= symbol_list_append
($1, symbol_list_type_set
($syms, $TAG, @TAG
));
543 // One or more symbol declarations for %token or %nterm.
545 token_decl
{ $$
= symbol_list_sym_new
($1, @
1); }
546 | token_decl
.1 token_decl
{ $$
= symbol_list_append
($1, symbol_list_sym_new
($2, @
2)); }
548 // One symbol declaration for %token or %nterm.
550 id
int.opt
[num
] alias
553 symbol_class_set
($id, current_class
, @id
, true
);
555 symbol_code_set
($id, $num, @num
);
557 symbol_make_alias
($id, $alias, @alias
);
567 %type
<symbol
*> alias
;
569 %empty
{ $$
= NULL
; }
570 | string_as_id
{ $$
= $1; }
573 $$
= symbol_get
($1, @
1);
574 symbol_class_set
($$
, token_sym
, @
1, false
);
575 $$
->translatable
= true
;
580 /*-------------------------------------.
581 | token_decls_for_prec (%left, etc.). |
582 `-------------------------------------*/
584 // A non empty list of possibly tagged tokens for precedence declaration.
586 // Similar to %token (token_decls), but in '%left FOO 1 "foo"', it treats
587 // FOO and "foo" as two different symbols instead of aliasing them.
588 token_decls_for_prec:
589 token_decl_for_prec
.1[syms
]
593 | TAG token_decl_for_prec
.1[syms
]
595 $$
= symbol_list_type_set
($syms, $TAG, @TAG
);
597 | token_decls_for_prec TAG token_decl_for_prec
.1[syms
]
599 $$
= symbol_list_append
($1, symbol_list_type_set
($syms, $TAG, @TAG
));
603 // One or more token declarations for precedence declaration.
604 token_decl_for_prec
.1:
606 { $$
= symbol_list_sym_new
($1, @
1); }
607 | token_decl_for_prec
.1 token_decl_for_prec
608 { $$
= symbol_list_append
($1, symbol_list_sym_new
($2, @
2)); }
610 // One token declaration for precedence declaration.
615 symbol_class_set
($id, token_sym
, @id
, false
);
617 symbol_code_set
($id, $num, @num
);
623 /*-----------------------------------.
624 | symbol_decls (argument of %type). |
625 `-----------------------------------*/
627 // A non empty list of typed symbols (for %type).
633 | TAG symbol_decl
.1[syms
]
635 $$
= symbol_list_type_set
($syms, $TAG, @TAG
);
637 | symbol_decls TAG symbol_decl
.1[syms
]
639 $$
= symbol_list_append
($1, symbol_list_type_set
($syms, $TAG, @TAG
));
643 // One or more token declarations (for %type).
647 symbol_class_set
($symbol, pct_type_sym
, @symbol
, false
);
648 $$
= symbol_list_sym_new
($symbol, @symbol
);
650 | symbol_decl
.1 symbol
652 symbol_class_set
($symbol, pct_type_sym
, @symbol
, false
);
653 $$
= symbol_list_append
($1, symbol_list_sym_new
($symbol, @symbol
));
657 /*------------------------------------------.
658 | The grammar section: between the two %%. |
659 `------------------------------------------*/
662 rules_or_grammar_declaration
663 | grammar rules_or_grammar_declaration
666 /* As a Bison extension, one can use the grammar declarations in the
667 body of the grammar. */
668 rules_or_grammar_declaration:
670 | grammar_declaration
";"
678 id_colon named_ref.opt
{ current_lhs
($1, @
1, $2); } ":" rhses
.1
680 /* Free the current lhs. */
681 current_lhs
(0, @
1, 0);
686 rhs
{ grammar_current_rule_end
(@rhs
); }
687 | rhses
.1 "|" rhs
{ grammar_current_rule_end
(@rhs
); }
691 %token PERCENT_EMPTY
"%empty";
694 { grammar_current_rule_begin
(current_lhs_symbol
, current_lhs_loc
,
695 current_lhs_named_ref
); }
696 | rhs symbol named_ref.opt
697 { grammar_current_rule_symbol_append
($2, @
2, $3); }
698 | rhs tag.opt
"{...}"[action
] named_ref.opt
[name
]
699 { grammar_current_rule_action_append
($action, @action
, $name, $
[tag.opt
]); }
701 { grammar_current_rule_predicate_append
($2, @
2); }
703 { grammar_current_rule_empty_set
(@
2); }
705 { grammar_current_rule_prec_set
($3, @
3); }
707 { grammar_current_rule_dprec_set
($3, @
3); }
709 { grammar_current_rule_merge_set
($3, @
3); }
711 { grammar_current_rule_expect_sr
($3, @
3); }
712 | rhs
"%expect-rr" INT
713 { grammar_current_rule_expect_rr
($3, @
3); }
717 %empty
{ $$
= NULL
; }
718 | BRACKETED_ID
{ $$
= named_ref_new
($1, @
1); }
722 /*---------------------.
723 | variable and value. |
724 `---------------------*/
730 /* Some content or empty by default. */
732 #include "muscle-tab.h"
739 %type
<value_type
> value
;
744 case muscle_code
: fprintf
(yyo
, "{%s}", $$.chars
); break
;
745 case muscle_keyword
: fprintf
(yyo
, "%s", $$.chars
); break
;
746 case muscle_string
: fprintf
(yyo
, "\"%s\"", $$.chars
); break
;
751 %empty
{ $$.kind
= muscle_keyword
; $$.chars
= ""; }
752 | ID
{ $$.kind
= muscle_keyword
; $$.chars
= $1; }
753 | STRING
{ $$.kind
= muscle_string
; $$.chars
= unquote
($1); gram_scanner_last_string_free
();}
754 |
"{...}" { $$.kind
= muscle_code
; $$.chars
= strip_braces
($1); gram_scanner_last_string_free
(); }
762 /* Identifiers are returned as uniqstr values by the scanner.
763 Depending on their use, we may need to make them genuine symbols. */
767 { $$
= symbol_from_uniqstr
($1, @
1); }
770 const char *var
= "api.token.raw";
771 if
(current_class
== nterm_sym
)
773 complain
(&@
1, complaint
,
774 _
("character literals cannot be nonterminals"));
777 if
(muscle_percent_define_ifdef
(var
))
779 complain
(&@
1, complaint
,
780 _
("character literals cannot be used together"
782 location loc
= muscle_percent_define_get_loc
(var
);
783 subcomplain
(&loc
, complaint
, _
("definition of %s"), var
);
785 $$
= symbol_get
(char_name
($1), @
1);
786 symbol_class_set
($$
, token_sym
, @
1, false
);
787 symbol_code_set
($$
, $1, @
1);
792 ID_COLON
{ $$
= symbol_from_uniqstr
($1, @
1); }
801 /* A string used as an ID. */
805 $$
= symbol_get
($1, @
1);
806 symbol_class_set
($$
, token_sym
, @
1, false
);
814 muscle_code_grow
("epilogue", translate_code
($2, @
2, true
), @
2);
815 code_scanner_last_string_free
();
822 yyreport_syntax_error
(const yypcontext_t
*ctx
)
825 /* Arguments of format: reported tokens (one for the "unexpected",
826 one per "expected"). */
827 enum { ARGS_MAX
= 5 };
828 const char *argv
[ARGS_MAX
];
830 yysymbol_kind_t unexpected
= yypcontext_token
(ctx
);
831 if
(unexpected
!= YYSYMBOL_YYEMPTY
)
833 argv
[argc
++] = yysymbol_name
(unexpected
);
834 yysymbol_kind_t expected
[ARGS_MAX
- 1];
835 int nexpected
= yypcontext_expected_tokens
(ctx
, expected
, ARGS_MAX
- 1);
839 for
(int i
= 0; i
< nexpected
; ++i
)
840 argv
[argc
++] = yysymbol_name
(expected
[i
]);
842 syntax_error
(*yypcontext_location
(ctx
), argc
, argv
);
847 /* Return the location of the left-hand side of a rule whose
848 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
849 the right-hand side, and return an empty location equal to the end
850 boundary of RHS[0] if the right-hand side is empty. */
853 lloc_default
(YYLTYPE const *rhs
, int n
)
857 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
858 The bug is fixed in 7.4.2m, but play it safe for now. */
859 loc.start
= rhs
[n
].end
;
860 loc.end
= rhs
[n
].end
;
862 /* Ignore empty nonterminals the start of the right-hand side.
863 Do not bother to ignore them at the end of the right-hand side,
864 since empty nonterminals have the same end as their predecessors. */
865 for
(int i
= 1; i
<= n
; i
++)
866 if
(! equal_boundaries
(rhs
[i
].start
, rhs
[i
].end
))
868 loc.start
= rhs
[i
].start
;
876 char *strip_braces
(char *code
)
878 code
[strlen
(code
) - 1] = 0;
884 translate_code
(char *code
, location loc
, bool plain
)
886 code_props plain_code
;
888 code_props_plain_init
(&plain_code
, code
, loc
);
890 code_props_symbol_action_init
(&plain_code
, code
, loc
);
891 code_props_translate_code
(&plain_code
);
892 gram_scanner_last_string_free
();
893 return plain_code.code
;
898 translate_code_braceless
(char *code
, location loc
)
900 return translate_code
(strip_braces
(code
), loc
, true
);
904 add_param
(param_type type
, char *decl
, location loc
)
906 static char const alphanum
[26 + 26 + 1 + 10 + 1] =
907 "abcdefghijklmnopqrstuvwxyz"
908 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
912 char const *name_start
= NULL
;
915 /* Stop on last actual character. */
916 for
(p
= decl
; p
[1]; p
++)
918 ||
! memchr
(alphanum
, p
[-1], sizeof alphanum
- 1))
919 && memchr
(alphanum
, p
[0], sizeof alphanum
- 10 - 1))
922 /* Strip the surrounding '{' and '}', and any blanks just inside
925 while
(c_isspace
((unsigned char) *p
))
929 while
(c_isspace
((unsigned char) *decl
))
934 complain
(&loc
, complaint
, _
("missing identifier in parameter declaration"));
937 char *name
= xmemdup0
(name_start
, strspn
(name_start
, alphanum
));
938 if
(type
& param_lex
)
939 muscle_pair_list_grow
("lex_param", decl
, name
);
940 if
(type
& param_parse
)
941 muscle_pair_list_grow
("parse_param", decl
, name
);
945 gram_scanner_last_string_free
();
950 handle_defines
(char const *value
)
953 char *file
= unquote
(value
);
954 spec_header_file
= xstrdup
(file
);
955 gram_scanner_last_string_free
();
961 handle_error_verbose
(location
const *loc
, char const *directive
)
963 bison_directive
(loc
, directive
);
964 muscle_percent_define_insert
(directive
, *loc
, muscle_keyword
, "",
965 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
970 handle_file_prefix
(location
const *loc
,
971 location
const *dir_loc
,
972 char const *directive
, char const *value_quoted
)
974 char *value
= unquote
(value_quoted
);
975 bison_directive
(loc
, directive
);
978 if
(location_empty
(spec_file_prefix_loc
))
980 spec_file_prefix_loc
= *loc
;
981 spec_file_prefix
= value
;
985 duplicate_directive
(directive
, spec_file_prefix_loc
, *loc
);
990 && STRNEQ
(directive
, "%file-prefix"))
991 deprecated_directive
(dir_loc
, directive
, "%file-prefix");
995 handle_language
(location
const *loc
, char const *lang
)
997 language_argmatch
(unquote
(lang
), grammar_prio
, *loc
);
1002 handle_name_prefix
(location
const *loc
,
1003 char const *directive
, char const *value_quoted
)
1005 char *value
= unquote
(value_quoted
);
1006 bison_directive
(loc
, directive
);
1009 size_t len1
= sizeof
(buf1
);
1010 char *old
= asnprintf
(buf1
, &len1
, "%s\"%s\"", directive
, value
);
1014 if
(location_empty
(spec_name_prefix_loc
))
1016 spec_name_prefix
= value
;
1017 spec_name_prefix_loc
= *loc
;
1020 size_t len2
= sizeof
(buf2
);
1021 char *new
= asnprintf
(buf2
, &len2
, "%%define api.prefix {%s}", value
);
1024 deprecated_directive
(loc
, old
, new
);
1029 duplicate_directive
(old
, spec_file_prefix_loc
, *loc
);
1037 handle_pure_parser
(location
const *loc
, char const *directive
)
1039 bison_directive
(loc
, directive
);
1040 deprecated_directive
(loc
, directive
, "%define api.pure");
1041 muscle_percent_define_insert
("api.pure", *loc
, muscle_keyword
, "",
1042 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
);
1046 /* Convert VERSION into an int (MAJOR * 100 + MINOR). Return -1 on
1049 Changes of behavior are only on minor version changes, so "3.0.5"
1050 is the same as "3.0": 300. */
1052 str_to_version
(char const *version
)
1054 IGNORE_TYPE_LIMITS_BEGIN
1058 long major
= strtol
(version
, &cp
, 10);
1059 if
(errno || cp
== version ||
*cp
!= '.' || major
< 0
1060 || INT_MULTIPLY_WRAPV
(major
, 100, &res
))
1065 long minor
= strtol
(cp
, &cp1
, 10);
1066 if
(errno || cp1
== cp ||
(*cp1
!= '\0' && *cp1
!= '.')
1067 ||
! (0 <= minor
&& minor
< 100)
1068 || INT_ADD_WRAPV
(minor
, res
, &res
))
1071 IGNORE_TYPE_LIMITS_END
1077 handle_require
(location
const *loc
, char const *version_quoted
)
1079 char *version
= unquote
(version_quoted
);
1080 required_version
= str_to_version
(version
);
1081 if
(required_version
== -1)
1083 complain
(loc
, complaint
, _
("invalid version requirement: %s"),
1085 required_version
= 0;
1089 const char* package_version
=
1090 0 < strverscmp
(api_version
, PACKAGE_VERSION
)
1091 ? api_version
: PACKAGE_VERSION
;
1092 if
(0 < strverscmp
(version
, package_version
))
1094 complain
(loc
, complaint
, _
("require bison %s, but have %s"),
1095 version
, package_version
);
1099 unquote_free
(version
);
1100 gram_scanner_last_string_free
();
1104 handle_skeleton
(location
const *loc
, char const *skel_quoted
)
1106 char *skel
= unquote
(skel_quoted
);
1107 char const *skeleton_user
= skel
;
1108 if
(strchr
(skeleton_user
, '/'))
1110 size_t dir_length
= strlen
(grammar_file
);
1111 while
(dir_length
&& grammar_file
[dir_length
- 1] != '/')
1113 while
(dir_length
&& grammar_file
[dir_length
- 1] == '/')
1115 char *skeleton_build
=
1116 xmalloc
(dir_length
+ 1 + strlen
(skeleton_user
) + 1);
1119 memcpy
(skeleton_build
, grammar_file
, dir_length
);
1120 skeleton_build
[dir_length
++] = '/';
1122 strcpy
(skeleton_build
+ dir_length
, skeleton_user
);
1123 skeleton_user
= uniqstr_new
(skeleton_build
);
1124 free
(skeleton_build
);
1126 skeleton_arg
(skeleton_user
, grammar_prio
, *loc
);
1131 handle_yacc
(location
const *loc
)
1133 const char *directive
= "%yacc";
1134 bison_directive
(loc
, directive
);
1135 if
(location_empty
(yacc_loc
))
1138 duplicate_directive
(directive
, yacc_loc
, *loc
);
1143 gram_error
(location
const *loc
, char const *msg
)
1145 complain
(loc
, complaint
, "%s", msg
);
1156 buf
[0] = '\''; buf
[1] = c
; buf
[2] = '\''; buf
[3] = '\0';
1157 return quotearg_style
(escape_quoting_style
, buf
);
1163 current_lhs
(symbol
*sym
, location loc
, named_ref
*ref
)
1165 current_lhs_symbol
= sym
;
1166 current_lhs_loc
= loc
;
1168 symbol_location_as_lhs_set
(sym
, loc
);
1169 /* In order to simplify memory management, named references for lhs
1170 are always assigned by deep copy into the current symbol_list
1171 node. This is because a single named-ref in the grammar may
1172 result in several uses when the user factors lhs between several
1173 rules using "|". Therefore free the parser's original copy. */
1174 free
(current_lhs_named_ref
);
1175 current_lhs_named_ref
= ref
;
1178 static void tron
(FILE *yyo
)
1180 begin_use_class
("value", yyo
);
1183 static void troff
(FILE *yyo
)
1185 end_use_class
("value", yyo
);
1193 struct obstack obstack_for_unquote
;
1198 obstack_init
(&obstack_for_unquote
);
1204 obstack_free
(&obstack_for_unquote
, 0);
1208 unquote_free
(char *last_string
)
1210 obstack_free
(&obstack_for_unquote
, last_string
);
1214 unquote
(const char *cp
)
1216 #define GROW(Char) \
1217 obstack_1grow
(&obstack_for_unquote
, Char
);
1218 for
(++cp
; *cp
&& *cp
!= '"'; ++cp
)
1227 case
'0': case
'1': case
'2': case
'3': case
'4':
1228 case
'5': case
'6': case
'7': case
'8': case
'9':
1230 int c
= cp
[0] - '0';
1231 if
(c_isdigit
(cp
[1]))
1234 c
= c
* 8 + cp
[0] - '0';
1236 if
(c_isdigit
(cp
[1]))
1239 c
= c
* 8 + cp
[0] - '0';
1245 case
'a': GROW
('\a'); break
;
1246 case
'b': GROW
('\b'); break
;
1247 case
'f': GROW
('\f'); break
;
1248 case
'n': GROW
('\n'); break
;
1249 case
'r': GROW
('\r'); break
;
1250 case
't': GROW
('\t'); break
;
1251 case
'v': GROW
('\v'); break
;
1256 while
(c_isxdigit
(cp
[1]))
1259 c
= (c
* 16 + (c_isdigit
(cp
[0]) ? cp
[0] - '0'
1260 : c_isupper
(cp
[0]) ? cp
[0] - 'A'
1273 assert
(*cp
== '"');
1275 assert
(*cp
== '\0');
1277 return obstack_finish0
(&obstack_for_unquote
);