alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (ArithmeticParsers.scala)

This example Scala source code file (ArithmeticParsers.scala) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Scala tags/keywords

arithmeticparsers2, array, binop, expr, expr, int, int, list, num, parser, parser, standardtokenparsers, string, string

The Scala ArithmeticParsers.scala source code

package examples.parsing

import scala.util.parsing.combinator1.syntactical.StandardTokenParsers

object ArithmeticParsers extends StandardTokenParsers {   
  lexical.delimiters ++= List("(", ")", "+", "-", "*", "/")

  def expr: Parser[Any] = term ~ rep("+" ~ term | "-" ~ term)
  def term = factor ~ rep("*" ~ factor | "/" ~ factor)
  def factor: Parser[Any] = "(" ~ expr ~ ")" | numericLit

  def main(args: Array[String]) {
    val tokens = new lexical.Scanner(args(0))
    println(args(0))
    println(phrase(expr)(tokens))
  }
}

object ArithmeticParsers1 extends StandardTokenParsers {   
  lexical.delimiters ++= List("(", ")", "+", "-", "*", "/")

  val reduceList: Int ~ List[String ~ Int] => Int = {
    case i ~ ps => (i /: ps)(reduce) 
  }

  def reduce(x: Int, r: String ~ Int) = (r: @unchecked) match {
    case "+" ~ y => x + y
    case "-" ~ y => x - y
    case "*" ~ y => x * y
    case "/" ~ y => x / y
  }

  def expr  : Parser[Int] = term ~ rep ("+" ~ term | "-" ~ term) ^^ reduceList
  def term  : Parser[Int] = factor ~ rep ("*" ~ factor | "/" ~ factor) ^^ reduceList
  def factor: Parser[Int] = "(" ~> expr <~ ")" | numericLit ^^ (_.toInt)

  def main(args: Array[String]) {
    val tokens = new lexical.Scanner(args(0))
    println(args(0))
    println(phrase(expr)(tokens))
  }
}

class Expr
case class BinOp(op: String, l: Expr, r: Expr) extends Expr
case class Num(n: Int) extends Expr

object ArithmeticParsers2 extends StandardTokenParsers {   
  lexical.delimiters ++= List("(", ")", "+", "-", "*", "/")

  val reduceList: Expr ~ List[String ~ Expr] => Expr = {
    case i ~ ps => (i /: ps)(reduce) 
  }

  def reduce(l: Expr, r: String ~ Expr) = BinOp(r._1, l, r._2)
  def mkNum(s: String) = Num(s.toInt)

  def expr  : Parser[Expr] = term ~ rep ("+" ~ term | "-" ~ term) ^^ reduceList
  def term  : Parser[Expr] = factor ~ rep ("*" ~ factor | "/" ~ factor) ^^ reduceList
  def factor: Parser[Expr] = "(" ~> expr <~ ")" | numericLit ^^ ((s: String) => Num(s.toInt))

  def main(args: Array[String]) {
    val parse = phrase(expr)
    val tokens = new lexical.Scanner(args(0))
    println(args(0))
    println(parse(tokens))
  }
}


Other Scala examples (source code examples)

Here is a short list of links related to this Scala ArithmeticParsers.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.