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. If the error was corrected in a later version or reprint the date of the correction will be displayed in the column titled "Date Corrected".

The following errata were submitted by our customers and approved as valid errors by the author or editor.

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

Version Location Description Submitted By Date submitted Date corrected
example2-3
top

The %option line is missing yylineno:

/* fb2-3 skeleton for include files */
%option noyywrap
%x IFILE
...

should read
/* fb2-3 skeleton for include files */
%option noyywrap yylineno
%x IFILE


Note from the Author or Editor:
accept proposed change, which is in the middle of p.28

jjberry  Aug 11, 2009  Jul 19, 2013
xv
Middle of page

Lex was designed by Mike Lesk and Eric Schmidt (the same
Eric Schmidt who now heads Google) to work with ***bison***
should be:
Lex was designed by Mike Lesk and Eric Schmidt (the same
Eric Schmidt who now heads Google) to work with ***yacc***

Note from the Author or Editor:
accept suggested correction

Anonymous  May 04, 2010  Jul 19, 2013
Other Digital Version
N/A
?

Where others have reported error in first bison example: I have to agree. In my case I get 32767 as the answer consistently if input is valid. Changing from $1 to $2 (as everyone has suggested) fixes this. (I'd love to know why 0,1 and 32767 are the values people get.)

Note from the Author or Editor:
on page 11, in line starting

| calclist exp EOL

in the printf call change $1 to $2

Martin Packer  Apr 14, 2012  Jul 19, 2013
Page 2
Footnote 1

'deterministic finite automation' should be written as 'deterministic finite automaton' (remove 'i' from 'automation')

Note from the Author or Editor:
Good eye, thanks.

Joe Jamison  May 26, 2022 
3.6
para 9

We can add these lines to the definition section to tell it how to resolve the conflicts:

Previously referred to as 'declarations' section?
And referred to as such in the next text para?

"We can add these lines to the definition section to tell it how to resolve the conflicts:"

Note from the Author or Editor:
yes, change definitions to declarations in last para on p. 59

Dave Pawson
 
Aug 30, 2009  Jul 19, 2013
Printed
Page 10
bottom

CFG is a Type 2 language, not a Type 3 as stated in the book

Note from the Author or Editor:
change 3 to 2 in two places in the footnote on page 10

Anonymous  Jan 19, 2012  Jul 19, 2013
Printed
Page 11
middle of page, in the action of the calclist rule

The second line of the calclist rule has the following print statement:

| calclist exp EOL { printf("= %d\n", $1); }

The $1 is wrong and should read $2, since we want to print the value of the exp, not the calclist.

Note from the Author or Editor:
Thanks, you are correct. It should be $2.

David Liu  Dec 07, 2019 
PDF
Page 12
5th paragraph

The text in the 5th paragraph of page 12 mentions a "calcset" start symbol, but what we have in the previous page is a grammar whose start symbol is named "calclist".

Note from the Author or Editor:
yes, "calcset" should be "calclist". Oops.

Fernando Carrijo  May 11, 2010  Jul 19, 2013
Printed
Page 25
last lines of example 2-2

The last test reads:

if (argc > 1) /* print total if more than one file */

It should really be:

if (argc > 2)

Note from the Author or Editor:
accept suggested change

Anonymous  Apr 16, 2010  Jul 19, 2013
PDF
Page 28
first pattern in example 2-3

The pattern is in error.

The pattern

^"#"[ \t]*include[ \t]*\[\"<]

must instead be

^"#"[ \t]*include[ \t]*[\"<]

if the scanner is to match any #include files at all.

Note the extra and erroneous backslash in the published text.

Note from the Author or Editor:
In the example on the last line of the page, delete the \ between * and [ as suggested.

Kyle Ferrio  Dec 11, 2010  Jul 19, 2013
Printed
Page 28
Bottom of page (first regular expression)

The regular expression for parsing the #include < or #include " portion of each file has a mistake - there is an extra \ .

Instead of

^"#"[ \t]*include[ \t]*\[\"<]

I've found the following to work:

^"#"[ \t]*include[ \t]*[\"<]

Note from the Author or Editor:
Erratum is correct. Take out the \ before the [.

Vuk Petrovic  Sep 30, 2016 
Printed
Page 29
example listing

the manual management of yylineno is incorrect. flex will automatically increment yylineno as the input is consumed. therefore, statements such as:
yylineno++;
are incorrect, leading to 'odds only' or 'evens only' line number outputs: example:
line one
line two

line four
produce:
1 line one
3 line two
6
7 line four
as output. The solution, to me, seems to be to eliminate all yylineno++ expression, and then, change the ^\n pattern action's to yylineno-1. For the sample above, this now yields.
1 line one
2 line two
3
4 line four
as one would hope for... Thanks.

Note from the Author or Editor:
This is correct. Earlier versions of flex didn't update yylineno on each \n but recent versions all do.

Lexi Haley  Sep 10, 2019 
Printed
Page 30
Example 2-3

The end of the first comment in the popfile function is missing:

/* get rid of current entry */
fclose(bs->f); ^^^^

Note from the Author or Editor:
accept suggested change

craie  Aug 07, 2010  Jul 19, 2013
Printed
Page 30
Example 2-3

Near the end of the newfile function, the fn parameter (a character pointer which originated from yytext) is copied into both bs->filename and curfilename. Although this happened beforehand, the author even warns against this type of activity on page 36 (first paragraph). Therefore I believe it should be strdup'd instead (at least the first and then you can copy it). Otherwise, by the time you take this record off the stack, it won't be pointing to the same information.

Note from the Author or Editor:
p. 30, second line, change fn to strdup(fn)

change sixth line to

curfilename = bs->filename

in middle of example, below the line fclose(bs->f); add new line

free(bs->fn);

craie  Aug 07, 2010  Jul 19, 2013
Other Digital Version
30
Error on Confirmed Errata page itself

On the "Confirmed Errata" web page at:

http://www.oreilly.com/catalog/errata.csp?isbn=9780596155988

One of the correction for P.30 reads as follow:

---

in middle of example, below the line fclose(bs->f); add new line

free(bs->fn);

---

This shoud likely read:

free(bs->f);

as fn is not a member of the bufstack structure.

Note from the Author or Editor:
Proposed change is correct

Jim Theriault  Nov 25, 2015 
Printed
Page 30
popfile code listing

The example code states;

free(bs->fn);

but should be;

free(bs->filename);

as that filename was allocated from the earlier strdup call.

Note from the Author or Editor:
There is no free(bs->fn), but it would be a good idea to put
free(bs->filename)
just above
free(bs)

Lexi Haley  Sep 10, 2019 
Printed
Page 34
middle of the code example

The call of perror in the code uses argv[1].

It should be: perror(argv[i]);

Note from the Author or Editor:
accept suggested correction argv[1] should be argv[i]

Anonymous  Apr 17, 2010  Jul 19, 2013
Printed
Page 35
5th paragraph

The lookup function uses strcmp (case sensitive string comparison) even though the author points out on page 33 that the yytext is not case folded in this case-insensitive example. Perhaps stricmp or strcasecmp was meant? Otherwise the symbol table will not reflect the case-insensitivity intended (and the instrumented statistics may be suspect).

This problem is repeated in the symcompare function on page 37.

Note from the Author or Editor:
In the code about 2/3 of the way down on page 35, change strcmp to strcasecmp

craie  Aug 07, 2010  Jul 19, 2013
PDF
Page 42
punctuators declaration

At the punctuators declaration pattern:

"="|"*="|"/="|"%="|"+="|"-="|"<<="|">>="|"&="|"^=""|="

I think that needs to be:

"="|"*="|"/="|"%="|"+="|"-="|"<<="|">>="|"&="|"^="|"|="
^
needs an extra `|' symbol

Note from the Author or Editor:
Suggested change is correct. Thanks.

crosvera  Jul 05, 2012  Jul 19, 2013
Printed
Page 44
last paragraph

For example 2-5, popfile() function should not be the same as the ones earlier in chapter 2. It should be modified a little, or a segment fault will raise, when parsing more than one C source files from command line.

Snippet of popfile() from example 2-3:

/* switch back to previous */
prevbs = bs->prev;
free(bs);

if (!prevbs)
return 0;

/* switch to previous */
yy_switch_to_buffer(prevbs->bs);
curbs = prevbs;
yylineno = curbs->lineno;
curfilename = curbs->filename;
return 1;

For this version, when a second C source file from command line is open, newfile() function will see non-null curbs, and no space will be allocated for curbs, but curbs has been freed by popfile() already.

So, for example 2-5, the code should be changed to:

/* switch back to previous */
prevbs = bs->prev;
free(bs);
/* current bs should be restored, before returning */
curbs = prevbs;

if (!prevbs)
return 0;

/* switch to previous */
yy_switch_to_buffer(prevbs->bs);
yylineno = curbs->lineno;
curfilename = curbs->filename;
return 1;

Note from the Author or Editor:
Yes, it's an error, don't immediately see how to change the book so it fits on the page.

Hongzhi Song  May 09, 2011 
PDF
Page 50
para. 1

The reader had to be pretty alert to realize that the shift/reduce sequence is correct for the grammar including

expression : NUMBER '+' NUMBER
| NUMBER '-' NUMBER

but not the "extended" grammar

expression : NUMBER
NUMBER '+' NUMBER
| NUMBER '-' NUMBER

A rearrangement of pp 48-50 would do a lot to clarify and avoid confusing the reader encountering LR grammar for the first time.

N.B. that the really alert reader will notice that the 'extended' grammar is missing the single quotes around the operator tokens.

Note from the Author or Editor:
On p.48 in the example in the middle, put single quotes around '+' and '-'

Kyle Ferrio  Dec 12, 2010  Jul 19, 2013
PDF
Page 63
bottom quarter of page

The following block of code correctly appears at the top of page 61 and is erroneously repeated near the bottom of page 63, both within Example 3-5 (fb3-2.h). It should appear exactly once:

/* interface to lexer */
extern int yylineno;
void yyerror(char *s, ...);

Note from the Author or Editor:
Yes, delete those three lines from page 63.

Kyle Ferrio  Dec 21, 2010  Jul 19, 2013
Page 73
Body of treefree()

The treefree() function only calls free() on child nodes for assignments (case '=':) and conditional expressions (case 'I': case 'W':). This will result in memory leaks if the child nodes are trees themselves. I think the two calls to free() should be to treefree() instead.

This also applies to the pure calculator example in section 9 (p227)

Note from the Author or Editor:
This is correct. Both calls to free() should be treefree(). Oops.

Mark Douglas  Jan 02, 2023 
PDF
Page 77
bottom quarter (Example 3-8, fb3-2func.c)

The call

yyerror("call to undefined function", fn->name);

will not write the intended diagnostic, unless changed to

yyerror("call to undefined function %s", fn->name);

for example.

Note from the Author or Editor:
Proposed change is correct.

Kyle Ferrio  Dec 21, 2010  Jul 19, 2013
Page 110
update_opts nonterminal definition

The nonterminal update_opts is defined with reference to insert_opts; I think the intent is to be recursive, i.e., the two references to insert_opts should be replaced with update_opts. This is consistent with the MySQL documentation for UPDATE and INSERT.

Note from the Author or Editor:
You're right, cut and paste error. Both references to insert_opts should be update_opts. Oops again.

Mark Douglas  Jan 02, 2023 
Printed
Page 216
purecalc.l

It's probably better practice to explicitly declare

%option extra-type="struct pcdata*"

somewhere in the first section of purecalc.l; doing so would at least be consistent with the pure scanner word count program given earlier in the chapter.

Note from the Author or Editor:
at bottom of p.216 insert suggested line below the %option header line

Anonymous  Apr 13, 2010  Jul 19, 2013
PDF
Page 234
First line of the "C++ Parsers" section

current:
create C++, scanners, the C++ code
proposed:
create C++ scanners, the C++ code
where the change is:
remove comma after C++

Note from the Author or Editor:
Report is correct, remove the extra comma.

Paolo Lamponi  Aug 05, 2014