Scala parser combinator based calculator that can also take a dataRecord

妖精的绣舞 提交于 2020-01-05 11:57:05

问题


I have created a Scala parser combinator to filter data records based on the answer I got to an previous question How to parse a string with filter citeria in scala and use it to filter objects

I would like to add the calculator parser combinator from the answer to this question Operator Precedence with Scala Parser Combinators to the bottom of the parser combinator that I created based on the first question. The calculator parser combinator therefore needs to accept a dataRecord so that an expression like "( doubleValue1 / 10 ) * 2 + doubleValue2" can be parsed to an function that subsequently can take a dataRecord.

This is what I came up with but the plus, minus, times and divide parser combinators are now broken because the + - * / operators are members of Double and not the function DataRecord => Double. How can I fix these parser combinators so that an expression like "( doubleValue1 / 10 ) * 2 + doubleValue2" can be succesfully parsed and results in an function that can take a dataRecord?

import scala.util.parsing.combinator._
import scala.util.parsing.combinator.JavaTokenParsers

object Main extends Arith with App {

  val dataRecord = new DataRecord(100, 75 )
  val input = "( doubleValue1 / 10   ) * 2 + doubleValue2"
  println(parseAll(arithmicExpr, input).get(dataRecord)) // prints 95
}

class DataRecord(  val doubleValue1 : Double,  val doubleValue2 : Double ) 

class Arith extends JavaTokenParsers {

  type D = Double
  type Extractor[Double] = DataRecord => Double


  //arithmic expression
  def arithmicExpr:       Parser[Extractor[D]]                = term ~ rep(plus | minus)     ^^ {case a~b => (a /: b)((acc,f) => f(acc))} 
  def plus:               Parser[Extractor[D]=>Extractor[D]]  = "+" ~ term                   ^^ {case "+"~b => _ + b}
  def minus:              Parser[Extractor[D]=>Extractor[D]]  = "-" ~ term                   ^^ {case "-"~b => _ - b}
  def term:               Parser[Extractor[D]]                = factor ~ rep(times | divide) ^^ {case a~b => (a /: b)((acc,f) => f(acc))}
  def times:              Parser[Extractor[D]=>Extractor[D]]  = "*" ~ factor                 ^^ {case "*"~b => _ * (b) }
  def divide:             Parser[Extractor[D]=>Extractor[D]]  = "/" ~ factor                 ^^ {case "/"~b => _ / b} 
  def factor:             Parser[Extractor[D]]                = fpn | "(" ~> arithmicExpr <~ ")" |  intExtractor 
  def fpn:                Parser[Extractor[D]]                = floatingPointNumber          ^^ (s => Function.const(s.toDouble)_)
  def intExtractor:       Parser[Extractor[D]]                = ("doubleValue1" | "doubleValue2")             ^^ {
    case "doubleValue1" => _.doubleValue1                                                                                             
    case "doubleValue2" => _.doubleValue2 
  }
}

回答1:


Your approach to avoid a left recursive grammar is nice, but makes the types really complex. I prefer a different approach:

object ArithParser extends JavaTokenParsers {
  //arithmic expression
  def arithmicExpr:       Parser[Extractor[D]] = plus
  def plus:               Parser[Extractor[D]] = repsep(times, "+") ^^ { summands : List[Extractor[D]] =>
    (in : DataRecord) => summands.map((e : Extractor[D]) => e(in)).foldLeft(0d)(_ + _)
  }
  def times:              Parser[Extractor[D]] = repsep(division, "*") ^^ { factors : List[Extractor[D]] =>
    (in : DataRecord) => factors.map((e : Extractor[D]) => e(in)).foldLeft(1d)(_ * _)
  }

  def division :           Parser[Extractor[D]] = rep1sep(number, "/") ^^ {divisons : List[Extractor[D]] =>
    (in : DataRecord) => divisons.map((e : Extractor[D]) => e(in)).reduce(_ / _)
  } | number
  def number :            Parser[Extractor[D]] = fpn | intExtractor
  def fpn:                Parser[Extractor[D]] = floatingPointNumber          ^^ (s => Function.const(s.toDouble)_)
  def intExtractor:       Parser[Extractor[D]] = ("doubleValue1" | "doubleValue2")             ^^ {
    case "doubleValue1" => _.doubleValue1
    case "doubleValue2" => _.doubleValue2
  }
}

You can find a live demo here.

This code can be further improved: It contains lots of repeating structures. Perhaps this is a good case for Stack exchange's code review site.

Enhancements for other arithmetic operators, for mathematical functions and especially for braces are straight forward.



来源:https://stackoverflow.com/questions/21680403/scala-parser-combinator-based-calculator-that-can-also-take-a-datarecord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!