利用LEX自动生成词法分析程序

上传者: sl198811 | 上传时间: 2021-03-23 17:07:36 | 文件大小: 402KB | 文件类型: RAR
实验二 词法分析器 一、实验目的 掌握词法分析器的构造原理,掌握手工编程或LEX编程方法之一。 二、实验内容 编写一个LEX源程序,使之生成一个词法分析器,能够输入的源程序转换为单词序列输出。 三、实验环境 Flex+VC6.0 四、实验注意 1.Id正则表达式:{letter}({letter}|{digit})* 2.Num正则表达式:{digit}+(\.{digit}+)?(E[+-]?{digit}+)? 3.注释:(\/\*(.)*\*\/) 4.关键字再加上其他字符就又能编程id,所以在词法分析时,id的判断应该放在关键字前面,这样才不会误判 5.由于本程序知识简单的打印数字,因此没有考虑数字的转换 6.">="比">"多一个字符,它应该放在前面判断,其他类似的也应该如此安排 五、实验代码 ******************************************************************************* 实验文件:lex.l、lex.yy.c 实验结果:lex.exe 运行方式:打开lex.exe,弹出input.txt,在其中输入所要测试的程序,保存并关闭,即可在output.txt中看到所得结果 ******************************************************************************* %{ void Install(char *type); %} %option noyywrap delim [ \t] newline [\n] digit [0-9] num {digit}+(\.{digit}+)?(E[+-]?{digit}+)? letter [A-Za-z] id {letter}({letter}|{digit})* key ("if"|"while"|"do"|"break"|"true") basic ("int"|"float"|"bool"|"char") op (">="|""|"<"|"="|"!="|"+"|"-"|"*"|"/") comment (\/\*(.)*\*\/) %% delim {;} newline {printf("\n");} {num} {Install("Num");} {key} {Install("Key");} {basic} {Install("Basic");} {op} {Install("Op");} ";" {Install("Comma");} {id} {Install("ID");} {comment} {Install("Comment");} "(" | "[" | "{" {Install("lbracket");} ")" | "]" | "}" {Install("rbracket");} %% void Install(char *s) { fprintf(yyout, "%s:%s ", s, yytext); } int main() { printf("please input the test program in input.txt\n"); system("input.txt"); yyin = fopen("input.txt", "r"); yyout = fopen("output.txt", "w" ); yylex(); fclose(yyout); fclose(yyin); printf("analysis result in output.txt\n"); system("output.txt"); return 0; } 六、实验小结 本次的实验由于使用了flex,所以代码较短,麻烦的事flex的正则式表达,由于该使用规则只有简单介绍,而网上找的教程难免有比重就轻之嫌,所以得到上述表达式着实费力,且有的没有成功,例如bracket的(\ ((.)*\ ))或者("("(.)*")")使用时都没有成功,所以便单独写出,有点不伦不类。至于其他的,都较为简单,完。

文件下载

资源详情

[{"title":"( 26 个子文件 402KB ) 利用LEX自动生成词法分析程序","children":[{"title":"lex词法分析","children":[{"title":"lex.yy.dsp <span style='color:#111;'> 3.39KB </span>","children":null,"spread":false},{"title":"input.txt <span style='color:#111;'> 176B </span>","children":null,"spread":false},{"title":"lex.ncb <span style='color:#111;'> 49.00KB </span>","children":null,"spread":false},{"title":"lex.yy.c <span style='color:#111;'> 39.88KB </span>","children":null,"spread":false},{"title":"lex.yy.plg <span style='color:#111;'> 1.05KB </span>","children":null,"spread":false},{"title":"lex.yy.opt <span style='color:#111;'> 47.50KB </span>","children":null,"spread":false},{"title":"lex.plg <span style='color:#111;'> 240B </span>","children":null,"spread":false},{"title":"output.txt <span style='color:#111;'> 574B </span>","children":null,"spread":false},{"title":"lex.l <span style='color:#111;'> 1.03KB </span>","children":null,"spread":false},{"title":"Debug","children":[{"title":"lex.ilk <span style='color:#111;'> 210.20KB </span>","children":null,"spread":false},{"title":"vc60.pdb <span style='color:#111;'> 52.00KB </span>","children":null,"spread":false},{"title":"lex.yy.exe <span style='color:#111;'> 200.09KB </span>","children":null,"spread":false},{"title":"vc60.idb <span style='color:#111;'> 33.00KB </span>","children":null,"spread":false},{"title":"lex.yy.obj <span style='color:#111;'> 30.09KB </span>","children":null,"spread":false},{"title":"lex.yy.pch <span style='color:#111;'> 171.81KB </span>","children":null,"spread":false},{"title":"lex.pdb <span style='color:#111;'> 417.00KB </span>","children":null,"spread":false},{"title":"lex.exe <span style='color:#111;'> 200.09KB </span>","children":null,"spread":false},{"title":"lex.yy.ilk <span style='color:#111;'> 210.04KB </span>","children":null,"spread":false},{"title":"lex.yy.pdb <span style='color:#111;'> 417.00KB </span>","children":null,"spread":false}],"spread":true},{"title":"lex.dsw <span style='color:#111;'> 531B </span>","children":null,"spread":false},{"title":"lex.yy.ncb <span style='color:#111;'> 41.00KB </span>","children":null,"spread":false},{"title":"~$实验二 报告.doc <span style='color:#111;'> 162B </span>","children":null,"spread":false},{"title":"lex.opt <span style='color:#111;'> 47.50KB </span>","children":null,"spread":false},{"title":"lex.exe <span style='color:#111;'> 200.06KB </span>","children":null,"spread":false},{"title":"lex.yy.dsw <span style='color:#111;'> 520B </span>","children":null,"spread":false},{"title":"lex.dsp <span style='color:#111;'> 4.20KB </span>","children":null,"spread":false}],"spread":false}],"spread":true}]

评论信息

  • u010389565 :
    没有注释看着太乱了,还好程序是没有问题的
    2015-07-07
  • 熊熊明 :
    程序是完整的,而且可以运行。但是,没有注释,而且代码写作随意。花了好长时间才明白。强烈建议发代码的同志们,辛苦下,把注释完善下。
    2015-05-31
  • 铃木一朗 :
    可以实现.不过程序应该更清楚才行.
    2014-11-18
  • 丨brokenYouth :
    运行OK,没有问题,有注释就更好了!谢谢! 有参考价值!!
    2014-04-29
  • mengdao2046 :
    能运行通过,希望有文字啊,要不后缀看着好乱的
    2013-06-13
  • glasshome :
    可以运行 要是有文字的解释说明更好了。。。
    2013-06-07
  • hellodxss :
    代码不错啊,谢谢
    2013-03-12
  • thericann :
    还可以 真好在做数据库设计 学习了
    2012-11-07
  • coder :
    可以用,挺好。如果需要,可以下载。
    2012-09-08
  • liu9201028 :
    运行通过了,谢谢
    2012-06-28

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明