Jan 1, 2014 • 2 min read

Easier Flex

Flex is a tool that is widely used in the “Design of Compilers Lab” of 6th Semester Computer Science and Engineering.

I am writing this post, so that one can compile in just one line.

In general we use commands like,

$ flex abcd.l > abcd.c
$ gcc -o abcd abcd.c -lfl
$ ./abcd

But this seemed somehow redundant. For example, in the below command, if I forget to give the ‘> abcd.c’ , the file automatically gets written into lex.yy.c, and keeps getting overwritten. In general, we hardly even look at the ‘.c’ file.

$ flex abcd.l > abcd.c

So now, we will see how to compile in just one line.

int c;
%option noyywrap
. {c++;}

int main(char *argc, int argv)
    return 0;
$ make abcd

And the output of the above command is below,

lex  -t abcd.l > abcd.c
cc    -c -o abcd.o abcd.c
cc   abcd.o   -o abcd
rm abcd.c abcd.o
$ ./abcd

and, we get the code running.

Explanation to why we add the one extra line to the source lex

Hope this post helped. You can write to the author at

Guest post by: Prashant Anantharaman (@Prashant Anantharaman)