[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 15/17] multistart: allow tokens as start symbols
From: |
Akim Demaille |
Subject: |
[PATCH 15/17] multistart: allow tokens as start symbols |
Date: |
Sun, 20 Sep 2020 10:37:47 +0200 |
After all, why not?
* src/reader.c (switching_token): Use symbol_id_get.
(check_start_symbols): Require that the start symbol is a token only
if it's the only one.
* examples/c/lexcalc/parse.y: Let NUM be a start symbol.
---
examples/c/lexcalc/parse.y | 2 +-
src/reader.c | 11 ++++++-----
src/symtab.c | 11 ++++++++---
3 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/examples/c/lexcalc/parse.y b/examples/c/lexcalc/parse.y
index b3aaf476..59708115 100644
--- a/examples/c/lexcalc/parse.y
+++ b/examples/c/lexcalc/parse.y
@@ -79,7 +79,7 @@
%type <int> exp expression line
%printer { fprintf (yyo, "%d", $$); } <int>
-%start input expression
+%start input expression NUM
// Precedence (from lowest to highest) and associativity.
%left "+" "-"
diff --git a/src/reader.c b/src/reader.c
index 1daccc22..4b6575d4 100644
--- a/src/reader.c
+++ b/src/reader.c
@@ -322,7 +322,8 @@ grammar_rule_check_and_complete (symbol_list *r)
const symbol *start = r->next->next->content.sym;
if (start->content->type_name)
obstack_printf (obstack_for_actions,
- "{ ]b4_accept([orig %d])[; }",
+ "{ ]b4_accept([%s%d])[; }",
+ start->content->class == nterm_sym ? "orig " : "",
start->content->number);
else
obstack_printf (obstack_for_actions,
@@ -828,9 +829,7 @@ switching_token (const symbol *start)
{
char buf[100];
size_t len = sizeof buf;
- char *name
- = asnprintf (buf, &len,
- "YY_PARSE_%s", start->alias ? start->alias->tag : start->tag);
+ char *name = asnprintf (buf, &len, "YY_PARSE_%s", symbol_id_get (start));
if (!name)
xalloc_die ();
// Setting the location ensures deterministic symbol numbers.
@@ -871,6 +870,7 @@ create_start_rules (void)
static void
check_start_symbols (void)
{
+ const bool multistart = start_symbols && start_symbols->next;
// Sanity checks on the start symbols.
for (symbol_list *list = start_symbols; list; list = list->next)
{
@@ -885,7 +885,8 @@ check_start_symbols (void)
// defined as a token and has no rules".
abort ();
}
- if (start->content->class == token_sym)
+ // If your only start symbol is a token, you're weird.
+ if (!multistart && start->content->class == token_sym)
complain (&start->location, complaint,
_("the start symbol %s is a token"),
start->tag);
diff --git a/src/symtab.c b/src/symtab.c
index 31a3c048..d0ec50a9 100644
--- a/src/symtab.c
+++ b/src/symtab.c
@@ -305,9 +305,14 @@ is_identifier (uniqstr s)
uniqstr
symbol_id_get (symbol const *sym)
{
- if (sym->alias)
- sym = sym->alias;
- return is_identifier (sym->tag) ? sym->tag : 0;
+ // There's one weird case: YYerror is the alias, and error is the
+ // base symbol. Return YYerror in that case.
+ if (sym->alias && is_identifier (sym->alias->tag))
+ return sym->alias->tag;
+ else if (is_identifier (sym->tag))
+ return sym->tag;
+ else
+ return NULL;
}
--
2.28.0
- [PATCH 05/17] multistart: turn start symbols into rules on $accept, (continued)
- [PATCH 05/17] multistart: turn start symbols into rules on $accept, Akim Demaille, 2020/09/20
- [PATCH 06/17] regen, Akim Demaille, 2020/09/20
- [PATCH 07/17] multistart: adjust computation of initial core and adjust reports, Akim Demaille, 2020/09/20
- [PATCH 08/17] multistart: also check the HTML report, Akim Demaille, 2020/09/20
- [PATCH 09/17] multistart: pass the list of start symbols to the backend, Akim Demaille, 2020/09/20
- [PATCH 10/17] multistart: equip yacc.c, Akim Demaille, 2020/09/20
- [PATCH 11/17] multistart: toy with it in lexcalc, Akim Demaille, 2020/09/20
- [PATCH 12/17] todo: more, Akim Demaille, 2020/09/20
- [PATCH 13/17] multistart: adjust reader checks for generated rules, Akim Demaille, 2020/09/20
- [PATCH 14/17] multistart: use b4_accept instead of action post-processing, Akim Demaille, 2020/09/20
- [PATCH 15/17] multistart: allow tokens as start symbols,
Akim Demaille <=
- [PATCH 16/17] yacc.c: also count calls to YYERROR in yynerrs, Akim Demaille, 2020/09/20
- [PATCH 17/17] multistart: also give access to yynerrs, Akim Demaille, 2020/09/20
- Re: [PATCH 00/17] RFC: multiple start symbols, Paul Eggert, 2020/09/20
- Re: [PATCH 00/17] RFC: multiple start symbols, Akim Demaille, 2020/09/23
- Re: [PATCH 00/17] RFC: multiple start symbols, Adrian Vogelsgesang, 2020/09/23
- Re: [PATCH 00/17] RFC: multiple start symbols, Akim Demaille, 2020/09/27
- Re: [PATCH 00/17] RFC: multiple start symbols, Rici Lake, 2020/09/27
- Re: multistart: returning structs, Akim Demaille, 2020/09/29
- Re: multistart: yynerrs, Akim Demaille, 2020/09/29
- Re: multistart: free choice of the start symbol, Akim Demaille, 2020/09/29