Windows下lex 与 yacc的使用

                                                                  Windowslex 与 yacc的使用

首先

下载下载flexbison。网址是http://pan.baidu.com/s/1dDlfiW5 

选择下载就好了,下载后解压到你电脑中的任一盘中。找到这个文件夹就行了。

 

然后里面就是这样:

 

打开flex

 

打开bison

 

 

 

至此万事俱备,我们可以开始两个简单的文件来测试一下。

1.新建文本文件,更改名称为lex.l,敲入下面代码

%{

int yywrap(void);

%}

%%

%%

int yywrap(void)

{

return 1;

}

2.新建文本文件,更改名称为yacc.y,敲入下面代码

%{

void yyerror(const char *s);

%}

%%

program:

;

%%

void yyerror(const char *s)

{

}

int main()

{

yyparse();

return 0;

}

我们暂且不讨论上面代码的意思。

打开控制台,进入到刚才所建立文件(lex.l,yacc.y)所在的文件夹。

1.输入 flex lex.l

2.输入 bison yacc.y

如果我们看到当前文件夹上多了两个文件(yacc.tab.clex.yy.c),那么说明lex&&yacc已经安装配置成功,接下来就好好享受这两个小工具的魅力吧。

 

现在我们来试用下lex

1、新建文本文件,更改名称为a.lex,敲入下面代码-------词法分析器的源代码

%{

int wordCount = 0;

int numcount = 0;

%}

chars [A-Za-z\_\'\.\"]

numbers ([0-9])+

delim [" "\n\t]

whitespace {delim}+

words {chars}+

%%

while  {ECHO; printf("%s\n",yytext);}

{words} { wordCount++; 

  /* increase the word count by one*/ }

{whitespace} { /* do nothing*/ }

([0-9])+ { numcount++; /* one may want to add some processing           here*/ }

%%

 

void main()

{

printf("ok1\n");

 

yylex(); /* start the  analysis*/

 

printf("ok2\n");

printf(" No of words: %d\n  number: %d\n", wordCount, numcount);

 

return 0;

 

}

 

int yywrap()

{

return1;

}

3、打开菜单,运行,输入cmd

输入:cd 文件夹路径

输入:flex a.lex   回车后生成一个 lex.yy.c文件

4.然后这个lex.yy.c文件打开并编译一下就行了。

5.输入测试代码

asd   asdf 23 q

a1

b2

!#@

while

 

运行看看就行了。

 

 

致谢  

笃行者------lex&yacc安装配置  http://blog.csdn.net/bedusing/article/details/5409495

 

posted @ 2015-04-03 23:04  tiantsing  阅读(2131)  评论(0编辑  收藏  举报