【问题标题】:type error on compilation with flex and bison使用 flex 和 bison 编译时出现类型错误
【发布时间】:2013-07-19 14:35:02
【问题描述】:

我必须使用 flex 和 bison 实现表达式树的解析器(如“(a > b) AND (c

在 g++ 编译 parser.y.c 文件时发生致命错误(由该命令生成:“bison -o parser.y.c -d parser.y”):

parser.y:54:36: erreur: request for member ‘nodeVal’ in ‘*(yyvsp + -8u)’, which is of pointer type ‘Node*’ (maybe you meant to use ‘->’ ?)
parser.y:58:14: erreur: request for member ‘nodeVal’ in ‘yyval’, which is of pointer type ‘Node*’ (maybe you meant to use ‘->’ ?)
parser.y:58:55: erreur: request for member ‘strVal’ in ‘*(yyvsp + -16u)’, which is of pointer type ‘Node*’ (maybe you meant to use ‘->’ ?)
parser.y:58:82: erreur: request for member ‘strVal’ in ‘* yyvsp’, which is of pointer type ‘Node*’ (maybe you meant to use ‘->’ ?)
parser.y:59:14: erreur: request for member ‘nodeVal’ in ‘yyval’, which is of pointer type ‘Node*’ (maybe you meant to use ‘->’ ?)
parser.y:59:55: erreur: request for member ‘strVal’ in ‘*(yyvsp + -16u)’, which is of pointer type ‘Node*’ (maybe you meant to use ‘->’ ?)

还有一个我不明白的警告: parser.lex:35:警告,la règle ne peut être pairée [英语:“规则无法匹配”]

希望有人能帮帮我!

这里是 parser.y 文件:

%{

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iostream>

#include "Node.h"
#include "parser.lex.h"

#define YYSTYPE Node*

int yyerror(char *s) {
    printf("%s\n",s);
}

extern "C++"
{
    int yyparse(void);
    int yylex(void);
    Node * rootNode;
}

%}

%union {
    Node * nodeVal;
    char * strVal;
}

%token <strVal> IDENT
%token <strVal> LT GT LE GE EQ NE
%token <strVal> AND OR
%token <strVal> LEFT_PARENTHESIS RIGHT_PARENTHESIS
%token FIN

%left   LT GT LE GE EQ NE
%left   AND OR

%type<nodeVal> Expression

%start Input
%%

Input:
          /* Vide */
        | Input Ligne
        ;

Ligne:
          FIN
        | Expression FIN                { rootNode = $1; }
        ;

Expression:
          IDENT LT IDENT  { $$=new Node("<", $1, $3); }
        | IDENT GT IDENT  { $$=new Node(">", $1, $3); }
        | IDENT LE IDENT  { $$=new Node("<=", $1, $3); }
        | IDENT GE IDENT  { $$=new Node(">=", $1, $3); }
        | IDENT EQ IDENT  { $$=new Node("=", $1, $3); }
        | IDENT NE IDENT  { $$=new Node("!=", $1, $3); }
        | Expression AND Expression  { $$=new Node("AND", $1, $3); }
        | Expression OR Expression  { $$=new Node("OR", $1, $3); }
        | LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS        { $$=$2; }
        ;

%%

void parse_string(const std::string & str)
{
    yy_scan_string(str.c_str());
    yyparse();
}

然后是 parser.lex 文件:

%{

#define YYSTYPE Node*

#include <cstdlib>

#include "BooleanNode.h"
#include "AttributeNode.h"
#include "parser.y.h"

extern "C++"
{
    int yylex(void);
}

%}

%option noyywrap

blancs          [ \t]+

ident           [a-zA-Z_]{1}[a-zA-Z0-9_]*

%%

{ident}         { return(IDENT); }

"<"    return(LT);
">"    return(GT);
"<="   return(LE);
">="   return(GE);
"="    return(EQ);
"!="   return(NE);

"AND"  return(AND);
"OR"   return(OR);

"("    return(LEFT_PARENTHESIS);
")"    return(RIGHT_PARENTHESIS);

"\n"  return(FIN);

最后是 Node.h 文件:

#ifndef _NODE_H_
#define _NODE_H_

#include <string>
#include <iostream>


class Node
{
public:

    enum E_op
    {
        AND = 0,
        OR,
        LT,
        GT,
        LE,
        GE,
        EQ,
        NE
    };

    Node(const std::string & op)
    {
        _op = op;
    }

    Node(const std::string & op, const std::string & left, const std::string & right)
    {
        _op = op;
    }

    Node(const std::string & op, Node * left, Node * right)
    {
        _op = op;
    }

    virtual ~Node()
    {

    }

    virtual void print() {}

protected:

    std::string _op;
};


#endif

更新

感谢 Jonathan Leffler 和其他一些更正(char* 而不是 %union 中的 std::string),编译顺利,但结果不是我所期望的。 使用“foo

** 新更新 **

我通过拆分表达式指令更正了错误:

Expression:
          id LT id  { $$ = new Node("<", $1, $3); }
        | id GT id  { $$ = new Node(">", $1, $3); }
        | id LE id  { $$ = new Node("<=", $1, $3); }
        | id GE id  { $$ = new Node(">=", $1, $3); }
        | id EQ id  { $$ = new Node("=", $1, $3); }
        | id NE id  { $$ = new Node("!=", $1, $3); }
        | Expression AND Expression  { $$ = new Node("AND", $1, $3); }
        | Expression OR Expression   { $$ = new Node("OR", $1, $3); }
        | LEFT_PARENTHESIS Expression RIGHT_PARENTHESIS   { $$ = $2; }
        ;

id:
          IDENT     { $$ = strdup(yytext); }

【问题讨论】:

    标签: c++ bison flex-lexer


    【解决方案1】:

    问题是您已经通过#define YYSTYPE Node * 定义声明了Yacc 堆栈包含Node * 元素,但是您的%union%token%type 声明说有StrVal 和@联合内的 987654328@ 类型。

    IIRC,当你不使用%union 时,你只使用YYSTYPE。删除该行应该可以解决其他问题。

    【讨论】:

    • 编译正常,但我仍然有问题(在更新部分中描述):(
    • class Node 中的构造函数似乎忽略了操作数,只记录了运算符;这可能导致观察到的行为。在没有准确代码的情况下弄清楚为什么您会看到您所看到的内容将很难,非常困难。请查看如何提供 SSCCE (Short, Self-Contained, Correct Example)。
    • 上图的node类是极简的,真实的已经完成了但是还是会报错。我发现错误出在哪里,我会更新问题。
    猜你喜欢
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多