C++11 std::unique_ptr as semantic value in Bison C++ mode

Recently I spent some time to write a DSL ( domain specific Language) lexer and parser.

Without double flex, bison (C++) will be used.  Bison ( version 3.0.2/3.04) generally support C++ object as the semantics value.  That is a good thing  since we do not need  %destructor  to enable memory deallocation during error recovery; the memory, for strings for instance, will be reclaimed by the regular destructors.

But when we want to create a large C++ object while parsing, we want to pass pointers instead of copying object. The raw new/delete is urgly,  thus naturally we want to use std::unique_ptr as the semantics value. Since std::unique_ptr is a object, bison C++ should support it, right?

It turned out that Bison 3.0.3/3.0.4 does not support well using std::unqiue_ptr as semantic value.  It can not even compile,  it need one hack!  we need to  replace one line with std::move, thanks  Sasah ( from bison mailing list)! !

— file.hh
+++ file.hh
@@ -178 +178 @@
– return *new (yyas_<T> ()) T (t);
+ return *new (yyas_<T> ()) T (std::move((T&)t));

we can use sed to replace it: sed -i ‘s/return \*new (yyas_<T> ()) T (t)/return \*new (yyas_<T> ()) T (std\:\:move((T\&)t))/’ your_file.hh

Now it compiles, and the parser works.

More details can be see at:



Hopefully in the future bison will officially support the std::unique_ptr!


Please rate this

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>