This book shows you how to use two Unix utilities, lex and
yacc, in program development. These tools help programmers build compilers and interpreters, but they also have a wider range of applications.
The second edition contains completely revised tutorial sections for novice users and reference sections for advanced users. This edition is twice the size of the first and has an expanded index.
The following material has been added:
Each utility is explained in a chapter that covers basic usage and simple, stand-alone applications
How to implement a full SQL grammar, with full sample code
Major MS-DOS and Unix versions of lex and yacc are explored in depth, including AT&T lex and yacc, Berkeley yacc, Berkeley/GNU Flex, GNU Bison, MKS lex and
yacc, and Abraxas PCYACC
Chapter 1 Lex and Yacc
The Simplest Lex Program
Recognizing Words with Lex
The Parts of Speech Lexer
Running Lex and Yacc
Lex vs. Hand-written Lexers
Chapter 2 Using Lex
A Word Counting Program
Parsing a Command Line
A C Source Code Analyzer
Chapter 3 Using Yacc
A Yacc Parser
Arithmetic Expressions and Ambiguity
Variables and Typed Tokens
Functions and Reserved Words
Building Parsers with Make
Chapter 4 A Menu Generation Language
Overview of the MGL
Developing the MGL
Building the MGL
Sample MGL Code
Chapter 5 Parsing SQL
A Quick Overview of SQL
Three Ways to Use SQL
The Syntax Checker
Chapter 6 A Reference for Lex Specifications
Structure of a Lex Specification
Input from Strings
Internal Tables (%N Declarations)
Line Numbers and yylineno
Multiple Lexers in One Program
Portability of Lex Lexers
Regular Expression Syntax
Returning Values from yylex()
yyinput(), yyoutput(), yyunput()
Chapter 7 A Reference for Yacc Grammars
Structure of a Yacc Grammar
Ambiguity and Conflicts
Bugs in Yacc
Error Token and Error Recovery
Inherited Attributes ($0)
Portability of Yacc Parsers
Precedence, Associativity, and Operator Declarations
Gregory Satir helps develop online publishing tools in the Portland, Oregon, office of Electronic Book Technologies. He graduated with a B.S. in computer science from Brown University. Doug Brown is a consultant/contractor in Beaverton, Oregon. He has been developing software for circuit simulation, synthesis, and testing since 1977. Doug coauthored lex & yacc, another O'Reilly & Associates Nutshell Handbook. He received an M.S. in electrical engineering from the University of Illinois at Urbana-Champaign in 1976.
John R. Levine writes, lectures, and consults on Unix and compiler topics. He moderates the online comp.compilers discussion group at Usenet. He worked on Unix versions Lotus 1-2-3 and the Norton Utilities and was one of the architects of AIX for the IBM RT PC. He received a Ph.D in computer science from Yale in 1984.
Tony Mason is currently a member of the AFS development team at Transarc Corporation, a small start-up company specializing in distributed systems software. Previously, he worked with the Distributed Systems Group at Stanford University in the area of distributed operating systems and data communications. He received a B.S. in mathematics from the University of Chicago in 1987.
Our look is the result of reader comments, our own experimentation, and feedback from distribution channels. Distinctive covers complement our distinctive approach to technical topics, breathing personality and life into potentially dry subjects. The animal featured on the cover of lex & yacc is a Victoria crowned pigeon, one of the largest members of the pigeon family. Unlike other birds, the crowned pigeon drinks water by immersing its bill and sucking. Incubation of the eggs (generally two) is shared by these monogamous birds, the male warming them by day, the female by night. The Victoria crowned pigeon is light and dark blue with purple markings and a fanlike head crest of lacy, light blue feathers. Though protected by law in its native country of New Guinea, it is nonetheless an easy target for hunters for its plumage and is in danger of extinction. Unix and its attendant programs can be unruly beasts. Nutshell Handbooks(R) help you tame them.
Edie Freedman designed this cover and the entire Unix bestiary that appears on other Nutshell Handbooks. The beasts themselves are adapted from 19th-century engravings from the Dover Pictorial Archive. The cover layout was produced with QuarkXPress 3.1 using the ITC Garamond font.
The inside layout was fomatted in sqtroff using ITC Garamond Light and ITC Garamond Book fonts, and was designed by Edie Freeman. The figures were created in Aldus Freehand 3.1 by Chris Reilley.
I have been refering the book for past three months which has helped me a lot in constructing a compiler(using c++,tool- vc++)as a part of my academic project. The book has helped in several stages of the process. The chapter 8(Yacc ambiguities and Conflicts) helped me in debugging several errors that i came across. I am very thankful to the author.
Now the compiler is working as a win32 console based application. Currently i am trying to integrate lex and yacc with vc++(MFC)so that my compiler will have a very good user interface.I am facing problem with this integration process as c++ code snipet in the MFC application cannot call the parser(.y files).
so i request your suggestion regarding this problem.
I have a copy of the 1st edition and recently downloaded the sample code.
I have to concur with Andy Wong Man Boon, above, that the sample code is pretty crummy. I downloaded the code because I'd a particular interest in the SQL grammar, and found that the sql1 example didn't compile, complaining about being unable to resolve yywrap(). After fixing this problem (either by linking with -lfl or putting in a substitute yywrap()), I then found that the executable gave:
1: syntax error at FROM
SQL parse failed
when fed SELECT foo FROM bar;!
Now, I'm no expert (otherwise I wouldn't have the book), but it seems to me that simple queries like this aren't allowed by the grammar in sql1.y. I got it to work by putting the following into the grammar:
How trustworthy can the code, or the book, be when this kind of simple mistake is made?
An informative and helpful source of information. The bits regarding the differences between yacc and flex are especially useful. More information about the errors generated by flex and bison would be useful. More explanations regarding the yylex, yyparse, yyparse_param, yyerror, and other yy functions are needed. Particularly, how are these functions attached to flex and bison? Also, how might a programmer manipulate these functions to obtain different results?