mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 04:13:03 +00:00
style: rename user_token_number as code
This should have been done in 3.6, but I wanted to avoid introducing conflicts into Vincent's work on counterexamples. It turns out it's completely orthogonal. * data/README.md, data/skeletons/bison.m4, data/skeletons/c++.m4, * data/skeletons/c.m4, data/skeletons/glr.c, data/skeletons/java.m4, * data/skeletons/lalr1.d, data/skeletons/lalr1.java, * data/skeletons/variant.hh, data/skeletons/yacc.c, src/conflicts.c, * src/derives.c, src/gram.c, src/gram.h, src/output.c, * src/parse-gram.c, src/parse-gram.y, src/print-xml.c, src/print.c, * src/reader.c, src/symtab.c, src/symtab.h, tests/input.at, * tests/types.at: s/user_token_number/code/g. Plus minor changes.
This commit is contained in:
6
TODO
6
TODO
@@ -58,12 +58,6 @@ Stop hard-coding "Calc". Adjust local.at (look for FIXME).
|
|||||||
See https://github.com/akimd/bison/pull/15.
|
See https://github.com/akimd/bison/pull/15.
|
||||||
|
|
||||||
** Clean up
|
** Clean up
|
||||||
Rename user_token_number for tokens as "code". It's not a "user number",
|
|
||||||
it's the token code, and the user can control it, but this code always
|
|
||||||
exists.
|
|
||||||
|
|
||||||
See also USER_NUMBER_UNDEFINED and NUMBER_UNDEFINED.
|
|
||||||
|
|
||||||
Rename endtoken as eoftoken.
|
Rename endtoken as eoftoken.
|
||||||
|
|
||||||
Also do it in data/skeletons.
|
Also do it in data/skeletons.
|
||||||
|
|||||||
@@ -95,11 +95,11 @@ The macro `b4_symbol(NUM, FIELD)` gives access to the following FIELDS:
|
|||||||
- `tag`: string
|
- `tag`: string
|
||||||
A human representation of the symbol. Can be 'foo', 'foo.id', '"foo"' etc.
|
A human representation of the symbol. Can be 'foo', 'foo.id', '"foo"' etc.
|
||||||
|
|
||||||
- `user_number`: integer
|
- `code`: integer
|
||||||
The code associated to the `id`.
|
The token code associated to the `id`.
|
||||||
The external number as used by yylex. Can be ASCII code when a character,
|
The external number as used by yylex. Can be ASCII code when a character,
|
||||||
some number chosen by bison, or some user number in the case of
|
some number chosen by bison, or some user number in the case of `%token
|
||||||
%token FOO <NUM>. Corresponds to yychar in yacc.c.
|
FOO <NUM>`. Corresponds to `yychar` in `yacc.c`.
|
||||||
|
|
||||||
- `is_token`: 0 or 1
|
- `is_token`: 0 or 1
|
||||||
Whether this is a terminal symbol.
|
Whether this is a terminal symbol.
|
||||||
|
|||||||
@@ -586,7 +586,7 @@ m4_define([b4_token_format],
|
|||||||
[b4_token_visible_if([$2],
|
[b4_token_visible_if([$2],
|
||||||
[m4_format([[$1]],
|
[m4_format([[$1]],
|
||||||
m4_expand(b4_symbol([$2], [id])),
|
m4_expand(b4_symbol([$2], [id])),
|
||||||
m4_expand(b4_symbol([$2], b4_api_token_raw_if([[number]], [[user_number]]))))])])
|
m4_expand(b4_symbol([$2], b4_api_token_raw_if([[number]], [[code]]))))])])
|
||||||
|
|
||||||
|
|
||||||
# b4_last_enum_token
|
# b4_last_enum_token
|
||||||
|
|||||||
@@ -594,11 +594,11 @@ m4_define([b4_yytranslate_define],
|
|||||||
{
|
{
|
||||||
]b4_translate[
|
]b4_translate[
|
||||||
};
|
};
|
||||||
const int user_token_number_max_ = ]b4_user_token_number_max[;
|
const int code_max_ = ]b4_code_max[;
|
||||||
|
|
||||||
if (t <= 0)
|
if (t <= 0)
|
||||||
return symbol_kind::]b4_symbol_prefix[YYEOF;
|
return symbol_kind::]b4_symbol_prefix[YYEOF;
|
||||||
else if (t <= user_token_number_max_)
|
else if (t <= code_max_)
|
||||||
return YY_CAST (symbol_kind_type, translate_table[t]);
|
return YY_CAST (symbol_kind_type, translate_table[t]);
|
||||||
else
|
else
|
||||||
return symbol_kind::]b4_symbol_prefix[YYUNDEF;]])[
|
return symbol_kind::]b4_symbol_prefix[YYUNDEF;]])[
|
||||||
|
|||||||
@@ -523,7 +523,7 @@ m4_define([b4_token_enum],
|
|||||||
[m4_format([ %-30s %s],
|
[m4_format([ %-30s %s],
|
||||||
m4_format([[%s = %s%s%s]],
|
m4_format([[%s = %s%s%s]],
|
||||||
b4_symbol([$1], [id]),
|
b4_symbol([$1], [id]),
|
||||||
b4_symbol([$1], b4_api_token_raw_if([[number]], [[user_number]])),
|
b4_symbol([$1], b4_api_token_raw_if([[number]], [[code]])),
|
||||||
m4_if([$1], b4_last_enum_token, [], [[,]])),
|
m4_if([$1], b4_last_enum_token, [], [[,]])),
|
||||||
[b4_symbol_tag_comment([$1])])])])
|
[b4_symbol_tag_comment([$1])])])])
|
||||||
|
|
||||||
|
|||||||
@@ -332,7 +332,7 @@ static YYLTYPE yyloc_default][]b4_yyloc_default;])[
|
|||||||
#define YYMAXLEFT ]b4_max_left_semantic_context[
|
#define YYMAXLEFT ]b4_max_left_semantic_context[
|
||||||
|
|
||||||
/* YYMAXUTOK -- Last valid token number (for yychar). */
|
/* YYMAXUTOK -- Last valid token number (for yychar). */
|
||||||
#define YYMAXUTOK ]b4_user_token_number_max[
|
#define YYMAXUTOK ]b4_code_max[
|
||||||
|
|
||||||
/* YYTRANSLATE(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM
|
/* YYTRANSLATE(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM
|
||||||
as returned by yylex, with out-of-bounds checking. */
|
as returned by yylex, with out-of-bounds checking. */
|
||||||
|
|||||||
@@ -143,7 +143,7 @@ m4_define([b4_token_enum],
|
|||||||
]],
|
]],
|
||||||
b4_symbol([$1], [tag]),
|
b4_symbol([$1], [tag]),
|
||||||
b4_symbol([$1], [id]),
|
b4_symbol([$1], [id]),
|
||||||
b4_symbol([$1], b4_api_token_raw_if([[number]], [[user_number]])))])])
|
b4_symbol([$1], b4_api_token_raw_if([[number]], [[code]])))])])
|
||||||
|
|
||||||
|
|
||||||
# b4_token_enums
|
# b4_token_enums
|
||||||
|
|||||||
@@ -841,11 +841,11 @@ m4_popdef([b4_at_dollar])])dnl
|
|||||||
]b4_translate[
|
]b4_translate[
|
||||||
@};
|
@};
|
||||||
|
|
||||||
immutable int user_token_number_max_ = ]b4_user_token_number_max[;
|
immutable int code_max_ = ]b4_code_max[;
|
||||||
|
|
||||||
if (t <= 0)
|
if (t <= 0)
|
||||||
return SymbolKind.]b4_symbol_prefix[YYEOF;
|
return SymbolKind.]b4_symbol_prefix[YYEOF;
|
||||||
else if (t <= user_token_number_max_)
|
else if (t <= code_max_)
|
||||||
{
|
{
|
||||||
import std.conv : to;
|
import std.conv : to;
|
||||||
return to!SymbolKind (translate_table[t]);
|
return to!SymbolKind (translate_table[t]);
|
||||||
|
|||||||
@@ -1085,10 +1085,10 @@ b4_dollar_popdef[]dnl
|
|||||||
}
|
}
|
||||||
]],
|
]],
|
||||||
[[ {
|
[[ {
|
||||||
int user_token_number_max_ = ]b4_user_token_number_max[;
|
int code_max_ = ]b4_code_max[;
|
||||||
if (t <= 0)
|
if (t <= 0)
|
||||||
return SymbolKind.]b4_symbol_prefix[YYEOF;
|
return SymbolKind.]b4_symbol_prefix[YYEOF;
|
||||||
else if (t <= user_token_number_max_)
|
else if (t <= code_max_)
|
||||||
return SymbolKind.get (yytranslate_table_[t]);
|
return SymbolKind.get (yytranslate_table_[t]);
|
||||||
else
|
else
|
||||||
return SymbolKind.]b4_symbol_prefix[YYUNDEF;
|
return SymbolKind.]b4_symbol_prefix[YYUNDEF;
|
||||||
|
|||||||
@@ -392,7 +392,7 @@ m4_define([_b4_type_clause],
|
|||||||
[b4_symbol_if([$1], [is_token],
|
[b4_symbol_if([$1], [is_token],
|
||||||
[b4_symbol_if([$1], [has_id],
|
[b4_symbol_if([$1], [has_id],
|
||||||
[tok == token::b4_symbol([$1], [id])],
|
[tok == token::b4_symbol([$1], [id])],
|
||||||
[tok == b4_symbol([$1], [user_number])])])])
|
[tok == b4_symbol([$1], [code])])])])
|
||||||
|
|
||||||
|
|
||||||
# _b4_token_constructor_define(SYMBOL-NUM...)
|
# _b4_token_constructor_define(SYMBOL-NUM...)
|
||||||
|
|||||||
@@ -592,7 +592,7 @@ union yyalloc
|
|||||||
/* YYNSTATES -- Number of states. */
|
/* YYNSTATES -- Number of states. */
|
||||||
#define YYNSTATES ]b4_states_number[
|
#define YYNSTATES ]b4_states_number[
|
||||||
|
|
||||||
#define YYMAXUTOK ]b4_user_token_number_max[
|
#define YYMAXUTOK ]b4_code_max[
|
||||||
|
|
||||||
|
|
||||||
/* YYTRANSLATE(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM
|
/* YYTRANSLATE(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM
|
||||||
|
|||||||
@@ -710,13 +710,13 @@ rule_conflicts_print (void)
|
|||||||
complain (&r->location, complaint,
|
complain (&r->location, complaint,
|
||||||
_("shift/reduce conflicts for rule %d:"
|
_("shift/reduce conflicts for rule %d:"
|
||||||
" %d found, %d expected"),
|
" %d found, %d expected"),
|
||||||
r->user_number, sr, expected_sr);
|
r->code, sr, expected_sr);
|
||||||
int rr = count_rule_rr_conflicts (r);
|
int rr = count_rule_rr_conflicts (r);
|
||||||
if (rr != expected_rr && (rr != 0 || expected_rr != -1))
|
if (rr != expected_rr && (rr != 0 || expected_rr != -1))
|
||||||
complain (&r->location, complaint,
|
complain (&r->location, complaint,
|
||||||
_("reduce/reduce conflicts for rule %d:"
|
_("reduce/reduce conflicts for rule %d:"
|
||||||
" %d found, %d expected"),
|
" %d found, %d expected"),
|
||||||
r->user_number, rr, expected_rr);
|
r->code, rr, expected_rr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (warning_is_enabled (Wcounterexample))
|
if (warning_is_enabled (Wcounterexample))
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ print_derives (void)
|
|||||||
fprintf (stderr, " %s derives\n", symbols[i]->tag);
|
fprintf (stderr, " %s derives\n", symbols[i]->tag);
|
||||||
for (rule **rp = derives[i - ntokens]; *rp; ++rp)
|
for (rule **rp = derives[i - ntokens]; *rp; ++rp)
|
||||||
{
|
{
|
||||||
fprintf (stderr, " %3d ", (*rp)->user_number);
|
fprintf (stderr, " %3d ", (*rp)->code);
|
||||||
rule_rhs_print (*rp, stderr);
|
rule_rhs_print (*rp, stderr);
|
||||||
fprintf (stderr, "\n");
|
fprintf (stderr, "\n");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ int nvars = 0;
|
|||||||
|
|
||||||
symbol_number *token_translations = NULL;
|
symbol_number *token_translations = NULL;
|
||||||
|
|
||||||
int max_user_token_number = 256;
|
int max_code = 256;
|
||||||
|
|
||||||
int required_version = 0;
|
int required_version = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -173,7 +173,7 @@ typedef struct
|
|||||||
{
|
{
|
||||||
/* The number of the rule in the source. It is usually the index in
|
/* The number of the rule in the source. It is usually the index in
|
||||||
RULES too, except if there are useless rules. */
|
RULES too, except if there are useless rules. */
|
||||||
rule_number user_number;
|
rule_number code;
|
||||||
|
|
||||||
/* The index in RULES. Usually the rule number in the source,
|
/* The index in RULES. Usually the rule number in the source,
|
||||||
except if some rules are useless. */
|
except if some rules are useless. */
|
||||||
@@ -262,7 +262,7 @@ extern symbol **symbols;
|
|||||||
by the user's yylex routine, it yields the internal token number
|
by the user's yylex routine, it yields the internal token number
|
||||||
used by the parser and throughout bison. */
|
used by the parser and throughout bison. */
|
||||||
extern symbol_number *token_translations;
|
extern symbol_number *token_translations;
|
||||||
extern int max_user_token_number;
|
extern int max_code;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
10
src/output.c
10
src/output.c
@@ -274,12 +274,12 @@ prepare_symbols (void)
|
|||||||
MUSCLE_INSERT_INT ("tokens_number", ntokens);
|
MUSCLE_INSERT_INT ("tokens_number", ntokens);
|
||||||
MUSCLE_INSERT_INT ("nterms_number", nvars);
|
MUSCLE_INSERT_INT ("nterms_number", nvars);
|
||||||
MUSCLE_INSERT_INT ("symbols_number", nsyms);
|
MUSCLE_INSERT_INT ("symbols_number", nsyms);
|
||||||
MUSCLE_INSERT_INT ("user_token_number_max", max_user_token_number);
|
MUSCLE_INSERT_INT ("code_max", max_code);
|
||||||
|
|
||||||
muscle_insert_symbol_number_table ("translate",
|
muscle_insert_symbol_number_table ("translate",
|
||||||
token_translations,
|
token_translations,
|
||||||
token_translations[0],
|
token_translations[0],
|
||||||
1, max_user_token_number + 1);
|
1, max_code + 1);
|
||||||
|
|
||||||
/* tname -- token names. */
|
/* tname -- token names. */
|
||||||
prepare_symbol_names ("tname");
|
prepare_symbol_names ("tname");
|
||||||
@@ -309,7 +309,7 @@ prepare_symbols (void)
|
|||||||
{
|
{
|
||||||
int *values = xnmalloc (ntokens, sizeof *values);
|
int *values = xnmalloc (ntokens, sizeof *values);
|
||||||
for (int i = 0; i < ntokens; ++i)
|
for (int i = 0; i < ntokens; ++i)
|
||||||
values[i] = symbols[i]->content->user_token_number;
|
values[i] = symbols[i]->content->code;
|
||||||
muscle_insert_int_table ("toknum", values,
|
muscle_insert_int_table ("toknum", values,
|
||||||
values[0], 1, ntokens);
|
values[0], 1, ntokens);
|
||||||
free (values);
|
free (values);
|
||||||
@@ -565,8 +565,8 @@ prepare_symbol_definitions (void)
|
|||||||
SET_KEY ("tag");
|
SET_KEY ("tag");
|
||||||
MUSCLE_INSERT_STRING (key, symbol_tag (sym));
|
MUSCLE_INSERT_STRING (key, symbol_tag (sym));
|
||||||
|
|
||||||
SET_KEY ("user_number");
|
SET_KEY ("code");
|
||||||
MUSCLE_INSERT_INT (key, sym->content->user_token_number);
|
MUSCLE_INSERT_INT (key, sym->content->code);
|
||||||
|
|
||||||
SET_KEY ("is_token");
|
SET_KEY ("is_token");
|
||||||
MUSCLE_INSERT_INT (key, i < ntokens);
|
MUSCLE_INSERT_INT (key, i < ntokens);
|
||||||
|
|||||||
@@ -2367,7 +2367,7 @@ yyreduce:
|
|||||||
(yyval.token_decl) = (yyvsp[-2].id);
|
(yyval.token_decl) = (yyvsp[-2].id);
|
||||||
symbol_class_set ((yyvsp[-2].id), current_class, (yylsp[-2]), true);
|
symbol_class_set ((yyvsp[-2].id), current_class, (yylsp[-2]), true);
|
||||||
if (0 <= (yyvsp[-1].yykind_82))
|
if (0 <= (yyvsp[-1].yykind_82))
|
||||||
symbol_user_token_number_set ((yyvsp[-2].id), (yyvsp[-1].yykind_82), (yylsp[-1]));
|
symbol_code_set ((yyvsp[-2].id), (yyvsp[-1].yykind_82), (yylsp[-1]));
|
||||||
if ((yyvsp[0].alias))
|
if ((yyvsp[0].alias))
|
||||||
symbol_make_alias ((yyvsp[-2].id), (yyvsp[0].alias), (yylsp[0]));
|
symbol_make_alias ((yyvsp[-2].id), (yyvsp[0].alias), (yylsp[0]));
|
||||||
}
|
}
|
||||||
@@ -2424,7 +2424,7 @@ yyreduce:
|
|||||||
(yyval.token_decl_for_prec) = (yyvsp[-1].id);
|
(yyval.token_decl_for_prec) = (yyvsp[-1].id);
|
||||||
symbol_class_set ((yyvsp[-1].id), token_sym, (yylsp[-1]), false);
|
symbol_class_set ((yyvsp[-1].id), token_sym, (yylsp[-1]), false);
|
||||||
if (0 <= (yyvsp[0].yykind_82))
|
if (0 <= (yyvsp[0].yykind_82))
|
||||||
symbol_user_token_number_set ((yyvsp[-1].id), (yyvsp[0].yykind_82), (yylsp[0]));
|
symbol_code_set ((yyvsp[-1].id), (yyvsp[0].yykind_82), (yylsp[0]));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -2573,7 +2573,7 @@ yyreduce:
|
|||||||
}
|
}
|
||||||
(yyval.id) = symbol_get (char_name ((yyvsp[0].CHAR)), (yylsp[0]));
|
(yyval.id) = symbol_get (char_name ((yyvsp[0].CHAR)), (yylsp[0]));
|
||||||
symbol_class_set ((yyval.id), token_sym, (yylsp[0]), false);
|
symbol_class_set ((yyval.id), token_sym, (yylsp[0]), false);
|
||||||
symbol_user_token_number_set ((yyval.id), (yyvsp[0].CHAR), (yylsp[0]));
|
symbol_code_set ((yyval.id), (yyvsp[0].CHAR), (yylsp[0]));
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -531,7 +531,7 @@ token_decl:
|
|||||||
$$ = $id;
|
$$ = $id;
|
||||||
symbol_class_set ($id, current_class, @id, true);
|
symbol_class_set ($id, current_class, @id, true);
|
||||||
if (0 <= $num)
|
if (0 <= $num)
|
||||||
symbol_user_token_number_set ($id, $num, @num);
|
symbol_code_set ($id, $num, @num);
|
||||||
if ($alias)
|
if ($alias)
|
||||||
symbol_make_alias ($id, $alias, @alias);
|
symbol_make_alias ($id, $alias, @alias);
|
||||||
}
|
}
|
||||||
@@ -593,7 +593,7 @@ token_decl_for_prec:
|
|||||||
$$ = $id;
|
$$ = $id;
|
||||||
symbol_class_set ($id, token_sym, @id, false);
|
symbol_class_set ($id, token_sym, @id, false);
|
||||||
if (0 <= $num)
|
if (0 <= $num)
|
||||||
symbol_user_token_number_set ($id, $num, @num);
|
symbol_code_set ($id, $num, @num);
|
||||||
}
|
}
|
||||||
| string_as_id
|
| string_as_id
|
||||||
;
|
;
|
||||||
@@ -763,7 +763,7 @@ id:
|
|||||||
}
|
}
|
||||||
$$ = symbol_get (char_name ($1), @1);
|
$$ = symbol_get (char_name ($1), @1);
|
||||||
symbol_class_set ($$, token_sym, @1, false);
|
symbol_class_set ($$, token_sym, @1, false);
|
||||||
symbol_user_token_number_set ($$, $1, @1);
|
symbol_code_set ($$, $1, @1);
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|||||||
@@ -379,7 +379,7 @@ print_grammar (FILE *out, int level)
|
|||||||
|
|
||||||
/* Terminals */
|
/* Terminals */
|
||||||
xml_puts (out, level + 1, "<terminals>");
|
xml_puts (out, level + 1, "<terminals>");
|
||||||
for (symbol_number i = 0; i < max_user_token_number + 1; i++)
|
for (int i = 0; i < max_code + 1; i++)
|
||||||
if (token_translations[i] != undeftoken->content->number)
|
if (token_translations[i] != undeftoken->content->number)
|
||||||
{
|
{
|
||||||
char const *tag = symbols[token_translations[i]]->tag;
|
char const *tag = symbols[token_translations[i]]->tag;
|
||||||
|
|||||||
@@ -362,7 +362,7 @@ print_terminal_symbols (FILE *out)
|
|||||||
{
|
{
|
||||||
/* TERMINAL (type #) : rule #s terminal is on RHS */
|
/* TERMINAL (type #) : rule #s terminal is on RHS */
|
||||||
fprintf (out, "%s\n\n", _("Terminals, with rules where they appear"));
|
fprintf (out, "%s\n\n", _("Terminals, with rules where they appear"));
|
||||||
for (symbol_number i = 0; i < max_user_token_number + 1; i++)
|
for (int i = 0; i < max_code + 1; ++i)
|
||||||
if (token_translations[i] != undeftoken->content->number)
|
if (token_translations[i] != undeftoken->content->number)
|
||||||
{
|
{
|
||||||
const char *tag = symbols[token_translations[i]]->tag;
|
const char *tag = symbols[token_translations[i]]->tag;
|
||||||
@@ -445,7 +445,7 @@ print_results (void)
|
|||||||
reduce_output (out);
|
reduce_output (out);
|
||||||
grammar_rules_partial_print (out,
|
grammar_rules_partial_print (out,
|
||||||
_("Rules useless in parser due to conflicts"),
|
_("Rules useless in parser due to conflicts"),
|
||||||
rule_useless_in_parser_p);
|
rule_useless_in_parser_p);
|
||||||
conflicts_output (out);
|
conflicts_output (out);
|
||||||
|
|
||||||
grammar_rules_print (out);
|
grammar_rules_print (out);
|
||||||
|
|||||||
@@ -634,7 +634,7 @@ packgram (void)
|
|||||||
if (lhs != grammar)
|
if (lhs != grammar)
|
||||||
grammar_rule_check_and_complete (lhs);
|
grammar_rule_check_and_complete (lhs);
|
||||||
|
|
||||||
rules[ruleno].user_number = ruleno;
|
rules[ruleno].code = ruleno;
|
||||||
rules[ruleno].number = ruleno;
|
rules[ruleno].number = ruleno;
|
||||||
rules[ruleno].lhs = lhs->content.sym->content;
|
rules[ruleno].lhs = lhs->content.sym->content;
|
||||||
rules[ruleno].rhs = ritem + itemno;
|
rules[ruleno].rhs = ritem + itemno;
|
||||||
@@ -782,7 +782,7 @@ check_and_convert_grammar (void)
|
|||||||
endtoken->content->class = token_sym;
|
endtoken->content->class = token_sym;
|
||||||
endtoken->content->number = 0;
|
endtoken->content->number = 0;
|
||||||
/* Value specified by POSIX. */
|
/* Value specified by POSIX. */
|
||||||
endtoken->content->user_token_number = 0;
|
endtoken->content->code = 0;
|
||||||
{
|
{
|
||||||
symbol *alias = symbol_get ("$end", empty_loc);
|
symbol *alias = symbol_get ("$end", empty_loc);
|
||||||
symbol_class_set (alias, token_sym, empty_loc, false);
|
symbol_class_set (alias, token_sym, empty_loc, false);
|
||||||
|
|||||||
75
src/symtab.c
75
src/symtab.c
@@ -33,6 +33,13 @@
|
|||||||
#include "gram.h"
|
#include "gram.h"
|
||||||
#include "intprops.h"
|
#include "intprops.h"
|
||||||
|
|
||||||
|
/** Undefined token code. */
|
||||||
|
#define CODE_UNDEFINED (-1)
|
||||||
|
|
||||||
|
/* Undefined symbol number. */
|
||||||
|
#define NUMBER_UNDEFINED (-1)
|
||||||
|
|
||||||
|
|
||||||
static struct hash_table *symbol_table = NULL;
|
static struct hash_table *symbol_table = NULL;
|
||||||
static struct hash_table *semantic_type_table = NULL;
|
static struct hash_table *semantic_type_table = NULL;
|
||||||
|
|
||||||
@@ -99,7 +106,7 @@ sym_content_new (symbol *s)
|
|||||||
res->prec_loc = empty_loc;
|
res->prec_loc = empty_loc;
|
||||||
res->prec = 0;
|
res->prec = 0;
|
||||||
res->assoc = undef_assoc;
|
res->assoc = undef_assoc;
|
||||||
res->user_token_number = USER_NUMBER_UNDEFINED;
|
res->code = CODE_UNDEFINED;
|
||||||
|
|
||||||
res->class = unknown_sym;
|
res->class = unknown_sym;
|
||||||
res->status = undeclared;
|
res->status = undeclared;
|
||||||
@@ -571,24 +578,24 @@ symbol_class_set (symbol *sym, symbol_class class, location loc, bool declaring)
|
|||||||
`------------------------------------------------*/
|
`------------------------------------------------*/
|
||||||
|
|
||||||
void
|
void
|
||||||
symbol_user_token_number_set (symbol *sym, int user_token_number, location loc)
|
symbol_code_set (symbol *sym, int code, location loc)
|
||||||
{
|
{
|
||||||
int *user_token_numberp = &sym->content->user_token_number;
|
int *codep = &sym->content->code;
|
||||||
if (sym->content->class != token_sym)
|
if (sym->content->class != token_sym)
|
||||||
complain (&loc, complaint,
|
complain (&loc, complaint,
|
||||||
_("nonterminals cannot be given a token code"));
|
_("nonterminals cannot be given a token code"));
|
||||||
else if (*user_token_numberp != USER_NUMBER_UNDEFINED
|
else if (*codep != CODE_UNDEFINED
|
||||||
&& *user_token_numberp != user_token_number)
|
&& *codep != code)
|
||||||
complain (&loc, complaint, _("redefining code of token %s"),
|
complain (&loc, complaint, _("redefining code of token %s"),
|
||||||
sym->tag);
|
sym->tag);
|
||||||
else if (user_token_number == INT_MAX)
|
else if (code == INT_MAX)
|
||||||
complain (&loc, complaint, _("code of token %s too large"),
|
complain (&loc, complaint, _("code of token %s too large"),
|
||||||
sym->tag);
|
sym->tag);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
*user_token_numberp = user_token_number;
|
*codep = code;
|
||||||
/* User defined $end token? */
|
/* User defined $end token? */
|
||||||
if (user_token_number == 0 && !endtoken)
|
if (code == 0 && !endtoken)
|
||||||
{
|
{
|
||||||
endtoken = sym->content->symbol;
|
endtoken = sym->content->symbol;
|
||||||
/* It is always mapped to 0, so it was already counted in
|
/* It is always mapped to 0, so it was already counted in
|
||||||
@@ -735,7 +742,7 @@ symbol_pack (symbol *sym)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
complain_user_token_number_redeclared (int num, symbol *first, symbol *second)
|
complain_code_redeclared (int num, symbol *first, symbol *second)
|
||||||
{
|
{
|
||||||
symbols_sort (&first, &second);
|
symbols_sort (&first, &second);
|
||||||
complain (&second->location, complaint,
|
complain (&second->location, complaint,
|
||||||
@@ -758,13 +765,13 @@ symbol_translation (symbol *sym)
|
|||||||
&& !sym->is_alias)
|
&& !sym->is_alias)
|
||||||
{
|
{
|
||||||
/* A token which translation has already been set?*/
|
/* A token which translation has already been set?*/
|
||||||
if (token_translations[sym->content->user_token_number]
|
if (token_translations[sym->content->code]
|
||||||
!= undeftoken->content->number)
|
!= undeftoken->content->number)
|
||||||
complain_user_token_number_redeclared
|
complain_code_redeclared
|
||||||
(sym->content->user_token_number,
|
(sym->content->code,
|
||||||
symbols[token_translations[sym->content->user_token_number]], sym);
|
symbols[token_translations[sym->content->code]], sym);
|
||||||
else
|
else
|
||||||
token_translations[sym->content->user_token_number]
|
token_translations[sym->content->code]
|
||||||
= sym->content->number;
|
= sym->content->number;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1040,53 +1047,53 @@ symbols_check_defined (void)
|
|||||||
static void
|
static void
|
||||||
symbols_token_translations_init (void)
|
symbols_token_translations_init (void)
|
||||||
{
|
{
|
||||||
bool num_256_available_p = true;
|
bool code_256_available_p = true;
|
||||||
|
|
||||||
/* Find the highest token code, and whether 256, the POSIX preferred
|
/* Find the highest token code, and whether 256, the POSIX preferred
|
||||||
token code for the error token, is used. */
|
token code for the error token, is used. */
|
||||||
max_user_token_number = 0;
|
max_code = 0;
|
||||||
for (int i = 0; i < ntokens; ++i)
|
for (int i = 0; i < ntokens; ++i)
|
||||||
{
|
{
|
||||||
sym_content *sym = symbols[i]->content;
|
sym_content *sym = symbols[i]->content;
|
||||||
if (sym->user_token_number != USER_NUMBER_UNDEFINED)
|
if (sym->code != CODE_UNDEFINED)
|
||||||
{
|
{
|
||||||
if (sym->user_token_number > max_user_token_number)
|
if (sym->code > max_code)
|
||||||
max_user_token_number = sym->user_token_number;
|
max_code = sym->code;
|
||||||
if (sym->user_token_number == 256)
|
if (sym->code == 256)
|
||||||
num_256_available_p = false;
|
code_256_available_p = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* If 256 is not used, assign it to error, to follow POSIX. */
|
/* If 256 is not used, assign it to error, to follow POSIX. */
|
||||||
if (num_256_available_p
|
if (code_256_available_p
|
||||||
&& errtoken->content->user_token_number == USER_NUMBER_UNDEFINED)
|
&& errtoken->content->code == CODE_UNDEFINED)
|
||||||
errtoken->content->user_token_number = 256;
|
errtoken->content->code = 256;
|
||||||
|
|
||||||
/* Set the missing user numbers. */
|
/* Set the missing codes. */
|
||||||
if (max_user_token_number < 256)
|
if (max_code < 256)
|
||||||
max_user_token_number = 256;
|
max_code = 256;
|
||||||
|
|
||||||
for (int i = 0; i < ntokens; ++i)
|
for (int i = 0; i < ntokens; ++i)
|
||||||
{
|
{
|
||||||
sym_content *sym = symbols[i]->content;
|
sym_content *sym = symbols[i]->content;
|
||||||
if (sym->user_token_number == USER_NUMBER_UNDEFINED)
|
if (sym->code == CODE_UNDEFINED)
|
||||||
{
|
{
|
||||||
IGNORE_TYPE_LIMITS_BEGIN
|
IGNORE_TYPE_LIMITS_BEGIN
|
||||||
if (INT_ADD_WRAPV (max_user_token_number, 1, &max_user_token_number))
|
if (INT_ADD_WRAPV (max_code, 1, &max_code))
|
||||||
complain (NULL, fatal, _("token number too large"));
|
complain (NULL, fatal, _("token number too large"));
|
||||||
IGNORE_TYPE_LIMITS_END
|
IGNORE_TYPE_LIMITS_END
|
||||||
sym->user_token_number = max_user_token_number;
|
sym->code = max_code;
|
||||||
}
|
}
|
||||||
if (sym->user_token_number > max_user_token_number)
|
if (sym->code > max_code)
|
||||||
max_user_token_number = sym->user_token_number;
|
max_code = sym->code;
|
||||||
}
|
}
|
||||||
|
|
||||||
token_translations = xnmalloc (max_user_token_number + 1,
|
token_translations = xnmalloc (max_code + 1,
|
||||||
sizeof *token_translations);
|
sizeof *token_translations);
|
||||||
|
|
||||||
/* Initialize all entries for literal tokens to the internal token
|
/* Initialize all entries for literal tokens to the internal token
|
||||||
number for $undefined, which represents all invalid inputs. */
|
number for $undefined, which represents all invalid inputs. */
|
||||||
for (int i = 0; i < max_user_token_number + 1; ++i)
|
for (int i = 0; i < max_code + 1; ++i)
|
||||||
token_translations[i] = undeftoken->content->number;
|
token_translations[i] = undeftoken->content->number;
|
||||||
for (int i = 0; symbols_sorted[i]; ++i)
|
for (int i = 0; symbols_sorted[i]; ++i)
|
||||||
symbol_translation (symbols_sorted[i]);
|
symbol_translation (symbols_sorted[i]);
|
||||||
|
|||||||
16
src/symtab.h
16
src/symtab.h
@@ -149,21 +149,13 @@ struct sym_content
|
|||||||
int prec;
|
int prec;
|
||||||
assoc assoc;
|
assoc assoc;
|
||||||
|
|
||||||
/** The user specified token number.
|
/** Token code, possibly specified by the user (%token FOO 42). */
|
||||||
|
int code;
|
||||||
E.g., %token FOO 42.*/
|
|
||||||
int user_token_number;
|
|
||||||
|
|
||||||
symbol_class class;
|
symbol_class class;
|
||||||
declaration_status status;
|
declaration_status status;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Undefined user number. */
|
|
||||||
# define USER_NUMBER_UNDEFINED -1
|
|
||||||
|
|
||||||
/* Undefined internal token number. */
|
|
||||||
# define NUMBER_UNDEFINED (-1)
|
|
||||||
|
|
||||||
/** Fetch (or create) the symbol associated to KEY. */
|
/** Fetch (or create) the symbol associated to KEY. */
|
||||||
symbol *symbol_from_uniqstr (const uniqstr key, location loc);
|
symbol *symbol_from_uniqstr (const uniqstr key, location loc);
|
||||||
|
|
||||||
@@ -235,8 +227,8 @@ void symbol_precedence_set (symbol *sym, int prec, assoc a, location loc);
|
|||||||
void symbol_class_set (symbol *sym, symbol_class class, location loc,
|
void symbol_class_set (symbol *sym, symbol_class class, location loc,
|
||||||
bool declaring);
|
bool declaring);
|
||||||
|
|
||||||
/** Set the \c user_token_number associated with \c sym. */
|
/** Set the \c code associated with \c sym. */
|
||||||
void symbol_user_token_number_set (symbol *sym, int user_number, location loc);
|
void symbol_code_set (symbol *sym, int code, location loc);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -397,12 +397,12 @@ m4@&t@_define([b4_symbol_dump],
|
|||||||
b4_symbol_if([$1], [is_token], [Token], [Nonterminal]), d@&t@nl
|
b4_symbol_if([$1], [is_token], [Token], [Nonterminal]), d@&t@nl
|
||||||
b4_symbol([$1], [tag]), d@&t@nl
|
b4_symbol([$1], [tag]), d@&t@nl
|
||||||
b4_symbol([$1], [id]), d@&t@nl
|
b4_symbol([$1], [id]), d@&t@nl
|
||||||
b4_symbol([$1], [user_number]), d@&t@nl
|
b4_symbol([$1], [code]), d@&t@nl
|
||||||
b4_symbol([$1], [type]),
|
b4_symbol([$1], [type]),
|
||||||
])
|
])
|
||||||
|
|
||||||
b4_output_begin([symbols.csv])
|
b4_output_begin([symbols.csv])
|
||||||
number, class, tag, id, user_number, type,
|
number, class, tag, id, code, type,
|
||||||
b4_symbol_foreach([b4_symbol_dump])d@&t@nl
|
b4_symbol_foreach([b4_symbol_dump])d@&t@nl
|
||||||
b4_output_end
|
b4_output_end
|
||||||
]])
|
]])
|
||||||
@@ -429,7 +429,7 @@ exp:;
|
|||||||
|
|
||||||
AT_BISON_CHECK([-Wno-other -S./dump-symbols.m4 input.y])
|
AT_BISON_CHECK([-Wno-other -S./dump-symbols.m4 input.y])
|
||||||
AT_CHECK([cat symbols.csv], [],
|
AT_CHECK([cat symbols.csv], [],
|
||||||
[[number, class, tag, id, user_number, type,
|
[[number, class, tag, id, code, type,
|
||||||
0, Token, "end of file", YYEOF, 0, ,
|
0, Token, "end of file", YYEOF, 0, ,
|
||||||
1, Token, error, YYerror, 256, ,
|
1, Token, error, YYerror, 256, ,
|
||||||
2, Token, "invalid token", YYUNDEF, 257, ,
|
2, Token, "invalid token", YYUNDEF, 257, ,
|
||||||
|
|||||||
@@ -235,8 +235,8 @@ m4_foreach([b4_skel], [[yacc.c], [glr.c], [lalr1.cc], [glr.cc]],
|
|||||||
[10 0.2])])
|
[10 0.2])])
|
||||||
|
|
||||||
# A Bison-defined union.
|
# A Bison-defined union.
|
||||||
# The tokens names are not available directly in C++, we use their
|
# The token names are not available directly in C++, we use their
|
||||||
# user number to keep it simple between C and C++.
|
# codes to keep it simple between C and C++.
|
||||||
AT_TEST([%skeleton "]b4_skel["
|
AT_TEST([%skeleton "]b4_skel["
|
||||||
%define api.value.type union],
|
%define api.value.type union],
|
||||||
[%token <int> ONE 101;
|
[%token <int> ONE 101;
|
||||||
|
|||||||
Reference in New Issue
Block a user