74 case c::s => ders(s, der(c, r)) |
74 case c::s => ders(s, der(c, r)) |
75 } |
75 } |
76 |
76 |
77 // extracts a string from value |
77 // extracts a string from value |
78 def flatten(v: Val) : String = v match { |
78 def flatten(v: Val) : String = v match { |
79 case Void => "" |
79 case Empty => "" |
80 case Chr(c) => c.toString |
80 case Chr(c) => c.toString |
81 case Left(v) => flatten(v) |
81 case Left(v) => flatten(v) |
82 case Right(v) => flatten(v) |
82 case Right(v) => flatten(v) |
83 case Sequ(v1, v2) => flatten(v1) + flatten(v2) |
83 case Seq(v1, v2) => flatten(v1) + flatten(v2) |
84 case Stars(vs) => vs.map(flatten).mkString |
84 case Stars(vs) => vs.map(flatten).mkString |
85 case Rec(_, v) => flatten(v) |
85 case Rec(_, v) => flatten(v) |
86 } |
86 } |
87 |
87 |
88 // extracts an environment from a value |
88 // extracts an environment from a value |
89 def env(v: Val) : List[(String, String)] = v match { |
89 def env(v: Val) : List[(String, String)] = v match { |
90 case Void => Nil |
90 case Empty => Nil |
91 case Chr(c) => Nil |
91 case Chr(c) => Nil |
92 case Left(v) => env(v) |
92 case Left(v) => env(v) |
93 case Right(v) => env(v) |
93 case Right(v) => env(v) |
94 case Sequ(v1, v2) => env(v1) ::: env(v2) |
94 case Seq(v1, v2) => env(v1) ::: env(v2) |
95 case Stars(vs) => vs.flatMap(env) |
95 case Stars(vs) => vs.flatMap(env) |
96 case Rec(x, v) => (x, flatten(v))::env(v) |
96 case Rec(x, v) => (x, flatten(v))::env(v) |
97 } |
97 } |
98 |
98 |
99 // injection part |
99 // injection part |
100 def mkeps(r: Rexp) : Val = r match { |
100 def mkeps(r: Rexp) : Val = r match { |
101 case EMPTY => Void |
101 case EMPTY => Empty |
102 case ALT(r1, r2) => |
102 case ALT(r1, r2) => |
103 if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
103 if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2)) |
104 case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2)) |
104 case SEQ(r1, r2) => Seq(mkeps(r1), mkeps(r2)) |
105 case STAR(r) => Stars(Nil) |
105 case STAR(r) => Stars(Nil) |
106 case RECD(x, r) => Rec(x, mkeps(r)) |
106 case RECD(x, r) => Rec(x, mkeps(r)) |
107 } |
107 } |
108 |
108 |
109 |
109 |
110 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
110 def inj(r: Rexp, c: Char, v: Val) : Val = (r, v) match { |
111 case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
111 case (STAR(r), Seq(v1, Stars(vs))) => Stars(inj(r, c, v1)::vs) |
112 case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2) |
112 case (SEQ(r1, r2), Seq(v1, v2)) => Seq(inj(r1, c, v1), v2) |
113 case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2) |
113 case (SEQ(r1, r2), Left(Seq(v1, v2))) => Seq(inj(r1, c, v1), v2) |
114 case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2)) |
114 case (SEQ(r1, r2), Right(v2)) => Seq(mkeps(r1), inj(r2, c, v2)) |
115 case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1)) |
115 case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1)) |
116 case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
116 case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2)) |
117 case (CHAR(d), Void) => Chr(c) |
117 case (CHAR(d), Empty) => Chr(c) |
118 case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
118 case (RECD(x, r1), _) => Rec(x, inj(r1, c, v)) |
119 } |
119 } |
120 |
120 |
121 // main lexing function (produces a value) |
121 // main lexing function (produces a value) |
122 def lex(r: Rexp, s: List[Char]) : Val = s match { |
122 def lex(r: Rexp, s: List[Char]) : Val = s match { |
123 case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched") |
123 case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched") |
124 case c::cs => inj(r, c, lex(der(c, r), cs)) |
124 case c::cs => inj(r, c, lex(der(c, r), cs)) |
125 } |
125 } |
126 |
126 |
127 def lexing(r: Rexp, s: String) : Val = lex(r, s.toList) |
127 def lexing(r: Rexp, s: String) : Val = lex(r, s.toList) |
|
128 |
|
129 lexing(("ab" | "ab") ~ ("b" | EMPTY), "ab") |
128 |
130 |
129 // some "rectification" functions for simplification |
131 // some "rectification" functions for simplification |
130 def F_ID(v: Val): Val = v |
132 def F_ID(v: Val): Val = v |
131 def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
133 def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v)) |
132 def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
134 def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v)) |
133 def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
135 def F_ALT(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
134 case Right(v) => Right(f2(v)) |
136 case Right(v) => Right(f2(v)) |
135 case Left(v) => Left(f1(v)) |
137 case Left(v) => Left(f1(v)) |
136 } |
138 } |
137 def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
139 def F_SEQ(f1: Val => Val, f2: Val => Val) = (v:Val) => v match { |
138 case Sequ(v1, v2) => Sequ(f1(v1), f2(v2)) |
140 case Seq(v1, v2) => Seq(f1(v1), f2(v2)) |
139 } |
141 } |
140 def F_SEQ_Void1(f1: Val => Val, f2: Val => Val) = (v:Val) => Sequ(f1(Void), f2(v)) |
142 def F_SEQ_Empty1(f1: Val => Val, f2: Val => Val) = |
141 def F_SEQ_Void2(f1: Val => Val, f2: Val => Val) = (v:Val) => Sequ(f1(v), f2(Void)) |
143 (v:Val) => Seq(f1(Empty), f2(v)) |
|
144 def F_SEQ_Empty2(f1: Val => Val, f2: Val => Val) = |
|
145 (v:Val) => Seq(f1(v), f2(Empty)) |
142 def F_RECD(f: Val => Val) = (v:Val) => v match { |
146 def F_RECD(f: Val => Val) = (v:Val) => v match { |
143 case Rec(x, v) => Rec(x, f(v)) |
147 case Rec(x, v) => Rec(x, f(v)) |
144 } |
148 } |
145 def F_ERROR(v: Val): Val = throw new Exception("error") |
149 def F_ERROR(v: Val): Val = throw new Exception("error") |
146 |
150 |