View Review Details


Book:   lex & yacc
Subject:   lex & yacc, 2nd Edition Review
Date:   2003-05-02 12:52:20
From:   Andy Wong Man Boon
Rating:  StarStarStarStarStar

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;


}


See larger cover