Happy Mittal
Happy Mittal

Reputation: 3747

Generating output from flex generated executable

I am having trouble in using flex.
I wrote a C file happy.c as

int num_lines = 0, num_chars = 0;

%%
\n      ++num_lines; ++num_chars;
.       ++num_chars;

%%
main()
    {
        yylex();
        printf( "# of lines = %d, # of chars = %d\n",
                         num_lines, num_chars );
        }

Then I gave the command flex happy.c which produced lex.yy
Then I gave the command gcc lex.yy.c -lfl which generated a exe file.
But I don't know how to use this exe file. For ex : When I gave the command

./a test  (here test is a simple txt file)

It produced nothing. I mean program seems to hang.

Please tell me how to use the exe file.

Upvotes: 0

Views: 407

Answers (1)

Bart Kiers
Bart Kiers

Reputation: 170188

Try this instead:

%{
int num_lines = 0, num_chars = 0;
%}
%%
\n      { ++num_lines; ++num_chars; }
.       { ++num_chars; }

%%
main()
{
  yylex();
  printf( "# of lines = %d, # of chars = %d\n", num_lines, num_chars );
}

and then:

bart@hades:~/Programming/GNU-Flex-Bison/lexer$ flex happy.c 
bart@hades:~/Programming/GNU-Flex-Bison/lexer$ gcc lex.yy.c -lfl
bart@hades:~/Programming/GNU-Flex-Bison/lexer$ ./a.out < test
# of lines = 2, # of chars = 8

where test contained:

foo
bar

Upvotes: 1

Related Questions