lex编译dos命令_如何在Windows上编译LEX / YACC文件?

I'm having Lex and YACC files to parse my files (.l file and .y file).

How to compile those files and how to make equivalent .c file for them in windows platform?

解决方案

As for today (2011-04-05, updated 2017-11-29) you will need the lastest versions of:

After that, do a full install in a directory of your preference without spaces in the name. I suggest C:\GnuWin32. Do not install it in the default (C:\Program Files (x86)\GnuWin32) because bison has problems with spaces in directory names, not to say parenthesis.

Also, consider installing Dev-CPP in the default directory (C:\Dev-Cpp)

After that, set the PATH variable to include the bin directories of gcc (in C:\Dev-Cpp\bin) and flex\bison (in C:\GnuWin32\bin). To do that, copy this: ;C:\Dev-Cpp\bin;C:\GnuWin32\bin and append it to the end of the PATH variable, defined in the place show by this figure:

If the figure is not in good resolution, you can see a step-by-step here.

Open a prompt, cd to the directory where your ".l" and ".y" are, and compile them with:

flex hello.l

bison -dy hello.y

gcc lex.yy.c y.tab.c -o hello.exe

You will be able to run the program. I made the sources for a simple test (the infamous Hello World):

Hello.l

%{

#include "y.tab.h"

int yyerror(char *errormsg);

%}

%%

("hi"|"oi")"\n" { return HI; }

("tchau"|"bye")"\n" { return BYE; }

. { yyerror("Unknown char"); }

%%

int main(void)

{

yyparse();

return 0;

}

int yywrap(void)

{

return 0;

}

int yyerror(char *errormsg)

{

fprintf(stderr, "%s\n", errormsg);

exit(1);

}

Hello.y

%{

#include

#include

int yylex(void);

int yyerror(const char *s);

%}

%token HI BYE

%%

program:

hi bye

;

hi:

HI { printf("Hello World\n"); }

;

bye:

BYE { printf("Bye World\n"); exit(0); }

;

Edited: avoiding "warning: implicit definition of yyerror and yylex".

Disclaimer: remember, this answer is very old (since 2011!) and if you run into problems due to versions and features changing, you might need more research, because I can't update this answer to reflect new itens. Thanks and I hope this will be a good entry point for you as it was for many.

Updates: if something (really small changes) needs to be done, please check out the official repository at github: https://github.com/drbeco/hellex

Happy hacking.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值