Ich bin immer noch auf my quest for a really simple language und ich weiß jetzt, dass es keine gibt. Also schreibe ich selbst mit ANTLR3.Erweitern einfache ANTLR grammer zur Unterstützung von Eingabevariablen
fand ich ein wirklich gutes Beispiel in this answer:
Exp.g:
grammar Exp;
eval returns [double value]
: exp=additionExp {$value = $exp.value;}
;
additionExp returns [double value]
: m1=multiplyExp {$value = $m1.value;}
('+' m2=multiplyExp {$value += $m2.value;}
| '-' m2=multiplyExp {$value -= $m2.value;}
)*
;
multiplyExp returns [double value]
: a1=atomExp {$value = $a1.value;}
('*' a2=atomExp {$value *= $a2.value;}
| '/' a2=atomExp {$value /= $a2.value;}
)*
;
atomExp returns [double value]
: n=Number {$value = Double.parseDouble($n.text);}
| '(' exp=additionExp ')' {$value = $exp.value;}
;
Number
: ('0'..'9')+ ('.' ('0'..'9')+)?
;
WS
: (' ' | '\t' | '\r'| '\n') {$channel=HIDDEN;}
;
Java-Code:
public Double evaluate(String string, Map<String, Double> input) throws RecognitionException {
ANTLRStringStream in = new ANTLRStringStream(string);
ExpLexer lexer = new ExpLexer(in);
CommonTokenStream tokens = new CommonTokenStream(lexer);
return new ExpParser(tokens).eval();
}
dieses ANTLR grammer Verwendung von I Ausdrücke auswerten können wie
(12+14)/2
und erhalten 13 als Ergebnis.
Nun fehlt mir nur noch eine Möglichkeit, einfache Doppelvariablen in diesen zu injizieren, so dass ich folgendes auswerten kann, indem ich {"A": 12.0, "B": 14.0} als Eingabe gebe Karte:
(A+B)/2
Irgendwelche Ideen?
danke nochmal !!! – arturh
Kein Problem, schon wieder! :) –