Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!mcsun!unido!mikros!mwtech!martin From: martin@mwtech.UUCP (Martin Weitzel) Newsgroups: comp.lang.c Subject: Re: Question about Lex Message-ID: <1159@mwtech.UUCP> Date: 31 May 91 10:32:37 GMT References: <1991May29.081912.16808@fel.tno.nl> Reply-To: martin@mwtech.UUCP (Martin Weitzel) Organization: MIKROS Systemware, Darmstadt/W-Germany Lines: 42 In article <1991May29.081912.16808@fel.tno.nl> 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. >However I cannot include the following in my *lexcommands* file: > >"string" fprintf(stderr,"Found *string* on line %d in %s.\n", > __LINE__,__FILE__); > >since this would report the line number in the file *lex.yy.c* ! It's the purpose of the macros __LINE__ and __FILE__ to point you to the source line - they don't have a meaning wrt the lines read at runtime. If you have a 'plain vanilla' lex (as delivered with many UNIXes), there might be an easy - yet undocumented - way: The standard input/unput-macros support a variable 'yylino' which is incremented with every line read. Of course, you can also build such a feature yourself by simply writing a rule which recognices '\n' in the input and increments an arbitrary variable. >Is there a solution to this problem ? Hope this helps. >I've experimented with #line, but I did not succeed. Also #line is not what you probably assume. It is meant to be used in conjunction with __LINE__ and __FILE__ if your C source is generated from some higher lever description. In this case #line sets the value of __LINE__ and __FILE__ to point errors to the 'original' (higher level) description. -- Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83