开发者

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

I've read http://msdn.m开发者_开发知识库icrosoft.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.


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.


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜