Adding constraints in grammar for Grammatical Evolution
You can enforce that units match by duplicating nonterminals, one per unit.  For instance, instead of var, you can have varA and varB (if A and B are the two types of units); instead of expr, you can have exprA and exprB; with rules like &lt;varA&gt; ::= Close, &lt;varB&gt; ::= Volume, &lt;exprA&gt; ::= ...|&lt;exprA&gt;&lt;matbi&gt;&lt;exprA&gt;|...|varA, &lt;exprB&gt; ::= ...|&lt;exprB&gt;&lt;matbi&gt;&lt;exprB&gt;|...|varB, and so on.

There's no good way to modify the grammar to ensure that it cannot generate any expression that would evaluate to 0.  That is best handled by other methods, not by changing the grammar.