1

Is there a way to have Yacc/Bison print out all the reductions it does to the input it processes? This would be a great debugging aid. I already tried

| Item1 { printf("Item1: %s\n", yytext); }

which only prints the last character and

| Item1 { printf("Item1: %s\n", $$); }

which results in a format argument is not a pointer warning from the compiler. Am I doing it wrong?

Pieter
  • 31,619
  • 76
  • 167
  • 242
  • Does this http://stackoverflow.com/questions/6588624/yacc-bison-the-pseudo-variables-1-2-and-how-to-print-them-using-pri help? – JohnB Oct 27 '12 at 08:35
  • Yikes, that'd involve lots of manual work. This is my first time building a grammar with Bison so I'm changing rules drastically all the time. If the `printf` thing with `$$` doesn't work, are there special `bison` parameters or something along those lines? I searched through the manual but no results so far. – Pieter Oct 27 '12 at 08:40
  • `$$` seems wrong to me in any case, since it is meant to carry the return value. You probably have to remember the position in the input stream when you enter the production rule, check the position in the input stream when you leave it, and print everything in between, if you do not want to "assemble" the matched string bottom up by passing it via $$ and reading it via $n as suggested in the link. – JohnB Oct 27 '12 at 08:49
  • What about [`#define YYDEBUG 1`](http://publib.boulder.ibm.com/infocenter/zvm/v5r4/index.jsp?topic=/com.ibm.zvm.v54.dmsp4/hcsp4b10121.htm)? It looks perfect on paper but I'm not seeing any debug output with the macro defined. – Pieter Oct 27 '12 at 12:52

1 Answers1

2

I was able to enable debug output by

  1. Putting #define YYDEBUG 1 in my C declarations

  2. Putting int yydebug = 1; in the additional C code section

For additional reading, check Using YYDEBUG to generate debugging information, Debugging Your Parser and this yacc example.

Pieter
  • 31,619
  • 76
  • 167
  • 242