progs/matcher/re1.sc
changeset 882 5fcad75ade92
parent 879 ad9d4a01e072
child 913 eef6a56c185a
equal deleted inserted replaced
881:3b2f76950473 882:5fcad75ade92
    17 case class CHAR(c: Char) extends Rexp            // matches a character c
    17 case class CHAR(c: Char) extends Rexp            // matches a character c
    18 case class ALT(r1: Rexp, r2: Rexp) extends Rexp  // alternative
    18 case class ALT(r1: Rexp, r2: Rexp) extends Rexp  // alternative
    19 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp  // sequence
    19 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp  // sequence
    20 case class STAR(r: Rexp) extends Rexp            // star
    20 case class STAR(r: Rexp) extends Rexp            // star
    21 
    21 
    22 
       
    23 // nullable function: tests whether a regular 
    22 // nullable function: tests whether a regular 
    24 // expression can recognise the empty string
    23 // expression can recognise the empty string
       
    24   
    25 def nullable(r: Rexp) : Boolean = r match {
    25 def nullable(r: Rexp) : Boolean = r match {
    26   case ZERO => false
    26   case ZERO => false
    27   case ONE => true
    27   case ONE => true
    28   case CHAR(_) => false
    28   case CHAR(_) => false
    29   case ALT(r1, r2) => nullable(r1) || nullable(r2)
    29   case ALT(r1, r2) => nullable(r1) || nullable(r2)