1

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).

8
  • 1
    Not sure if it's relevant but your makefile looks a bit odd. -lfl probably shouldn't be a dependency and -ocount_words should be -o count_words (note the space). Commented Jun 16, 2024 at 14:46
  • 4
    Also, doesn't yylex read from stdin by default? If so your executable is blocking reading from its standard input. Commented Jun 16, 2024 at 14:50
  • @G.M. Your comment regarding stdin solved the issue. Commented Jun 16, 2024 at 16:02
  • 1
    If you specify -lfl as a dependency make will try to create/update a file named -lfl based on any rules it finds -- so I'm surprised the build succeeds. Re. stdin: it sounds like you really need to brush up on your C before going any further. Try echo 'This is a test fue' | ./count_words . Commented Jun 16, 2024 at 16:04
  • @G.M. I was also surprised to see -lfl as a dependency in the Makefile. But that's what the author does in the book (page 5) if my reading is correct: github.com/Vauteck/docs_utils/blob/master/autotools/… Commented Jun 16, 2024 at 16:28

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.