equal
deleted
inserted
replaced
8 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence |
8 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence |
9 case class STAR(r: Rexp) extends Rexp // star |
9 case class STAR(r: Rexp) extends Rexp // star |
10 |
10 |
11 // nullable function: tests whether a regular |
11 // nullable function: tests whether a regular |
12 // expression can recognise the empty string |
12 // expression can recognise the empty string |
13 def nullable (r: Rexp) : Boolean = r match { |
13 def nullable(r: Rexp) : Boolean = r match { |
14 case ZERO => false |
14 case ZERO => false |
15 case ONE => true |
15 case ONE => true |
16 case CHAR(_) => false |
16 case CHAR(_) => false |
17 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
17 case ALT(r1, r2) => nullable(r1) || nullable(r2) |
18 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
18 case SEQ(r1, r2) => nullable(r1) && nullable(r2) |
38 case Nil => r |
38 case Nil => r |
39 case c::s => ders(s, der(c, r)) |
39 case c::s => ders(s, der(c, r)) |
40 } |
40 } |
41 |
41 |
42 // main matcher function |
42 // main matcher function |
43 def matches(r: Rexp, s: String) : Boolean = nullable(ders(s.toList, r)) |
43 def matches(r: Rexp, s: String) : Boolean = |
|
44 nullable(ders(s.toList, r)) |
44 |
45 |
45 |
46 |
46 //examples from the homework |
47 //examples from the homework |
47 |
48 |
48 val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b'))) |
49 val r = STAR(ALT(SEQ(CHAR('a'), CHAR('b')), CHAR('b'))) |