Click here to Skip to main content
Click here to Skip to main content

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

, 11 Sep 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Create a "C" project in VS2012, then add Flex lexing and Bison parsing capability.

Introduction

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.  

Background 

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.


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


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. ");
	getchar();

    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."


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 http://sourceforge.net/projects/winflexbison/ 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 http://en.wikipedia.org/wiki/GNU_bison 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:

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. Smile | :) You should end up with this:

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

History

Revision 1.0 on 2013.9.11

License

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

Share

About the Author

pmulhrsw
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

 
QuestionHow to set breakpoints in generated C file? How to generated c++ file? PinmemberNasi(just.nasi@gmail.com)19-Oct-14 22:28 
QuestionWhy doesn't it know some Yacc's commands? PinmemberNasi(just.nasi@gmail.com)18-Oct-14 5:39 
AnswerRe: Why doesn't it know some Yacc's commands? PinmemberNasi(just.nasi@gmail.com)19-Oct-14 22:13 
GeneralMy vote of 5 PinprofessionalJeremy Falcon22-Sep-14 12:35 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 12 Sep 2013
Article Copyright 2013 by pmulhrsw
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid