在这里,将介绍由英国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
Simple calculator. Features floating point arithmetic using
the addition, subtraction, multiplication and divide
operators, and unary minus. Expressions can be grouped
using parentheses, and simple error recovery is supported.
************************************************************/
#include <ctype.h>
# include <stdio.h>
# define YYSTYPE double /* double type for YACC stack */
% }
%token NUMBER
%%
lines : lines expr'' {printf("%g", $2 ); }
| lines''
|/* e */
| error'' { yyerror("reenter last line:" ); yyerrok(); }
;
expr : expr'+' term { $$= $1+ $3 ; }
| expr'-' term { $$= $1- $3 ; }
| term
;
term : term'*' factor { $$= $1* $3 ; }
| term'/' factor { $$= $1/ $3 ; }
| factor
;
factor :'(' expr')' { $$= $2 ; }
| '(' expr error { $$= $2; yyerror("missing ')'" ); yyerrok(); }
| '-' factor { $$= -$2 ; }
| NUMBER
;
%%
int main(void)
{
return yyparse();
}
int yylex(void)
{
int c;
while ((c= getchar())== '' );
if (c == '.'|| isdigit(c)) {
ungetc(c, stdin);
scanf("%lf",& yylval);
returnNUMBER ;
}
return c;
}
/* ***********************************************************
calc.y
Simple calculator. Features floating point arithmetic using
the addition, subtraction, multiplication and divide
operators, and unary minus. Expressions can be grouped
using parentheses, and simple error recovery is supported.
************************************************************/
#include <ctype.h>
# include <stdio.h>
# define YYSTYPE double /* double type for YACC stack */
% }
%token NUMBER
%%
lines : lines expr'' {printf("%g", $2 ); }
| lines''
|/* e */
| error'' { yyerror("reenter last line:" ); yyerrok(); }
;
expr : expr'+' term { $$= $1+ $3 ; }
| expr'-' term { $$= $1- $3 ; }
| term
;
term : term'*' factor { $$= $1* $3 ; }
| term'/' factor { $$= $1/ $3 ; }
| factor
;
factor :'(' expr')' { $$= $2 ; }
| '(' expr error { $$= $2; yyerror("missing ')'" ); yyerrok(); }
| '-' factor { $$= -$2 ; }
| NUMBER
;
%%
int main(void)
{
return yyparse();
}
int yylex(void)
{
int c;
while ((c= getchar())== '' );
if (c == '.'|| isdigit(c)) {
ungetc(c, stdin);
scanf("%lf",& yylval);
returnNUMBER ;
}
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实例。