progs/comb1a.scala
changeset 686 05cfce0fdef7
parent 683 c6c79d21f8a8
equal deleted inserted replaced
685:75d9f9e5906f 686:05cfce0fdef7
    64 // convenience
    64 // convenience
    65 implicit def string2parser(s: String) = StringParser(s)
    65 implicit def string2parser(s: String) = StringParser(s)
    66 implicit def char2parser(c: Char) = CharParser(c)
    66 implicit def char2parser(c: Char) = CharParser(c)
    67 
    67 
    68 implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
    68 implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
    69   def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
    69   def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
    70   def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
    70   def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
    71   def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
    71   def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
    72 }
    72 }
    73 
    73 
    74 implicit def StringOps(s: String) = new {
    74 implicit def StringOps(s: String) = new {
    75   def || (q : => Parser[String, String]) = new AltParser[String, String](s, q)
    75   def ||(q : => Parser[String, String]) = new AltParser[String, String](s, q)
    76   def || (r: String) = new AltParser[String, String](s, r)
    76   def ||(r: String) = new AltParser[String, String](s, r)
    77   def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
    77   def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
    78   def ~[S] (q : => Parser[String, S]) = 
    78   def ~[S](q : => Parser[String, S]) = 
    79     new SeqParser[String, String, S](s, q)
    79     new SeqParser[String, String, S](s, q)
    80   def ~ (r: String) = 
    80   def ~(r: String) = 
    81     new SeqParser[String, String, String](s, r)
    81     new SeqParser[String, String, String](s, r)
    82 }
    82 }
    83 
    83 
    84 // NumParserInt can now be written as
    84 // NumParserInt can now be written as
    85 val NumParserInt = NumParser ==> (s => s.toInt)
    85 val NumParserInt = NumParser ==> (s => s.toInt)
   104 P.parse_all("()")
   104 P.parse_all("()")
   105 
   105 
   106 // Arithmetic Expressions (Terms and Factors)
   106 // Arithmetic Expressions (Terms and Factors)
   107 
   107 
   108 lazy val E: Parser[String, Int] = 
   108 lazy val E: Parser[String, Int] = 
   109   (T ~ "+" ~ E) ==> { case x ~ y ~ z => x + z } ||
   109   (T ~ "+" ~ E) ==> { case x ~ _ ~ z => x + z } ||
   110   (T ~ "-" ~ E) ==> { case x ~ y ~ z => x - z } || T 
   110   (T ~ "-" ~ E) ==> { case x ~ _ ~ z => x - z } || T 
   111 lazy val T: Parser[String, Int] = 
   111 lazy val T: Parser[String, Int] = 
   112   (F ~ "*" ~ T) ==> { case x ~ y ~ z => x * z } || F
   112   (F ~ "*" ~ T) ==> { case x ~ _ ~ z => x * z } || F
   113 lazy val F: Parser[String, Int] = 
   113 lazy val F: Parser[String, Int] = 
   114   ("(" ~ E ~ ")") ==> { case x ~ y ~ z => y } || NumParserInt
   114   ("(" ~ E ~ ")") ==> { case _ ~ y ~ _ => y } || NumParserInt
   115 
   115 
   116 /* same parser but producing a string
   116 /* same parser but producing a string
   117 lazy val E: Parser[String, String] = 
   117 lazy val E: Parser[String, String] = 
   118   (T ~ "+" ~ E) ==> { case ((x, y), z) => "(" + x + ")+(" + z + ")"} || T 
   118   (T ~ "+" ~ E) ==> { case x ~ y ~ z => "(" + x + ")+(" + z + ")"} || T 
   119 lazy val T: Parser[String, String] = 
   119 lazy val T: Parser[String, String] = 
   120   (F ~ "*" ~ T) ==> { case ((x, y), z) => "(" + x + ")*("+ z + ")"} || F
   120   (F ~ "*" ~ T) ==> { case x ~ y ~ z => "(" + x + ")*("+ z + ")"} || F
   121 lazy val F: Parser[String, String] = 
   121 lazy val F: Parser[String, String] = 
   122   ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser
   122   ("(" ~ E ~ ")") ==> { case x ~ y ~ z => y } || NumParser
   123 */
   123 */
   124 
   124 
   125 println(E.parse_all("1+3+4"))
   125 println(E.parse_all("1+3+4"))
   126 println(E.parse("1+3+4"))
   126 println(E.parse("1+3+4"))
   127 println(E.parse_all("4*2+3"))
   127 println(E.parse_all("4*2+3"))
   189 (One || Two).parse("111")
   189 (One || Two).parse("111")
   190 
   190 
   191 
   191 
   192 
   192 
   193 // a problem with the arithmetic expression parser -> gets 
   193 // a problem with the arithmetic expression parser -> gets 
   194 // slow with deep nestedness
   194 // slow with deeply nested parentheses
   195 println("Runtime problem")
   195 println("Runtime problem")
   196 E.parse("1")
   196 E.parse("1")
   197 E.parse("(1)")
   197 E.parse("(1)")
   198 E.parse("((1))")
   198 E.parse("((1))")
   199 E.parse("(((1)))")
   199 E.parse("(((1)))")