1、
import scala.util.parsing.combinator._
class ExprParser extends RegexParsers{
val number="[0-9]+".r
def expr:Parser[Any]=term ~ opt(("+" | "-") ~ expr)
def term:Parser[Any]=factor ~ rep("*" ~ factor)
def factor:Parser[Any]=number | "(" ~ expr ~ ")"
}
object Parser_ex extends App{
val parser=new ExprParser
val result=parser.parseAll(parser.expr,"3-4*5") //"3-4-5"会出错,后面例子有解决办法
if(result.successful)
println(result.get)
val result2=parser.parse(parser.expr,"3-4/5") //从左解析,直到找不到可以解析的。解析不了"/",会停下来。
if(result2.successful)
println(result2.get)
}
2、
import scala.util.parsing.combinator._
class ExprParser extends RegexParsers{
val number="[0-9]+".r
def expr:Parser[Int]=term ~ opt(("+" | "-") ~ expr) ^^ {
case t ~ None => t
case t ~ Some("+" ~ e) => t+e
case t ~ Some("-" ~ e) => t-e
}
def term:Parser[Int]=factor ~ rep("*" ~ factor) ^^ {
case f ~ r => f*r.map(_._2).product
}
def factor:Parser[Int]=number ^^ {_.toInt} | "(" ~ expr ~ ")" ^^ {
case _ ~ e ~ _ => e
}
}
object Parser_ex2 extends App{
val parser=new ExprParser
val result=parser.parseAll(parser.expr,"3-4*5") //"3-4-5"会出错,后面例子有解决办法
if(result.successful)
println(result.get)
val result2=parser.parse(parser.expr,"3-4/5") //从左解析,直到找不到可以解析的。解析不了"/",会停下来。
if(result2.successful)
println(result2.get)
}
3、
import scala.util.parsing.combinator._
class ExprParser extends RegexParsers{
val number="[0-9]+".r
def expr:Parser[Int]=term ~ opt(("+" | "-") ~ expr) ^^ {
case t ~ None => t
case t ~ Some("+" ~ e) => t+e
case t ~ Some("-" ~ e) => t-e
}
def term:Parser[Int]=factor ~ rep("*" ~> factor) ^^ {
case f ~ r => f*r.product
}
def factor:Parser[Int]=number ^^ {_.toInt} | "(" ~> expr <~ ")"
}
object Parser_ex3 extends App{
val parser=new ExprParser
val result=parser.parseAll(parser.expr,"3-4*5") //"3-4-5"会出错,后面例子有解决办法
if(result.successful)
println(result.get)
val result2=parser.parse(parser.expr,"3-4/5") //从左解析,直到找不到可以解析的。解析不了"/",会停下来。
if(result2.successful)
println(result2.get)
}
4、
import scala.util.parsing.combinator._
class Expr
case class Number(value:Int) extends Expr
case class Operator(op:String,left:Expr,right:Expr) extends Expr
class ExprParser extends RegexParsers{
val wholeNumber="[0-9]+".r
def expr:Parser[Expr]=term ~ opt(("+" | "-") ~ expr) ^^ {
case t ~ None => t
case t ~ Some("+" ~ e) => Operator("+",t,e)
case t ~ Some("-" ~ e) => Operator("-",t,e)
}
def term:Parser[Expr]=(factor ~ opt("*" ~> term)) ^^ {
case a ~ None => a
case a ~ Some(b) => Operator("*",a,b)
}
def factor:Parser[Expr]=wholeNumber ^^ (n => Number(n.toInt)) | "(" ~> expr <~ ")"
}
object Parser_ex4 extends App{
val parser=new ExprParser
val result=parser.parseAll(parser.expr,"3-4*5") //"3-4-5"会出错,后面例子有解决办法
if(result.successful)
println(result.get)
}
5、
import scala.util.parsing.combinator._
class ExprParser extends RegexParsers{
val number="[0-9]+".r
def expr:Parser[Int]=term ~ rep(("+" | "-") ~ term ^^ {
case "+" ~ t => t
case "-" ~ t => -t
}) ^^ {
case t ~ r => t+r.sum
}
def term:Parser[Int]=factor ~ rep("*" ~> factor) ^^ {
case f ~ r => f*r.product
}
def factor:Parser[Int]=number ^^ {_.toInt} | "(" ~> expr <~ ")"
}
object Parser_ex5 extends App{
val parser=new ExprParser
val result=parser.parseAll(parser.expr,"3-4-5")
if(result.successful)
println(result.get)
}
6、
import scala.util.parsing.input._
import scala.util.parsing.combinator._
class ExprParser extends RegexParsers with PackratParsers {
lazy val ones:PackratParser[Any]=ones ~ "1" ^^ {case a ~ b => a+b } | "1"
def parseAll[T](p:Parser[T],input:String)=
phrase(p)(new PackratReader(new CharSequenceReader(input)))
val number="[0-9]+".r
/*
lazy val expr:PackratParser[Any]=opt(log(expr)("expr:") ~ ("+" | "-")) ~ term
lazy val term:PackratParser[Any]=factor ~ rep("*" ~ factor)
lazy val factor:PackratParser[Any]=number | "(" ~ expr ~ ")"
*/
//(Some(((Some(((None~(3~List()))~-))~(4~List()))~-))~(5~List())) //在不知道如何做的时候,参考parser_ex.scala打印出解析结果
//opt(log(expr)("expr:") ~ ("+" | "-")) ~ term
/*
trying expr: at scala.util.parsing.combinator.PackratParsers$PackratReader@16b83
cc
expr: --> [1.1] failure: Base Failure
3-4-5
^
trying expr: at scala.util.parsing.combinator.PackratParsers$PackratReader@16b83
cc
expr: --> [1.2] parsed: (None~(3~List()))
trying expr: at scala.util.parsing.combinator.PackratParsers$PackratReader@16b83
cc
expr: --> [1.4] parsed: (Some(((None~(3~List()))~-))~(4~List()))
trying expr: at scala.util.parsing.combinator.PackratParsers$PackratReader@16b83
cc
expr: --> [1.6] parsed: (Some(((Some(((None~(3~List()))~-))~(4~List()))~-))~(5~L
ist()))
(Some(((Some(((None~(3~List()))~-))~(4~List()))~-))~(5~List()))
*/
lazy val expr:PackratParser[Int]=opt(expr ~ ("+" | "-")) ~ term ^^ {
case (None ~ (r)) => r //记住这个吧:(3~List())是用(r)来匹配,并且r就表示3,试出来的。
case (Some((e ~ "+")) ~ (r)) => e+r
case (Some((e ~ "-")) ~ (r)) => e-r
}
lazy val term:PackratParser[Int]=factor ~ rep("*" ~> factor) ^^ {
case f ~ r => f*r.product
}
lazy val factor:PackratParser[Int]=number ^^ {_.toInt} | "(" ~> expr <~ ")"
}
object Parser_ex6 extends App{
val parser=new ExprParser
val result=parser.parseAll(parser.expr,"3-4-5")
if(result.successful)
println(result.get)
val result2=parser.parseAll(parser.expr,"3-4*5")
if(result2.successful)
println(result2.get)
val result3=parser.parseAll(parser.ones,"111111")
if(result3.successful)
println(result3.get)
}