Lex & Yacc 2nd Edition. Error(?)

Andrew Ireland <andy>
20 Jan 1996 14:47:38 -0500

          From comp.compilers

Related articles
Lex & Yacc 2nd Edition. Error(?) andy (Andrew Ireland) (1996-01-20)
| List of all articles for this month |
From: Andrew Ireland <andy>
Newsgroups: comp.compilers
Date: 20 Jan 1996 14:47:38 -0500
Organization: MR-Memex Ltd, East Kilbride, Scotland
Keywords: lex, yacc, books, errors

I think there is an error in the redefinition of YY_INPUT given on page 157
for flex in the 2nd edition of `lex and yacc'.


Example 6-1 reads as follows:


%{
#undef YY_INPUT
#define YY_INPUT(b, r, ms) (r = my_yyinput(b, ms))
%}
    . .
extern char myinput[];
extern char *myinputptr; /* current position in myinput */
extern int myinputlim; /* end of data */


int
my_yyinput(char *buf, int max_size)
{
int n = min(max_size, myinputlim - myinputptr);


if (n > 0) {
memcpy(buf, myinputptr, n);
myinputptr += n;
}
return n;
}


attempting to compile this produces the following error:


"main.c", line 29: operands of - have incompatible types
*** Error code 1


(this is on SunOS, and with Sun cc, and flex-2.4.6)


to get round this I have redefined the first line of code in my_yyinput()
to :


int n = min(max_size, (myinput + myinputlim) - myinputptr);


I think it should also be made clear that min is a function which returns
the minimum integer value of its two input arguments, and _not_ the function
min defined in #include <mp.h> (MISCELLANEOUS LIBRARY FUNCTIONS). Which is
what my linker originaly thought it was!


-*-


This solved my problem, but when I tried to call my function again with new
input, it seemed as if the internal state of the lexer was still at the last
token (NUL) or EOF. We have got round this by calling yyrestart() without any
parameters, (after all we are not reading from a file) after every call to
yyparse(), and this seems to work, but is it correct?
(sorry bookquestions@ora.com this last para is intended for dicussion on
comp.compilers)


Cheers,
Andy
--
Andrew Ireland <entirely my own opinions>
MR-Memex Ltd., 2 Redwood Court, Peel Park, East Kilbride G74 5PF, U.K.
andy@memex.co.uk Tel: +44 13552 33804 Fax: +44 13552 39676
[It's a bug, myinputlim was supposed to be a pointer. Don't know how the
wrong version of the code snuck into the book. -John]


--


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.