progs/comb1.scala
changeset 531 f6e937ed0332
parent 470 8df654e9eb4e
child 586 451a95e1bc25
equal deleted inserted replaced
530:cec95ad3a837 531:f6e937ed0332
    75 }
    75 }
    76 
    76 
    77 // a parse palindromes
    77 // a parse palindromes
    78 lazy val Pal : Parser[String, String] = 
    78 lazy val Pal : Parser[String, String] = 
    79   (("a" ~ Pal ~ "a") ==> { case ((x, y), z) => x + y + z } ||
    79   (("a" ~ Pal ~ "a") ==> { case ((x, y), z) => x + y + z } ||
    80    ("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } || "")
    80    ("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } || "a" || "b" || "")
    81 
    81 
    82 println("Palindrome: " + Pal.parse_all("ababbaba"))
    82 println("Palindrome: " + Pal.parse_all("abaaaba"))
    83 
    83 
    84 // well-nested parenthesis parser
    84 // well-nested parenthesis parser
    85 lazy val P : Parser[String, String] = 
    85 lazy val P : Parser[String, String] = 
    86   "(" ~ P ~ ")" ~ P ==> { case (((u, x), y), z) => "{" + x + "}" + z } || ""
    86   "(" ~ P ~ ")" ~ P ==> { case (((u, x), y), z) => "{" + x + "}" + z } || ""
    87 
    87 
   101 
   101 
   102 println(E.parse("4*2+3"))
   102 println(E.parse("4*2+3"))
   103 println(E.parse_all("4*2+3"))
   103 println(E.parse_all("4*2+3"))
   104 println(E.parse("1 + 2 * 3"))
   104 println(E.parse("1 + 2 * 3"))
   105 println(E.parse_all("(1+2)+3"))
   105 println(E.parse_all("(1+2)+3"))
   106 println(E.parse_all("1+2+3"))  // this is not parsed, because of 
   106 println(E.parse_all("1+2+3"))  
   107                                // how the grammar is set up
   107 
       
   108 // no left-recursion allowed, otherwise will loop
       
   109 lazy val EL: Parser[String, Int] = 
       
   110   ((EL ~ "+" ~ EL) ==> { case ((x, y), z) => x + z} || 
       
   111    (EL ~ "*" ~ EL) ==> { case ((x, y), z) => x * z} ||
       
   112    ("(" ~ EL ~ ")") ==> { case ((x, y), z) => y} ||
       
   113    NumParser)
       
   114 
       
   115 //println(E.parse_all("1+2+3"))
       
   116 
   108 
   117 
   109 // a repetition parser
   118 // a repetition parser
   110 
   119 
   111 def RepParser[I  <% Seq[_], T](p: => Parser[I, T]): Parser[I, List[T]] = {
   120 def RepParser[I  <% Seq[_], T](p: => Parser[I, T]): Parser[I, List[T]] = {
   112   p ==> { case x => x :: Nil } ||
   121   p ==> { case x => x :: Nil } ||
   122 
   131 
   123 // non-ambiguous vs ambiguous grammars
   132 // non-ambiguous vs ambiguous grammars
   124 lazy val S : Parser[String, String] =
   133 lazy val S : Parser[String, String] =
   125   ("1" ~ S ~ S) ==> { case ((x, y), z) => x + y + z } || ""
   134   ("1" ~ S ~ S) ==> { case ((x, y), z) => x + y + z } || ""
   126 
   135 
   127 S.parse_all("1" * 15)
   136 S.parse("1" * 15)
   128 
   137 
   129 lazy val U : Parser[String, String] =
   138 lazy val U : Parser[String, String] =
   130   ("1" ~ U) ==> { case (x, y) => x + y  } || ""
   139   ("1" ~ U) ==> { case (x, y) => x + y  } || ""
       
   140 
       
   141 U.parse("1" * 15)
   131 
   142 
   132 U.parse("11")
   143 U.parse("11")
   133 U.parse("11111")
   144 U.parse("11111")
   134 U.parse("11011")
   145 U.parse("11011")
   135 
   146 
       
   147 U.parse_all("1" * 100)
   136 U.parse_all("1" * 100 + "0")
   148 U.parse_all("1" * 100 + "0")
   137 
   149 
   138 lazy val UCount : Parser[String, Int] =
   150 lazy val UCount : Parser[String, Int] =
   139   ("1" ~ UCount) ==> { case (x, y) => y + 1 } || 
   151   ("1" ~ UCount) ==> { case (x, y) => y + 1 } || 
   140   "" ==> { (x) => 0 }
   152   "" ==> { (x) => 0 }
   166 (1, "one", '1')._3
   178 (1, "one", '1')._3
   167 for ((x, y) <- List((1, "one"), (2, "two"), (3, "three"), (4,"many")); if (y == "many"))
   179 for ((x, y) <- List((1, "one"), (2, "two"), (3, "three"), (4,"many")); if (y == "many"))
   168   yield (x.toString + y)
   180   yield (x.toString + y)
   169 
   181 
   170 
   182 
       
   183 
       
   184 // Example section for lazy evaluation
   171 def square(n: Int) = {
   185 def square(n: Int) = {
   172   n * n
   186   n * n
   173 }
   187 }
   174 
   188 
   175 square(4 + 3 + 5)
   189 square(4 + 3 + 5)
   177 def bar(): Int = {
   191 def bar(): Int = {
   178   bar()
   192   bar()
   179   3
   193   3
   180 }
   194 }
   181 
   195 
   182 
   196 //would loop
       
   197 square(bar())
       
   198 
       
   199 // lazy
   183 def foo(n: => Int) = {
   200 def foo(n: => Int) = {
   184   print("finished")
   201   print("finished")
   185 }
   202 }
   186 
   203 
   187 foo(bar())
   204 foo(bar())
   188 
   205 
   189 square(12) + square(10)
       
   190 
       
   191 
       
   192 def time_needed[T](i: Int, code: => T) = {
       
   193   val start = System.nanoTime()
       
   194   for (j <- 1 to i) code
       
   195   val end = System.nanoTime()
       
   196   (end - start)/(i * 1.0e9)
       
   197 }