From 9ebc82df03b2919afe61ddfd9ae76743663fa6e6 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sun, 24 Nov 2013 18:14:11 +1100 Subject: [PATCH] parsergen: centralise (some of) the collecting of next token. A future patch will introduce next sites where we want to discard the current token. Rather than calling "token_next" at each site, make it possible to just set "tk = NULL", and the next token will automatically be collected when needed. Signed-off-by: NeilBrown --- csrc/parsergen.mdc | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/csrc/parsergen.mdc b/csrc/parsergen.mdc index 252b3d6..eb63507 100644 --- a/csrc/parsergen.mdc +++ b/csrc/parsergen.mdc @@ -2396,18 +2396,20 @@ drop input tokens until we find one we can shift into the new error state. FILE *trace, const char *non_term[], int knowns) { struct parser p = { 0 }; - struct token *tk; + struct token *tk = NULL; int accepted = 0; void *ret; - tk = tok_copy(token_next(tokens)); while (!accepted) { + struct token *err_tk; + if (!tk) + tk = tok_copy(token_next(tokens)); p.next.sym = tk->num; if (trace) parser_trace(trace, &p, tk, states, non_term, knowns); if (shift(&p, tk, states)) { - tk = tok_copy(token_next(tokens)); + tk = NULL; continue; } if (states[p.next.state].reduce_prod >= 0) { @@ -2436,12 +2438,18 @@ drop input tokens until we find one we can shift into the new error state. * Then we discard input tokens until * we find one that is acceptable. */ + + err_tk = tok_copy(*tk); p.next.sym = TK_error; - while (shift(&p, tk, states) == 0 + while (shift(&p, err_tk, states) == 0 && p.tos > 0) // discard this state pop(&p, 1, do_free); - tk = tok_copy(*tk); + if (p.tos == 0) { + free(err_tk); + // no state accepted TK_error + break; + } while (search(&states[p.next.state], tk->num) < 0 && tk->num != TK_eof) { free(tk); -- 2.43.0