90 P.parse_all(")(") |
90 P.parse_all(")(") |
91 P.parse_all("()") |
91 P.parse_all("()") |
92 |
92 |
93 // arithmetic expressions |
93 // arithmetic expressions |
94 lazy val E: Parser[String, Int] = |
94 lazy val E: Parser[String, Int] = |
95 (F ~ "*" ~ F) ==> { case ((x, y), z) => x * z } || F |
95 (T ~ "+" ~ E) ==> { case ((x, y), z) => x + z } || |
|
96 (T ~ "-" ~ E) ==> { case ((x, y), z) => x - z } || T |
|
97 lazy val T: Parser[String, Int] = |
|
98 ((F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } || F) |
96 lazy val F: Parser[String, Int] = |
99 lazy val F: Parser[String, Int] = |
97 ((T ~ "+" ~ T) ==> { case ((x, y), z) => x + z } || |
|
98 (T ~ "-" ~ T) ==> { case ((x, y), z) => x - z } || T) |
|
99 lazy val T: Parser[String, Int] = |
|
100 ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser |
100 ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser |
101 |
101 |
102 println(E.parse("1*2+3")) |
102 println(E.parse("4*2+3")) |
|
103 println(E.parse_all("4*2+3")) |
103 println(E.parse("1 + 2 * 3")) |
104 println(E.parse("1 + 2 * 3")) |
104 println(E.parse_all("(1+2)+3")) |
105 println(E.parse_all("(1+2)+3")) |
105 println(E.parse_all("1+2+3")) // this is not parsed, because of |
106 println(E.parse_all("1+2+3")) // this is not parsed, because of |
106 // how the grammar is set up |
107 // how the grammar is set up |
107 |
108 |