How can I use flex & bison in Visual Studio 2010?
Asked Answered
F

3

12

I've read http://msdn.microsoft.com/en-us/library/aa730877%28vs.80%29.aspx but this document was for VS 2005. I stuck on the part 'Importing a .rules File in Visual C++' in the document. It seems that VS 2010 does not support .rules file, instead, it seems to use .targets file for custom build rules.

Does anyone know how to use flex & bison in VS 2010? Thanks.

Filtration answered 3/5, 2011 at 10:54 Comment(3)
Based on my confusion regarding this question; I think you meant to tag this with gnu-flex; not Adobe Flex. I went ahead and retagged it.Trifolium
Thanks for retagging, www.Flextras.com. But it seems that nobody knows the answer for my question. :-p Do I have to install VS 2005?Filtration
Versions of Visual C++ prior to 2010 used the VCBuild build system; Visual C++ 2010 moved to the MSBuild system. The targets/rules/properties are all totally different. I'm sure you can use Flex and Bison with Visual C++ 2010, but unless someone has already assembled and published new configuration instructions, it will may take quite a bit of work (and likely a fairly good understanding of MSBuild).Organicism
R
3

The good news is, that targets file works after a conversion to 2010.

  • Install the example. Open it with the vs2010 converter and it will do the conversion from the 2005 *.rules to 2010 *.targets, *.props and *.xml file
  • install cygwin, and include flex, its sourcecode, Bison and its source code
  • Add the cygwin path to the end of your Executable paths list and the path to the source to the end of your include paths list

Although the example wont run because of dependencies you are now able to create your own project and build your own lexer and parser. The only issue you will have is the use of unistd.h being included. Add this and it wont:

#define YY_NO_UNISTD_H 

I think you can get the rest of the things you need to build your parser in 2010 in the flex & bison book.

Retrogradation answered 18/5, 2011 at 17:59 Comment(2)
The only catch is capturing the output is not working. It only shows you that bison and flex are triggered and the error codes (if there are any). I am working on that though, and ill likely have the new build targets file up on my blog with a proper tutorial on the setup.Retrogradation
You don't need Cygwin to use Flex and Bison in Windows, not even for Visual Studio.Lithoid
V
1

This question is old but for Flex&Bison newbies like me: You can use "Win flex-bison" tool available from here and it also contains a tutorial how to configure VS C++ project in MSVS2010+

Note: If the link is not working, use google and search for the "Win flex-bison" ;-)

Velar answered 12/10, 2014 at 8:12 Comment(1)
Flex and Bison are moving to GitHub but I don't know where the special Windows versions are in GitHub. They still exist in SourceForge and work in VS 2015 and I hope they also work in VS 2017 or someone converts them.Lithoid
L
1

The Custom Build Rules that Jarda specifies are the easiest. The following is an alternative to the Custom Build Rules.

Don't use precompiled headers unless you know how to use them well enough to use them for this. I had a problem with it complaining about macro redefinitions and premature end-of-file, until I turned off use of precompiled headers.

Generate a "Win32 Console Application" and make it an empty project (no generated source code). For example, I am using "SimpleFlex" for my project name.

Optional: You can customize the filters for VS folders so that the Flex input file is shown in the Source Files folder. In the properties of the Source Files folder, add the extension ("l" or "lex") to the list of extensions.

Then create a file with a "l" (or "lex") extension for the project; for example, "SimpleFlex.l". In the file, use one of the samples from below. Then in the project settings, create a Custom Build Step. If you are not familiar with Custom Builds, then look for the "Custom Build Step" tab in the project settings. Use the following for the Custom Build Step:

Description: Generating lexical analyzer

Commands: C:\Software\FLEX252\flex.exe -o$(ProjDir)\$(InputName).cpp $(InputPath)

Outputs: $(ProjDir)\$(InputName).cpp

Where:

Description is actually anything you want to use

Commands Consists of the path to Flex, the output file and the input file. You will need to change the path for Flex to whatever is correct for your system.

Outputs Specifies the filename of the output file.

After providing the code for the Flex input file creating the Custom Build Step, compile the file. You can use Ctrl-F7 to just compile. Actually, at this point, you can just build the project; there is nothing for the build to do except generate the scanner (the cpp file). The custom build should execute Flex, but the only way you will know it does is because the description is shown in the Build output. The cpp file should have been generated and then it can be added to the project. If you get the errors I describe above (macro redefinitions and premature end-of-file) then turn off precompiled headers for the project.

The procedure for Bison is very similar.

Lithoid answered 14/4, 2017 at 20:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.