progs/comb1.scala
changeset 185 ea8b94d4755e
parent 183 b17eff695c7f
child 360 c6c574d2ca0c
equal deleted inserted replaced
184:2e9134d25a2b 185:ea8b94d4755e
    68 
    68 
    69 lazy val Pal : Parser[String, String] = 
    69 lazy val Pal : Parser[String, String] = 
    70   (("a" ~ Pal ~ "a") ==> { case ((x, y), z) => x + y + z } ||
    70   (("a" ~ Pal ~ "a") ==> { case ((x, y), z) => x + y + z } ||
    71    ("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } || "")
    71    ("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } || "")
    72 
    72 
    73 Pal.parse_all("ababbaba")
    73 println("Palindrom" + Pal.parse_all("ababbaba"))
    74 
    74 
    75 
    75 
    76 lazy val P : Parser[String, String] = 
    76 lazy val P : Parser[String, String] = 
    77   "(" ~ P ~ ")" ~ P ==> { case (((u, x), y), z) => "{" + x + "}" + z } || ""
    77   "(" ~ P ~ ")" ~ P ==> { case (((u, x), y), z) => "{" + x + "}" + z } || ""
    78 
    78 
    86   ((T ~ "+" ~ T) ==> { case ((x, y), z) => x + y + z } ||
    86   ((T ~ "+" ~ T) ==> { case ((x, y), z) => x + y + z } ||
    87    (T ~ "-" ~ T) ==> { case ((x, y), z) => x + y + z } || T)
    87    (T ~ "-" ~ T) ==> { case ((x, y), z) => x + y + z } || T)
    88 lazy val T: Parser[String, String] = 
    88 lazy val T: Parser[String, String] = 
    89   ("(" ~ E ~ ")") ==> { case ((x, y), z) => x + y + z } || NumParser
    89   ("(" ~ E ~ ")") ==> { case ((x, y), z) => x + y + z } || NumParser
    90 
    90 
    91 
    91 println(E.parse_all("1*2+3"))
       
    92 println(E.parse_all("1+2*3"))
    92 println(E.parse_all("1+2+3"))
    93 println(E.parse_all("1+2+3"))
    93 
       
    94 
    94 
    95 
    95 
    96 // non-ambiguous vs ambiguous
    96 // non-ambiguous vs ambiguous
    97 lazy val U : Parser[String, String] =
    97 lazy val U : Parser[String, String] =
    98   ("1" ~ U) ==> { case (x, y) => x + y } || ""
    98   ("1" ~ U) ==> { case (x, y) => x + y } || ""