Question about Lex
Martin Weitzel
martin at mwtech.UUCP
Fri May 31 20:32:37 AEST 1991
In article <1991May29.081912.16808 at fel.tno.nl> 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.
>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 at mwtech.UUCP, voice: 49-(0)6151-6 56 83
More information about the Comp.lang.c
mailing list