Parsing into polymorphic objects (X3)

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Parsing into polymorphic objects (X3)

Kádár, Balázs

I've seen some question on the subject, but the answers weren't very
convincing ("don't use polymorphic objects"), and it may have changed
with X3.

I gave it a try, and created a parser for a simple Shapes grammar. [1]
The interface is fixed: the parser gets a string and should return a
unique_ptr to the base class that can be used polymorphically by the
client. The grammar in the example is actually too simple, in the real
problems the exact type to return may be decided only at the end of the

I tried two approaches one uses semantic actions to create the objects
I want directly, the other parses into structs representing the AST and
then transforms them into the correct objects.

Polymorphic approach
In branch polymorphic of [1] I made the attribute of the rule
std::unique_ptr<Shape>. To compile this I had to patch the library to
perfect-forward the attribute in call_rule_definition [2].
The semantic actions are definitely not nice in this solution... but
maybe it could be a bit better if I knew Fusion better?

AST approach
Branch ast in [1]. The grammar itself is really better without the
semantic actions, but I really don't want to duplicate my polymorphic
objects into structs and then repeat them again for
BOOST_FUSION_ADAPT_STRUCT. This way if I change an object, I have to do
it in 3 places. If I want to add a new one, that's changes in 5 places
(derived object, AST struct, BOOST_FUSION_ADAPT_STRUCT, the variant and
the factory that turns the AST object into the polymorphic one).

Is there a way to improve either solution? Or is there an even better
approach for such problems?

Just some additional notes.
All-in-all X3 looks like a great improvement over qi (I even got some
comprehensible compile errors!). But there's a minor thing that's
bugging me (especially visible in the ast implementation) – creating a
rule is somewhat verbose:
    rule<struct X, AST::X> x = "x";
    auto x_def = ...
That's a total of 6 exes.

I also tried to compile the example on Windows using Visual Studio
2015. Although one of my earlier tries seemed to work, it has trouble
compiling this example project. Is there hope to have X3 work with

1. On the ast branch VS2015 has some problems with decltype in
    error C2995: 'bool `anonymous-namespace'::ShapeGrammar::parse_rule
    (unknown-type,Iterator &,const Iterator &,const Context &,Attribute
    &)': function template has already been defined
Note unknown-type as the first parameter in the error message. After
replacing the macro call with its expansion and replacing decltype with
the actual type it compiles correctly.

2. On the polymorphic branch after doing a similar fix as above it
tries to call the createTriangle lambda with 0 parameters instead of 1.

Thank you for your help,

Balázs Kádár


Mit freundlichen Grüßen

Balázs Kádár
Dwp Software Kft im Auftrag der dwpbank

Deutsche WertpapierService Bank AG
Hermina ut 17
1146 Budapest
Tel.: +49 69 5099 8405
Fax: +49 69 5099 85 8405
E-Mail: [hidden email]

Deutsche WertpapierService Bank AG | Wildunger Straße 14 | 60487 Frankfurt am Main
Sitz der AG: Frankfurt am Main, HRB 56913 | USt.-ID: DE 813759005
Vorstand: Thomas Klanten, Dr. Christian Tonnesen
Aufsichtsrat: Wilfried Groos (Vors.)

Spirit-general mailing list
[hidden email]