Lex and yacc are tools designed for writers of compilers and interpreters, although they are also useful for many applications that will interest the noncompiler writer. Any application that looks for patterns in its input, or has an input or command language is a good candidate for lex and yacc. Furthermore, they allow for rapid application prototyping, easy modification, and simple maintenance of programs. To stimulate your imagination, here are a few things people have used lex and yacc to develop:
Scope of This Book
, gives an overview of how and why lex and yacc are used to create compilers and interpreters, and demonstrates some small lex and yacc applications. It also introduces basic terms we use throughout the book.
, describes how to use lex. It develops lex applications that count words in files, analyze program command switches and arguments, and compute statistics on C programs.
, gives a full example using lex and yacc to develop a fully functional desktop calculator.
, demonstrates how to use lex and yacc to develop a menu generator.
, develops a parser for the full SQL relational data base language. First we use the parser as a syntax checker, then extend it into a simple preprocessor for SQL embedded in C programs.
, provide detailed descriptions of the features and options available to the lex and yacc programmer. These chapters and the two that follow provide technical information for the now experienced lex and yacc programmer to use while developing new lex and yacc applications.
, explains yacc ambiguities and conflicts, which are problems that keep yacc from parsing a grammar correctly. It then develops methods that can be used to locate and correct such problems.
, discusses techniques that the compiler or interpreter designer can use to locate, recognize, and report errors in the compiler input.
, describes the command-line syntax of AT&T lex and the error messages it reports and suggests possible solutions.
, describes the command-line syntax of AT&T yacc and lists errors reported by yacc. It provides examples of code which can cause such errors and suggests possible solutions.
, describes the command-line syntax of Berkeley yacc, a widely used free version of yacc distributed with Berkeley UNIX, and lists errors reported by Berkeley yacc with suggested solutions.
, discusses differences found in bison, the Free Software Foundations implementation of yacc.
, discusses flex, a widely used free version of lex, lists differences from other versions, and lists errors reported by flex with suggested solutions.
, discusses the MS-DOS and OS/2 version of lex and yacc from Mortice Kern Systems.
, discusses PCYACC, the MS-DOS and OS/2 versions of lex and yacc from Abraxas Software.
, discusses the versions of lex and yacc defined by the IEEE POSIX 1003.2 standard.
.
.
The Glossary lists technical terms language and compiler theory.
The Bibliography lists other documentation on lex and yacc, as well as helpful books on compiler design.
We presume the reader is familiar with C, as most examples are in C, lex, or yacc, with the remainder being in the special purpose languages developed within the text.
Availability of Lex and Yacc
Lex and yacc were both developed at Bell Laboratories in the 1970s. Yacc was the first of the two, developed by Stephen C. Johnson. Lex was designed by Mike Lesk and Eric Schmidt to work with yacc. Both lex and yacc have been standard UNIX utilities since 7th Edition UNIX. System V and older versions of BSD use the original AT&T versions, while the newest version of BSD uses flex (see below) and Berkeley yacc. The articles written by the developers remain the primary source of information on lex and yacc.
The GNU Project of the Free Software Foundation distributes bison , a yacc replacement; bison was written by Robert Corbett and Richard Stallman. The bison manual, written by Charles Donnelly and Richard Stallman, is excellent, especially for referencing specific features. discusses bison.
BSD and GNU Project also distribute flex ( F ast Lex ical Analyzer Generator), a rewrite of lex intended to right some of that tools deficiencies, according to its reference page. Flex was originally written by Jef Poskanzer; Vern Paxson and Van Jacobson have considerably improved it and Vern currently maintains it. covers topics specific to flex.
There are at least two versions of lex and yacc available for MS-DOS and OS/2 machines. MKS (Mortice Kern Systems Inc.), publishers of the MKS Toolkit, offers lex and yacc as a separate product that supports many PC C compilers. MKS lex and yacc comes with a very good manual. covers Abraxas version lex and yacc.
Sample Programs
The programs in this book are available free from UUNET (that is, free except for UUNETs usual connect-time charges). If you have access to UUNET, you can retrieve the source code using UUCP or FTP. For UUCP, find a machine with direct access to UUNET, and type the following command: