Click here to Skip to main content
14,872,121 members
Articles / Desktop Programming / Win32
Posted 11 Sep 2013


12 bookmarked

HowTo: VS2012 "C" project /w Flex & Bison

Rate me:
Please Sign up or sign in to vote.
4.91/5 (8 votes)
11 Sep 2013CPOL4 min read
Create a "C" project in VS2012, then add Flex lexing and Bison parsing capability.


Installing the new Visual Studio 2012, I was surprised to find no "C" project template, only C++. Googling yielded no C-only templates and few concrete examples; it seems C has fallen to the wayside... even though the compiler still supports it. So a method of creating a C project from the C++ template is provided. 

Also discussed is how to setup this project to use a Windows version of Flex and Bison - tools for lexical analysis and parsing of data streams.  


Lex and Yacc originated in the UNIX world long ago. Yacc stands for 'yet another compiler compiler', and Lex is a 'lexical analyzer'. Lex reads a file or data stream and breaks it up into its component pieces, which it calls tokens. Tokens may be numbers, keywords, strings, punctuation, etc. Yacc commonly receives this stream of tokens, structures them according to rules, then looks for matching patterns. If a match is found, some action is taken. These two tools together make it possible to parse an input stream and act on it, much more cleanly and effectively than writing hundreds of lines of traditional C code. The Lex and Bison syntax is very powerful and very compact.

Today, the GNU equivalents of Lex and Yacc are called Flex and Bison (heh!) They are still only Unix platform tools, however. Nothing quite like these have ever made it to the Win32 world. A few ports of these were made over the years, but they required lots of GNU support libraries and were not updated regularly at all. (The last Flex port I found is almost a decade old!) Of course, it was too old to work with the example used. 

But a few developers over at SourceForge have created a package called WinFlexBison. This is a compact, all-inclusive port of modern Flex and Bison for Win32, and is maintained regularly. This is the package we will use.  

As a side note, WinFlexBison can output C, C++, and Java code, but only C is used here.

Creating a Win32 "C" Project in VS2012

To create a Win32 "C" project in Visual Studio 2012:

1. Create a new project. Pick Win32 C++ Console. Give it a descriptive name. Next.
Image 1

2. Uncheck the box for "use precompiled headers" and check the box for "create empty project." Click Finish.
Image 2

3. Right-click "Source Files", click "Add" then "New Item." Call it main.c. Click it and copy this text into it:

#include <stdio.h>

int main(void)
	printf("Press ENTER to close. ");

    return 0;

4. Right-click the main project in the solution explorer at left, and choose Properties, C/C++, All Options, and set "Compile As" to "C Code."
Image 3

5. Hit F5 to compile and debug. There's your "C" project!

Using Flex and Bison in Windows

To use Flex and Bison in windows,

1. Get WinFlexBison from and extract somewhere meaningful, then add that path to the system path. If you need help with the system path, see this and this. Close any command prompts.

2. Create these new files in the project: Expression.c/.h, Lexer.l/.c/.h, Parser.y/.c/.h

3. Copy the file data from into these files (some will remain empty.) Incorporate the pause code from earlier into the end of the real main(). Be sure to SAVE all of these files as data is entered, we will need it for the next step.

4. Open a command prompt, navigate to your .Y and .L files, and run these commands:
Image 4
No error messages from Flex or Bison? Good. Can also test other settings this way. This example liked having the --wincompat option turned on.

5. Try to build the project. It will probably reveal a few warnings. You will need to work through these in a minute.

Now to automate the building of the .l and .y files:

1. Right-click Parser.y -> Properties -> "Item Type" to "Custom Build Tool."

2. Click "Custom Build Tool" on the left, and type "c:\your-path-to-winflexbison\win_bison parser.y" as the command, substituting the real path.

3. Change the description to "BISON Custom Build Tool."

4. Set "Outputs" to "parser.h,parser.c"

Repeat for Lexer.l, but with "yourpath\win_flex --wincompat lexer.l" as the command, etc.

Hit F5... Voila! Now go get them bugs. :) You should end up with this:

Image 5

Points of Interest

If you follow this guide verbatim (and the example at Wikipedia hasn't changed), there should be two "errors." One, "yyerror" will be defined twice. This is because this function appears in both Lexer.l and Parser.y. The solution is to change one of them to just:

int yyerror(SExpression **expression, yyscan_t scanner, const char *msg);

The other issue was a main.c(24): warning C4090: 'function' : different 'const' qualifiers. That's rather vague, but looking at that line of code, this simple change fixes it:
SExpression *getAST(char *expr)	// was const char, removed const.

Source Code


Revision 1.0 on 2013.9.11


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Instructor / Trainer
United States United States
Pmulhrsw has been coding off-and-on for about thirty years in Win32 Assembler, PIC Assembler, some C, and Java. I work as an Adjunct Faculty Professor by day, teaching computer science. At night, I tinker with electronics, PIC microcontrollers, and play in two bands.

Comments and Discussions

QuestionUnresolved Externals Pin
SparkyNZ3-Apr-17 18:08
MemberSparkyNZ3-Apr-17 18:08 
AnswerRe: Unresolved Externals Pin
SparkyNZ3-Apr-17 18:32
MemberSparkyNZ3-Apr-17 18:32 
Praiseworks on VC2015 and Win 10 Pin
Member 1134898721-Sep-16 3:11
MemberMember 1134898721-Sep-16 3:11 
Questionworks! Pin
Member 964836629-Jan-16 18:32
MemberMember 964836629-Jan-16 18:32 
GeneralMy vote of 5 Pin
Alexandru Lungu17-Jun-15 3:40
professionalAlexandru Lungu17-Jun-15 3:40 
GeneralEmpty project Pin
Alexandru Lungu17-Jun-15 3:37
professionalAlexandru Lungu17-Jun-15 3:37 
QuestionHow to set breakpoints in generated C file? How to generated c++ file? Pin
Nasi( 21:28
MemberNasi( 21:28 
QuestionWhy doesn't it know some Yacc's commands? Pin
Nasi( 4:39
MemberNasi( 4:39 
AnswerRe: Why doesn't it know some Yacc's commands? Pin
Nasi( 21:13
MemberNasi( 21:13 
GeneralMy vote of 5 Pin
Jeremy Falcon22-Sep-14 11:35
professionalJeremy Falcon22-Sep-14 11:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.