c++ - type error on compilation with flex and bison -


i have implement parser of expression tree (like "(a > b) , (c <= d)") using flex , bison, fails solve type errors...

the fatal errors occur during g++ compilation of parser.y.c file (which generated command : "bison -o parser.y.c -d parser.y") :

parser.y:54:36: erreur: request member ‘nodeval’ in ‘*(yyvsp + -8u)’, of pointer type ‘node*’ (maybe meant use ‘->’ ?) parser.y:58:14: erreur: request member ‘nodeval’ in ‘yyval’, of pointer type ‘node*’ (maybe meant use ‘->’ ?) parser.y:58:55: erreur: request member ‘strval’ in ‘*(yyvsp + -16u)’, of pointer type ‘node*’ (maybe meant use ‘->’ ?) parser.y:58:82: erreur: request member ‘strval’ in ‘* yyvsp’, of pointer type ‘node*’ (maybe meant use ‘->’ ?) parser.y:59:14: erreur: request member ‘nodeval’ in ‘yyval’, of pointer type ‘node*’ (maybe meant use ‘->’ ?) parser.y:59:55: erreur: request member ‘strval’ in ‘*(yyvsp + -16u)’, of pointer type ‘node*’ (maybe meant use ‘->’ ?) 

there warning don't understand : parser.lex:35: warning, la règle ne peut être pairée [english : "rule cannot matched"]

i hope can me !

here, parser.y file :

%{  #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> , or %token <strval> left_parenthesis right_parenthesis %token fin  %left   lt gt le ge eq ne %left   , 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 , 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 parser.lex file :

%{  #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 node.h file :

#ifndef _node_h_ #define _node_h_  #include <string> #include <iostream>   class node { public:      enum e_op     {         , = 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

thanks jonathan leffler , others corrections (char* instead of std::string in %union), compilation goes result not expected. "foo < bar" expression, "ident lt ident" directive executed value of $1 , $3 null...

** new update **

i corrected error splitting expression directive :

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 , 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 you've declared yacc stack contains node * elements via #define yystype node * define, %union, %token , %type declarations there strval , nodeval types within union.

iirc, use yystype when not use %union. removing line should resolve other problems.


Comments