赞
踩
dropincc.java 是
/** * EBNF of Calculator: * <pre> * calc ::= expr $ * expr ::= addend (('+'|'-') addend)* * addend ::= factor (('*'|'/') factor)* * factor ::= '(' expr ')' * | '\\d+(\\.\\d+)?' * </pre> */ public static void main(String... args) throws Throwable { Lang c = new Lang("Calculator"); Grule expr = c.newGrule(); c.defineGrule(expr, CC.EOF).action(new Action() { public Double act(Object matched) { return (Double) ((Object[]) matched)[0]; } }); TokenDef a = c.newToken("\\+"); Grule addend = c.newGrule(); expr.define(addend, CC.ks(a.or("\\-"), addend)).action(new Action() { public Double act(Object matched) { Object[] ms = (Object[]) matched; Double a0 = (Double) ms[0]; Object[] aPairs = (Object[]) ms[1]; for (Object p : aPairs) { String op = (String) ((Object[]) p)[0]; Double a = (Double) ((Object[]) p)[1]; if ("+".equals(op)) { a0 += a; } else { a0 -= a; } } return a0; } }); TokenDef m = c.newToken("\\*"); Grule factor = c.newGrule(); addend.define(factor, CC.ks(m.or("/"), factor)).action(new Action() { public Double act(Object matched) { Object[] ms = (Object[]) matched; Double f0 = (Double) ms[0]; Object[] fPairs = (Object[]) ms[1]; for (Object p : fPairs) { String op = (String) ((Object[]) p)[0]; Double f = (Double) ((Object[]) p)[1]; if ("*".equals(op)) { f0 *= f; } else { f0 /= f; } } return f0; } }); factor.define("\\(", expr, "\\)").action(new Action() { public Double act(Object matched) { return (Double) ((Object[]) matched)[1]; } }).alt("\\d+(\\.\\d+)?").action(new Action() { public Double act(Object matched) { return Double.parseDouble((String) matched); } }); Exe exe = c.compile(); System.out.println(exe.eval("1 +2+3+(4 +5*6*7*(64/8/2/(2/1 )/1)*8 +9 )+ 10")); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。