Errata

flex & bison

Errata for flex & bison

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released.

The following errata were submitted by our customers and have not yet been approved or disproved by the author or editor. They solely represent the opinion of the customer.

Color Key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted by Date submitted
PDF Page 8
Example 1-4. Calculator scanner fb1-4.l

The definition of NUMBER is not complete: it doesn't work with the ABS operator:

[0-9]+ { yylval = atoi(yytext); return NUMBER; }

should read

[-]?[0-9]+ { yylval = atoi(yytext); return NUMBER; }

to be possible to work with expressions like:
-5
3 - -5
3 - |-5 ; ABS in action

etc.

Miro Kropáček  Nov 11, 2023 
PDF Page 33
1st paragraph

Chapter-1 compiling flex and bison programs together .
Example 1-6


I have not written the code myself just used the ftp to download the code .

I believe there is no
int yylval in fb1-5.l ,included it but yet there is an error.

make Makefile.ch1 fb1-5
make: Nothing to be done for 'Makefile.ch1'.
yacc fb1-5.y
fb1-5.y: warning: 3 shift/reduce conflicts [-Wconflicts-sr]
mv -f y.tab.c fb1-5.c
cc -c -o fb1-5.o fb1-5.c
cc fb1-5.o -o fb1-5
fb1-5.o: In function `yyparse':
fb1-5.c:(.text+0x32a): undefined reference to `yylex'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'fb1-5' failed
make: *** [fb1-5] Error 1
rm fb1-5.o fb1-5.c

sourav  Aug 21, 2015 
Printed Page 38
na

- ch2 Using flex,
C Language Cross-Reference (example fb2-5) ... pg38
This example (fb2-5) fails ... (using multiple input files)
=
Clarification:
This submission is for those readers who still wonder why example fb2-5 does
not work for them. To make fb2-5 perform as intended the following bugs must
be removed:

1. bug in newfile()
reported by craie (August, 2010)
2. bug in popfile() -- i.e. in case of using multiple input files, this bug
must have been removed (but not limited to that condition)
reported by Hongzhi Song (May, 2011)
3. bug in the rules section -- where newfile() is invoked in case an include
directive is encountered (and a proper "roll-back is missing, in case the
return value of newfile() indicates that the scan must be aborted).
(in case of using multiple input files, this bug must have been removed)
4. bug in addref() -- where parameter filename must be strdup'd

To address the bugs in newfile() and popfile(), I will present slightly modified
versions of these functions first..

J.H. vd Water  Apr 29, 2016 
Printed Page 38
na

-----

- ch2 Using flex,
C Language Cross-Reference (example fb2-5) ... pg38
Parameter fn (argument yytext) must be strdup'd before copying it to the filename
field of the 'bufstack' datablock ... on the other hand, copying fn to curfilename
will do (while newfile() is executing)

int
newfile(char *fn)
{
// slightly modified "official" version
/* die if no file or no room */
FILE *f = fopen(fn, "r");
if (!f) { perror(fn); return 0; }

struct bufstack *bs = malloc(sizeof(struct bufstack));
if (!bs) { perror("malloc"); exit(1); }

/* remember state */ // there will be no datablock if invoked from main
if (curbs) curbs->lineno = yylineno;

bs->prev = curbs;
curbs = bs;

/* set up current entry */
curbs->bs = yy_create_buffer(f, YY_BUF_SIZE);
curbs->f = f;
curbs->filename = strdup(fn); // reported by craie (August 2010)
if (!curbs->filename) { perror("strdup"); exit(1); } // added

yy_switch_to_buffer(curbs->bs);
yylineno = 1;
curfilename = fn; // it is ok to use fn until 'include' is encountered
return 1;
}

J.H. vd Water  Apr 29, 2016 
Printed Page 38
na

- ch2 Using Flex,
C Language Cross-Reference (example fb2-5) ... pg38
To remove a bug in popfile(), which has _already_ been reported by by Hongzhi
Song (May, 2011), popfile() should be rewritten as follows:
- note: this bug will only reveal itself in fb2-5, which accepts multiple
input files (fb2-3 does not)
- note: this bug will reveal itself in fb2-3 if the bug in the rules section
is removed
- note: the dangling reference in curfilename (as result of not using strdup
in newfile() ) does not matter in fb2-3, as curfilename is not used there

int
popfile()
{
// alternative for the "official" version
if (!curbs) return 0; // if newfile has never been invoked before

/* get rid of current entry */
fclose(curbs->f);
yy_delete_buffer(curbs->bs);
//free(curbs->filename); // irrelevant in fb2-3

/* switch back to previous */
struct bufstack *bs = curbs;

curbs = curbs->prev; // also honours the report made by Hongzhi Song
free(bs);

if (!curbs) { // null-pointer => rootfile termination
//curfilename = rootfile; // for completeness (rootfile = argv[i])
return 0;
}
yy_switch_to_buffer(curbs->bs);
yylineno = curbs->lineno;
//curfilename = curbs->filename; // irrelevant in fb2-3
return 1;
}

J.H. vd Water  Apr 29, 2016 
Printed Page 38
na

- ch2 Using Flex,
Start states and nested input files (example fb2-3) ... pg28
C Language Cross-Reference (example fb2-5) ... pg38
To remove a bug in the rules section (where newfile() is invoked in case an
include directive is encountered), replace this statement
if (!newfile(yytext)) yyterminate()
by these statements
if (!newfile(yytext))
{
while (curbs) { popfile(); }
BEGIN INITIAL;
yyterminate();
}
- note: this bug will only reveal itself in fb2-5, which accepts multiple
input files (fb2-3 does not)
=
Clarification:
When newfile() returns a failure, the scanning of an input file (rootfile) is
aborted. All allocated datablocks should be reclaimed (by repeatedly calling
popfile() as needed), and the lexer must be reset to its initial state before
calling yyterminate().
Leaving the lexer in the IFILE state on abort, will confuse the lexer when it
is about to start scanning the next file (multiple input files specified).
As result of using popfile() to reclaim the datablocks, the null-pointer will
be reassigned to variable curbs, which will prevent newfile() from attempting
to write to a non-existing datablock (as noted by Hongzhi Song).

J.H. vd Water  Apr 29, 2016