Home > Mobile >  How to generate state and action table in YACC/Bison?
How to generate state and action table in YACC/Bison?

Time:11-25

I am clueless about how I can print a LALR(1) parsing table from the yacc program. I know that behind the scene, yacc converts the grammar into parsing table, with other extra C code.

I would like to print the states, as well as the actions (shift, reduce and accept) on the console, similar to this image. I've come up with this. The program prints Valid, if it follows the grammar rule, else Invalid declaration.

CodePudding user response:

I'm not sure what you mean by "parsing table".

As you say in your first paragraph, a parsing table drives the parser; it lists the possible transitions from each parser state. Bison will print the entire state table in a file with prefix .output if you use the commandline option --report=states,itemsets. You can get it as a Graphviz file as well, although that's only useful for small grammars, using the --graph option.

However, the image you link in the second paragraph does not show a parsing table. What it shows is the progress of the parser with a specific input. This is usually called a "trace" and bison can produce that as well. You need to do two things:

  1. Provide the -t command-line option (which you can also write out as --debug), which causes tracing code to be generated.

  2. Set the global variable yydebug to 1 in your main() function. Please note that yydebug won't exist unless you followed step 1.

  • Related