Using the lex program with the yacc program

You can also use the lex program with a parser generator, such as the yacc command. The yacc command generates a program, called a parser, that analyzes the construction of more than one-word input.

This parser program operates well with the lexical analyzers that the lex command generates. The parsers recognize many types of grammar with no regard to context. These parsers need a preprocessor to recognize input tokens such as the preprocessor that the lex command produces.

The lex program recognizes only extended regular expressions and formats them into character packages called tokens, as specified by the input file. When using the lex program to make a lexical analyzer for a parser, the lexical analyzer (created from the lex command) partitions the input stream. The parser (from the yacc command) assigns structure to the resulting pieces. You can also use other programs along with the programs generated by either the lex or yacc commands.

A token is the smallest independent unit of meaning as defined by either the parser or the lexical analyzer. A token can contain data, a language keyword, an identifier, or other parts of a language syntax.

The yacc program looks for a lexical analyzer subroutine named yylex, which is generated by the lex command. Normally, the default main program in the lex library calls the yylex subroutine. However, if the yacc command is installed and its main program is used, the yacc program calls the yylex subroutine. In this case, where the appropriate token value is returned, each lex program rule should end with the following:
return(token);
The yacc command assigns an integer value to each token defined in the yacc grammar file through a #define preprocessor statement. The lexical analyzer must have access to these macros to return the tokens to the parser. Use the yacc -d option to create a y.tab.h file, and include the y.tab.h file in the lex specification file by adding the following lines to the definition section of the lex specification file:
%{
#include "y.tab.h"
%}
Alternately, you can include the lex.yy.c file in the yacc output file by adding the following line after the second %% (percent sign, percent sign) delimiter in the yacc grammar file:
#include "lex.yy.c"

The yacc library should be loaded before the lex library to obtain a main program that invokes the yacc parser. You can generate lex and yacc programs in either order.