File I/O with lex/yacc
Thomas Tornblom
thomas at uplog.se
Thu Aug 30 18:38:01 AEST 1990
In article <1990Aug29.172830.14348 at cunixf.cc.columbia.edu> gld at 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 at uplog.se
Snail mail: TeleLOGIC Uppsala AB Phone: +46 18 189406
Box 1218 Fax: +46 18 132039
S - 751 42 Uppsala, Sweden
More information about the Comp.unix.questions
mailing list