I'm on Linux Ubuntu.
I'm reimplementing the Flex scanner example provided at page 5 of "Managing projects with GNU make" by ROBERT MECKLENBURG.
count_words.c (application source code):
#include <stdio.h>
#include <stdlib.h>
extern int fee_count, fie_count, foe_count, fum_count;
extern int yylex( void );
int main( int argc, char ** argv )
{
yylex();
printf( "%d %d %d %d\n", fee_count, fie_count, foe_count, fum_count );
exit( 0 );
}
lexer.l (input file of the Flex scanner):
int fee_count = 0;
int fie_count = 0;
int foe_count = 0;
int fum_count = 0;
%%
fee fee_count++;
fie fie_count++;
foe foe_count++;
fum fum_count++;
Makefile:
count_words: count_words.o lexer.o -lfl
gcc count_words.o lexer.o -lfl -ocount_words
count_words.o: count_words.c
gcc -c count_words.c
lexer.o: lexer.c
gcc -c lexer.c
lexer.c: lexer.l
flex -t lexer.l > lexer.c
Proper tabs are inserted before each command.
Not pasting lexer.c because it's huge but here's a link: https://drive.google.com/file/d/1lN7Jr93xa5hVKIHtCZgXmGRy1u3x4eE3/view?usp=sharing
When I run ./count_words This is a test fue , I get a blank output as if there was an infinite loop.
What is wrong?
I checked that Flex is installed. It is:
$which flex
/usr/bin/flex
I checked that libfl.a is installed. It is:
libfl-dev/jammy,now 2.6.4-8build2 amd64 [installed]
static library for flex (a fast lexical analyzer generator)
I did notice that the book forgets to include stdlib.h for exit() and did include it.
I tried to find issues in lexer.c but the file is too complicated for me.
The book later recommends to append the following lines to lexer.l:
.
\n
I tried it but it does not solve the problem (no surprise).
-lflprobably shouldn't be a dependency and-ocount_wordsshould be-o count_words(note the space).yylexread fromstdinby default? If so your executable is blocking reading from its standard input.stdinsolved the issue.-lflas a dependencymakewill try to create/update a file named-lflbased on any rules it finds -- so I'm surprised the build succeeds. Re.stdin: it sounds like you really need to brush up on yourCbefore going any further. Tryecho 'This is a test fue' | ./count_words.