[配置] WinFlexBison VS2017

配置过程

  • 下载
    https://sourceforge.net/projects/winflexbison/?source=typ_redirect
  • 解压缩
    D:\WorkSpace\3_FlexBison\win_flex_bison-latest


    解压缩出来的文件
  • 新建MFC工程
    把FirstTry.cpp移除,因为后面的定义的Parser.y文件里面定义了一个main,这个main会生成到Parser.tab.cpp中。


    image.png
  • 生成依赖项


    生成依赖项

    按图示点击其上的查找现有的按钮,然后找到你下载的WinFlexBison工具的解压目录中的custom_build_rules目录下的win_flex_bison_custom_build.targets。

  • 设置可执行目录
    D:\WorkSpace\3_FlexBison\win_flex_bison-latest
    可执行目录
  • 新建Parser.y
%{

#include <stdio.h>
#include <ctype.h>
#include <math.h>

#define YYSTYPE  double

void yyerror(const char *text);

int yylex(void);
%}

/////////////////////////////////////////////////////////////////////////////
// declarations section
// place any declarations here

%token NUMBER

%left '+' '-'
%left '*' '/'
%right '^'
%%

/////////////////////////////////////////////////////////////////////////////
// rules section
// place your YACC rules here (there must be at least one)
command : exp {printf("%lf\n",$1);}
    ;

exp     : NUMBER         {$$ = $1;}
       | exp '+' exp     {$$ = $1 + $3;}
       | exp '-' exp     {$$ = $1 - $3;}
       | exp '*' exp     {$$ = $1 * $3;}
       | exp '/' exp     {
                            if(0 != $3)
                            {
                               $$ = $1 / $3;
                            }
                            else
                            {
                               $$=0;
                            }
                        }
       | exp '^' exp     {$$ = pow($1,$3);}
       | '(' exp ')'     {$$ = $2;}
    ;
%%

/////////////////////////////////////////////////////////////////////////////
// programs section

int yylex(void)
{
   // place your token retrieving code here
   int c = 0;

   while( (c = getchar()) == ' ');
   if( isdigit(c) )
   {
       ungetc(c,stdin);
       scanf_s("%lf",&yylval);
       return (NUMBER);
   }

   if( '\n' == c )
   {
       return 0;
   }
   return c;
}

int main(void)
{
    yyparse();
    system("PAUSE");
    return 0;
}

void yyerror(const char *text)
{
   fprintf(stderr,"%s\n",text);
}
  • 编译
    生成Parser.tab.h和Parser.tab.c,把这两个文件加入到工程里面。注意要设置Parser.tab.c不使用预编译头。


    不使用预编译头
  • 再次编译运行


    运行
  • 编译效果
    Yacc编译Parser.y -> 生成Parser.tab.h和Parser.tab.cpp。
    C/C++编译链接Parser.tab.h和Parser.tab.cpp -> FirstTry.exe。


    image.png

参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容