• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala StandardTokenParsers类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Scala中scala.util.parsing.combinator.syntactical.StandardTokenParsers的典型用法代码示例。如果您正苦于以下问题:Scala StandardTokenParsers类的具体用法?Scala StandardTokenParsers怎么用?Scala StandardTokenParsers使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了StandardTokenParsers类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。

示例1: JSON

//设置package包名称以及导入依赖的类
package examples.parsing

import scala.util.parsing.combinator.syntactical.StandardTokenParsers

object JSON extends StandardTokenParsers {   
  lexical.delimiters += ("{", "}", "[", "]", ":", ",")
  lexical.reserved += ("null", "true", "false")

  def obj   : Parser[Any] = "{" ~ repsep(member, ",") ~ "}"
  def arr   : Parser[Any] = "[" ~ repsep(value, ",") ~ "]"
  def member: Parser[Any] = ident ~ ":" ~ value
  def value : Parser[Any] = ident | numericLit | obj | arr | 
                            "null" | "true" | "false"

  def main(args: Array[String]) {
    val input = args.mkString(" ")
    val tokens = new lexical.Scanner(input)
    println(input)
    println(phrase(value)(tokens))
  }
}
object JSON1 extends StandardTokenParsers {   
  lexical.delimiters += ("{", "}", "[", "]", ":", ",")
  lexical.reserved += ("null", "true", "false")

  def obj: Parser[Map[String, Any]] = 
    "{" ~> repsep(member, ",") <~ "}" ^^ (Map() ++ _)

  def arr: Parser[List[Any]] =
    "[" ~> repsep(value, ",") <~ "]" 

  def member: Parser[(String, Any)] = 
    ident ~ ":" ~ value ^^ { case name ~ ":" ~ value => (name -> value) }

  def value: Parser[Any] = 
    ident | numericLit ^^ (_.toInt) | obj | arr |
    "null" ^^^ null | "true" ^^^ true | "false" ^^^ false

  def main(args: Array[String]) {
    val input = args.mkString(" ")
    val tokens = new lexical.Scanner(input)
    println(input)
    println(phrase(value)(tokens))
  }
} 
开发者ID:chenc10,项目名称:spark-SSR-ICDCS17,代码行数:46,代码来源:JSON.scala


示例2: miniML

//设置package包名称以及导入依赖的类
package examples.parsing

import scala.util.parsing.combinator.syntactical.StandardTokenParsers
import scala.util.parsing.combinator.syntactical.StandardTokenParsers

object miniML extends StandardTokenParsers {   
  lexical.delimiters += ("(", ")", ".", "=")
  lexical.reserved += ("lambda", "let", "in")

  def expr: Parser[Any] = (
    "let" ~ ident ~ "=" ~ expr ~ "in" ~ expr
  | "lambda" ~ ident ~ "." ~ expr
  | simpleExpr ~ rep(expr)
  )
  def simpleExpr: Parser[Any] = (
    ident
  | "(" ~ expr ~ ")"
  )

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

object miniML1 extends StandardTokenParsers {   

  class Expr
  case class Let(x: String, expr: Expr, body: Expr) extends Expr
  case class Lambda(x: String, expr: Expr) extends Expr
  case class Apply(fun: Expr, arg: Expr) extends Expr
  case class Var(x: String) extends Expr

  lexical.delimiters += ("(", ")", ".", "=")
  lexical.reserved += ("lambda", "let", "in")

  def expr: Parser[Expr] = (
    "let" ~ ident ~ "=" ~ expr ~ "in" ~ expr ^^ { case "let" ~ x ~ "=" ~ e ~ "in" ~ b => Let(x, e, b) }
  | "lambda" ~ ident ~ "." ~ expr            ^^ { case "lambda" ~ x ~ "." ~ e => Lambda(x, e) }
  | simpleExpr ~ rep(expr)                   ^^ { case f ~ as => (f /: as) (Apply) }
  )
  def simpleExpr: Parser[Expr] = (
    ident                                    ^^ { Var }
  | "(" ~> expr <~ ")"                       
  )

  def main(args: Array[String]) {
    val input = args.mkString(" ")
    val tokens = new lexical.Scanner(input)
    println(input)
    println(phrase(expr)(tokens))
  }
} 
开发者ID:chenc10,项目名称:spark-SSR-ICDCS17,代码行数:56,代码来源:MiniML.scala


示例3: listParsers

//设置package包名称以及导入依赖的类
package examples.parsing

import scala.util.parsing.combinator.syntactical.StandardTokenParsers

object listParsers extends StandardTokenParsers {   
  lexical.delimiters ++= List("(", ")", ",")

  def expr: Parser[Any] = "(" ~ exprs ~ ")" | ident | numericLit
  def exprs: Parser[Any] = expr ~ rep ("," ~ expr)

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

object listParsers1 extends StandardTokenParsers {   
  lexical.delimiters ++= List("(", ")", ",")

  def expr: Parser[Any] = "(" ~> exprs <~ ")" | ident | numericLit

  def exprs: Parser[List[Any]] = expr ~ rep ("," ~> expr) ^^ { case x ~ y => x :: y }

  def main(args: Array[String]) {
    val input = args.mkString(" ")
    val tokens = new lexical.Scanner(input)
    println(input)
    println(phrase(expr)(tokens))
  }
} 
开发者ID:chenc10,项目名称:spark-SSR-ICDCS17,代码行数:33,代码来源:ListParsers.scala


示例4: Exercise08

//设置package包名称以及导入依赖的类
package forimpatient.chapter19

import scala.collection.mutable.Map
import scala.util.parsing.combinator.syntactical.StandardTokenParsers


object Exercise08 extends App {
  println("Chapter 19 Exercise 08")

  val parser = new ExprParser
  val result = parser.parseAll(parser.program, "a = 3 - 4 - 5; out = b; b = a + 1; out = b; out = 5 * 2 ^ 3")


  class ExprParser extends StandardTokenParsers {
    lexical.delimiters += ("+", "-", "*", "(", ")", ";", "%", "/", "^", "=")

    val number = numericLit
    val varName = ident
    val variables = Map[String, Int]()

    def parseAll[T](p: Parser[T], in: String): ParseResult[T] = phrase(p)(new lexical.Scanner(in))

    def program: Parser[Any] = rep(statement)

    def expr: Parser[Int] = term ~ rep(("+" | "-") ~ term ^^ {
      case "+" ~ t => t
      case "-" ~ t => -t
    }) ^^ {case t ~ r => t + r.sum}

    def term: Parser[Int] = pow ~ rep(("*" | "/" | "%") ~ pow) ^^ {
      case f ~ r => r.foldLeft(f)((n, pair) => pair._1 match {
        case "*" => n * pair._2;
        case "/" => n / pair._2
        case "%" => n % pair._2
      })
    }

    def pow: Parser[Int] = rep(factor <~ "^") ~ factor ^^ {
      case r ~ f => r.foldRight(f)(math.pow(_, _).toInt)
    }

    def factor: Parser[Int] = number ^^ (_.toInt)| "(" ~> expr <~ ")" | variable

    def variable: Parser[Int] = varName ^^ {
      v => variables.getOrElse(v, 0)
    }

    def statement: Parser[Any] = varName ~ ("=" ~> expr <~ opt(";")) ^^ {
      case "out" ~ e => println(e)
      case v ~ e => variables(v) = e
    } | expr
  }
} 
开发者ID:Kiryna,项目名称:Scala-for-the-Impatient,代码行数:54,代码来源:Exercise08.scala



注:本文中的scala.util.parsing.combinator.syntactical.StandardTokenParsers类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala SocketAddress类代码示例发布时间:2022-05-23
下一篇:
Scala Credentials类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap