编译原理学习-Windows下Lex和yacc使用

来源:互联网 发布:银行网络金融部 编辑:程序博客网 时间:2024/04/28 05:00

在windows上初步配置和使用Lex和yacc的步骤如下,首先下载flex和bison。下载http://pan.baidu.com/s/1dDlfiW5 并解压。
为了方便,如果安装了VS2015或其它版本,先在cmd下配置好cl .exe,以便编译生成的c文件,配置方法是:
在cmd下输入set,看看VS的配置,如我的电脑上:

......USERDOMAIN_ROAMINGPROFILE=YANGLEIUSERNAME=yangleiUSERPROFILE=C:\Users\yangleiVS140COMNTOOLS=G:\VS2015\Common7\Tools\VSSDK140Install=G:\VS2015\VSSDK\

那么只需

call "%VS140COMNTOOLS%"vsvars32.bat

应该就可以使用cl .exe了。
接下来测试一下,在解压的windows-flex-biso文件夹中进入flex文件夹,编写代码:

%{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(){return 1;}

保存为a .lex,cmd进入该文件夹,

>flex a.lex

得到lex.yy.c文件,接下来对它进行编译:

>cl lex.yy.c用于 x86 的 Microsoft (R) C/C++ 优化编译器 19.00.24213.1 版版权所有(C) Microsoft Corporation。保留所有权利。lex.yy.ca.lex(50): warning C4098: “main”:“void”函数返回值Microsoft (R) Incremental Linker Version 14.00.24213.1Copyright (C) Microsoft Corporation.  All rights reserved./out:lex.yy.exelex.yy.obj

得到lex.yy.exe,可以打开输入自己的测试代码啦。

1 0
原创粉丝点击