if (!tk)
tk = tok_copy(token_next(tokens));
next.sym = tk->num;
- if (trace)
- parser_trace(trace, &p, &next, tk, states, non_term, config->known_count);
+ parser_trace(trace, &p, &next, tk, states, non_term, config->known_count);
if (next.sym == TK_in) {
next.starts_indented = 1;
next.indents = 1;
free(tk);
tk = NULL;
+ parser_trace_action(trace, "Record");
continue;
}
if (next.sym == TK_out) {
}
free(tk);
tk = NULL;
+ parser_trace_action(trace, "Cancel");
continue;
}
// fall through and force a REDUCE (as 'shift'
if (! tos->newline_permitted) {
free(tk);
tk = NULL;
+ parser_trace_action(trace, "Discard");
continue;
}
}
tk = NULL;
next.starts_indented = 0;
next.indents = 0;
+ parser_trace_action(trace, "Shift");
continue;
}
if (states[tos->state].reduce_prod >= 0) {
accepted = 1;
ret = res;
}
+ parser_trace_action(trace, "Reduce");
continue;
}
if (tk->num == TK_out) {
frame.sym = states[tos->state].shift_sym;
shift(&p, &frame, tok_copy(*tk), states);
// FIXME need to report this error somehow
+ parser_trace_action(trace, "Synthesize");
continue;
}
/* Error. We walk up the stack until we
* Then we discard input tokens until
* we find one that is acceptable.
*/
+ parser_trace_action(trace, "ERROR");
err_tk = tok_copy(*tk);
next.sym = TK_error;
const char *non_term[], int knowns)
{
int i;
+ if (!trace)
+ return;
for (i = 0; i < p->tos; i++) {
struct frame *f = &p->stack[i];
if (i) {
if (n->indents)
fprintf(trace, "%c%d", n->starts_indented?':':'.',
n->indents);
- fputs("]\n", trace);
+ fputs("]", trace);
+ }
+
+ void parser_trace_action(FILE *trace, char *action)
+ {
+ if (trace)
+ fprintf(trace, " - %s\n", action);
}
# A Worked Example