Windows10下配置flex+bison
原文地址:https://blog.csdn.net/cwj1412/article/details/104829148
本文只是学习记录,大部分都是看这个博主滴
下载flex和bison
网址是http://gnuwin32.sourceforge.net/packages/flex.htm和http://gnuwin32.sourceforge.net/packages/bison.htm,打开点击两个setup下载即可。主要需要 lib文件夹下的 libfl.a 和 liby.a 这两个库。
下载完成后双击运行安装
我安装在电脑的D盘
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9Pv7KgYA-1648804058100)(C:\Users\hsh\AppData\Roaming\Typora\typora-user-images\image-20220401165447933.png)]
安装win_flex
从http://sourceforge.net/projects/winflexbison/下载已经编译好的压缩文件 win_flex_bison-2.5.1.zip(不到700kb),然后解压放在任意盘(这里我放在了D盘)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lxIPkJyB-1648804058101)(C:\Users\hsh\AppData\Roaming\Typora\typora-user-images\image-20220401165554693.png)]
- 配置环境变量:右击此电脑->属性->高级系统设置->“高级”一栏下的环境变量,在系统变量Path中增加win_flex_bison的路径。
- 以上,配置完毕。接下来测试一下是否可以正确实现功能。我在网上随便搜了一个写好的程序,它的功能是计算输入的字符串中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;
}
新建.l文件
打开cmd,用cd进入到希望创建文件的盘,使用指令type nul>test.l新建一个.l文件。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9fWqaKSt-1648804058102)(C:\Users\hsh\AppData\Roaming\Typora\typora-user-images\image-20220401170043111.png)]
可以看到在指定路径下已经产生了一个.l文件:
可以直接右击以记事本方式打开,然后把lex代码写进去,保存。
运行后产生.c,运行即可
接下来,运行写好的代码。在命令框输入命令:win_flex test.l,将产生编译好的.c文件:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LRC9QNAa-1648804058103)(C:\Users\hsh\AppData\Roaming\Typora\typora-user-images\image-20220401165840038.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iIIzzHai-1648804058103)(C:\Users\hsh\AppData\Roaming\Typora\typora-user-images\image-20220401165907802.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0602Kapa-1648804058104)(C:\Users\hsh\AppData\Roaming\Typora\typora-user-images\image-20220401165934295.png)]
编译运行这个c语言文件就行了。
按下编译运行这个快乐按钮,收获编译成功的可执行文件:
输入字符串123ABC,程序运行输出A-Z字符数为3。