std::shared_ptr mit yacc



  • Hi,

    ich habe ein paar schwierigkeiten shared_ptrs in mein yacc file einzubauen (ohne boost)...wie bekomme ich das am besten hin?

    mein background research hat folgendes ergeben:

    %union in yacc/bison only allows POD types...hmm
    
    %{
    #include <cstdio>
    #include <cstdlib>
    #include <cassert>
    #include <memory>
    #include "Token.h"
    #include "Tree.h"
    
    extern void yyerror(char* message);
    extern int yylex(void);
    extern int yyparse(void);
    
    %}
    
    %union {
    	int ival;	/* NUMBER */
    	int opval;	/* ADDOP MULOP */
    	Tree *tval;
    }
    
    %left <opval> 	ADDOP
    %left <opval> 	MULOP
    %token <ival>	NUMBER
    
    %type <tval> expr
    
    %%
    
    expr_list
    	: expr_list expr '\n' 
    		{ $2->print(0); 
    		  fprintf(stderr, "Value = %d\n", eval($2)); }
    	| expr '\n' 
    		{ $1->print(0); 
    		  fprintf(stderr, "Value = %d\n", eval($1)); }
    
    expr 
    	: expr ADDOP expr 
    		{ $$ = new Tree(Token(ADDOP,$2),$1,$3); }
    	| expr MULOP expr 
    		{ $$ = new Tree(Token(MULOP,$2),$1,$3); }
    	| '(' expr ')'
    		{ $$ = $2; }
    	| NUMBER
    		{ $$ = new Tree(Token(NUMBER,$1),nullptr,nullptr); }
    	;
    
    %%
    

    ich moechte:

    new Tree(Token(NUMBER,$1),nullptr,nullptr)
    

    durch:

    std::make_shared<Tree>(new Tree(Token(ADDOP,$2),$1,$3))
    


  • Was für ein yacc benutzt Du denn?



  • ich bin hier auf osx und windows.

    auf osx verwende ich: yacc + lex: http://www.unix.com/man-page/osx/1/yacc/
    auf windows verwende ich: bison + flex



  • Also bison in beiden Fällen, wenn ich Deinen Link richtig interpretiere.

    Dann steht doch einiges in der Doku dazu: http://www.gnu.org/software/bison/manual/html_node/C_002b_002b-Variants.html#C_002b_002b-Variants



  • ich weiss trotzdem nicht wie ich shared_ptr einbauen kann ohne boost::variant!?



  • hab das hier auf osx:

    $ yacc --version
    bison (GNU Bison) 2.3
    Written by Robert Corbett and Richard Stallman.
    
    Copyright (C) 2006 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    $ flex --version
    flex 2.5.35 Apple(flex-31)
    


  • mit folgenden change bekomme ich diesen fehler... vorschlaege/loesungen?

    y.tab.cpp:1123:11: error: call to implicitly-deleted default constructor of 'YYSTYPE'
      YYSTYPE yyval;
    
    %{ 
    #include <cstdio> 
    #include <cstdlib> 
    #include <cassert> 
    #include <memory> 
    #include "Token.h" 
    #include "Tree.h" 
    
    extern void yyerror(char* message); 
    extern int yylex(void); 
    extern int yyparse(void); 
    
    %} 
    
    %union { 
        int ival;   /* NUMBER */ 
        int opval;  /* ADDOP MULOP */ 
        std::shared_ptr<Tree> tval; 
    } 
    
    %left <opval>   ADDOP 
    %left <opval>   MULOP 
    %token <ival>   NUMBER 
    
    %type <tval> expr 
    
    %% 
    
    expr_list 
        : expr_list expr '\n' 
            { $2.get()->print(0); 
              fprintf(stderr, "Value = %d\n", eval($2.get())); } 
        | expr '\n' 
            { $1.get()->print(0); 
              fprintf(stderr, "Value = %d\n", eval($1.get())); } 
    
    expr 
        : expr ADDOP expr 
            { $$ = std::make_shared<decltype($$)>(new Tree(Token(ADDOP,$2),$1,$3)); } 
        | expr MULOP expr 
            { $$ = std::make_shared<decltype($$)>(new Tree(Token(MULOP,$2),$1,$3)); } 
        | '(' expr ')' 
            { $$ = $2; } 
        | NUMBER 
            { $$ = std::make_shared<decltype($$)>(new Tree(Token(NUMBER,$1),nullptr,nullptr)); } 
        ; 
    
    %%
    


  • geri1 schrieb:

    $$ = std::make_shared<decltype($$)>(new Tree(Token(ADDOP,$2),$1,$3));
    

    make_shared will keinen Pointer haben, sondern die Argumente, um den selber anzulegen.
    siehe [url] http://www.cplusplus.com/reference/memory/make_shared [/url]

    also eher so:

    $$ = std::make_shared<Tree>(Token(ADDOP, $2), $1, $3));
    


  • ok, habe deine changes eingebaut. jedoch bekomme ich noch folgende errors:

    $ mingw32-make clean; mingw32-make
    rm -f y.tab.* lex.yy.* y.output y.tab.o lex.yy.o Token.o Tree.o Eval.o main.o ma
    in.exe
    bison  -dv -o y.tab.cpp calc.y
    g++ -g -Wall -std=gnu++11   -c -o y.tab.o y.tab.cpp
    y.tab.cpp:1058:9: error: use of deleted function 'YYSTYPE::YYSTYPE()'
     YYSTYPE yylval;
             ^
    y.tab.cpp:123:15: note: 'YYSTYPE::YYSTYPE()' is implicitly deleted because the d
    efault definition would be ill-formed:
     typedef union YYSTYPE
                   ^
    calc.y:18:24: error: union member 'YYSTYPE::tval' with non-trivial 'constexpr st
    d::shared_ptr<_Tp>::shared_ptr() [with _Tp = Tree]'
      std::shared_ptr<Tree> tval;
                            ^
    y.tab.cpp: In function 'int yyparse()':
    y.tab.cpp:1110:30: error: use of deleted function 'YYSTYPE::YYSTYPE()'
         YYSTYPE yyvsa[YYINITDEPTH];
                                  ^
    y.tab.cpp:1110:30: error: use of deleted function 'YYSTYPE::~YYSTYPE()'
    y.tab.cpp:123:15: note: 'YYSTYPE::~YYSTYPE()' is implicitly deleted because the
    default definition would be ill-formed:
     typedef union YYSTYPE
                   ^
    calc.y:18:24: error: union member 'YYSTYPE::tval' with non-trivial 'std::shared_
    ptr<Tree>::~shared_ptr()'
      std::shared_ptr<Tree> tval;
                            ^
    y.tab.cpp:1110:30: error: use of deleted function 'YYSTYPE::~YYSTYPE()'
         YYSTYPE yyvsa[YYINITDEPTH];
                                  ^
    y.tab.cpp:1122:11: error: use of deleted function 'YYSTYPE::YYSTYPE()'
       YYSTYPE yyval;
               ^
    y.tab.cpp:1122:11: error: use of deleted function 'YYSTYPE::~YYSTYPE()'
    y.tab.cpp:1296:12: error: use of deleted function 'YYSTYPE& YYSTYPE::operator=(c
    onst YYSTYPE&)'
       *++yyvsp = yylval;
                ^
    y.tab.cpp:123:15: note: 'YYSTYPE& YYSTYPE::operator=(const YYSTYPE&)' is implici
    tly deleted because the default definition would be ill-formed:
     typedef union YYSTYPE
                   ^
    calc.y:18:24: error: union member 'YYSTYPE::tval' with non-trivial 'std::shared_
    ptr<_Tp>& std::shared_ptr<_Tp>::operator=(const std::shared_ptr<_Tp>&) [with _Tp
     = Tree]'
      std::shared_ptr<Tree> tval;
                            ^
    y.tab.cpp:1326:9: error: use of deleted function 'YYSTYPE& YYSTYPE::operator=(co
    nst YYSTYPE&)'
       yyval = yyvsp[1-yylen];
             ^
    y.tab.cpp:1388:12: error: use of deleted function 'YYSTYPE& YYSTYPE::operator=(c
    onst YYSTYPE&)'
       *++yyvsp = yyval;
                ^
    y.tab.cpp:1414:35: warning: deprecated conversion from string constant to 'char*
    ' [-Wwrite-strings]
           yyerror (YY_("syntax error"));
                                       ^
    y.tab.cpp:1528:12: error: use of deleted function 'YYSTYPE& YYSTYPE::operator=(c
    onst YYSTYPE&)'
       *++yyvsp = yylval;
                ^
    y.tab.cpp:1557:35: warning: deprecated conversion from string constant to 'char*
    ' [-Wwrite-strings]
       yyerror (YY_("memory exhausted"));
                                       ^
    y.tab.cpp: In function 'void __static_initialization_and_destruction_0(int, int)
    ':
    y.tab.cpp:1058:9: error: use of deleted function 'YYSTYPE::~YYSTYPE()'
     YYSTYPE yylval;
             ^
    y.tab.cpp: In function 'void __tcf_0()':
    y.tab.cpp:1058:9: error: use of deleted function 'YYSTYPE::~YYSTYPE()'
    <builtin>: recipe for target 'y.tab.o' failed
    mingw32-make: *** [y.tab.o] Error 1
    


  • folgender thread erklaert das porblem:

    http://stackoverflow.com/questions/26964941/lex-yacc-no-member-name-and-declaration-error
    

    wie kann ich das problem fixen?



  • help!?


Anmelden zum Antworten