LEX词法分析器问题

ghostmirror 2003-10-21 12:54:12
我按照规则写了个 .lex文件,编译后生成 .c文件,问我如何生成exe文件??
用C的编译器????
另一个问题,如何知道自己写的.lex文件没有 逻辑错误??也就是如何调试lex
文件???
...全文
175 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mis98ZB 2003-10-25
  • 打赏
  • 举报
回复
至于调试,如果没有写自己的函数的话,黑盒吧!
mis98ZB 2003-10-25
  • 打赏
  • 举报
回复
cc yy.lex.c -ll
tudou614 2003-10-25
  • 打赏
  • 举报
回复
大家都写了啊

不行,我要落后了

回去赶紧敲一个
sdtea 2003-10-24
  • 打赏
  • 举报
回复
mark
pxwzd123 2003-10-24
  • 打赏
  • 举报
回复
FTTTTTTTTTTTTTTTTTT
alphapaopao 2003-10-21
  • 打赏
  • 举报
回复
你可以先生成一个简单的 .lex, 然后生成 .c, 这个 .c 一般可以直接编译,用 vc 编译。

当然,默认生成的 main() 比较有问题,强烈建议你把它替换掉。在 .lex 里面可以给出你自己的 main()。

下面是我的 .lex

DIGIT [0-9]
ID [_a-zA-Z][_a-zA-Z0-9]*
ALPHA_IN_STRING [^\\\"\n]|\\[^\n]|\\\n
ALPHA_QUOTE [^\\\'\"\n]|\\[^\n]
NAT DIGIT+
SIGNED_NAT ("+"|"-")?{NAT}
NUMBER {SIGNED_NAT}("."{NAT})?((E|e){SIGNED_NAT})?

%%

{NUMBER} printf("浮点数: %s\n", yytext);

{DIGIT}+ {
printf( "An integer: %s (%d)\n", yytext,
atoi( yytext ) );
}

if|else|main|struct|__asm|else|main|struct|__assume|enum|__multiple_inheritance|switch |
auto|__except|__single_inheritance|template |
__based|explicit|__virtual_inheritance|this |
bool|extern|mutable|thread |
break|false|naked|throw |
case|__fastcall|namespace|true |
catch|__finally|new|try |
__cdecl|float|noreturn|__try |
char|for|operator|typedef |
class|friend|private|typeid |
const|goto|protected|typename |
const_cast|if|public|union |
continue|inline|register|unsigned |
__declspec|__inline|reinterpret_cast|using|declaration |
default|int|return|uuid |
delete|__int8|short|__uuidof |
dllexport|__int16|signed|virtual |
dllimport|__int32|sizeof|void |
do|__int64|static|volatile |
double|__leave|static_cast|wmain |
dynamic_cast|long|__stdcall|while {
printf( "A keyword: %s\n", yytext );
}

{ID} printf( "An identifier: %s\n", yytext );



"+"|"-"|"*"|"/"|"["|"]"|";"|"."|","|"!"|"~" |
"{"|"}"|"("|")"|"="|":"|"::"|"<"|">"|"&"|"%"|"^"|"|"|"?" /*操作符*/printf( "An operator: %s\n", yytext );



"##" printf("连接符: %s\n", yytext);

"++" printf("incremental : %s\n", yytext);

"\""{ALPHA_IN_STRING}*"\"" /*字符串*/printf("string: %s\n", yytext);
\'{ALPHA_QUOTE}\' /*字符*/printf("char: %s\n", yytext);

"\\""\n" printf("续行符\n");

"//"[^\n]*"\n" printf("comments: %s", yytext);

"/*"(
  • *
  • (([^*/])+([/])*)*)*"*/" printf("c comments:\n\n%s[END]\n\n\n", yytext);

    "#"[^\n]*"\n" printf("preprocessor: %s", yytext);

    [ \t\n]+ /* eat up whitespace */

    . printf( "\007\n\n===================================Unrecognized character: %s\n\n\n", yytext );

    %%

    int yywrap()
    {
    return 1;
    }
    int main()
    {
    yyin = fopen("D:\\ag\\MyProjects\\src\\aglib.cpp", "r");
    yyout = fopen("yyout.txt", "w");
    yylex();
    }
ttmmdd 2003-10-21
  • 打赏
  • 举报
回复
建个项目,加上这个.C,掉用LEX涵数.
可能要你自己写一些类似保存状态,进步之类的代码,不过多很简单.

33,008

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧