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选项卡。
3      Show Directories for下拉列表框中选择 Include Files
4      Directories框中,点击最后的空目录,并填入 Parser Generatorinclude子目录的路径。
5      Show Directories for下拉列表框中选择 Library Files
6      Directories框中,点击最后的空目录,并填入 Parser Generatorlib/msdev子目录的路径。
7      Show Directories for下拉列表框中选择 Source Files
8      Directories框中,点击最后的空目录,并填入 Parser GeneratorSource子目录的路径。
9       点击 OK按钮, Options对话框将接受设置并关闭。
VC++在就可以找到包含文件 yacc.hlex.h以及 YACCLex的库文件。
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++现在可以从特定的库中接受 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
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);
        
return   NUMBER ;
    }
    
return  c;
}
 
下面是我电脑上测试的时候的一些截图,希望对你配置机器有帮助:
 
 
 
 
 
 
 
 
 
 
 
 
 参考文献:
 [1] 吕映芝,张素琴,蒋维杜,编译原理.北京: 清华大学出版社, 1998.1
 [2] IBM的一篇翻译教程《Yacc 与 Lex 快速入门》 http://www.ibm.com/developerworks/cn/linux/sdk/lex/index.html
 
英文资源:
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值