Question about Lex
Ger Timmens
gtir5 at fel.tno.nl
Fri May 31 18:34:23 AEST 1991
gtir5 at fel.tno.nl (Ger Timmens) writes:
>This is what I do:
>1. lex lexcommands /* ==> lex.yy.c */
>2. cc lex.yy.c /* ==> a.out */
>3. a.out < input > output /* ==> output */
>I've got the following problem:
>When I encounter a string in the file *input* I want to
>generate an error message reporting the line number and
>file.
[deleted text]
Here are the solutions:
The line number: use yylineno (a global Lex integer).
The file name: instead of redirecting the input and output,
you use fixed file names. So
3. a.out < input > output
becomes
3. a.out input output
And you connect Lex's stdin and stdout (yyin and yyout) to these files:
So you get the following Lex file: (Thanks to you all) !!!!
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
%{
char InFile[20];
char OutFile[20];
%}
%%
"string" fprintf(stderr,"Found %s at line %d in file %s\n",
yytext,yylineno,InFile);
%%
void main(int argc, char **argv)
{
if (argc != 3)
{
fprintf(stderr,"Usage: %s <input-file>", argv[0]);
fprintf(stderr," <ouput-file>\n");
exit(1);
}
strcpy(InFile, argv[1]);
strcpy(OutFile, argv[2]);
yyin = fopen(InFile, "r");
yyout = fopen(OutFile, "w");
yylex();
fclose(yyin);
fclose(yyout);
}
More information about the Comp.lang.c
mailing list