views:

62

answers:

1

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 ?

+1  A: 

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.

Stargazer712
Thanks for the hint