Next: , Previous: Locations, Up: Concepts


1.7 Bison Output: the Parser Implementation File

When you run Bison, you give it a Bison grammar file as input. The most important output is a C source file that implements a parser for the language described by the grammar. This parser is called a Bison parser, and this file is called a Bison parser implementation file. Keep in mind that the Bison utility and the Bison parser are two distinct programs: the Bison utility is a program whose output is the Bison parser implementation file that becomes part of your program.

The job of the Bison parser is to group tokens into groupings according to the grammar rules—for example, to build identifiers and operators into expressions. As it does this, it runs the actions for the grammar rules it uses.

The tokens come from a function called the lexical analyzer that you must supply in some fashion (such as by writing it in C). The Bison parser calls the lexical analyzer each time it wants a new token. It doesn't know what is “inside” the tokens (though their semantic values may reflect this). Typically the lexical analyzer makes the tokens by parsing characters of text, but Bison does not depend on this. See The Lexical Analyzer Function yylex.

The Bison parser implementation file is C code which defines a function named yyparse which implements that grammar. This function does not make a complete C program: you must supply some additional functions. One is the lexical analyzer. Another is an error-reporting function which the parser calls to report an error. In addition, a complete C program must start with a function called main; you have to provide this, and arrange for it to call yyparse or the parser will never run. See Parser C-Language Interface.

Aside from the token type names and the symbols in the actions you write, all symbols defined in the Bison parser implementation file itself begin with ‘yy’ or ‘YY’. This includes interface functions such as the lexical analyzer function yylex, the error reporting function yyerror and the parser function yyparse itself. This also includes numerous identifiers used for internal purposes. Therefore, you should avoid using C identifiers starting with ‘yy’ or ‘YY’ in the Bison grammar file except for the ones defined in this manual. Also, you should avoid using the C identifiers ‘malloc’ and ‘free’ for anything other than their usual meanings.

In some cases the Bison parser implementation file includes system headers, and in those cases your code should respect the identifiers reserved by those headers. On some non-GNU hosts, <alloca.h>, <malloc.h>, <stddef.h>, and <stdlib.h> are included as needed to declare memory allocators and related types. <libintl.h> is included if message translation is in use (see Internationalization). Other system headers may be included if you define YYDEBUG to a nonzero value (see Tracing Your Parser).