progs/comb1.scala
changeset 586 451a95e1bc25
parent 531 f6e937ed0332
child 588 a4646557016d
equal deleted inserted replaced
585:6ee22f196884 586:451a95e1bc25
    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 } || "a" || "b" || "")
    80    ("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } || "a" || "b" || "")
    81 
    81 
       
    82 Pal.parse_all("abaaaba")
       
    83 
    82 println("Palindrome: " + Pal.parse_all("abaaaba"))
    84 println("Palindrome: " + Pal.parse_all("abaaaba"))
    83 
    85 
    84 // well-nested parenthesis parser
    86 // well-nested parenthesis parser
    85 lazy val P : Parser[String, String] = 
    87 lazy val P : Parser[String, String] = 
    86   "(" ~ P ~ ")" ~ P ==> { case (((u, x), y), z) => "{" + x + "}" + z } || ""
    88   "(" ~ P ~ ")" ~ P ==> { case (((u, x), y), z) => "{" + x + "}" + z } || ""
    89 P.parse_all("(((()()))()))")
    91 P.parse_all("(((()()))()))")
    90 P.parse_all(")(")
    92 P.parse_all(")(")
    91 P.parse_all("()")
    93 P.parse_all("()")
    92 
    94 
    93 // arithmetic expressions
    95 // arithmetic expressions
       
    96 
    94 lazy val E: Parser[String, Int] = 
    97 lazy val E: Parser[String, Int] = 
    95   (T ~ "+" ~ E) ==> { case ((x, y), z) => x + z } ||
    98   (T ~ "+" ~ E) ==> { case ((x, y), z) => x + z } ||
    96   (T ~ "-" ~ E) ==> { case ((x, y), z) => x - z } || T 
    99   (T ~ "-" ~ E) ==> { case ((x, y), z) => x - z } || T 
    97 lazy val T: Parser[String, Int] = 
   100 lazy val T: Parser[String, Int] = 
    98   ((F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } || F)
   101   ((F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } || F)
    99 lazy val F: Parser[String, Int] = 
   102 lazy val F: Parser[String, Int] = 
   100   ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser
   103   ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser
   101 
   104 
       
   105 
   102 println(E.parse("4*2+3"))
   106 println(E.parse("4*2+3"))
   103 println(E.parse_all("4*2+3"))
   107 println(E.parse_all("4/2+3"))
   104 println(E.parse("1 + 2 * 3"))
   108 println(E.parse("1 + 2 * 3"))
   105 println(E.parse_all("(1+2)+3"))
   109 println(E.parse_all("(1+2)+3"))
   106 println(E.parse_all("1+2+3"))  
   110 println(E.parse_all("1+2+3"))  
       
   111 
       
   112 
   107 
   113 
   108 // no left-recursion allowed, otherwise will loop
   114 // no left-recursion allowed, otherwise will loop
   109 lazy val EL: Parser[String, Int] = 
   115 lazy val EL: Parser[String, Int] = 
   110   ((EL ~ "+" ~ EL) ==> { case ((x, y), z) => x + z} || 
   116   ((EL ~ "+" ~ EL) ==> { case ((x, y), z) => x + z} || 
   111    (EL ~ "*" ~ EL) ==> { case ((x, y), z) => x * z} ||
   117    (EL ~ "*" ~ EL) ==> { case ((x, y), z) => x * z} ||