I run the following commands to execute a flex program

flex myname.lex
gcc lex.yy.c -lfl
./a.out

But then the terminal doesnt display any output. Nor it returns to the main prompt.The program doesnt have any errors.

Thanks in advance..

Recommended Answers

All 4 Replies

What should the program output? Can we see some code? What does the -lfl option(s) do?

In general, a lexer expects input. If you do not specifically write a prompt to display it will just quietly wait for input.

The code is just an example.

/*
 *
 * myname.lex : A sample Flex program
 *              that does token replacement.
 */

%%

%NAME     { printf("%s",getenv("LOGNAME")); }
%HOST     { printf("%s",getenv("HOST"));    }
%HOSTTYPE { printf("%s",getenv("HOSTTYPE"));}
%HOME     { printf("%s",getenv("HOME"));    }

%%


/*myname.txt*/
Hello, my name name is %NAME. Actually 
"%NAME" isn't my real name, it is the
alias I use when I'm on %HOST, which
is the %HOSTTYPE I use. My HOME 
directory is %HOME. 

Then I used following commands :
flex myname.lex
gcc lex.yy.c -lfl
./a.out myname.txt

What is wrong in this?

The executable created by lex does not take any command line arguments and if you give it any, it will silently ignore them. It reads its input from stdin and writes its output to stdout. So as L7Sqr said, it doesn't do anything because it's waiting for you to enter some input.

If you want to use your lexer on your myname.txt, you should pipe the contents of the file into your lexer using input redirection, i.e. ./a.out < myname.txt.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.