Re: Where I can find examples using flex++ and bison++ for Microsoft Developer Studio ?

librik@netcom.com (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@pophost.eunet.be (NcsDrd) (2000-04-14)
Re: Where I can find examples using flex++ and bison++ for Microsoft D librik@netcom.com (2000-04-20)
| List of all articles for this month |

From: librik@netcom.com (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" <ncsdrd@pophost.eunet.be> 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
        http://www.monmouth.com/~wstreett/lex-yacc/lex-yacc.html
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 y.tab.c 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
Outputs: y.tab.c
y.tab.h


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++ ->
FileExtensions
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.
main()
{
yylex();
return 0;
}


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


main()
{
yyparse();
return 0;
}


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


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





Post a followup to this message

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