author | Christian Urban <urbanc@in.tum.de> |
Wed, 16 Oct 2019 11:06:32 +0100 | |
changeset 658 | a18e3c027c1f |
parent 629 | 1b718d6065c2 |
child 667 | 412556272333 |
permissions | -rw-r--r-- |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
import scala.language.implicitConversions |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
import scala.language.reflectiveCalls |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
|
629 | 4 |
/* Note, in the lectures I did not show the implicit type |
5 |
* constraint IsSeq, which means that the input type 'I' needs |
|
6 |
* to be a sequence. */ |
|
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
7 |
|
629 | 8 |
type IsSeq[A] = A => Seq[_] |
9 |
||
10 |
abstract class Parser[I : IsSeq, T] { |
|
461 | 11 |
def parse(ts: I): Set[(T, I)] |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
|
461 | 13 |
def parse_all(ts: I) : Set[T] = |
360
c6c574d2ca0c
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
185
diff
changeset
|
14 |
for ((head, tail) <- parse(ts); |
c6c574d2ca0c
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
185
diff
changeset
|
15 |
if (tail.isEmpty)) yield head |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
|
629 | 18 |
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], |
19 |
q: => Parser[I, S]) extends Parser[I, (T, S)] { |
|
461 | 20 |
def parse(sb: I) = |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
for ((head1, tail1) <- p.parse(sb); |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
(head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
|
629 | 25 |
class AltParser[I : IsSeq, T](p: => Parser[I, T], |
26 |
q: => Parser[I, T]) extends Parser[I, T] { |
|
461 | 27 |
def parse(sb: I) = p.parse(sb) ++ q.parse(sb) |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
|
629 | 30 |
class FunParser[I : IsSeq, T, S](p: => Parser[I, T], |
31 |
f: T => S) extends Parser[I, S] { |
|
461 | 32 |
def parse(sb: I) = |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
for ((head, tail) <- p.parse(sb)) yield (f(head), tail) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
|
628 | 36 |
// atomic parsers for characters, numbers and strings |
461 | 37 |
case class CharParser(c: Char) extends Parser[String, Char] { |
177
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
172
diff
changeset
|
38 |
def parse(sb: String) = |
462 | 39 |
if (sb != "" && sb.head == c) Set((c, sb.tail)) else Set() |
177
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
172
diff
changeset
|
40 |
} |
53def1fbf472
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
172
diff
changeset
|
41 |
|
588 | 42 |
import scala.util.matching.Regex |
43 |
case class RegexParser(reg: Regex) extends Parser[String, String] { |
|
44 |
def parse(sb: String) = reg.findPrefixMatchOf(sb) match { |
|
45 |
case None => Set() |
|
46 |
case Some(m) => Set((m.matched, m.after.toString)) |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
|
588 | 50 |
val NumParser = RegexParser("[0-9]+".r) |
593 | 51 |
def StringParser(s: String) = RegexParser(Regex.quote(s).r) |
588 | 52 |
|
628 | 53 |
// NumParserInt2 transforms a "string integer" into an Int; |
624 | 54 |
// needs new, because FunParser is not a case class |
628 | 55 |
val NumParserInt2 = new FunParser(NumParser, (s: String) => s.toInt) |
624 | 56 |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
|
360
c6c574d2ca0c
update
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
185
diff
changeset
|
58 |
// convenience |
462 | 59 |
implicit def string2parser(s: String) = StringParser(s) |
593 | 60 |
implicit def char2parser(c: Char) = CharParser(c) |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
|
624 | 62 |
implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new { |
628 | 63 |
def || (q : => Parser[I, T]) = new AltParser[I, T](p, q) |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
implicit def StringOps(s: String) = new { |
628 | 69 |
def || (q : => Parser[String, String]) = new AltParser[String, String](s, q) |
70 |
def || (r: String) = new AltParser[String, String](s, r) |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
def ~[S] (q : => Parser[String, S]) = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
new SeqParser[String, String, S](s, q) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
def ~ (r: String) = |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
new SeqParser[String, String, String](s, r) |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
} |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
|
624 | 78 |
// NumParserInt can now be written as |
79 |
val NumParserInt = NumParser ==> (s => s.toInt) |
|
80 |
||
588 | 81 |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
lazy val Pal : Parser[String, String] = |
628 | 83 |
(("a" ~ Pal ~ "a") ==> { case ((x, y), z) => x + y + z } || |
84 |
("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } || "a" || "b" || "") |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
|
586 | 86 |
Pal.parse_all("abaaaba") |
593 | 87 |
Pal.parse("abaaaba") |
586 | 88 |
|
531 | 89 |
println("Palindrome: " + Pal.parse_all("abaaaba")) |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
|
624 | 91 |
// well-nested parentheses parser (transforms '(' -> '{' , ')' -> '}' ) |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
lazy val P : Parser[String, String] = |
628 | 93 |
"(" ~ P ~ ")" ~ P ==> { case (((_, x), _), y) => "{" + x + "}" + y } || "" |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
|
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
P.parse_all("(((()()))())") |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
P.parse_all("(((()()))()))") |
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
P.parse_all(")(") |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
98 |
P.parse_all("()") |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
|
628 | 100 |
// Arithmetic Expressions (Terms and Factors) |
586 | 101 |
|
367
04127a5aad23
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
366
diff
changeset
|
102 |
lazy val E: Parser[String, Int] = |
628 | 103 |
(T ~ "+" ~ E) ==> { case ((x, y), z) => x + z } || |
104 |
(T ~ "-" ~ E) ==> { case ((x, y), z) => x - z } || T |
|
470
8df654e9eb4e
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
467
diff
changeset
|
105 |
lazy val T: Parser[String, Int] = |
628 | 106 |
(F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } || F |
367
04127a5aad23
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
366
diff
changeset
|
107 |
lazy val F: Parser[String, Int] = |
628 | 108 |
("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParserInt |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
|
628 | 110 |
/* same parser but producing a string |
599 | 111 |
lazy val E: Parser[String, String] = |
628 | 112 |
(T ~ "+" ~ E) ==> { case ((x, y), z) => "(" + x + ")+(" + z + ")"} || T |
599 | 113 |
lazy val T: Parser[String, String] = |
628 | 114 |
(F ~ "*" ~ T) ==> { case ((x, y), z) => "(" + x + ")*("+ z + ")"} || F |
599 | 115 |
lazy val F: Parser[String, String] = |
628 | 116 |
("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser |
117 |
*/ |
|
586 | 118 |
|
599 | 119 |
println(E.parse_all("1+3+4")) |
120 |
println(E.parse("1+3+4")) |
|
593 | 121 |
println(E.parse_all("4*2+3")) |
122 |
println(E.parse_all("4*(2+3)")) |
|
594 | 123 |
println(E.parse_all("(4)*((2+3))")) |
586 | 124 |
println(E.parse_all("4/2+3")) |
367
04127a5aad23
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
366
diff
changeset
|
125 |
println(E.parse("1 + 2 * 3")) |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
126 |
println(E.parse_all("(1+2)+3")) |
531 | 127 |
println(E.parse_all("1+2+3")) |
128 |
||
586 | 129 |
|
130 |
||
531 | 131 |
// no left-recursion allowed, otherwise will loop |
132 |
lazy val EL: Parser[String, Int] = |
|
628 | 133 |
(EL ~ "+" ~ EL ==> { case ((x, y), z) => x + z} || |
134 |
EL ~ "*" ~ EL ==> { case ((x, y), z) => x * z} || |
|
135 |
"(" ~ EL ~ ")" ==> { case ((x, y), z) => y} || |
|
593 | 136 |
NumParserInt) |
531 | 137 |
|
593 | 138 |
//println(EL.parse_all("1+2+3")) |
531 | 139 |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
140 |
|
462 | 141 |
|
142 |
||
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
143 |
// non-ambiguous vs ambiguous grammars |
628 | 144 |
|
145 |
// ambiguous |
|
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
146 |
lazy val S : Parser[String, String] = |
628 | 147 |
("1" ~ S ~ S) ==> { case ((x, y), z) => x + y + z } || "" |
172
47b5c91eff47
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
148 |
|
628 | 149 |
S.parse("1" * 10) |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
150 |
|
628 | 151 |
// non-ambiguous |
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
152 |
lazy val U : Parser[String, String] = |
628 | 153 |
("1" ~ U) ==> { case (x, y) => x + y } || "" |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
154 |
|
599 | 155 |
U.parse("1" * 25) |
531 | 156 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
157 |
U.parse("11") |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
158 |
U.parse("11111") |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
159 |
U.parse("11011") |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
160 |
|
531 | 161 |
U.parse_all("1" * 100) |
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
162 |
U.parse_all("1" * 100 + "0") |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
163 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
164 |
lazy val UCount : Parser[String, Int] = |
628 | 165 |
("1" ~ UCount) ==> { case (x, y) => y + 1 } || "" ==> { x => 0 } |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
166 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
167 |
UCount.parse("11111") |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
168 |
UCount.parse_all("11111") |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
169 |
|
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
170 |
|
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
171 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
172 |
// Single Character parser |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
173 |
lazy val One : Parser[String, String] = "1" |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
174 |
lazy val Two : Parser[String, String] = "2" |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
175 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
176 |
One.parse("1") |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
177 |
One.parse("111") |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
178 |
|
366
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
179 |
(One ~ One).parse("111") |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
180 |
(One ~ One ~ One).parse("111") |
5a83336a9690
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
362
diff
changeset
|
181 |
(One ~ One ~ One ~ One).parse("1111") |
362
57ea439feaff
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
360
diff
changeset
|
182 |
|
628 | 183 |
(One || Two).parse("111") |
184 |
||
185 |
||
467
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
186 |
|
b5ec11e89768
fixed bug
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
462
diff
changeset
|
187 |
|
531 | 188 |
|
628 | 189 |
// a problem with the parser -> gets slow with nestedness |
190 |
E.parse("1") |
|
191 |
E.parse("(1)") |
|
192 |
E.parse("((1))") |
|
193 |
E.parse("(((1)))") |
|
194 |
E.parse("((((1))))") |
|
195 |
E.parse("((((((1))))))") |
|
196 |
E.parse("(((((((1)))))))") |