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:
- Specifications written in the target language, enclosed between %{ and %} (each symbol at the begining of a line) that will be put at the top of the scanner generated by Yacc.
- Declaration of the tokens that can be encontered, with the %token keyword.
- The type of the terminal, using the reserved word: %union
- Informations about operators' priority or associativity.
- The axiom of the grammar, using the reserved word %start (if not specified, the axiom is the first production of the second part of the file).
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:
- ddeclarations and/or definitions enclosed between %{ and %}
- Productions of the language's grammar.
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.