Search code examples
windowstext-filesbisonflex-lexer

How to compile a lex file on windows?


I have correctly downloaded and installed flex, bison and Dev C++ in C:\ . Then I tried to compile myfile.l in command prompt, but it gives the error:

flex: can't open myfile.l.

What is the problem?


Solution

  • I can see from your question that you are a beginner in need of some instruction, so what follows is in tutorial style. I hope you don't mind the tone. Many of my students encounter the same problem as you did when first starting.

    The files containing the code for flex, bison or the compiler gcc (or g++) are just text files, and not some specially encoded form of file. They are only named something.l, something.y and something.c (or something.cpp) by convention. We could just call them something.txt or even something.l.txt if we wanted to. The reason they are named the way they are is to enable all the different components of one program to be distinguished without cluttering up the name space. So, if we have a project, such as some homework done in flex and bison, we can use the word homework as the base name and have the following file set:

    homework.l     <-- The lexer  source file for flex  created for the homework
    homework.y     <-- The parser source file for bison created for the homework
    homework.cpp   <-- The C++    source file for g++   created for the homework
    homework.obj   <-- The object file created by g++
    homework.exe   <-- The final windows executable created by g++
    

    (There will be many other files as well, but I'll skip over that for now).

    As the source files are just forms of a text file they can be created by a text editor. You indicated you are using Dev C++ which is an IDE (Integrated Development Environment) which combines a text editor, a compiler and a debugger into one package. The text editor part works just like any other text editor, such as Notepad, NotePad++, vim, emacs or one of the myriad of other editor tools.

    Commonly, by default, a text editor tool on Windows will save a text file with the postfix language code of .txt. Here is an example dialogue of saving such a file:

    image of notepad++ save as file browser

    You can see that if I saved my file, which I called SOlexer.l by pressing the     Save     button, I would get a file called SOlexer.l.txt because the default is to always make a file withe the suffix of .txt.

    Now this does not need to be a problem. If I tried to invoke flex, as you did:

    $ flex SOlexer.l
    flex: can't open SOlexer.l
    

    I would get the same error message. However I can just call flex with the actual file name used and it would work:

    $ flex SOlexer.l.txt
    $
    

    Alternatively, I could rename the file, and then use it:

    $ rename SOlexer.l.txt SOlexer.l
    $ flex SOlexer.l
    $
    

    Problem solved!

    However, as you have discovered, it is best to just create the file with the desired (and more convenient) name in the first place. To do this one has to just make a different selection from the menu when saving the file, like this:

    image of notepad++ save as file browser pull down

    If we click on All types (*.*) we can create the file without the .txt suffix. This should work for most similar tools also.

    To help my students who had difficulty using flex and bison on Windows I made a series of video tutorials. You are welcome to use them also.

    In conclusion, although you had trouble getting your flex file to build, your problem is nothing to do with flex or bison, but a simple beginners problem with learning how to create and edit files on a Windows system.