I have to write an interpreter for tristate logic as a Uni assignment.
I got most of the things down, like expression tree and such, there is just one thing that's bugging me, that is input, or a particular part of it.

The thing is, in the expression there can be a reference to another expression (they are numbered). My problem is, how does one expression know about the address of another?

I can see two approaches to this, neither satisfactory.
One thing is to keep a vector of pointers as a static class member, second is to move parsing outside of the class, which incurs writing much more code.

If I have been unclear, consider this sample input. S followed by a number is an expression or a reference to one.
Oh yeah, forgot to mention, this is Polish (prefix) notation.

S1 = OR T x1
S0 = AND S2 S1
print S0
S1 = AND F x0
S3 = ARG2 S0
S4 = OR S1 x0

Recommended Answers

All 4 Replies

If I understand your problem correctly, you need a symbol table, which maps expression name to expression instance. std::map would do it just fine.

The expressions are numbered, so a normal vector of pointers will do as well :)
The problem is where to put that vector - outside the class, which destroys my object approach, or inside as a static member, which seems ugly to me.

I don't think having a symbol table outside the class (I suppose we are talking of expression class) would destroy anything. It is a very standard application of a singleton.

Yeah, but then I wanted to have the reading routine as a method, since it can be so nicely written recursively, but maybe that's just me being stubborn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.