在这里,将介绍由英国
Bumble-Bee Software公司生产的
Windows环境下的
YACC和
LEX集成环境
Parser Generator。
Parser Generator是
Windows下
YACC和
LEX的实现。它包括一个图形用户界面,同时包括
YACC和
Lex两个版本,分别叫做
AYACC和
Alex。
Parser Generator非常适合于与
VC++集成。
在安装了
Parser Generator后,执行以下步骤,即可使
VC++编译和连接由
Parser Generator产生的文件。
1.目录设置
在
VC++中执行以下步骤,每个步骤只执行一次。
(
1)
选择
Tools菜单中的
Options命令,在屏幕上即会出现
Options对话框。
(
2)
选择
Directories选项卡。
(
3)
在
Show Directories for下拉列表框中选择
Include Files。
(
4)
在
Directories框中,点击最后的空目录,并填入
Parser Generator的
include子目录的路径。
(
5)
在
Show Directories for下拉列表框中选择
Library Files。
(
6)
在
Directories框中,点击最后的空目录,并填入
Parser Generator的
lib/msdev子目录的路径。
(
7)
在
Show Directories for下拉列表框中选择
Source Files。
(
8)
在
Directories框中,点击最后的空目录,并填入
Parser Generator的
Source子目录的路径。
(
9)
点击
OK按钮,
Options对话框将接受设置并关闭。
VC++在就可以找到包含文件
yacc.h和
lex.h以及
YACC和
Lex的库文件。
2.项目设置
对于每个
VC++项目,都需在
VC++中执行以下步骤:
(
1)
选择
Project菜单中的
Settings命令,在屏幕上即会出现
Project Settings对话框。
(
2)
在
Settings for下拉列表框中选择
Win32 Debug。
(
3)
选择
C/C++标签。
(
4)
在
Category下拉列表框中选择
General。
(
5)
在
Preprocessor Definitions框中,在当前文本的最后,输入
YYDEBUG。
(
6)
选择
Link标签。
(
7)
在
Category下拉列表框中选择
General。
(
8)
在
Object/Library Modules框中,在当前文本的后面,输入
yld.lib ylmtd.lib 。
(
9)
在
Settings for下拉列表框中选择
Win32 Release。
(
10)
重复第
8步的工作。
(
11)
点击
OK按钮,
Project Settings对话框将接受设置并关闭。
VC++现在可以从特定的库中接受
YACC和
Lex所需的函数和变量。
3.应用
(
1)在
Parser Generator下的编辑窗口输入
YACC源程序(扩展名必须为
.y)。
(
2)用
Parser Generator下的
Project菜单的
Compile file命令编译源程序,生成相应的
C语言源程序(
.cpp)。
(
3)用
VC++编译,连接
C语言源程序,生成可执行程序(
.exe)后即可执行。
4。Example:
这是Parser Generator自带的一个例子,可以通过编译:
%
{
/* ***********************************************************
calc.y
Simplecalculator.Featuresfloatingpointarithmeticusing
theaddition,subtraction,multiplicationanddivide
operators,andunaryminus.Expressionscanbegrouped
usingparentheses,andsimpleerrorrecoveryissupported.
*********************************************************** */
# include<ctype.h>
# include<stdio.h>
# defineYYSTYPEdouble/*doubletypeforYACCstack*/
% }
% token NUMBER
%%
lines : linesexpr ' ' { printf ( " %g " , $ 2 );}
| lines ' '
| /* e */
| error ' ' {yyerror( " reenterlastline: " );yyerrok();}
;
expr : expr ' + ' term{$$ = $ 1 + $ 3 ;}
| expr ' - ' term{$$ = $ 1 - $ 3 ;}
| term
;
term : term ' * ' factor{$$ = $ 1 * $ 3 ;}
| term ' / ' factor{$$ = $ 1 / $ 3 ;}
| factor
;
factor : ' ( ' expr ' ) ' {$$ = $ 2 ;}
| ' ( ' exprerror{$$ = $ 2 ;yyerror( " missing')' " );yyerrok();}
| ' - ' factor{$$ = - $ 2 ;}
| NUMBER
;
%%
intmain(void)
{
return yyparse();
}
intyylex(void)
{
intc;
while ((c = getchar()) == ' ' );
if (c == ' . ' || isdigit(c)){
ungetc(c , stdin);
scanf( " %lf " , & yylval);
return NUMBER ;
}
return c;
}
/* ***********************************************************
calc.y
Simplecalculator.Featuresfloatingpointarithmeticusing
theaddition,subtraction,multiplicationanddivide
operators,andunaryminus.Expressionscanbegrouped
usingparentheses,andsimpleerrorrecoveryissupported.
*********************************************************** */
# include<ctype.h>
# include<stdio.h>
# defineYYSTYPEdouble/*doubletypeforYACCstack*/
% }
% token NUMBER
%%
lines : linesexpr ' ' { printf ( " %g " , $ 2 );}
| lines ' '
| /* e */
| error ' ' {yyerror( " reenterlastline: " );yyerrok();}
;
expr : expr ' + ' term{$$ = $ 1 + $ 3 ;}
| expr ' - ' term{$$ = $ 1 - $ 3 ;}
| term
;
term : term ' * ' factor{$$ = $ 1 * $ 3 ;}
| term ' / ' factor{$$ = $ 1 / $ 3 ;}
| factor
;
factor : ' ( ' expr ' ) ' {$$ = $ 2 ;}
| ' ( ' exprerror{$$ = $ 2 ;yyerror( " missing')' " );yyerrok();}
| ' - ' factor{$$ = - $ 2 ;}
| NUMBER
;
%%
intmain(void)
{
return yyparse();
}
intyylex(void)
{
intc;
while ((c = getchar()) == ' ' );
if (c == ' . ' || isdigit(c)){
ungetc(c , stdin);
scanf( " %lf " , & yylval);
return NUMBER ;
}
return c;
}
下面是我电脑上测试的时候的一些截图,希望对你配置机器有帮助:
参考文献:
[1] 吕映芝,张素琴,蒋维杜,编译原理.北京: 清华大学出版社, 1998.1
[2] IBM的一篇翻译教程《Yacc 与 Lex 快速入门》
http://www.ibm.com/developerworks/cn/linux/sdk/lex/index.html
英文资源:
Lex and Yacc, Levine, Mason 和 Branson, O�Reilly 及其合作公司,2nd Ed。
Program Development in UNIX, J. T. Shen, Prentice-Hall India。
Compilers: Principles, Techniques and Tools, Ahoo, Sethi 和 Ullman, Addison-Wesley Pub. Co., 1985,11。
Lex and Yacc and compiler writing指导。 Java 版的 Lex 指导, 叫做
Jlex。
使用 Lex 和 Yacc 的 formalizing a grammar实例。
使用 Lex 和 Yacc 的 formalizing a grammar实例。