scala 连接符解析 ~ ^^ <~ ~> 基础
来源:互联网 发布:逆战猎场刷永久必知 编辑:程序博客网 时间:2024/06/05 07:29
scala 连接符解析 ~ ^^ <~ ~>
import scala.annotation.migrationimport scala.util.parsing.combinator.JavaTokenParsersabstract class Exprcase class Variable(name: String) extends Exprcase class Number(value: Double) extends Exprcase class UnaryOp(op: String, expr: Expr) extends Exprcase class BinaryOp(op: String, left: Expr, right: Expr) extends Exprobject Calc { object Arith extends JavaTokenParsers { def compose(bi: Expr, list: List[String ~ Expr]): Expr = list match { case Nil => bi case (op ~ e) :: t => compose(BinaryOp(op, bi, e), t) } def expr: Parser[Expr] = term ~ rep("+"~ term |"-"~ term) ^^ { case t ~ list => compose(t, list) } def term: Parser[Expr] = factor ~ rep("*"~ factor |"/"~ factor) ^^ { case t ~ list => compose(t, list) } def factor: Parser[Expr] = floatingPointNumber ^^ { x => Number(x.toDouble) } |"("~> expr <~")" def parse(str: String): Expr = { val result = parseAll(expr, str) if (result.successful) result.get else Number(0) } } def parse(str: String) = Arith.parse(str) def evaluate(e: Expr): Double = simplify(e) match { case Number(x) => x case UnaryOp("-", x) => -evaluate(x) case BinaryOp("+", x1, x2) => (evaluate(x1) + evaluate(x2)) case BinaryOp("-", x1, x2) => (evaluate(x1) - evaluate(x2)) case BinaryOp("*", x1, x2) => (evaluate(x1) * evaluate(x2)) case BinaryOp("/", x1, x2) => (evaluate(x1) / evaluate(x2)) } /* * Lex's version: */ def simplify(e: Expr): Expr = { // first simplify the subexpressions val simpSubs = e match { // Ask each side to simplify case BinaryOp(op, left, right) => BinaryOp(op, simplify(left), simplify(right)) // Ask the operand to simplify case UnaryOp(op, operand) => UnaryOp(op, simplify(operand)) // Anything else doesn't have complexity (no operands to simplify) case _ => e } // now simplify at the top, assuming the components are already simplified def simplifyTop(x: Expr) = x match { // Double negation returns the original value case UnaryOp("-", UnaryOp("-", x)) => x // Positive returns the original value case UnaryOp("+", x) => x // Multiplying x by 1 returns the original value case BinaryOp("*", x, Number(1)) => x // Multiplying 1 by x returns the original value case BinaryOp("*", Number(1), x) => x // Multiplying x by 0 returns zero case BinaryOp("*", x, Number(0)) => Number(0) // Multiplying 0 by x returns zero case BinaryOp("*", Number(0), x) => Number(0) // Dividing x by 1 returns the original value case BinaryOp("/", x, Number(1)) => x // Dividing x by x returns 1 case BinaryOp("/", x1, x2) if x1 == x2 => Number(1) // Adding x to 0 returns the original value case BinaryOp("+", x, Number(0)) => x // Adding 0 to x returns the original value case BinaryOp("+", Number(0), x) => x // Anything else cannot (yet) be simplified case e => e } simplifyTop(simpSubs) } def main(args: Array[String]) { // (3 + 4) * (5 - 2) / (4 - 1) val expr = BinaryOp("/", BinaryOp("*", BinaryOp("+", Number(3), Number(4)), BinaryOp("-", Number(5), Number(2))), BinaryOp("-", Number(4), Number(1))) println(evaluate(expr)) // val e2 = parse("(3 + 4) * (5 - 2) / (4 - 1) + (-5)") val e2 = parse("(3 + 4) * 5") println(e2) println(evaluate(e2)) }}
http://www.qingpingshan.com/jb/python/173159.html
https://zhuanlan.zhihu.com/#/p/21830284
0 0
- scala 连接符解析 ~ ^^ <~ ~> 基础
- scala 连接符解析 基础 @
- Spark基础-Scala语句解析
- Scala学习整理[第三十一章 连接符解析]<Programming In Scala>
- scala基础-访问修饰符
- scala 解析
- Scala基础
- Scala 基础
- Scala 基础
- Scala 基础
- Scala基础
- scala 基础
- scala 基础
- Scala基础
- Scala基础
- Scala基础
- Scala基础
- Scala 基础
- 程序员面试
- MySQL入门教程
- python 细节上的一些笔记
- 存储管理——内存管理
- JSP的一个小程序
- scala 连接符解析 ~ ^^ <~ ~> 基础
- 遍历目录、批量替换文件内容的类
- 71+6
- PTA 数据结构习题集(中文) 4-2顺序表操作集
- Flask系列教程(一)-----------入门
- 系统调用
- Scala中的Map使用例子
- HDU——校赛1007
- 143. Reorder List leetcode list