Interpreter: A way to include language elements in a program. Given
a language, define a representation for its grammar along with an
interpreter that uses the representation to interpret sentences in the
language.
This pattern doesn’t use very often. It can be thought as a special case of
composite pattern.
You need to have a grammar. This is start symbol is Expression.
VarExp is leaf. That is a context free grammer
With this grammer, you can design class diagram as below:
For this prammer, You need to parse a sentence defined by this
grammar. The last purpose of parsing is to calculate the value of
expression. So interprete() should be a calculate function. and
interprete will be called recursively.
parse function is not key problem to be solved by Interpreter patten.
For regular grammar, you can use FST, for simple arithmetic( this
example), You can use stack and scan a terminal in an expression from
left to right.