10.3. Running Bison

Command

  bison -y -v -d parser.y
runs Bison on file parser.y. Option -y tells it to do things the same way that Yacc does, so that what you read in the Dragon Book will be correct.

When run as it is above, Bison writes three files.

  1. File y.tab.c contains the parsing tables and a definition of function yyparse, with heading

      void yyparse(void)
    
    Calling yyparse will run the parser. It assumes that the lexer is called yylex and that yylex puts token attributes into variable yylval, of type YYSTYPE.

  2. Option -d asks Bison to write a file y.tab.h that contains definitions of tokens.

    For example, if you have a token called TOK_ARROW, then Bison chooses a number for that token and defines TOK_ARROW in y.tab.h.

    You will want to make your lexer include y.tab.h so that it gets the correct numbers for tokens. You no longer need token.h.

  3. Option -v asks Bison to write file y.output. It contains the information about the LR(0) finite state machine that Bison created, and shows where conflicts are, if there are any.


Debug mode for yyparse

You can ask yyparse to show you what it is doing as follows.

  1. In the section %{ … %} at the beginning of the parser file, write

    #define YYDEBUG 1
    
    to get debugging code to be compiled into yyparse.

  2. Set global variable yydebug to 1 to turn on debugging. You will need to include line

      extern int yydebug;
    
    to set yydebug in a module other than the parser module.

Bison will write a trace to the standard output. It might take a bit of work to figure out exactly what it is saying, but remember, from the point of view of a bottom-up parser, productions are used backwards, and that is how Bison shows them. Ignore the $ signs.