[配置] WinFlexBison VS2017

配置过程

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


    [配置] WinFlexBison VS2017_第1张图片
    解压缩出来的文件
  • 新建MFC工程
    把FirstTry.cpp移除,因为后面的定义的Parser.y文件里面定义了一个main,这个main会生成到Parser.tab.cpp中。


    [配置] WinFlexBison VS2017_第2张图片
    image.png
  • 生成依赖项


    [配置] WinFlexBison VS2017_第3张图片
    生成依赖项

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

  • 设置可执行目录
    D:\WorkSpace\3_FlexBison\win_flex_bison-latest
    [配置] WinFlexBison VS2017_第4张图片
    可执行目录
  • 新建Parser.y
%{

#include 
#include 
#include 

#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不使用预编译头。


    [配置] WinFlexBison VS2017_第5张图片
    不使用预编译头
  • 再次编译运行


    [配置] WinFlexBison VS2017_第6张图片
    运行
  • 编译效果
    Yacc编译Parser.y -> 生成Parser.tab.h和Parser.tab.cpp。
    C/C++编译链接Parser.tab.h和Parser.tab.cpp -> FirstTry.exe。


    [配置] WinFlexBison VS2017_第7张图片
    image.png

参考

  • 让VS2015与Win Flex Bison共舞
  • vs2013 下 flex-bison的配置
  • 预编译头文件来自编译器的早期版本,或者预编译头为 C++ 而在 C 中使用它(或相反)

你可能感兴趣的:([配置] WinFlexBison VS2017)