LEX Question

Henry Spencer henry at utzoo.uucp
Sat Feb 25 02:58:32 AEST 1989


In article <416 at chem.ucsd.EDU> tps at chem.ucsd.edu (Tom Stockfisch) writes:
>>Re-defining "input" is in the documentation as the way to alter input; any
>>version of lex that follows the manual (and hence deserves to be called
>>"lex") will permit it and have it work as documented.  "yyin" is not
>>documented at all...
>
>I must be missing something.  Looking at lex.yy.c on my machine shows that
>input() is defined as
>
># define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==10? \
>	(yylineno++,yytchar):yytchar)==EOF?0:yytchar)
>
>so if I re-#define input(), don't I have to use yytchar,yysptr, and yysbuf?

No; they are lex's buffered-input system.  If you redefine input(), you
have to provide your own buffering scheme, or have it provided for you
by a library.  You need not, and should not, use lex's own internal
variables to do it with.  The input interface to lex is fully defined
in the documentation:  input() should yield the next character, unput(c)
should push c back into the input stream.  Meet those specs (and a few
more details that are in the documentation) and it works fine; I've done
it on occasion.  Messing with mysterious and undocumented variables whose
names start with "yy" is neither necessary nor desirable.

I have, on occasion, just assigned a stdio file pointer to yyin... but
I've never claimed it was portable or good practice, and I've always
documented it as a bug.

If you define your own input stuff, the variables lex uses for its input
system just go unused.  This is a minor waste of space.  It could undoubtedly
be eliminated by some cleverness in lex, if anyone felt it worth bothering
about.
-- 
The Earth is our mother;       |     Henry Spencer at U of Toronto Zoology
our nine months are up.        | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list