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) {
* 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);