app7.scala
changeset 66 9215b9fb8852
parent 7 73cf4406b773
child 70 e6868bd2942b
equal deleted inserted replaced
65:ade6af51402c 66:9215b9fb8852
     1 def matches(r: Rexp, s: String) : Boolean = 
     1 abstract class Parser[I, T] {
     2   nullable(derivs(r, s.toList))
     2   def parse(ts: I): Set[(T, I)]
       
     3 
       
     4   def parse_all(ts: I) : Set[T] =
       
     5     for ((head, tail) <- parse(ts); if (tail.isEmpty)) 
       
     6       yield head
       
     7 
       
     8   def || (right : => Parser[I, T]) : Parser[I, T] = 
       
     9       new AltParser(this, right)
       
    10   def ==>[S] (f: => T => S) : Parser [I, S] = 
       
    11       new FunParser(this, f)
       
    12 }
     3 
    13 
     4 
    14 
     5 /* Examples */
       
     6 
       
     7 println(matches(SEQ(SEQ(CHAR('c'), CHAR('a')), CHAR('b')),"cab"))
       
     8 println(matches(STAR(CHAR('a')),"aaa"))
       
     9 
       
    10 /* Convenience using implicits */
       
    11 implicit def string2rexp(s : String) : Rexp = {
       
    12   s.foldRight (EMPTY: Rexp) ( (c, r) => SEQ(CHAR(c), r) )
       
    13 }
       
    14 
       
    15 println(matches("cab" ,"cab"))
       
    16 println(matches(STAR("a"),"aaa"))
       
    17 println(matches(STAR("a"),"aaab"))