[comp.unix.questions] File I/O with lex/yacc

gld@cunixd.cc.columbia.edu (Gary L Dare) (08/30/90)

Is there any way to get around the use of standard input/output in a
lex file???  The C file generated by lex (from the lexical analyzer
spec you feed in) has the following line hard-coded:

	FILE *yyin = {stdin}, *yyout = {stdout};

however, there doesn't seem to be a way to sneak in a redefinition of
yyin or yyout as a pointer to a file (a single, in-line statement)
except by hand, 'cos a switch{} is generated in C from the lexical
analysis rules.

Is there any way to force a single, inline statement into the C
file generated by lex, other than by hand?

gld
--
~~~~~~~~~~~~~~~~~~~~~~~~ Je me souviens ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Gary L. Dare				No golf courses on
> gld@cunixD.cc.columbia.EDU		Mohawk Indian
> gld@cunixc.BITNET			burial grounds! (Oka, Quebec)

thomas@uplog.se (Thomas Tornblom) (08/30/90)

In article <1990Aug29.172830.14348@cunixf.cc.columbia.edu> gld@cunixd.cc.columbia.edu (Gary L Dare) writes:


   Is there any way to get around the use of standard input/output in a
   lex file???  The C file generated by lex (from the lexical analyzer
   spec you feed in) has the following line hard-coded:

	   FILE *yyin = {stdin}, *yyout = {stdout};

   however, there doesn't seem to be a way to sneak in a redefinition of
   yyin or yyout as a pointer to a file (a single, in-line statement)
   except by hand, 'cos a switch{} is generated in C from the lexical
   analysis rules.

If you want to have yyin and yyout set to something else, just 
fopen() them to whatever file you want before calling yylex().

   Is there any way to force a single, inline statement into the C
   file generated by lex, other than by hand?

I'm not sure I know what you mean by this. You can put inline code
almost wherever you want.

In the definitions part you can:

%{
#include <foobar.h>
...
[whatever]
%}

In the rules part you can have C code that executes whenever the rule
is matched.

^foo	{printf("matched foo at beginning of line\n"); }

and in the subroutine part you can stick in c code.



	  
-- 
Real life:	Thomas Tornblom		Email:	thomas@uplog.se
Snail mail:	TeleLOGIC Uppsala AB		Phone:	+46 18 189406
		Box 1218			Fax:	+46 18 132039
		S - 751 42 Uppsala, Sweden

bogatko@lzga.ATT.COM (George Bogatko) (08/30/90)

In article <1990Aug29.172830.14348@cunixf.cc.columbia.edu>, gld@cunixd.cc.columbia.edu (Gary L Dare) writes:
> 
> Is there any way to get around the use of standard input/output in a
> lex file???  The C file generated by lex (from the lexical analyzer

There are two ways.
1.  Use freopen(...stdin) freopen(...stdout).

    freopen("your_in_file", "r", stdin);
    freopen("your_out_file", "w", stderr;

2. use a close/open combination, ala:

	close(0);
	open("your_in_file", ...);
	close(1);
	open("your_out_file", ...);
	blah, blah, blah.

   the second way can be useful if you want to use lex to parse the input from
   a tty line different than your terminal line.

Method 1 is far less messy than method 2.

GB

vu0310@bingvaxu.cc.binghamton.edu (R. Kym Horsell) (08/30/90)

In article <1990Aug29.172830.14348@cunixf.cc.columbia.edu> gld@cunixd.cc.columbia.edu (Gary L Dare) writes:
>
>Is there any way to get around the use of standard input/output in a
>lex file???  The C file generated by lex (from the lexical analyzer
>spec you feed in) has the following line hard-coded:
>
>	FILE *yyin = {stdin}, *yyout = {stdout};

This is a rather unfortunate idea from those responsible -- I have
discovered some C compilers that don't even *support* initialization
of pointers (dumb linkers?) so it should have been taken care of
by the main supplied in the lex & yacc libraries.

If *you* put another initialization into your lex file (which
you *could* do) you would probably get a compiler error (double
definition). 

The cleanest solution is therefore to write your *own* main
and do the assignments yourself.

-Kym Horsell

gwyn@smoke.BRL.MIL (Doug Gwyn) (08/31/90)

In article <3918@bingvaxu.cc.binghamton.edu> vu0310@bingvaxu.cc.binghamton.edu.cc.binghamton.edu (R. Kym Horsell) writes:
-In article <1990Aug29.172830.14348@cunixf.cc.columbia.edu> gld@cunixd.cc.columbia.edu (Gary L Dare) writes:
->The C file generated by lex (from the lexical analyzer
->spec you feed in) has the following line hard-coded:
->	FILE *yyin = {stdin}, *yyout = {stdout};
-This is a rather unfortunate idea from those responsible -- I have
-discovered some C compilers that don't even *support* initialization
-of pointers (dumb linkers?) so it should have been taken care of
-by the main supplied in the lex & yacc libraries.

Perhaps whoever coded the template assumed that you'd have a C
compiler that worked properly.

-If *you* put another initialization into your lex file (which
-you *could* do) you would probably get a compiler error (double
-definition). 

No, just do the initialization dynamically rather than statically.

You should basically NEVER use the main() routines from the lex & yacc
libraries.  They were provided just for wimps.