Flex 和野牛 C++

Flex & Bison C++

我想启动一个 Flex & Bison 翻译器,它将从给定的文件中读取并输出到另一个文件,同时理解我们在输入中给他的内容。 例如,如果我给出 "This is a string" 12 4.5,输出文件将是

字符串>“这是一个字符串”

Space > *

整数 > 12

Space > *

浮动 > 4.5

问题是我试图在所有这些下面的基础上,我从读取输入文件和输出文件并打开它们的地方开始。我正在研究 visual studio 所以我将命令行参数添加为 read exe input.txt output.txt 并且我正在管理它们Grammar.y 文件中的主要 class。在那之后,如果 yylex(); 函数找到了一些东西,我会尝试用 return 做一些正则表达式。我正在提供代码,但到目前为止我遇到了 2 个问题。

首先,编译器指出我没有声明标记,但我已将它们放入 .y 文件中,如下所示。

其次,我补充说我不知道​​自己做错了什么,它根本不起作用,所以任何建议都会有所帮助。

这是我的 Grammar.l 文件

%option noyywrap

%{
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include "Grammar.tab.h"
    #define YY_DECL int yylex(yy::parser::semantic_type *yylval)
    FILE *fout;

    using namespace std;
%}

%%

[0-9]+                          { yylval->ival = atoi(yytext); return INTEGER; }
[0-9]+"."[0-9]+ | "."?[0-9]?    { yylval->fval = atof(yytext); return FLOAT; }
[a-zA-Z0-9]+                    { yylval->sval = yytext; return STRING; }
" "*                            { return SPACE; }
"\t"*                           { return TAB; }

%%

这是 Grammar.y 文件

%language "C++"
%start root

%{
    #include <stdio.h>
    #include <stdlib.h>
    #include "Grammar.tab.h"
    #define SIZE 512

    using namespace std;

    extern "C" int yylex();
    extern "C" FILE *yyin;
    extern "C" FILE *fout;

    extern int yylex(yy::parser::semantic_type *yylval);
%}

%union{
    int ival;
    float fval;
    char* sval;
}

%token <ival> INTEGER
%token <fval> FLOAT
%token <sval> STRING
%token SPACE
%token TAB

%%

root : ;

%%

void main( int argc, char ** argv){

    if ( argc < 4 ){
        printf("\nError!!! Missing Command line arguments\nUsage exe <inputfile> <outputfile>");
        exit(1);
    }
    else{
        fopen_s(&yyin, argv[3],"r");
        if (yyin == NULL) {
            printf("3[0;31mError oppening input file.3[0m");
        }

        fopen_s(&fout, argv[4],"r");
        if (fout == NULL) {
            printf("3[0;31mError oppening output file.3[0m");
        }

        do
        {
            yylex();
        }while(!feof(yyin));
        fclose(yyin);
    }
    fclose(fout);
}

namespace yy{
    void parser::error (const location_type& loc, const std::string& msg){
        std::cerr << "error at " << loc << ": " << msg << std::endl;
    }
}

当您请求 C++ 解析器时,bison 会将令牌类型保留在全局命名空间之外。这使得用法与您在 Internet 上找到的大多数示例完全不同,这些示例采用 C 接口。

所以不能只使用INTEGER,例如,你需要指定全名:

[0-9]+                          { yylval->ival = atoi(yytext);
                                  return yy::parser::token::INTEGER; }

您可以在序言中使用 using 指令将其缩短一点。

您的编译器也会抱怨在您的 main 函数中调用 yylex。请注意,您已将 yylex 声明为:

extern int yylex(yy::parser::semantic_type *yylval);

这意味着它需要一个指向 yy::parser::semantic_type 的指针的参数(即 %union 声明所描述的 union )。那么,为了调用该函数,您需要一个指向此类对象的指针,这意味着您需要该对象的一个​​实例指向:

    yy::parser::semantic_type yylval;
    int token;
    do
    {
        token = yylex(&yylval);
        /* Here you need to do something in order to print
         * the token and possibly the associated semantic value.
         * So you'll probably need something like this:
         */
        switch(token) {
          case yy::parser::token::INTEGER {
            fprintf(fout, "INTEGER: %d\n", yylval->ival);
            break;
          }
          // ...
          case 0: break;
        }            
    } while(token);

请注意,我更改了 feof 测试,以便循环在 yylex returns 0 时终止,这就是 yylex 表示它已到达终点的方式输入的。