Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 303
David Johns Avatar asked May 03 '11 10:05

David Johns


People also ask

What does Flex actually do?

A flex container expands items to fill available free space or shrinks them to prevent overflow. Most importantly, the flexbox layout is direction-agnostic as opposed to the regular layouts (block which is vertically-based and inline which is horizontally-based).

How does flex one work?

If an element has flex: 1 , this means the size of all of the other elements will have the same width as their content, but the element with flex: 1 will have the remaining full space given to it.

Does Flex work on text?

Flex[box] is for boxes and not for texts so never make your text container a flexbox container.


3 Answers

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.

like image 99
Bob_Gneu Avatar answered Sep 22 '22 13:09

Bob_Gneu


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" ;-)

like image 20
Jarda Avatar answered Sep 21 '22 13:09

Jarda


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.

like image 26
user34660 Avatar answered Sep 21 '22 13:09

user34660