一、安装配置Parser Generator

1.安装Parser Generator->百度云下载地址
2.安装vc6.0->百度云下载地址
3.安装完后,先打开Parser Generator,Project->LibBuilder,双击Visual c++(32-bit),出现如下弹窗:
image.png
4.options里的设置如下:

- Complier Version: Version6
- Unicode: True
- Treat wchar_t as Built-in Type: Fasle
- Complier Bin Directory: vc安装目录..\MICROSOFT VISUAL STUDIO\VC98\BIN
- Complier Bin Directory(2): vc安装目录..\MICROSOFT VISUAL STUDIO\COMMON\MSDEV98\BIN
- Complier Include Directory: vc安装目录..\MICROSOFT VISUAL STUDIO\VC98\INCLUDE
- Complier Include Directory(2): vc安装目录..\MICROSOFT VISUAL STUDIO\VC98\MFC\INCLUDE
- Complier Library Directory: vc安装目录..\MICROSOFT VISUAL STUDIO\VC98\LIB
- Complier Library Directory(2): vc安装目录..\MICROSOFT VISUAL STUDIO\VC98\MFC\LIB

5.新建项目: Project->ParserWizard,一路下一步,按默认即可
image.png
image.png
image.png
image.png
6.在mylexer.l里写如下示例代码:

%{
    #include <stdio.h>
    #include <stdlib.h>
%}
/*difine*/
IF if
THEN then
ELSE else
WHILE while
BEGIN begin
END end
INT [0-9]+
FLOAT {INT}[\.][0-9]+
REAL {FLOAT}([e|E][+|-][0-9]+)
ID    [a-zA-Z]+[0-9a-zA-Z]*
LE [<][=]
GE [>][=]
NE [<][>]
EQ [=]
LT [<]
GT [>]
IS [:][=]
PL [\+]
MI [-]
MU [\*]
DI [\/]
%%
{IF}              {printf("(IF,   )\n");}
{THEN}            {printf("(THEN, )\n");}
{ELSE}            {printf("(ELSE, )\n");}
{WHILE}           {printf("(WHILE,    )\n");}
{BEGIN}           {printf("(BEGIN,    )\n");}
{END}             {printf("(END,  )\n");}
{ID}              {printf("(ID, '%s')\n",yytext);}
{REAL}              {printf("(REAL, %g)\n",atof(yytext));}
{FLOAT}              {printf("(REAL, %s)\n",yytext);}    
{INT}             {printf("(INT, %s)\n",yytext);}
{LE}              {printf("(LE,   )\n");}
{GE}              {printf("(GE,   )\n");}
{NE}              {printf("(NE,   )\n");}
{EQ}              {printf("(GQ,   )\n");}
{LT}              {printf("(LT,   )\n");}
{GT}              {printf("(GT,   )\n");}
{IS}              {printf("(IS,   )\n");}
{PL}              {printf("(PL,   )\n");}
{MI}              {printf("(MI,   )\n");}
{MU}              {printf("(MU,   )\n");}
{DI}              {printf("(DI,   )\n");}
[ \t\n]+        ;       /* ignore whitespace */
[!-~]+              { printf("Invalid input: %s\n",yytext); } 
%%

void main()
{
    yyin=fopen("E:\\example.txt","r");
    yylex();
    fclose(yyin);
    system("PAUSE");
}

//必须要包含的函数
int yywrap()
{
    return 1;
}

然后Project->Complie File, 如果为灰色,则先Rebuild All,稍等片刻,再Complie File
编译成功页面如下:
image.png

在该工程下会生成mylexer.c和mylexer.h文件
image.png

二、配置Visual Studio2017

1.新建vc++空项目,菜单栏项目->属性

2.VC++目录:

  • 包含的目录:..\Parser Generator 2\Cpp\Include
  • 库目录:..\Parser Generator 2\CppLib\msvc32
  • 源目录:..\Parser Generator 2\Cpp\Source
  • image.png

3.C/C++(此选项没有则先写一个C的helloworld运行一下)

  • 常规->附加包含目录:..\Parser Generator 2\Cpp\Lib
  • 预处理器->预处理器定义:_CRT_SECURE_NO_DEPRECATE
  • 代码生成->运行库:多线程(/MT)

image.png
image.png
image.png

4.链接器

  • 常规->附加库目录:..\Parser Generator 2\Cpp\Lib\msvc32
  • 输入->附加依赖项:legacy_stdio_definitions.lib;ylmt.lib

image.png
image.png

5.将Parser Generator生成的.c和.h文件加到VS的源文件和头文件中
image.png

6.至此,所有的配置已经完成,每次在Parser Generator写完LEX的程序Ctrl+F7编译后在VS里运行程序即可

总结

本次配置花费了很多时间才成功,如果有疏漏的地方,欢迎评论区回复。


陈杰
167 声望332 粉丝

为API生,为框架死;