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) |