Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
语法分析中的bison错误_Bison - Fatal编程技术网

语法分析中的bison错误

语法分析中的bison错误,bison,Bison,我对此有点困惑。。。我对野牛的语法如下 %% program: | program func_defn ; datatype: INTEGER { $$ = currentType = Type::IntegerTy; } | FLOAT { $$ = currentType = Type::FloatTy; } | VOID { $$ = currentType = Type::V

我对此有点困惑。。。我对野牛的语法如下

%%
program:
    | program func_defn
    ;

datatype: INTEGER          { $$ = currentType = Type::IntegerTy; }
    | FLOAT                { $$ = currentType = Type::FloatTy; }
    | VOID                 { $$ = currentType = Type::VoidTy; }
    | STRING_LITERAL       { $$ = currentType = Type::StringTy; }
    ;

retlist : datatype
    {
        builder->pushDataType(&getType($1));
    }
    ;

arglist: datatype IDENTIFIER 
    {
        builder->pushDataType(&getType($1));
        Symbol *sym = builder->addSymbol($2, getType($1));
        builder->pushArgName(sym);
    }
    | arglist ',' datatype IDENTIFIER 
    {
        builder->pushDataType(&getType($3));
        Symbol *sym = builder->addSymbol($4, getType($3));
        builder->pushArgName(sym);
    }
    |
    ;
//as and when we find the identifiers , we need to add them to a list and use them while constructing the prototype/func

func_defn: FUNCTION IDENTIFIER LSQUARE retlist RSQUARE '(' arglist ')' '{' 
    {
        const std::string& string = $5;
        FunctionProtoType* fp = builder->getProtoType(string);//use current dataTypeList

        /*
        if(fp == NULL) //find the prototype in the module. if not found, add a new one
            builder->addProtoType(string, getType($1), &fp);
        */

        IcErr err = builder->addFunction(*fp);
        if(err != eNoErr)
            yyerror(errMsg[err]);
    }
    ;

%%
我正在尝试解析这一行

function first_method [int] (int a, int b) {
然而,这似乎根本无法解析。我没有任何改变或减少冲突

Compiling file: ica_test\func_defn.icaStarting parse
Entering state 0
Reducing stack by rule 1 (line 80):
-> $$ = nterm program (1.1: )
Stack now 0
Entering state 1
Reading a token: Next token is token FUNCTION (1.1: )
Shifting token FUNCTION (1.1: )
Entering state 3
Reading a token:  Next token is token IDENTIFIER (1.1: )
Shifting token IDENTIFIER (1.1: )
Entering state 5
Reading a token: _Next token is token IDENTIFIER (1.1: )

function first_method [int] (int a, int b) {
          ^
Error on Line 1, Column 9: syntax error, unexpected IDENTIFIER, expecting LSQUAR
EError: popping token IDENTIFIER (1.1: )
Stack now 0 1 3
Error: popping token FUNCTION (1.1: )
Stack now 0 1
Error: popping nterm program (1.1: )
Stack now 0
Cleanup: discarding lookahead token IDENTIFIER (1.1: )
Stack now 0

Stopping compilation as we found some syntax errors in ica_test\func_defn.ica
!Press any key to continue . . .

我猜您的
标识符的lexer规则
不能包含
(下划线)字符,因此
first\u方法
被标记为标识符
first
,然后
与任何规则都不匹配,因此会回显到stdout(输出中
Next
之前的多余下划线)。然后它获取另一个标识符(
方法
),这会导致语法错误,因为它需要一个
[

我说“猜猜”,因为你没有显示你的lexer代码,但看起来很有可能

您应该通过修改
标识符
规则来解决这个问题,但您还应该添加一个最终的lexer规则,如:

.        { return *yytext; }

匹配任何单个字符并将其返回到解析器,而不是将其回显到输出。这样,您就不会将随机字符回显到输出并被忽略,而且您将得到更合理的语法错误,它们将作为标记显示在YYDEBUG文本输出中,这样您就知道发生了什么。

我猜您的lex
IDENTIFIER
的er规则不能包含
(下划线)字符,因此
first\u方法
被标记为标识符
first
,然后
与任何规则都不匹配,因此会回显到stdout(输出中
Next
之前的多余下划线)。然后它会获得另一个标识符(
方法
),这会导致语法错误,因为它需要一个
[

我说“猜猜”,因为你没有显示你的lexer代码,但看起来很有可能

您应该通过修改
标识符
规则来解决这个问题,但您还应该添加一个最终的lexer规则,如:

.        { return *yytext; }

匹配任何单个字符并将其返回到解析器,而不是将其回显到输出。这样,您就不会将随机字符回显到输出并被忽略,而且您会得到更合理的语法错误,它们将作为标记显示在YYDEBUG文本输出中,这样您就知道发生了什么。

y有什么问题吗我们的
标识符
定义?-看起来在
函数
之后可以识别两个标识符。实际上只存在一个定义..%token IDENTIFIER,标识符在您的lex文件中是如何定义的?当@500 InternalServerError被识别时,您的问题似乎是您的词法分析器在一行中生成两个“标识符”对于“函数优先_方法”,这令人费解。它是否在下划线上拆分?至少,您需要跟踪
yylex()
返回到您的解析器,找出您获得双重标识符的原因。您的
标识符定义是否有问题?
函数之后似乎识别出两个标识符。实际上只存在一个定义。..%token identifier以及如何在您的lex文件中定义标识符?如@500再次确认错误,您的问题似乎是您的词法分析器在一行中为“函数优先”方法生成了两个“标识符”,这令人费解。它是否在下划线上拆分?至少,您需要跟踪
yylex()
返回给您的解析器的内容,以找出获得双标识符的原因。