A little manual for Lex and Yacc

Etienne Bernard (eb at pltplp dot net)

Version française

XML version, March 24th, 2000

Lex and Yacc are very useful tools for people who need to manipulate a grammar. If you are writing a compiler or an interpreter, you will have to manipulate a grammar, but this is not restrictive. Every people writing programs that have to react to certain messages, following a particular protocol (e.g. FTP or HTTP) may find Lex and Yacc useful. This little manual is a presentation of these tools, using a "learn by practice" method. Please note that English is not my native language, so I hope you will excuse my mistakes.

1. The grammar used

2. Use of Lex in syntaxical analysis

2.1. First part of a Lex file: declarations
2.2. Regular expressions
2.3. Second part of a Lex file: productions
2.4. Third part: additional code
2.5. Conclusion about Lex

3. Syntaxical analysis with Yacc

3.1. The first part of a Yacc file
3.2. Second part of a Yacc file
3.3. Third part of a Yacc file
3.4. Conclusion about Yacc

4. An example: a little expression interpreter

4.1. The Lex part of the interpreter
4.2. The Yacc part of the interpreter
4.3. Compiling and running the example
4.4. A better calculator

5. Conclusion