author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Fri, 16 Oct 2015 08:42:21 +0100 | |
changeset 352 | 1e1b0fe66107 |
parent 93 | 4794759139ea |
permissions | -rw-r--r-- |
92
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
// regular expressions |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
abstract class Rexp |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
3 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
case object NULL extends Rexp |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
case object EMPTY extends Rexp |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
case class CHAR(c: Char) extends Rexp |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
8 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
case class STAR(r: Rexp) extends Rexp |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
case class NOT(r: Rexp) extends Rexp |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
// some convenience for typing in regular expressions |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
def charlist2rexp(s : List[Char]) : Rexp = s match { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
15 |
case Nil => EMPTY |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
case c::Nil => CHAR(c) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
case c::s => SEQ(CHAR(c), charlist2rexp(s)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
implicit def string2rexp(s : String) : Rexp = charlist2rexp(s.toList) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
22 |
// nullable function: tests whether the regular |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
// expression can recognise the empty string |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
def nullable (r: Rexp) : Boolean = r match { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
case NULL => false |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
case EMPTY => true |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
case CHAR(_) => false |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
28 |
case ALT(r1, r2) => nullable(r1) || nullable(r2) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
case STAR(_) => true |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
case NOT(r) => !(nullable(r)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
34 |
// tests whether a regular expression |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
// recognises nothing |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
def zeroable (r: Rexp) : Boolean = r match { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
case NULL => true |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
case EMPTY => false |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
case CHAR(_) => false |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
case ALT(r1, r2) => zeroable(r1) && zeroable(r2) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
case SEQ(r1, r2) => if (nullable(r1)) (zeroable(r1) && zeroable(r2)) else zeroable(r1) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
//zeroable(r1) || zeroable(r2) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
case STAR(_) => false |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
case NOT(r) => !(zeroable(r)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
46 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
// derivative of a regular expression w.r.t. a character |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
def der (c: Char, r: Rexp) : Rexp = r match { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
case NULL => NULL |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
case EMPTY => NULL |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
case CHAR(d) => if (c == d) EMPTY else NULL |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
53 |
case ALT(r1, r2) => ALT(der(c, r1), der(c, r2)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
case SEQ(r1, r2) => |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
else SEQ(der(c, r1), r2) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
57 |
case STAR(r) => SEQ(der(c, r), STAR(r)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
58 |
case NOT(r) => NOT(der (c, r)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
59 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
60 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
61 |
// derivative w.r.t. a string (iterates der) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
62 |
def ders (s: List[Char], r: Rexp) : Rexp = s match { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
63 |
case Nil => r |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
64 |
case c::s => ders(s, der(c, r)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
65 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
66 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
67 |
// main matcher function |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
68 |
def matcher(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
69 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
70 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
71 |
// regular expression for specifying |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
72 |
// ranges of characters |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
73 |
def RANGE(s : List[Char]) : Rexp = s match { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
74 |
case Nil => NULL |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
75 |
case c::Nil => CHAR(c) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
76 |
case c::s => ALT(CHAR(c), RANGE(s)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
77 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
78 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
79 |
//one or more |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
80 |
def PLUS(r: Rexp) = SEQ(r, STAR(r)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
81 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
82 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
83 |
//some regular expressions |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
84 |
val LOWERCASE = RANGE("abcdefghijklmnopqrstuvwxyz".toList) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
85 |
val UPPERCASE = RANGE("ABCDEFGHIJKLMNOPQRSTUVWXYZ".toList) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
86 |
val LETTER = ALT(LOWERCASE, UPPERCASE) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
87 |
val DIGITS = RANGE("0123456789".toList) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
88 |
val NONZERODIGITS = RANGE("123456789".toList) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
89 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
90 |
val IDENT = SEQ(LETTER, STAR(ALT(LETTER,DIGITS))) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
91 |
val NUMBER = ALT(SEQ(NONZERODIGITS, STAR(DIGITS)), "0") |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
92 |
val WHITESPACE = RANGE(" \n".toList) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
93 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
94 |
val ALL = ALT(ALT(LETTER, DIGITS), WHITESPACE) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
95 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
96 |
val COMMENT = SEQ(SEQ("/*", NOT(SEQ(SEQ(STAR(ALL), "*/"), STAR(ALL)))), "*/") |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
97 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
98 |
println(matcher(NUMBER, "0")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
99 |
println(matcher(NUMBER, "01")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
100 |
println(matcher(NUMBER, "123450")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
101 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
102 |
println(matcher(SEQ(STAR("a"), STAR("b")), "bbaaa")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
103 |
println(matcher(ALT(STAR("a"), STAR("b")), "")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
104 |
println(matcher("abc", "")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
105 |
println(matcher(STAR(ALT(EMPTY, "a")), "")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
106 |
println(matcher(STAR(EMPTY), "a")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
107 |
println(matcher("cab","cab")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
108 |
println(matcher(STAR("a"),"aaa")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
109 |
println(matcher("cab" ,"cab")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
110 |
println(matcher(STAR("a"),"aaa")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
111 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
112 |
println(matcher(COMMENT, "/* */")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
113 |
println(matcher(COMMENT, "/* 34 */")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
114 |
println(matcher(COMMENT, "/* foobar comment */")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
115 |
println(matcher(COMMENT, "/* test */ test */")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
116 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
117 |
// an example list of regular expressions |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
118 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
119 |
abstract class Token |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
120 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
121 |
case object T_WHITESPACE extends Token |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
122 |
case object T_COMMENT extends Token |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
123 |
case class T_IDENT(s: String) extends Token |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
124 |
case class T_OP(s: String) extends Token |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
125 |
case class T_NUM(n: Int) extends Token |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
126 |
case class T_KEYWORD(s: String) extends Token |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
127 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
128 |
val regs: List[Rexp]= List("if", "then", "else", "+", IDENT, NUMBER, WHITESPACE) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
129 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
130 |
type Rule = (Rexp, List[Char] => Token) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
131 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
132 |
val rules: List[Rule]= |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
133 |
List(("if", (s) => T_KEYWORD(s.mkString)), |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
134 |
("then", (s) => T_KEYWORD(s.mkString)), |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
135 |
("else", (s) => T_KEYWORD(s.mkString)), |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
136 |
("+", (s) => T_OP(s.mkString)), |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
137 |
(IDENT, (s) => T_IDENT(s.mkString)), |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
138 |
(NUMBER, (s) => T_NUM(s.mkString.toInt)), |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
139 |
(WHITESPACE, (s) => T_WHITESPACE), |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
140 |
(COMMENT, (s) => T_COMMENT)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
141 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
142 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
143 |
def error (s: String) = throw new IllegalArgumentException ("Could not lex " + s) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
144 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
145 |
def munch(r: Rexp, action: List[Char] => Token, s: List[Char], t: List[Char]) : Option[(List[Char], Token)] = |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
146 |
{ println("string " + s) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
147 |
println(" rexp " + r) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
148 |
s match { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
149 |
case Nil if (nullable(r)) => Some(Nil, action(t)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
150 |
case Nil => { println("1"); None } |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
151 |
case c::s if (zeroable(der (c, r)) && nullable(r)) => Some(c::s, action(t)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
152 |
case c::s if (zeroable(der (c, r))) => { println("2"); None } |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
153 |
case c::s => munch(der (c, r), action, s, t ::: List(c)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
154 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
155 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
156 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
157 |
def lex_one (rs: List[Rule], s: List[Char]) : (List[Char], Token) = { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
158 |
val somes = rs.map { (r) => munch(r._1, r._2, s, Nil) } .flatten |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
159 |
if (somes == Nil) error(s.mkString) else (somes sortBy (_._1.length) head) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
160 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
161 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
162 |
def lex_all (rs: List[Rule], s: List[Char]) : List[Token] = s match { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
163 |
case Nil => Nil |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
164 |
case _ => lex_one(rs, s) match { |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
165 |
case (rest, t) => t :: lex_all(rs, rest) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
166 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
167 |
} |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
168 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
169 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
170 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
171 |
println(matcher(COMMENT, "/*ifff if 34 34*/")) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
172 |
rules.map { (r) => munch(r._1, r._2, "/*ifff if 34 34*/ ".toList, Nil) } |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
173 |
println(lex_all(rules, "ifff if 34 34".toList)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
174 |
println(lex_all(rules, " /*ifff if 34 34*/ ".toList)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
175 |
println(lex_all(rules, "ifff $ if 34".toList)) |
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
176 |
|
e85600529ca5
moved scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
177 |