The problem that I am chewing on comes from parsing, i.e. constructing objects in a sequential manner. The grammar is not prefix free, that is, there are more than one syntactical elements sharing the same sequence of terminals at the beginning. To condense it to a simple example, lets say we have
foo = "bar" ";" foo_special = foo "baz" ";"
The implementation language is object oriented in the C++ fashion, that is, a compile-time defined class definition with data elements.
Moreover the problem domain at first sight (that is, how I view it currently)
makes it very natural and concise to handle non-terminals as objects of their respective classes and
foo_special a specialisation of
foo. My naive mind therefore yelled inheritance! and I implemented it accordingly. You may already guess where all this leads to:
When I see "bar" in the parsing stream, I have no idea if I should construct a
Put differently, what is the generally preferred solution in a situation where you can't immediately decide on the object to construct but the possible alternatives share enough similarities to make code sharing via hierarchy attractive?