From 399f329936d9b11f48257f0f6aa323d65ccc6aaf Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sun, 22 Jun 2014 15:18:32 +1000 Subject: [PATCH] parsergen: improve tracing. 1/ perform the "is null?" test on trace find in parser_trace. code is cleaner that way 2/ Report the action at each step in the parse. Signed-off-by: NeilBrown --- csrc/parsergen.mdc | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/csrc/parsergen.mdc b/csrc/parsergen.mdc index 19703bc..9306592 100644 --- a/csrc/parsergen.mdc +++ b/csrc/parsergen.mdc @@ -2601,14 +2601,14 @@ since the last state which could have been at the start of a line. 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) { @@ -2625,6 +2625,7 @@ since the last state which could have been at the start of a line. } free(tk); tk = NULL; + parser_trace_action(trace, "Cancel"); continue; } // fall through and force a REDUCE (as 'shift' @@ -2634,6 +2635,7 @@ since the last state which could have been at the start of a line. if (! tos->newline_permitted) { free(tk); tk = NULL; + parser_trace_action(trace, "Discard"); continue; } } @@ -2641,6 +2643,7 @@ since the last state which could have been at the start of a line. tk = NULL; next.starts_indented = 0; next.indents = 0; + parser_trace_action(trace, "Shift"); continue; } if (states[tos->state].reduce_prod >= 0) { @@ -2673,6 +2676,7 @@ since the last state which could have been at the start of a line. accepted = 1; ret = res; } + parser_trace_action(trace, "Reduce"); continue; } if (tk->num == TK_out) { @@ -2683,6 +2687,7 @@ since the last state which could have been at the start of a line. 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 @@ -2692,6 +2697,7 @@ since the last state which could have been at the start of a line. * 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; @@ -2777,6 +2783,8 @@ end inside square brackets. 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) { @@ -2806,7 +2814,13 @@ end inside square brackets. 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 -- 2.43.0