由于编译原理课程设计中要求使用lex和yacc编写一个程序设计语言的编译工具,老师虽然提供了基本的lex和yacc,可是还是想自己动手。尝试了好多最后感觉UnxUtils这个Windows下的Unix工具包用起来还不错。
首先下载到UnxUtils和UnxUpdate后,将两者合二为一,并把其中/usr/local/wbin的绝对路径加入path中,然后就可以通过命令行来使用flex和bison。
使用flex把l文件转换成c文件后,将/usr/local/lib下的libfl.lib 和 libfl.a 放在为c文件所建工程的目录下,然后在VC下的菜单:工程->设置->Link标签的对象/库模块中添加libfl.lib 和libfl.a 即可.
对于bison,要把/usr/local/share/目录下的两个文件bison.hairy 和 bison.simple的路径添加到环境变量中,即在环境变量中添加如下两项:
名称 | 值 |
BISON_HAIRY | 自己的实际路径/UnxUtils/usr/local/share/biosn.hairy |
BISON_SIMPLE | 自己的实际路径/UnxUtils/usr/local/share/biosn.simple |
Notice:
用flex转换成的C程序在编译时会出现警告exit等函数未定义等,是由于没有引入stdlib.h这个头文件。
另外,在C程序段写的函数最好在声明段先声明一下。
下面是自己参照的一篇设置:
Using Flex in Visual Studio 6 (VC++)
This tutorial is for users who are already familiar with Visual Studio VC++ and flex.
- Create a new workspace and add your lex file (ie lexer.l)
- Save the following files in the same directory as your workspace: flex.exe , libfl.lib, libfl.a
- Go to Project/Settings/ and select your project in the explorer window at the left.
- Select the Link tab. In the "Object/library modules" section, append libfl.lib and libfl.a to the end of the list.
- If your lex file reads input from the command line, (such as a filename like inputfile.txt), then select the Debug tab and add the command line parameters to the "Program Arguments" field.
- Return to the left hand explorer window and expand your project. Select your lex file (ie lexer.l)
- Under the "General" tab, select Always use custom build step.
- Under the "Custom Build" tab, place flex followed by the name of your lexer file into the "Commands" box (ie flex lexer.l)
Then place this text into the "Outputs" box: lex.yy.c - Build your project once (Control-F5). You should get the message "Cannot execute program".
- The previous step should create a file called lex.yy.c in your workspace directory. Add this file to your project.
- I suggest you add the input file (if your lex program requires one) to your project as well so that you can easily edit it, but this is not required.
- If you get an error that looks something like:
lex.yy.obj : error LNK2001: unresolved external symbol _yywrap
Debug/compiler.exe : fatal error LNK1120: 1 unresolved externals
This means you are not linking in the library files correctly. Check step 2, 3, and 4 again. - The next time you build (Control-F5), your project should run. If it doesn't, send me a message and I'll see if I can help.