By John Levine, Tony Mason, Doug Brown
Second Edition
October 1992
Pages: 384
ISBN 10: 1-56592-000-7 |
ISBN 13: 9781565920002
![]()
![]()
![]()
![]()
(Average of 9 Customer Reviews)
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
- 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
Cover | Table of Contents | Colophon
Featured customer reviews
Attractive Book, March 11 2008
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
Compile error in make, help, May 16 2007
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
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
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
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
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
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
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
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







