lex & yacc, Second Edition

By John Levine, Tony Mason, Doug Brown
October 1992
Pages: 384
ISBN 10: 1-56592-000-7 | ISBN 13: 9781565920002
starstarstarstarstar (3) (Average of 10 Customer Reviews)

Buy 2 Get 1 Free Free ShippingGuarantee

Book description

Shows programmers how to use two Unix utilities, lex and yacc, in program development. You'll find tutorial sections for novice users, reference sections for advanced users, and a detailed index. Major MS-DOS and Unix versions of lex and yacc are explored in depth. Also covers Bison and Flex.
Full Description

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
Post-purchase benefits:

Register your book | Submit Errata | Examples


Browse within this book

| Table of Contents | Index | Colophon

Book details

Second Edition: October 1992
ISBN: 1-56592-000-7
Pages: 384
Average Customer Reviews: starstarstarstarstar (3) (Based on 10 Reviews)


Featured customer reviews

Write a Review


Nice book,  February 20 2009
Rating: StarStarStarStarStar
Submitted by dmkmil   [Respond | View]

I bought the book back in 1993 and it pleases me each time a read it.

If you have a project involving yacc and lex , this is the best source I have found the last years.

However, a new edition would be nice !

Notice : Take care as in certain examples memory is dynamically allocated but not freed.




Attractive Book,  March 11 2008
Rating: StarStarStarStarStar
Submitted by supersimha   [Respond | View]

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.

Thanking You Narasimha.G


lex & yacc, 2nd Edition Review,  December 26 2007
Submitted by Anonymous Reader   [Respond | View]

comment je peux telecharger le livre lex&yacc

Read all reviews


Compile error in make, help,  May 16 2007
Rating: StarStarStarStarStar
Submitted by kkc98   [Respond | View]

I try compile all code using "make" command in linux, but it come back with compile error, please help.

thank you!

/* output
[work]$ make
flex ch1-01.l
mv lex.yy.c ch1-01.c
gcc -g -o ch1-01.pgm ch1-01.c -ly -ll
/usr/bin/ld: cannot find -ly
collect2: ld returned 1 exit status
make: *** [ch1-01.pgm] Error 1
[work]$

*/


lex & yacc, 2nd Edition Review,  March 18 2005
Submitted by Anonymous Reader   [Respond | View]

What do you mean by:
either by linking with -lfl or putting in a substitute yywrap()), I then found that the executable gave:

Q1. I tried linking with -lfl, still does not work.

Q2. What should I put for substitute yywrap()?


lex & yacc, 2nd Edition Review,  November 26 2003
Rating: StarStarStarStarStar
Submitted by Mike   [Respond | View]

The book is full of code examples that show how to do things that may be very hard to re-invent. It is listed as a reference by yacc implementer. I find this book to be very interesting and detailed.

---------

Mike


lex & yacc, 2nd Edition Review,  September 10 2003
Rating: StarStarStarStarStar
Submitted by Emmet Caulfield   [Respond | View]



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:

<blockquote>

1: syntax error at FROM

SQL parse failed

</blockquote>

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:

<blockquote>

sql: query_exp

;

</blockquote>

How trustworthy can the code, or the book, be when this kind of simple mistake is made?

Emmet




lex & yacc, 2nd Edition Review,  June 13 2003
Rating: StarStarStarStarStar
Submitted by Philippe   [Respond | View]

If you plan to update some of the sample code, in addition to what is explain in the previous message, I would recommend to use :

static int add_word(int type, char *word);

static int lookup_word(char *word);

for the declaration of the functions used in the first sample.

Regards, Philippe

Feel free to visit http://perso.wanadoo.fr/philippe.baucour/




lex & yacc, 2nd Edition Review,  May 02 2003
Rating: StarStarStarStarStar
Submitted by Andy Wong Man Boon   [Respond | View]

Well..it's time for a 3rd edition.

After going through Chapter 1 to 7, I found

that this is a good book on the subject "lex & yacc".

Unfortunately, the examples in the book (Chap 1 to 5) do not

compile and link properly. Even if an example happens to compiled and linked

successfully, the executable will crash when executed :(

(i.e. Example 1-7 (pg. 17))

I felt that failure in compiling and linking the examples

will discourage lex & yacc newbies from experimenting and learning lex & yacc.

I've included some fixes below to make the examples compile and link properly.

(I'm using Cygwin on W2K, flex 2.5.4 and bison 1.875)

Hopefully, these fixes will help lex & yacc newbies who is reading

this book and tinkering with the examples and exercises.

-Andy Wong Man Boon (Email me at andyw_apcd@hotmail.com

if you want fixes for the other examples)

{15} to {18}

For ch1-05.l, add...

%{

...

int lookup_word(char *word);

int add_word(int type, char *word);

/* We do not require yyunput so do not define it to avoid

'C' compiler warning */

#define YY_NO_UNPUT

%}

%option noyywrap

%%

...

%%

...

int add_word(int type, char *word) {...}

int lookup_word(char *word) {...}

For ch1-05.y, add..

%{

#include <stdio.h>

/* prototype for lexer function */

int yylex(void);

/* prototypes for error handling functions */

int yyerror(char *s);

%}

...

%%

...

%%

...

int main(void)

{

yyin = stdin; /* Initialise yyin ! */

...

return 0;

}

int yyerror(char *s)

{

fprintf(stderr, "yyerror: %s\n", s);

return 1; /* Recovery not attempted */

}

Compile ch1-05.l and ch1-05.y as follows:-

flex ch1-05.l

bison -y -d ch1-05.y

gcc -Wall -c lex.yy.c y.tab.c

gcc -Wall -o ch1-05 lex.yy.o y.tab.o

{24}

For Ex1-10.l, the returning values from

yylex() would be clearer if you add...

%{

#include <stdio.h>

...

#define YY_NO_UNPUT

%}

%option noyywrap

%%

...

\n { return '\n'; }

"$" { return 0; /* end of input */ }

%%

int main(void)

{

int val;

while( (val=yylex()) > 0) {

printf("token is %d\n", val);

}

printf("token is %d\n", val);

return 0;

}

Compile Ex1-10.l as follows:-

flex Ex1-10.l

gcc -Wall lex.yy.c -o Ex1-10

{59}

For ch3-01.y, the returning value of yyparse()

would be clearer if you add to the main() the following:-

int main(void)

{

...

bRet = yyparse();

if( bRet != 0) {

printf("yyparse() error: ret %d\n", bRet);

} else {

printf("yyparse() ok: ret %d\n", bRet);

}

return 0;

}


lex & yacc, 2nd Edition Review,  December 24 2001
Rating: StarStarStarStarStar
Submitted by Fractal A.   [Respond | View]

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?




lex & yacc, 2nd Edition Review,  February 05 2001
Rating: StarStarStarStarStar
Submitted by Fabio Rotondo   [Respond | View]

I bought this book because a very short article on a magazine excited me, but

after I have read it completely, I still have a lot of questions and black holes

in the way things work between Lex & Yacc.

It is a good book, with lots of examples, but for somebody like me, with no

background experience about these two tools, sometimes it went just too quickly.

I think that some intermediate steps would do the trick.

It is a good reference, anyway.


lex & yacc, 2nd Edition Review,  September 13 2000
Rating: StarStarStarStarStar
Submitted by Jonghoon   [Respond | View]

OK..


lex & yacc, 2nd Edition Review,  October 26 1999
Submitted by Andreas H. Studer   [Respond | View]



The book is IMO well written, but misses some points. - I found out that calling tokens exactly the same name in YACC "like they are" can produce problems. YACC produces #define's out of the tokens and I wrote a C++-wrapper around the parser. So to make it easy, I first called all methods like the Tokens if they belongs to a token. But the Include of the C++-wrapper-header is after the defines, so if you'll have methods/variables/functions/whatever in this header with the same name like the tokens, you'll get in big troubles... and since it's a #define-problem, it's not that easy to find out... (in fact I thought first that the compiler has a serious problems with mixed C/C++-code :-) So my solution is: call all YACC Tokens with T_ something and nonterminals (to make it complete) NT_ something. Then you have less problems with this. - yyerror(...) and dislike functions are often in the book programmed like this: "void yyerror(...)". the problem is that compilers like Visual C++ expect for external binding functions a form like "int yyerror(...)". In fact if you'll look to the skeleton-sourcecode, you'll see exactly this. More than that: MSVC++ MUST have a return value for a function if it is declared that way. So you MUST include "return 0;" at the end. - the BISON-part is too small. Even if the documentation of BISON is well, I think it would be useful to have a closer look at this well prooven alternative to YACC. I think it's the most used YACC-alternative since Linux became so popular. A closer look to the "hairy-parser" and multi-threading ready parsers out of Bison would be helpful since it's not that well explained in the documentation of BISON. - It is really not a big problem to wrap a C++-class around a YACC-generated parser. I would include some thoughts in the book how to implement it. BISON is C++-compatible, but I don't know about the others. So to figure out which are would be helpful too. After all I had some problems with BISON on Windows, but the most-hard to figure out problems where solved by the book. It is also very well written, easy to understand and explains all important points well. It also helps the user to think the "yacc/lex"-way. Even if it is the only (?) book which describes this both Unix-tools, I can't imagine that it could be really alot better. Andreas H. Studer


lex & yacc, 2nd Edition Review,  March 24 1999
Submitted by Hyuksoo, Chang   [Respond | View]



I found errors in p.46, 2nd Edition.
4th line from the bottom up in p.46:
"Similarly, this next expression matches the first
but not the second."

==> 'first' and 'second' must be exchanged like the
following :
"Similarly, this next expression matches the second
but not the first."



lex & yacc, 2nd Edition Review,  January 23 1999
Submitted by Jun Sun   [Respond | View]



I read it through and used it frequently as I
was doing a project. Overall it is informative
enough to let me finish my project smoothly.

Plus :

. Mentions a lot of variants. Good coverage.
. SQL parser is particularly helpful to me as
I am writing a SQL-like parser. It gave me
a kick start.

Minus :

. Lacks a intuitive explanation of the interfacing
between lex and yacc. It took me a quite while
to figure out all the details and the underlying
mechanisms.

. It would be much better to provide some insight
into the design of lex/yacc programs. For
example :
1. parsed information can be carried forward
through either static global variable or
yylval struct. (Once I realized this,
my project overall design become clear.)
2. How to interface with C++ program?
3. Where actions happen? Along the parsing
or just carrying the information during
parsing and do the actions in the end?

. Sample code still has bugs, - like everyone
else. :-)


Media reviews

"The book is...well-written and thorough. I had the pleasant surprise numerous times of reading some passage, thinking to myself, `Oh, they've missed this perhaps subtle point, make a note,' and then the next sentence or paragraph would cover exactly that point...I'm glad I'll finally have a good book to recommend to folks." --Vern Paxson, Developer of Flex

"Even after many years of using lex and yacc, this book showed me new things about them, and new uses for facilities I thought I knew. It will have an honoured position on my bookshelf" --Pete Jinks, Sun UK User

"If you have an application for lex and yacc, you should have this book on your shelf. Few other books provide such an appropriate combination of details and illustrative examples for these tools." --G.K. Jenkins, Computing Reviews, July 1993

"We have colleagues who, without turning a hair, spell 'Turing Machine' with an 'ou'. And we have other colleagues who approach the whole collection of problems placed on their desks during their careers like complete troglodytes. Both may, in their life- time, encounter the universally esteemed Unix operating system and along with it tools like lex & yacc. The Nutshell Handbook of the same name by Tony Mason and Doug Brown has both ends of the developer spectrum in view as its purchasers. This is a book for developers by developers, and even insofar as possible, all developers. There is but one prerequisite: you have to speak C and know what a Makefile is.

"lex (lexical analyzer) and yacc (yet another compiler compiler) are work tools that are typically used for development of compilers and interpreters. The handbook does not restrict itself to a limited description of both tools; fortunately it gets right to the point. Beginning with the first example the stress is on making the tool friendly to the reader in an interactive way: to put the bridle on the horse from the top down, to clarify the details one by one.

"Except for Chapters 6 and 7, in which lex and yacc methods and and resources are treated together--almost in reference format-- all the chapters are concerned with basics. Altogether, four chapters undertake to introduce the tools, starting with minimal examples like the inevitable pocket calculator and proceeding to menu and screen generators. All programming examples are carefully developed and not just presented to the reader. That takes space but makes for understanding--even if it leads the more experienced reader to take a shortcut or in certain cases obliges him or her to go into reverse.

"A manual should not just facilitate the reader's imitation of the examples offered; the knowledge gained should be transferable. Upon first trying to use lex and yacc people ususally stumble over two problems: first, over unsavory grammars larded with ambiguities (typical case: which if does this else belong to?), and second, over deep-seated logical errors and strategies for recovering them: How nonsensical must a program be for the compiler not to consider it meaningful to carry through to the bitter end, or else, the reverse? The authors' decision to devote a complete chapter to each of these points seems to me praiseworthy and practical.

"The appendices are equally practical: Appendices A and B summarize options and error messages. For each error message there is a short explanation along with advice on fixing the problem. Appendices C and D describe the free GNU analogs to lex and yacc, namely,
flex and bison. Nor have the authors forgotten to include the address of the Free Software Foundation from which they can be obtained. Besides program listings, a bibliography, and an index, the book fortunately also includes a glossary in which the most important subjects that you come across in the book are compiled.

"In short, the book is an excellent, shapely thing. If there's anything to complain about, it's only this: why not before now? The book appeared in May, 1990. I wish I'd had it much sooner." --Heinz Weber

Hide extended reviews


See larger cover