Install Bison Flex Cygwin

This post is for all those guys and babes out there who could’nt figure out how to compile their lex and yacc programs in windows. Students in my college install a whole Linux operating system to do their compiler lab practices. Why do it the geeky Linux way if it can be done under Windows? Also, why waste your hard disk space and time(for loading the Linux OS) to do such a simple task.

  1. Install Bison Flex Cygwin 2
  2. Install Bison Flex Cygwin 8
  3. Install Bison Flex Cygwin Download
  4. Install Bison Flex Cygwin Free
  5. Install Bison Flex Cygwin X
Install

To begin with, you must download the files below first(around 400KB in total). All the files should be in the same directory(flex.exe is standalone but bison.exe is not).

If not, flex and bison both use the standard GNU build process, so to install them. For Windows users, both bison and flex are included in the Cygwin Linux. Lex and Yacc(Bison) in Windows. Tags: bison, flex, windows. Their lex and yacc programs in windows. Students in my college install a whole Linux.

http://www.monmouth.com/~wstreett/lex-yacc/bison.hairy (Sorry for the trouble guys!!! The link is now corrected)

Using flex and bison in MSVC. Flex bison libtool make gcc-core gdb ddd ctags colorgcc gperf libisl-devel libisl-doc libmpfr-devel libmpc-devel libmpc-doc libgmp-devel libgmp-doc binutils automake autoconf autoconf2.5 automake1.15 libcairomm1.01-devel libcairomm1.01-doc libcairomm1.01 libelf-devel libncurses-devel aspell-en enscript less source-highlight ghostscript gv.

Another requirement is that you must have a c/c++ compiler(like Turbo C++ ) installed in your system. I believe everyone who has reached compiler level programming will have a c/c++ compiler installed in their system(duh!).

Now you can start your work.

1. Create a lex program and save it(like ‘x.l’ or ‘x.txt’)

2. Reach your directory through command prompt.

3. Call flex by “flex <filename>” (eg: “flex x.l”)

4. If your lex input file doesnt have any errors, a file name “lex.yy.c” will be generated.

5. I suggest you rename “lex.yy.c” to some other more sensible name like “lex.c”, because double extensions is not a good idea in windows environment. Move the file to the folder of your compiler.

6 . Call the compiler and input lex.c to it (eg: “tcc lex.c”, where tcc is the Turbo C compiler; use any C/C++ compiler you have). If what you have is a more sophisticated environment like visual studio, you have to do the normal procedure to get a program to compile. If you use Turbo C, an object file “lex.obj’ and the executable “lex.exe” will be generated if there are no compilation problems

7. Thats it. Now just run lex.exe. Its your lexical analyser.

It is very similar to the way you do it in linux.

All the seven steps look like a complicated procedure. I created a batch file(.bat files similar to shell scrpits in linux) , to do the whole job from calling flex to get the lex.exe up and running. My batch file(rflex.bat) looks like below.

flex input.txt
ren lex.yy.c lex.c
copy lex.c f:tcbin
f:
cd tc
cd bin
tcc lex.c
lex
i:
cd “Compiler lab”

del lex.yy.c
del lex.c

And i called this batch file from “i:Compiler lab” command prompt, the place where my flex .exe is stored. The path of my compiler is “f:tcbintcc.exe “. The batch file will get much smaller, if your compiler’s path is included in system paths or environmental paths or variables. By the way, your lexical analyser will stop after you press ctrl+z(^z character) and press enter(similar to ctrl+d in linux).

So you will have to modify the above batch file if want to use it in your environment. After successfully making the batch file all you have to do is make your input file and call the batch file from your flex and bison folder.

To do yacc or bison, you have to do a similar procedure. I leave that part to you. If you can’t figure it out, just post a comment requesting how to do it. I have successfully created a batch for that too. For more information, click the link below.

Install Bison Flex Cygwin

Leave a Reply

Hi
I have just begun working with FLex and find that although my lex.yy.c file has been generated, upon renaming(nam.c )and compiling using Turbo C++ 3.0 I am confronted with the following linker error

Undefined symbol _yywrap in module nam.c

Is there a way out of this?

Hi
I have just started working with Flex and find that although lex.yy.c is being generated ,Renaming (nam.c) and compiling it with Turbo C 3.0 I am confronted with the following linker error
Undefined symbol _yywrap in module nam.c

Is there a way out of this?

After the generation of the lex.yy.c and after renaming to lex.c and compiling it with c compiler got the error of undefined reference to yywrap

I have downloaded files you said. I made the lex files to produce an .exe but in yacc files i get always errors. I procude the .c file by typing bison blabla.y but then i get errors the i dont declare yyerror and that kind of things. Could you please provide the commands i need to type in order to create an .exe ?

Install Bison Flex Cygwin

Thank you

Hi, my name is Joe Garvey, a mature student in Cork University College, Ireland. I must consider using flex and bison in a college project. I’ve downloaded the flex.exe and bison.exe and the bison.simple- but when I try to download (save target as) bison.hairy it saves automatically as bison.simple Any idea as to what if any suffix I should force on the bison.hairy to get it usefully downloaded. This system here is an intel duo core Dell with Windows Vista Business onboard. Do you know of any issues with the Vista consideration? I believe I have a DJGPP compiler onboard here, working, and before I travel headlong into setting this lot up, it would be nice to know of any Vista issues (there have been so many!). Kind regards, and many thanks for your clear and useful webpage etc. Joe.

Hello,

So I have the file lex.yy.c and it works fine. But I have absolutely no idea how to proceed with Bison. Could you possibly describe the procedure for Bison like you’ve done with Flex?

Awesome man. This will give me yet another reason to delay installing Linux on my PC:)

Hi,
>>
I am confronted with the following linker error: Undefined symbol _yywrap in module nam.c
Is there a way out of this?
<<
Yes, there’s simple way out of this.
Write your yywrap() function
OR
Just put
%option noyywrap
in your flex source file.
-Damodar

I would be hesitent downloading .exe files. Moreover this version of bison and lex is quite old, and does not support c++ interface. Does anyone know how to build bison under windows e.g. VS and not under cygwin?

hi,
i follow your instruction but when i compile it some link error occur.what can i do for it?

Free

I tried every link and none of them are working!!!

It seems may of you meets the following linker error:

lex.yy.obj : error LNK2019: unresolved external symbol _yywrap referenced in fun
ction _yylex

this is why the .l file doesn´t supply a version of yywrap()
try to type:

%option noyywrap

at the beginning of the .l file (in the definitios part)

hi,
i have a question.how do run bison.exe? is that the same way of running flex.exe. if so then how do we parse the input. do we need to run the exe file generated by flex and bison together or first do we need to run exe file generated by flex or bison. how does it works. can anyone give me detailed steps to run this exe as parser. expecting your reply at the earliest. thanks.

Raj,

it seems you have mixed up the way flex and bison work. actually they DO NOT return any exe file, but c source files, to be after compiled and linked together with the rest of your code.

here comes general steps to follow:

1)you have written two files:
lexer.l containing your lexical rules
parser.y containing gramar rules

2)run flex.exe by typing
flex.exe lexer.l
you´ll get a file called lex.yy.c

3)after you run bison
bison -d -o y.tab.c parser.y
and you will get the file y.tab.c and the corresponding header file y.tab.h
please note that i used the “-o” option to call the output y.tab.c

4)you compile and link your main.c and the files you got. with msvc you tipe:
cl lex.yy.c y.tab.c main.c /Femain.exe /DMSDOS

/Fe is an option to call the output main.exe
/DMSDOS is an option to define the macro MSDOS which is essential to don´t have error

have funn!
Pier

Hey I can do LEX prog with this but I’ve prob with Yacc(Bison).

From cmd I executed “yacc -d calc.txt” & “lex cal.txt” & 3 files name “calc.txt_tab.c” also header file & lex.yy.c are created.

But How to run them in XP with TurboC & cmd.
Like in Linux the command is gcc calc.tab.c lex.yy.c -ly -ll
& then ./a.out.
How to do with XP?…plz rpl.

hey can you send me a sample lex program that i can use to test? if the compiler and flex perfectly works. because the compiler gives me an “declaration syntax error” while compiling for the program starts with %{. so kindly help 🙂

I get this error… Undefine symbol _main in module c0.ASM… Pls tell me how to correct it…

I got an error message while running the program through compiler unresolved external symbol _yylex

download bison.zip from here it works.

will dev-c++ work for this??i mean im not able to install turbo-c++ so im using dev…
and do u ve ab solution for this??windows 7 64-bit m/c ,not able to find an appropriate turbo-c.

thanks a lot
Even i thought y shud i install a whole new OS for just 2-3 assignments

Hi.
I’ve lost my old compiler flex. I’ve download it from this page. My Error is :
flex.exe : input rules are too complicated. I had before a file named flex254a.exe. I dont find this. May you Help me. Thanks

Hi,
I have done what procedure you have told , but when I compile lex.yy.c in turbo C ,it gives me error “UNABLE TO OPEN INCLUDE FILE blablah.h”

With Flex in windows i get the folowing error flex : input rules are too complicated = 32000 NFA: Can You Help Me?
Thanks.

I am getting the following error:

Error 1 error LNK2001: unresolved external symbol _yylval G:PROJlex.yy.obj

Sir, I Have “Borland Turbo C++ 4.5” installed on my system, then how to execute the lex file through command prompt?please do answer!

Install py2exe python 2.7. hi,
i have downloaded all those file which u have been posted,after then i kept in seperate folder named “lex” in C Directory.
i went cmd prompt in that i entered like below c:lex>”flex x.l”
and i kept sample program namely X.l in that same folder.but i did’t get any thing.simply it tells command is not recognised. i’m using windows7

very good help file but my program said cant opne the file , what i have to do ? i run it windows 7

Also try the Tiny C Compiler together with Bison/flex.
Great tips! Nice blog.

am getting a error as “flex cant open file”…any solution.

‘tcc’ is not recognized as an internal or external command,
operable program or batch file.
i’m gettin this error help me out


Recommended reading

  • flex & bison: Text Processing Toolsby John Levine.LookInside

Affiliate disclosure: we get a small commission for purchases made through the above links

This is part two of a series explaining how to convert example lex and yacc code from Lex & Yaccby Levine, Mason and Brown [1] to work on modern systems using flex and bison.Part one looked at compiling on a modern Linux system.This page explains how we made it compile without error on our Windows 7 system using Microsoft Visual Studio 2008 (MSVC++9).Note: This page is a bit out of date now, being written for MSVC 2008. See the note on Update for MSVC 2010+ below.

Most solutions we've seen for MSVC involve processing the flex and bison files on a Linux system like Cygwin and then copying the resulting .c and .h files across to MSVC to be compiled. Our solution here lets you put your .l (or .flex) and .y files directly into your MSVC project folder, edit them at will, and then process and compile it all with a single click.

YMMV. No doubt there are alternative and better ways to do it. Please let us know your suggestions.

Update for MSVC 2010+:We were surprised when we upgraded to MSVC 2013 and found that everything we described here just worked for our old MSVC 2008 projects.However, if you are starting with a later version of MSVC, you may find these instructions atWin flex-bison:setup custom build rules for Visual Studio 2010 and up of more use.
Book: There is a more up-to-date book than the one we originally used:Flex & Bisonby John Levine [2] and a better download with more recent versions of Flex and Bison.
The changes to Flex and Bison mean that some of our original settings need to be modified. The graphics below may not reflect these changes.
30 May 2014: See the comments below from Alex Zhondin ('lexxmark') who is the owner of the winflexbison project at sourceforge.Thanks, Alex.

Contents

  1. Setting up flex and bison on Windows - a one-off
  2. Add Custom Build Rules for flex and bison files - another one-off

Setting up flex and bison on Windows

Old instructions (superseded)

  1. Download and install the latest GNU binaries for Windows:
    • Flex for Windows.
    • Bison for Windows.
    • In each case download the 'Setup' file for the 'Complete package, except sources'. As at March 2013, these should be flex-2.5.4a-1.exe (1.2 MB) and bison-2.4.1-setup.exe (3.7 MB). These files have not been updated for a while.
  2. Important: Install these packages in the new folder C:GnuWin32, not the default folder with 'Program Files' in the name. You must not have any spaces in the installation folder name or you will have problems. So when the setup program asks for the installation folder name, you must change it to a name without spaces. We suggest C:GnuWin32.

A new and better download

  1. Download the latest Win flex-bison package from sourceforgewin_flex_bison-latest.zip. The version we downloaded (648 kB, last updated 2013-11-22) contained win_flex version 2.5.37 and win_bison version 2.7.
  2. Unzip and copy the whole folder somewhere convenient. Do not store in a folder with spaces in the path.We use C:GnuWin32win_flex_bison. Make sure you include the subdirectory data which includes the m4sugarstuff.

Create a new project in MSVC++

  1. In Microsoft Visual Studio create a new project: Visual C++ > Win32 > Win32 Console Application.
  2. Select the minimum application settings: Console application and Empty project.

We will make some more changes to the configuration properties below after we've added our files.First we must do a one-off and add the custom build rules for flex and bison files.

Add Custom Build Rules for flex and bison files

You only need to do this step once and it is set for all subsequent projects.

  1. Right-click on the project in the Solution Explorer and select Custom Build Rules....
  2. Click on New Rule File...
  3. Set:

    • Display Name to Flex and Bison Tools
    • File Name to FlexBison.rules
    • and enter a Directory whichyou know will be backed up and be available for future projects.

    We will use this 'rules' file in all our flex and bison projects.Save this file by clicking OK and then edit it again using Modify Rule File.

  4. Add two build rules using Add Build Rule, one for Bison and one for Flex. Important: You should add Bison first.

    The important entries for Bison are:

    • Name = Bison
    • Command Line = C:GnuWin32binbison.exe -d -v -b y $(InputPath)
    • Command Line = C:GnuWin32win_flex_bisonwin_bison.exe -d -v -b y $(InputPath)
    • File Extensions = *.y
    • Outputs = y.tab.c,y.tab.h

    The important entries for Flex are:

    • Name = Flex
    • Command Line = C:GnuWin32binflex.exe $(InputPath)
    • Command Line = C:GnuWin32win_flex_bisonwin_flex.exe --wincompat $(InputPath)
    • File Extensions = *.l;*.flex
    • Outputs = lex.yy.c

    Note the --wincompat option for win_flex.

  5. Save all your changes. You should have Custom Build Rule Files that look like this for 'Flex and Bison Tools':

More changes to the Configuration Properties

You need to make these changes for every project.

  1. Make sure the General Configuration Properties have Character Set = Not Set.We want to use the ANSI/ASCII character set, not Unicode.
  2. Go to the C++ Preprocessor screen (Configuration Properties > C/C++ > Preprocessor)and add the Preprocessor Definitions_CRT_SECURE_NO_DEPRECATE and_CRT_NONSTDC_NO_DEPRECATE.

    These avoid more annoying but irrelevant warning messages from MSVC. Incidentally, you can do most of this setup as default: seeMSVC++: Getting rid of that _CRT_SECURE_NO_DEPRECATE warning.

  3. Go to the C++ Advanced Properties screen (Configuration Properties > C/C++ > Advanced) and set Compile As = Compile as C Code (/TC) and Disable Specific Warnings = 4102.

    The 4102 warning we disable is caused by the automatically-generated code [The label is defined but never referenced].This is generally one of those warnings you can safely disable without fear of hiding any real errors you might make in your code.

    Update December 2013: The 4102 warning does not seem to happen with the new win_flex and win_bison.Instead we get [warning C4715: 'lookup' : not all control paths return a value].If you wish, you can change the 4102 above to 4715 to suppress this message, but be aware that this is not such a safe warning to turn off!
  4. Go to the Linker Input screen (Configuration Properties > Linker > Input)and set Additional Dependencies = C:GnuWIn32liblibfl.a C:GnuWIn32libliby.a. Use a space as separator between the two entries.
    Update December 2013:This step is probably unnecessary with the latest win_flex and win_bison, providing you provide your own main() and yyerror() functions, which you should do anyway.And you can avoid providing a yywrap() function by setting %option noyywrap in your flex file.

    These library files (they are named '.a' but behave like '.lib' files) provide very minimal default functions.The library libfl.a provides yywrap() and main(),and liby.a provides yyerror() and its own version of main().In practice, you are probably better off providing your own versions.If you provide your own functions, you don't need to refer to these two libraries.

Add two wrapper files to your project

Add these two one-liner wrapper files wrapper-flex.c and wrapper-bison.c to your project.

These make it easier to handle the frequently-changing lex.yy.c and y.tab.c files.Note this relies on bison using the '-b y' option (see the note below).The disadvantage of this technique is that you must always keep each set of project files in separate folders.

Add the .l and .y files to your project and compile it

Finally, we can add our flex and bison files to the MSVC project.In this case we add our modified ch3-03.l and ch3-03.y files and flex_memory_fix.h(zipped files here, 2 kB).When we add these files, with the .l and .y extensions, MSVC should prompt us to use the FlexBison.rules custom build rules we made earlier.

Bison

Install Bison Flex Cygwin 2

Keep all these files (ch3-03.l, ch3-03.y, flex_memory_fix.h, wrapper-bison.c, wrapper-flex.c) in the same project folder.The files (lex.yy.c, y.output, y.tab.c, y.tab.h) are created automatically, so don't mess with them.

Update December 2013:The file flex_memory_fix.h is no longer needed with the latest version of win_bison.

If all has gone correctly, you should be able to compile this project. (It might take two goes the first time.)On our VS2008 system, we get this output:

Note the 'Running bison' and 'Running flex' lines in the output. These are the values in 'Execution Description' we set when we created the build rules. Note it is important that bison runs first before flex.

A note about the '-b y' option for bison

The -b y option we set for win_bison makes sure the names of the output filesare always y.tab.h and y.tab.c (instead of foo.tab.h where foo.y is the name of yourbison file). This is convenient for us in the setup above. However, you may need to change your flex foo.l file if it refersto a file like foo.tab.h and make that y.tab.h

A final twist for C++ and unistd.h

There is one more twist to all the above if you actually want to compile C++ code with flex and bison (as opposed to C code with the /TC option).If you change the C/C++ > Advanced > Compile As option back to Compile as C++ Code (/TP),then you will run into problems where the automatically-generated source code looks for but cannot find unistd.h.

As far as we know, this include file is not available in MSVC/Windows. A copy that works on Windows is in the GNU folder C:GnuWin32include. One fix is toadd C:GnuWin32include to the VC++ Directories Include Files in Tools > Options > Projects and Solutions.

References

  • [1]Levine, John, Tony Mason, and Doug Brown. Lex & Yacc, 2nd Edition, O'Reilly, 1992
  • [2]Levine, John.Flex & Bison: Text Processing Tools, O'Reilly, 2009

Comments by the winflexbison project owner

Hello everyone!
I've just seen your article athttps://www.di-mgt.com.au/flex_and_bison_in_msvc.htmland have two comments.

First of all I'm owner of the winflexbison project at sourceforge andglad to see my project is useful for you.

  1. At the end of the article you noted about 'unistd.h' include problem. Ithought I resoled this problem with '--wincompat' key. You can see littleexplanation in UNISTD_ERROR.readme file in win_flex_bison root folder. Ifthere is still problems with 'unistd.h' include, please let me know - itshould be fixed.
  2. Also you noted about warning C4715 'not all control paths return avalue' in generated source files. I think I should fix it too.

Hi Alex,Great to hear from you. Your project is really good.

Some of the comments on the page refer to my attempts with the earlier version. I think the unistd.h problem related to that. To be honest I do everything in plain ANSI C, and never use the C++ options, so I wouldn't have noticed the effect of using the --wincompat option on the new version. I'll fix up the comment in due course. Our ISP is moving the site over to a new server at the moment and taking their time over it, so we cannot make any changes for the time being.

The other warning about control paths is mildly irritating, and as a rule I don't like turning off warnings. If you could fix it it would be great. But it's not critical.

The product is really great. Please keep up the good work.

Dave | Moderator - Mar 25 05:40 AM 2014

Recently I was asked to add custom build rules for VS2010 and upper to winflexbison package (see http://sourceforge.net/p/winflexbison/discussion/general/thread/6c305702). So you can use those ruleset if you will migrate from VS2008.

Install Bison Flex Cygwin 8

During creation rules I've noticed a little problem with invoking win_flex.exe or win_bison.exe tools from VS build system. May be you will be interested. The invoking working directory is not the same as *.y or *.l files placed in. So generated C code have #line directives with absolute paths to source grammar file. It can be critical if you place generated files under version control system and on different machines you have different paths.

Install Bison Flex Cygwin Download

I hope I describe the problem more or less clear. To fix it I had to change simple invoke command like:

to more magic one:

This will setup correct working directory and #line directives will be relative then.Though for learning purposes this is not so critical.Best wishes,

Contact

Install Bison Flex Cygwin Free

To comment on this page or to contact us, please send us a message.If you send us a relevant comment for this page, we'll post it here. Just mention 'flex_and_bison_in_msvc' in your message.

Install Bison Flex Cygwin X

This page first published 22 March 2013. Last updated 30 June 2020.