Re: Where I can find examples using flex++ and bison++ for Microsoft Developer Studio ? (David Librik)
20 Apr 2000 01:37:16 -0400

          From comp.compilers

Related articles
Where I can find examples using flex++ and bison++ for Microsoft Devel (NcsDrd) (2000-04-14)
Re: Where I can find examples using flex++ and bison++ for Microsoft D (2000-04-20)
| List of all articles for this month |

From: (David Librik)
Newsgroups: comp.compilers
Date: 20 Apr 2000 01:37:16 -0400
Organization: MindSpring Enterprises
References: 00-04-106
Keywords: yacc, C++, Windows

"NcsDrd" <> writes:

>I really need some examples of using flex++ and bison++ for Microsoft
>Developer Studio as there are no reference books available.

Assuming you are using the (somewhat outdated) Win32 FLEX and BISON from
Here is some information I wrote up on how to use these tools from within
Visual C 6.0 (Microsoft Developer Studio). I assume you have the excellent
_lex & yacc_ book by Levine et al.

- David Librik


How to use flex and bison from VC++.

flex, bison, bison.simple, and bison.hairy should be located
in a directory on your PATH. Let's say it's C:\bin.

Unless you are writing a yywrap() function, your lex file foo.l should start:
%option noyywrap

In the initial %{ %} header there should be:
#include <stdlib.h>
#include <malloc.h>

If you have no yacc file, you must provide a main() function in your lex file.
A sample one is at the bottom of this document.

Your yacc file foo.y should have in its initial %{ %} header:
#include <stdlib.h>
#include <malloc.h>
int yyerror(char *);
int yylex(void);

Also your yacc file must contain yyerror() and main() functions in
its third section. Sample ones are at the bottom of this document.

You will need to set up your project and custom build rules as follows.

Add your .l and .y files to the project.
(You may want to set the Properties of the Source Files folder in
your project so it has ";l;y" at the end of the File Extensions)
Also add lex.yy.c and to the project,
even though they don't exist yet.

Set the Custom Build for foo.l (ALL CONFIGURATIONS):
Description: Flexing $(InputPath)
Commands: flex $(InputPath)
Outputs: lex.yy.c

Set the Custom Build for foo.y (ALL CONFIGURATIONS):
Description: Bisonning $(InputPath)
Commands: set BISON_SIMPLE=C:\bin\bison.simple
set BISON_HAIRY=C:\bin\bison.hairy
bison -d -v -b y $(InputName).y

To avoid having to put those "sets" in the commands, you can set the
environment variables in your AUTOEXEC.BAT or WinNT "Environment" dialog.

To turn on C/C++ style text editing for your .l and .y files, edit the
Registry as follows:
Run regedit
Select: HKEY_CURRENT_USER -> Software -> Microsoft -> DevStudio ->
6.0 -> Text Editor -> Tabs/Language Settings -> C/C++ ->
Modify FileExtensions to add l and/or y to the end of the semicolon-
separated list already present; e.g.
.....;tli;rc;rc2;l;y <-- You added ;l;y


Here is a very simple main() function for your lex file, to be used if there
is no yacc file.
return 0;

Here are very simple yyerror() and main() functions for your yacc file.
yyerror (char *s)
printf ("%s\n", s);
return 0;

return 0;

A better main function, which runs until EOF, might be:
extern FILE *yyin;

do {
} while (!feof(yyin))
return 0;

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.