I am trying to build class hierarchy for the abstract syntax tree of First Order Logic as specified in the grammar https://github.com/antlr/grammars-v4/blob/master/fol/fol.g4 (ANTLR parser generator).
I have 2 options for class hierarchy:
expression->lhs|operation|rhs has constructors (whose arguments are assigned to the instance attributes – I am not specifying it further, it is obvious):
FOLFormula(FOLFormula lhs, FOLOperation operation, FOLFormula rhs); //FOLFormula has several other constructors FOLOperationt(); //instances can be: or, and, implication, biconditional
expression->operation->lhs|rhs has constructors:
FOLFormula(FOLOperation operation); //FOLFormula has several other consturctors FOLOperation(FOLFormula lhs, FOLFormula rhs);
I am resonating in my decision which hierarchy to choose? From the web examples, that implement calculators, I can see that majority prefers
expression->lhs|operation|rhs, but I am uneasy by this choice, because it strictly says that operation is binary operation, but we know, that binary operation is just special kind of n-ary function for boolean arguments and boolean return type. I have found 2 other arguments why
expression->lhs|operation|rhs is preferable (and not the
- All the specifications of the FOL syntax writes A&B and so on and noone specifies & and &(A, B) separately, though, I am not sure why is that? Maybe it is just convention for readability and not the suggestion for the implementation;
- The mentioned grammar https://github.com/antlr/grammars-v4/blob/master/fol/fol.g4 clearly writes rule in the format
expression->lhs|operation|rhsand the parser …Condition class for this grammar rule is generated in such a way that it contains (as direct children) both operation and both its arguments (lhs, rhs) and no the 2-level hierarchy. So – I am going to use visitor pattern for the generation of AST from the Parse Tree then I should take into account the generated class structure for the concrete parse tree and follow this structure in my AST generation, it can be easier.
Are there any other arguments pro and con against each of the choices. Maybe I am just overthinking about the decision for which the academic community already has established decision and best practice?