VC与YACC、LEX集成

在这里,将介绍由英国 Bumble-Bee Software公司生产的 Windows环境下的 YACCLEX集成环境 Parser GeneratorParser GeneratorWindowsYACCLEX的实现。它包括一个图形用户界面,同时包括 YACCLex两个版本,分别叫做 AYACCAlexParser Generator非常适合于与 VC++集成。
在安装了 Parser Generator后,执行以下步骤,即可使 VC++编译和连接由 Parser Generator产生的文件。
1.目录设置
VC++中执行以下步骤,每个步骤只执行一次。
1 选择 Tools菜单中的 Options命令,在屏幕上即会出现 Options对话框。
2 选择 Directories选项卡。
3Show Directories for下拉列表框中选择 Include Files
4Directories框中,点击最后的空目录,并填入 Parser Generatorinclude子目录的路径。
5Show Directories for下拉列表框中选择 Library Files
6Directories框中,点击最后的空目录,并填入 Parser Generatorlib/msdev子目录的路径。
7Show Directories for下拉列表框中选择 Source Files
8Directories框中,点击最后的空目录,并填入 Parser GeneratorSource子目录的路径。
9 点击 OK按钮, Options对话框将接受设置并关闭。
VC++在就可以找到包含文件 yacc.hlex.h以及 YACCLex的库文件。
2.项目设置
对于每个 VC++项目,都需在 VC++中执行以下步骤:
1 选择 Project菜单中的 Settings命令,在屏幕上即会出现 Project Settings对话框。
2Settings for下拉列表框中选择 Win32 Debug
3 选择 C/C++标签。
4Category下拉列表框中选择 General
5Preprocessor Definitions框中,在当前文本的最后,输入 YYDEBUG
6 选择 Link标签。
7Category下拉列表框中选择 General
8Object/Library Modules框中,在当前文本的后面,输入 yld.lib ylmtd.lib
9Settings for下拉列表框中选择 Win32 Release
10 重复第 8步的工作。
11 点击 OK按钮, Project Settings对话框将接受设置并关闭。
VC++现在可以从特定的库中接受 YACCLex所需的函数和变量。
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;
}
下面是我电脑上测试的时候的一些截图,希望对你配置机器有帮助:
参考文献:
[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实例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值