Windows10下配置flex+bison(附运行例子)

Windows10下配置flex+bison(附运行例子)

因为最近上编译课,老师要求我们自己安装配置flex(她还强调说windows配置很简单的网上一搜一大把教程),然而作为新手小白我参考了好几个网页,花了一个小时才配好。。。最后才找到合适的方法,安装配置成功。于是决定把我安装过程分享出来,也算是自己mark一下。

主要参考的网页:

https://blog.csdn.net/dongbi0665/article/details/102331311?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
https://blog.csdn.net/liwei_cmg/article/details/1774665?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task

  1. 下载flex和bison,网址是http://gnuwin32.sourceforge.net/packages/flex.htm和http://gnuwin32.sourceforge.net/packages/bison.htm,打开点击两个setup下载即可。主要需要 lib文件夹下的 libfl.a 和 liby.a 这两个库。
    在这里插入图片描述
    在这里插入图片描述
    下载完成后双击运行安装(这里我直接按默认路径安在c盘)
    在这里插入图片描述

  2. 从http://sourceforge.net/projects/winflexbison/下载已经编译好的压缩文件 win_flex_bison-2.5.1.zip(不到700kb),然后解压放在任意盘(这里我放在了D盘)
    在这里插入图片描述

  3. 配置环境变量:右击此电脑->属性->高级系统设置->“高级”一栏下的环境变量,在系统变量Path中增加win_flex_bison的路径。
    在这里插入图片描述

  4. 以上,配置完毕。接下来测试一下是否可以正确实现功能。我在网上随便搜了一个写好的程序,它的功能是计算输入的字符串中A-Z的字符数:

/*** Definition Section has one variable 
which can be accessed inside yylex() 
and main() ***/
%{ 
int count = 0; 
%} 

/*** Rule Section has three rules, first rule 
matches with capital letters, second rule 
matches with any character except newline and 
third rule does not take input after the enter ***/
%% 
[A-Z]{printf("%s capital letter\n", yytext); 
    count++;} 
.     {printf("%s not a capital letter\n", yytext);} 
\n{return 0;} 
%% 

/*** Code Section prints the number of 
capital letter present in the given input ***/
int yywrap(){} 
int main(){ 

// Explanation: 
// yywrap() - wraps the above rule section 
/* yyin - takes the file pointer which contains the input */
/* yylex() - this is the main flex function which runs the Rule Section */
// yytext is the text in the buffer
// Uncomment the lines below 
// to take input from file 
// FILE *fp; 
// char filename[50]; 
// printf("Enter the filename: \n"); 
// scanf("%s",filename); 
// fp = fopen(filename,"r"); 
// yyin = fp; 

yylex(); 
printf("\nNumber of Captial letters "
    "in the given input - %d\n", count); 
    
return 0;
}

打开cmd,用cd进入到希望创建文件的盘,使用指令type nul>test.l新建一个.l文件。在这里插入图片描述
可以看到在指定路径下已经产生了一个.l文件:在这里插入图片描述
可以直接右击以记事本方式打开,然后把lex代码写进去,保存。在这里插入图片描述

  1. 接下来,运行写好的代码。在命令框输入命令:win_flex test.l,将产生编译好的.c文件:在这里插入图片描述在这里插入图片描述
  2. 接下来只要编译运行这个c语言文件就行了。方法有很多,可以通过安装cygwin或mingw,然后配置环境,使用它们自带的gcc编译。这里使用DEV C++(是领我入门计算机的老古董啊)进行编译:
    在这里插入图片描述
    按下编译运行这个快乐按钮,收获编译成功的可执行文件:
    在这里插入图片描述
    输入字符串123ABC,程序运行输出A-Z字符数为3。
以上是我配置+运行例子的过程。可能例子相对简单,没有遇到一些很复杂的问题。如有遇到问题,可以到我参考的几个网址里查找详细解决方法:)

第一次尝试写教程,如有不足,请多多指教~

  • 29
    点赞
  • 96
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值