I've been asked to make an expression evaluator using Composite, Recursive Descendent Parser and Interpreter.
Here's the grammar :
<cond> → <termb> [OR <termb>]*
<termb>→<factb>[AND <factb>]*
<factb>→<expr> RELOP <expr> | NOT <factb> | OPAR <cond> CPAR
<expr> → [PLUS | MINUS] <term> [(PLUS <term>) | (MINUS <term>)]*
<term> → <termp> [(MULT <termp>) | (DIV <termp>) | (REM <termp>)]*
<termp> → <fact> [POWER <fact>]*
<fact> → ID | NUM | OPAR1 <expr> CPAR1
----TERMINALS----
ID → ("A" | ... | "Z" | "a" | ...| "z") [("A"| ... | "Z" | "a" | ...| "z" | "0" | ... | "9")]*
NUM → ("0" | ... | "9") [("0" | ... | "9")]*
OPAR → "("
CPAR → ")"
OPAR1 → "["
CPAR1 → "]"
RELOP → EQ | NEQ | GT | GE | LT | LE
EQ → "= ="
NEQ → "!="
GT → ">"
GE → ">="
LT → "<"
LE → "<="
POWER → "^"
DIV → "/"
REM → "%"
MULT → "*"
MINUS → "−"
PLUS → "+"
AND → “and” or “&&”
OR → “or” or “||”
NOT → “not” or “!”
The assignment is:
The goal of the project, based on Composite, Recursive Builder and Interpreter, is to get a conditional expression, do a syntax analysis and build its composite tree. Starting from the tree, you've got to evaluate the result of the condition, based on an external context (read from a properties file) that contains the value of the internal variables
Now, the first thing that I noticed is that Interpreter uses a Composite structure, so it seemed a good idea to extend my Composite structure with an evaluate(:Context) method.
I've asked around, but I've been told that this is not the way to do the assignment. Seems like I've got build the Interpreter tree, starting from the Composite one (which is quite nonsens for me, as I've already got a tree to work with!).
So I've built my tree using Composite + Recursive Builder, it recognizes the input and it build the tree without any kind of problem.
But the question is : how do I apply Interpreter to my structure?
Here's my class diagram (something is Italian but it's quite understandable)
If I got it right, Interpreter uses a class for each grammar rule, so I've got to make a cond class, then a termb and so on.
But ho do I link them to my composite?