[英]type error on compilation with flex and bison
I have to implement a parser of expression tree (like "(a > b) AND (c <= d)") using flex and bison, but I fails to solve type errors... 我必须使用flex和bison实现表达式树的解析器(例如“(a> b)AND(c <= d)”),但是我无法解决类型错误。
The fatal errors occur during the g++ compilation of the parser.yc file (which is generated by this command : "bison -o parser.yc -d parser.y") : 致命错误发生在parser.yc文件的g ++编译过程中(由以下命令生成:“ bison -o parser.yc -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 ‘->’ ?)
There is also a warning that I don't understand : parser.lex:35: warning, la règle ne peut être pairée [english : "rule cannot be matched"] 还有一个我不理解的警告:parser.lex:35:警告,警告:[英语:“规则无法匹配”]
I hope someone can help me ! 我希望有一个人可以帮助我 !
Here, the parser.y file : 在这里,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();
}
then the parser.lex file : 然后是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);
and finally the Node.h file : 最后是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
UPDATE UPDATE
Thanks to Jonathan Leffler and some others corrections (char* instead of std::string in %union), the compilation goes well but the result is not what I expected. 多亏了Jonathan Leffler和其他一些更正(%union中的char *而不是std :: string),编译进展顺利,但结果不是我期望的。 With the "foo < bar" expression, the "IDENT LT IDENT" directive is executed but the value of $1 and $3 is NULL...
使用“ foo <bar”表达式,执行“ IDENT LT IDENT”指令,但$ 1和$ 3的值为NULL ...
** NEW UPDATE ** **新更新**
I corrected the error by splitting the Expression directive : 我通过拆分Expression指令纠正了错误:
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); }
The problem is that you've declared that the Yacc stack contains Node *
elements via the #define YYSTYPE Node *
define, but your %union
, %token
and %type
declarations say that there are StrVal
and NodeVal
types within the union. 问题是您已经通过
#define YYSTYPE Node *
定义声明了Yacc堆栈包含Node *
元素,但是您的%union
, %token
和%type
声明说联合中存在StrVal
和NodeVal
类型。
IIRC, you only use YYSTYPE
when you do not use %union
. IIRC,仅在不使用
%union
时才使用YYSTYPE
。 Removing that line should resolve the other problems. 删除该行应解决其他问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.