码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • Windows中使用Lex(Win flex-bison)


    Lex是Lexical Analyzer Generator(取前三个字母)的缩写,是Unix环境下非常著名的工具,主要功能是生成一个词法分析器(scanner)的C源码,描述规则采用正则表达式(regular expression)。

    Lex已经广泛地用于描述各种语言的词法分析器。

    flex (the fast lexical analyser)意思是快速词法分析器。

    Win-flex bison是flex和bison在Windows平台的一个移植版本,它支持flex(快速词法分析器)和bison(GNU解析器生成器)。

    Win-flex bison的下载网址: Win flex-bison download | SourceForge.net 

    点击“Download”按钮,开始下载文件“win_flex_bison-latest.zip”,文件大小仅有692KB。

     

    解压到自己喜欢的位置。

     

    你可以在命令行直接使用win_flex和win_bison,或者在Visual Studio中借助CustomBuildRules使用它们(详见 Win flex-bison / Wiki / Visual Studio custom build rules )

    flex/bison文件的例子可参看网页 Win flex-bison - Browse Files at SourceForge.net 

    在命令行输入 win_flex --help ,可以获得相关用法:

    Usage: win_flex [OPTIONS] [FILE]...

    Generates programs that perform pattern-matching on text.

    Table Compression:

      -Ca, --align      trade off larger tables for better memory alignment

      -Ce, --ecs        construct equivalence classes

      -Cf               do not compress tables; use -f representation

      -CF               do not compress tables; use -F representation

      -Cm, --meta-ecs   construct meta-equivalence classes

      -Cr, --read       use read() instead of stdio for scanner input

      -f, --full        generate fast, large scanner. Same as -Cfr

      -F, --fast        use alternate table representation. Same as -CFr

      -Cem              default compression (same as --ecs --meta-ecs)

    Debugging:

      -d, --debug             enable debug mode in scanner

      -b, --backup            write backing-up information to lex.backup

      -p, --perf-report       write performance report to stderr

      -s, --nodefault         suppress default rule to ECHO unmatched text

      -T, --trace             win_flex should run in trace mode

      -w, --nowarn            do not generate warnings

      -v, --verbose           write summary of scanner statistics to stdout

          --hex               use hexadecimal numbers instead of octal in debug outputs

    Files:

      -o, --outfile=FILE      specify output filename

      -S, --skel=FILE         specify skeleton file

      -t, --stdout            write scanner on stdout instead of lex.yy.c

          --yyclass=NAME      name of C++ class

          --header-file=FILE   create a C header file in addition to the scanner

          --tables-file[=FILE] write tables to FILE

    Scanner behavior:

      -7, --7bit              generate 7-bit scanner

      -8, --8bit              generate 8-bit scanner

      -B, --batch             generate batch scanner (opposite of -I)

      -i, --case-insensitive  ignore case in patterns

      -l, --lex-compat        maximal compatibility with original lex

      -X, --posix-compat      maximal compatibility with POSIX lex

      -I, --interactive       generate interactive scanner (opposite of -B)

          --yylineno          track line count in yylineno

    Generated code:

      -+,  --c++               generate C++ scanner class

      -Dmacro[=defn]           #define macro defn  (default defn is '1')

      -L,  --noline            suppress #line directives in scanner

      -P,  --prefix=STRING     use STRING as prefix instead of "yy"

      -R,  --reentrant         generate a reentrant C scanner

           --bison-bridge      scanner for bison pure parser.

           --bison-locations   include yylloc support.

           --stdinit           initialize yyin/yyout to stdin/stdout

           --nounistd          do not include

           --wincompat         windows compatibility (uses instead of and _isatty, _fileno functions)

           --noFUNCTION        do not generate a particular FUNCTION

    Miscellaneous:

      -c                      do-nothing POSIX option

      -n                      do-nothing POSIX option

      -?

      -h, --help              produce this help message

      -V, --version           report win_flex version

    例1

    参考网页 Windows环境下lex入门 - 朱迎春 - 博客园 中的例子。

    (1)创建文本文件“a.l”(即编写lex程序)

    使用文本编辑器创建文件“d:/temp/a.l”,内容如下:

    1. %{
    2. int num_lines = 0, num_chars = 0;
    3. %}
    4. %%
    5. \n      ++num_lines; ++num_chars;
    6. .       ++num_chars;
    7. %%
    8. int main()
    9. {
    10. yyin = fopen("d:/temp/a.l","r");
    11. yylex();
    12. fclose(yyin);
    13. printf("lines = %d, chars = %d\n", num_lines, num_chars);
    14. }
    15. int yywrap()
    16. {
    17. return 1;
    18. }

    双百分号“%%”,是lex编译器的专用字符串,用于区分lex程序文件中的声明部分、转换规则(每个规则由模式和动作两部分组成,模式即正则表达式,动作即程序代码)、辅助过程(即C语言编写的函数)。

    (2)使用win_flex编译文件“a.l”

    在命令行窗口输入命令(wincompat参数,命令lex编译器创建Windows兼容的程序),:

    D:\Programs\win_flex_bison-latest\win_flex.exe --wincompat --outfile=d:/temp/a.yy.c d:/temp/a.l

    正常执行后,生成文件“d:/temp/a.yy.c”,这个文件较大,内容较多。

    (3)使用C语言编译器编译a.yy.c

    我使用的C语言编译器是Visual Studio 2022。进入VS2022的开发者命令行窗口,进入目录“d:\temp”执行如下编译命令: cl a.yy.c

    命令执行成功后,在目录中生成文件“a.yy.exe”和“a.yy.obj”.

    关于进入VS2022的开发者命令行窗口的方法,可参看网页 今日头条 的相关部分。

    (4)运行程序文件“a.yy.exe”

    在命令行窗口运行命令“a.yy”,结果如下图所示:

    该程序的运行结果是,对文件中的行数和字符数进行计数。

    例2

    参考网页 windows 下使用lex_铿老爷的博客-CSDN博客_lex windows 中的例子。

    (1)创建文本文件“a.l”(即编写lex程序)

    使用文本编辑器创建文件“d:/temp/b.l”,内容如下:

    1. %{  
    2.     #include  
    3.     #include   
    4.     int count = 0;  
    5. %}   
    6.   
    7. delim [" "\n\t\r]   
    8. whitespace {delim}+   
    9. operator \+|-|\*|\/|:=|>=|<=|#|=|<<|>>|\+\+
    10. reservedWord int|include|main|return|using|if|namespace
    11. delimiter [,\.;\(\)\"\<\>\{\}]
    12. constant ([0-9])+
    13. identfier [A-Za-z]([A-Za-z]|[0-9])*  
    14. %%   
    15. {reservedWord} {count++;printf("%d\t(rw,%s)\n",count,yytext);}  
    16. \"[^\"]*\" {count++;printf("%d\t(ct,%s)\n",count,yytext);}
    17. {operator} { count++;printf("%d\t(op,%s)\n",count,yytext); }  
    18. {delimiter} {count++;printf("%d\t(de,%s)\n",count,yytext);}  
    19. {constant} {count++;printf("%d\t(ct,%s)\n",count,yytext);}  
    20. {identfier} {count++;printf("%d\t(id,%s)\n",count,yytext);}   
    21. {whitespace} { /* do    nothing*/ }
    22. %%
    23. int main()
    24. {
    25. yyin = fopen("d:/temp/input.txt","r");
    26. yylex();
    27. fclose(yyin);
    28. }
    29. int yywrap()
    30. {
    31. return 1;
    32. }

    上述程序中使用的“d:/temp/input.txt”文件内容

    1. #include
    2. using namespace std;
    3. int main(){
    4. cout<<"Hello World!"<
    5. }

    (2)使用win_flex编译文件“b.l”

    在命令行窗口输入命令(wincompat参数,命令lex编译器创建Windows兼容的程序),:

    D:\Programs\win_flex_bison-latest\win_flex.exe --wincompat --outfile=d:/temp/b.yy.c d:/temp/b.l

    正常执行后,生成文件“d:/temp/b.yy.c”,这个文件较大,内容较多。

    (3)使用C语言编译器编译b.yy.c

    我使用的C语言编译器是Visual Studio 2022。进入VS2022的开发者命令行窗口,进入目录“d:\temp”执行如下编译命令: cl b.yy.c

    命令执行成功后,在目录中生成文件“b.yy.exe”和“b.yy.obj”。

    (4)运行程序文件“b.yy.exe”

    在命令行窗口运行命令“b.yy”,结果如下图所示:

     

    但是有个问题,就是在“d:/temp/input.txt”文件的”Hello World”的左边的双引号前面添加空格,就会导致程序的结果不同(不符合预期),这个问题原因还找不到。

    小结

    通过上面两个例子,可以看出在Windows中使用Lex的步骤为:

    1. 编写lex程序。即扩展名为“.l”的文本文件。
    2. 编译lex程序。即使用win_flex.exe处理lex程序得到扩展名为“.c”的文件。
    3. 得到可执行的程序。即使用C语言编译器,生成扩展名为“.exe”的文件。
    4. 运行可执行程序。

    相关网页

    lex(计算机领域的词法分析器)_百度百科 ,lex计算机领域的词法分析器

    Win flex-bison - Browse Files at SourceForge.net , Win flex-bison Files

    Windows环境下lex入门 - 朱迎春 - 博客园 ,Windows环境下lex入门

    windows 下使用lex_铿老爷的博客-CSDN博客_lex windows ,windows 下使用lex

    今日头条 ,SQLite 3.37.2源码下载及编译(Win10+VS2022)

     

     

  • 相关阅读:
    leetcode(力扣) 452. 用最少数量的箭引爆气球 & 435. 无重叠区间 (贪心)
    日系简约商务通用PPT模板
    RF优化问题分析和处理
    Hive常用操作持续更新!!!
    鼎盛合 | 宠物智能投食机方案设计开发
    [计算机提升] 计算机进阶概念:路径
    【Spring-boot】Spring实现策略模式
    ESP32连接室内WiFi,手持端Blinker.apk远程在线控制(移动网)
    【Nginx】(二)Nginx 工作流程与模块功能详解
    五城联动!尧泰汉海“四季有爱·益路向阳”公益活动圆满落幕
  • 原文地址:https://blog.csdn.net/Alexabc3000/article/details/126248537
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号