Before you start
UNIX® programmers often find that they need to understand text and other structures with a flexible, but standardized format. By using the lex and yacc tools, you can build a parsing engine that processes text according to specific rules. You can then incorporate it into your applications for everything from configuration parsing right up to building your own programming language. By the end of this tutorial, you'll understand how to define lexical elements, write yacc rules, and use the rule mechanism to build and define a range of different parsing engines and applications.
There are many ways to understand and extract text in UNIX. You can use grep, awk, Perl, and other solutions. But sometimes you want to understand and extract data in a structured, but unrestricted format. This is where the UNIX lex and yacc tools are useful. The previous tools, awk, Perl, along with the shell and many other programming languages, use lex and yacc to generate parsing applications to parse and understand text and translate it into the information, or data structures, that you need.
Lex is a lexical analysis tool that can be used to identify specific text strings in a structured way from source text. Yacc is a grammar parser; it reads text and can be used to turn a sequence of words into a structured format for processing.
In this tutorial, you'll examine how to use lex and yacc, first to build a calculator. Using the calculator as an example, you'll further examine the output and information generated by the lex and yacc system and study how to use it to parse other types of information.
To use the examples in this tutorial, you will need to have access to the following tools:
- Lex: This tool is a standard component on most UNIX operating systems. The GNU flex tool provides the same functionality.
- Yacc: This tool is standard on most UNIX operating systems. The GNU bison tool provides the same functionality.
- C compiler: Any standard C compiler, including Gnu CC, will be fine.
- Make tool: This tool is required to use the sample Makefile to simplify building.
GNU tools can be downloaded from the GNU Web site, or your local GNU mirror.