Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!mcsun!hp4nl!tnofel!felfs!gtir5 From: gtir5@fel.tno.nl (Ger Timmens) Newsgroups: comp.lang.c Subject: Re: Question about Lex Message-ID: <1991May31.083423.28748@fel.tno.nl> Date: 31 May 91 08:34:23 GMT References: <1991May29.081912.16808@fel.tno.nl> Organization: TNO Physics and Electronics Laboratory Lines: 58 gtir5@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 ", argv[0]); fprintf(stderr," \n"); exit(1); } strcpy(InFile, argv[1]); strcpy(OutFile, argv[2]); yyin = fopen(InFile, "r"); yyout = fopen(OutFile, "w"); yylex(); fclose(yyin); fclose(yyout); }