Flex和Bison C ++

问题描述

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

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

空格> *

整数> 12

空格> *

浮动> 4.5

问题是我试图在所有这些内容的下面进行选择,并且我从读取输入文件输出文件并将其打开的位置开始。我正在使用 Visual Studio ,所以我将命令行参数添加read exe input.txt output.txt,并在Grammar.y的主类中进行管理文件。之后,我尝试使用一些yylex();函数找到内容时返回的正则表达式。我正在提供代码,但到目前为止,我有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("\033[0;31mError oppening input file.\033[0m");
        }

        fopen_s(&fout,argv[4],"r");
        if (fout == NULL) {
            printf("\033[0;31mError oppening output file.\033[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 ++解析器时,野牛会将令牌类型保留在全局名称空间之外。这使用法与您在Internet上找到的大多数假定C接口的示例完全不同。

因此,您需要指定全名,而不是仅使用map

INTEGER

您可以在序言中使用[0-9]+ { yylval->ival = atoi(yytext); return yy::parser::token::INTEGER; } 指令将其缩短一点。

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

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返回0时终止,这是yylex表示已到达输入结尾的方式。