LEX Question

Dave Jones djones at megatest.UUCP
Wed Feb 15 12:02:25 AEST 1989


> In article <32240 at auc.UUCP> tab at auc.UUCP (0-Terrence Brannon ) writes:
>>My question is, is there anyway that I could have the lex program accept
>>integers from somewhere other that stdin? ...
> 

The officially sanctioned way is to redefine the macros
input(), and unput(). But if you want to do a quick and dirty,
probably unportable hack, read on.

WARNING!! PURISTS ARE CAUTIONED NOT TO READ BEYOND THIS POINT
IN OTHER THAN A THOROUGHLY KICKED BACK AND CHILLED OUT MOOD.
If mild flamage results, discontinue reading. In case of violent
flamage, or flames that persist beyond one reply posting, make
some funny faces in the mirror, while repeating, "Whootie, whootie, 
whootie," in a high-pitched voice.

Now then, the following is probably true for all BSD derived
Unixes, and maybe for others, I dunno.

I don't know if it's documented, but you can assign another
FILE* to the variable [yyin]. Then the standard input() and unput()
will work on that stream instead of stdin.

If you want the input to come from something other than a file
descriptor, as you seemed to imply, you can try being even more
devious:  Munge around with the struct _iobuf, perhaps using _IOMYBUF
and _IOSTRG flags. (If you have the source to sscanf() take a look
at that. It uses the same trick.)  I have not tried this, so if it 
doesn't work, well that's life.  You will have to be sure that the 
buffer is never depleted, less _filbuf get called.

And remember, if anyone asks, _I_ never told you to do it that way!



More information about the Comp.lang.c mailing list