Using flex

本文详细介绍了如何在Windows环境下利用UnxUtils工具包配置lex和yacc,包括环境变量设置、libfl.lib和libfl.a的使用及在Visual Studio中的集成步骤,帮助读者顺利完成编译原理课程设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

由于编译原理课程设计中要求使用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.

  1. Create a new workspace and add your lex file (ie lexer.l)

  2. Save the following files in the same directory as your workspace: flex.exe , libfl.lib, libfl.a

  3. Go to Project/Settings/ and select your project in the explorer window at the left.

  4. Select the Link tab. In the "Object/library modules" section, append libfl.lib and libfl.a to the end of the list.

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

  6. Return to the left hand explorer window and expand your project. Select your lex file (ie lexer.l)

  7. Under the "General" tab, select Always use custom build step.

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

  9. Build your project once (Control-F5). You should get the message "Cannot execute program".

  10. The previous step should create a file called lex.yy.c in your workspace directory. Add this file to your project.

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

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

  13. 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.
编译器构建是一个复杂的过程,需要使用一些工具和技术来实现。而Flex和Bison是非常流行的用于构建编译器的工具之一。 Flex是一个用于生成词法分析器的工具,它基于一组正则表达式规则,将输入的字符流分解成一个个的词法单元。通过定义一些规则,Flex可以将输入的源代码转化成一系列的词法单元,供后续的语法分析使用。 而Bison是一个用于生成语法分析器的工具,它通过定义一组语法规则和语义动作,将词法分析生成的词法单元序列转化成一颗语法树。Bison可以根据语法规则,自动生成语法分析器的代码,通过递归下降的方式对输入的源代码进行语法分析。 使用Flex和Bison构建编译器的过程通常分为以下几个步骤: 1. 设计词法规则:根据编程语言的词法规范,使用正则表达式定义一组词法规则,用于将输入的字符流拆分成一个个的词法单元。 2. 设计语法规则:根据编程语言的语法规范,使用BNF(巴科斯-诺尔范式)或类似的语法表示方法,定义一组语法规则,用于将词法单元转化成语法树。 3. 实现词法分析器:使用Flex根据词法规则生成词法分析器的C代码,该代码将输入字符流转化成一系列词法单元。 4. 实现语法分析器:使用Bison根据语法规则生成语法分析器的C代码,该代码将词法单元序列转化成语法树。 5. 实现语义动作:在Bison生成的语法分析器中,添加适当的语义动作,用于对语法树进行语义分析和中间代码生成等操作。 6. 实现语言特性:根据编程语言的特性,添加相应的功能,例如处理变量声明、函数调用、控制结构等。 通过以上步骤,使用Flex和Bison可以快速构建一个简单的编译器。当然,这只是编译器构建过程的一部分,还需要考虑符号表管理、优化和代码生成等方面。但Flex和Bison提供了强大的词法和语法分析功能,为编译器的开发提供了很好的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值