3. Syntaxical analysis with Yacc

Yacc (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar and to produce the source code of the syntaxical analyser of the language produced by this grammar. It is also possible to make it do semantic actions.

As for a Lex file, a Yacc file can be divided into three parts:

declarations
%%
productions
%%
additionnal code

and only the first %% and the second part are mandatory

3.1. The first part of a Yacc file

The first part of a Yacc file may contain:

The yylval variable, implicitely declared of the %union type is really important in the file, since it is the variable that contains the description of the last token read.

3.2. Second part of a Yacc file

This part can not be empty. It may contain:

These productions look like:

nonterminal_notion:
                    body_1       { semantical_action_1 }
                  | body_2       { semantical_action_2 }
                  | ...
                  | body_n       { semantical_action_n }
                  ;

provided that the body_i may be terminal or nonterminal notions of the language.

And finally...

3.3. Third part of a Yacc file

This part contains the additional code, must contain a main() function (that should call the yyparse() function), and an yyerror(char *message) function, that is called when a syntax error is found.

3.4. Conclusion about Yacc

This presentation is far from being exhaustive, and I didn't explain you everything. We will clarify some points in the following example.