Hello,
I have a source code and I would like to add line numbers.
What I've done :
%{
int lines=0;
%}
LINE \n
%%
{LINE} {ECHO;printf("%d", ++lines);}
However, I don't know how to catch the first line. Can you help me ?
Hello,
I have a source code and I would like to add line numbers.
What I've done :
%{
int lines=0;
%}
LINE \n
%%
{LINE} {ECHO;printf("%d", ++lines);}
However, I don't know how to catch the first line. Can you help me ?
Add the line:
printf("%d", ++lines);
as the first thing in main. Its a hack, but an effective one :)
Edit: The result should look something like this:
%{
int lines=0;
%}
LINE \n
%%
{LINE} {ECHO;printf("%d", ++lines);}
%%
main()
{
printf("%d", ++lines);
yylex();
}
Disclaimer: syntax from a book, not actually compiled. You might have to massage it a little bit.