您的位置:首页 > 编程语言 > C语言/C++

windows环境下parser generator和vc++的各项配置 c语言的词法分析Lex

2010-03-17 17:24 288 查看
一.parser generator的环境设置
( 本人强烈建议尽量保留默认值,不要求改的就别改了,容易乱。呵呵
VC++ 与 parser generator 安装目录都是默认的,即
C:/Program Files/Microsoft Visual Studio
C:/Program Files/Parser Generator 2
还有图片如果看不清,右键打开单独查看就好了。

安装好parser generator,打开集成开发环境,点项目Project菜单,在下拉菜单中选LibBuilder,弹出LibBuilder对话框选择属性按钮,弹出compiler properties 对话框:
Name 选择Visual C++ (32-bit)
Directory :msvc32
options:
依次为 vc++版本(Versoin 6)
是否支持unicode码。
是否把w_char_t作为内建类型。
如下图:









二.vc++环境的设置
1.目录设置
为了在VC++中可以找到包含文件yacc.h和lex.h,需要对VC++进行相关的目录设置
选择Tools(工具)/Options(选项),打开选项选项卡
选择Directories(目录)选项卡。
在Show Directories for(目录)下拉列表框中选择Include Files。
在Directories框中,点击最后的空目录,并填入C:/Program Files/Parser Generator 2/Cpp/Include (根据个人安装目录需要做相应的调整)





在Show Directories for(目录)下拉列表框中选择Library Files。
在Directories框中,点击最后的空目录,并填入C:/Program Files/Parser Generator 2/Cpp/Lib/msvc32

在Show Directories for下拉列表框中选择Source Files。
在Directories框中,点击最后的空目录,并填入C:/Program Files/Parser Generator 2/Cpp/Source





点击OK(确定)按钮
2.项目设置
对于每个VC++项目,都需在VC++中进行以下设置,以使VC++可以从特定的库中接受LEX所需要的函数和变量(就是说每建立一个工程下面的工作都是必须的,呵呵)
(1) 选择 工程 / 设置,打开Project Settings对话框。
(2) 在Settings for(设置)下拉列表框中选择Win32 Debug。
(3) 选择C/C++选项卡, 在Category(分类)下拉列表框中选择General。(常规)
在Preprocessor Definitions(预处理程序定义)框中,在当前文本的最后,输入
,_YYDEBUG (看好了,一个都别差了)





(4) 选择Link(连接)选项卡,在Category(分类)下拉列表框中选择General。
在Object/Library Modules(对象/库模块)框中,在当前文本的后面,输入
yld.lib ylmtd.lib ylmtdlld.lib ylmtrd.lib (别忘了,他们之间有个空格)
建工程时若果提示 ylmtdlld.lib打不开,删了它就好了。





(5) 在Settings for下拉列表框中选择Win32 Release。
(6) 在Object/Library Modules(对象/库模块)框中,在当前文本的后面,输入
yl.lib ylmt.lib ylmtr.lib ylmtri.lib





(7) 点击OK按钮,Project Settings对话框设置完毕。
这样设置之后我们就可以用vc++编译Parser Generator 生成的c文件了。具体为:
先打开Parser Generator 集成开发环境。点击Project菜单,点击下拉菜单中的ParseWizard菜单,一路下去建立一个工程。然后点击project菜单下的的Rebuild All。在项目文件夹下就生成了相应c文件与h文件。
再用VC++建立一个空控制台工程,然后把用用Parser Generator
生成的c文件和.h头文件加入工程中。接着对VC++进行上面所说的项目设置,然后就可以编译运行了。新手可以使用parser generator 自带的范例,这样方便检验环境的配置是否正确。
parser generator 2的默认输入是标准输入流stdin, 如果用户想改变输入方式,假如想输入mytext.txt,则可以对mylexer.l文件中的main函数如下编写:
int main(void)
{
yyin = fopen("mytext.txt", "r");
if (yyin != NULL) {
return yylex();
}
return 1;
} 则可以以只读方式打开放在C:/Program Files/Microsoft Visual Studio/MyProjects/1下与debug同目录下的mytext.txt文件。





下面是我的,C语言的lex。使用文件输入输出的。在debug下,传递参数给main。有不明白lex文法的,还有它的上下文相关性,可以看一下。

http://hi.baidu.com/jsjrj01%5Fcaojie/blog/item/bc7234dd6f3091ea76c6380a.html

http://hi.baidu.com/jsjrj01%5Fcaojie/blog/item/1c6a052a7a17359e033bf614.html

%{
/****************************************************************************
mylexer.l
ParserWizard generated Lex file.

Date: 2008年3月27日
****************************************************************************/
#include <stdio.h>
%}

/////////////////////////////////////////////////////////////////////////////
// declarations section

// place any declarations here
digit [0-9]
int_num {digit}+
letter [A-Za-z]

identifier ({letter}|[_])({letter}|{digit}|[_])*
other_char [!-@/[-~]
error {digit}({letter})+
pointer /*{identifier}
string {({letter}|{digit}|{other_char})+}

const_string /".*/"

keyword "auto"|"double"|"int"|"struct"|"break"|"else"|"long"|"switch"|"case"|"enum"|"register"|"typedef"|"char"|"extern"|"return"|"union"|"const"|"float"|"short"|"unsigned"|"continue"|"for"|"signed"|"void"|"default"|"goto"|"sizeof"|"do"|"if"|"static"|"while"|"main"
const_real [-+]?{int_num}[.]{int_num}?([E][+|-]?{int_num})?
const_int ("0"|"0x")?{int_num}
signed_int [-+]?{int_num}
operator ","|";"|"("|")"|"{"|"}"|"["|"]"|"->"|"."|"!"|"~"|"++"|"--"|"*"|"&"|"sizeof"|"/"|"%"|"+"|"-"|">"|"<"|">="|"<="|"=="|"!="|"&"|"^"|"|"|"&"|"||"|"+="|"-="|"*="|"/="|"%="|">>="|"<<="|"&="|"^="|"|="|"="
whilespace [/t| |/n]+
%%

/////////////////////////////////////////////////////////////////////////////
// rules section

// place your Lex rules here
{keyword} {fprintf(yyout,"%-15s %s/n",yytext,yytext);}
{const_string} {fprintf(yyout,"CONST_string %s/n",yytext);}
{const_real} {fprintf(yyout,"CONST_real %s/n",yytext);}
{const_int} {fprintf(yyout,"CONST_int %s/n",yytext);}
{signed_int} {fprintf(yyout,"Signed_int %s/n",yytext);}
{operator} {fprintf(yyout,"%-15s %s/n",yytext,yytext);}
{identifier} {fprintf(yyout,"ID %s/n",yytext);}
{pointer} {fprintf(yyout,"Pointer %s/n",yytext);}
{error} {fprintf(yyout,"error: %s/n",yytext);}
{whilespace} {}
"/*" { char c;
int done =0;
do{
while((c=input())!='*');
while((c=input())=='*');
if(c=='/')
done=1;
}while(done==0);
fprintf(yyout,"annotation/n");
}
%%

/////////////////////////////////////////////////////////////////////////////
// programs section

int main(int argc,char**argv)
{
//set the inputfile "yyin"
if(argc >= 2)
yyin=fopen(argv[1],"r");//yyin is LEX inputfile,here ytyin is used for sore the name of inputfile.
else
yyin=stdin; //stdin is the standerd input
//set the outputfile "yyout"
if(argc >= 3)
yyout=fopen(argv[2],"w");//yyout is LEX outputfile,here ytyin is used for sore the name of outputfile.
else
yyout=stdout; //stdout is the standerd output
yylex();
return 0;
}
/*int yywrap()
{
return 1;
}*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: