// Main Part 3 about Regular Expression Matching //============================================== object M3 { abstract class Rexp case object ZERO extends Rexp case object ONE extends Rexp case class CHAR(c: Char) extends Rexp case class ALTs(rs: List[Rexp]) extends Rexp // alternatives case class SEQs(rs: List[Rexp]) extends Rexp // sequences case class STAR(r: Rexp) extends Rexp // star //the usual binary choice and binary sequence can be defined //in terms of ALTs and SEQs def ALT(r1: Rexp, r2: Rexp) = ALTs(List(r1, r2)) def SEQ(r1: Rexp, r2: Rexp) = SEQs(List(r1, r2)) // some convenience for typing regular expressions def charlist2rexp(s: List[Char]): Rexp = s match { case Nil => ONE case c::Nil => CHAR(c) case c::s => SEQ(CHAR(c), charlist2rexp(s)) } import scala.language.implicitConversions given Conversion[String, Rexp] = (s => charlist2rexp(s.toList)) extension (r: Rexp) { def | (s: Rexp) = ALT(r, s) def % = STAR(r) def ~ (s: Rexp) = SEQ(r, s) } // some examples for the conversion and extension: // val areg : Rexp = "a" | "b" // => ALTs(List(CHAR('a'), CHAR('b'))) // // val sreg : Rexp = "a" ~ "b" // => SEQs(List(CHAR('a'), CHAR('b'))) // // val star_reg : Rexp = ("a" ~ "b").% // => STAR(SEQs(List(CHAR('a'), CHAR('b')))) // ADD YOUR CODE BELOW //====================== // (1) /* def nullable (r: Rexp) : Boolean = { "" match { case r => true case _ => false } } */ // (2) def der (c: Char, r: Rexp) : Rexp = ??? // (3) def denest(rs: List[Rexp]) : List[Rexp] = ??? // (4) def flts(rs: List[Rexp], acc: List[Rexp] = Nil) : List[Rexp] = ??? // (5) def ALTs_smart(rs: List[Rexp]) : Rexp = ??? def SEQs_smart(rs: List[Rexp]) : Rexp = ??? // (6) def simp(r: Rexp) : Rexp = ??? // (7) def ders (s: List[Char], r: Rexp) : Rexp = ??? def matcher(r: Rexp, s: String): Boolean = ??? // (8) def size(r: Rexp): Int = ??? // Some testing data //=================== /* simp(ALT(ONE | CHAR('a'), CHAR('a') | ONE)) // => ALTs(List(ONE, CHAR(a))) simp(((CHAR('a') | ZERO) ~ ONE) | (((ONE | CHAR('b')) | CHAR('c')) ~ (CHAR('d') ~ ZERO))) // => CHAR(a) matcher(("a" ~ "b") ~ "c", "ab") // => false matcher(("a" ~ "b") ~ "c", "abc") // => true // the supposedly 'evil' regular expression (a*)* b val EVIL = SEQ(STAR(STAR(CHAR('a'))), CHAR('b')) matcher(EVIL, "a" * 1000) // => false matcher(EVIL, "a" * 1000 ++ "b") // => true // size without simplifications size(der('a', der('a', EVIL))) // => 36 size(der('a', der('a', der('a', EVIL)))) // => 83 // size with simplification size(simp(der('a', der('a', EVIL)))) // => 7 size(simp(der('a', der('a', der('a', EVIL))))) // => 7 // Python needs around 30 seconds for matching 28 a's with EVIL. // Java 9 and later increase this to an "astonishing" 40000 a's in // 30 seconds. // // Lets see how long it really takes to match strings with // 5 Million a's...it should be in the range of a few // of seconds. def time_needed[T](i: Int, code: => T) = { val start = System.nanoTime() for (j <- 1 to i) code val end = System.nanoTime() "%.5f".format((end - start)/(i * 1.0e9)) } for (i <- 0 to 5000000 by 500000) { println(s"$i ${time_needed(2, matcher(EVIL, "a" * i))} secs.") } // another "power" test case simp(Iterator.iterate(ONE:Rexp)(r => SEQ(r, ONE | ONE)).drop(50).next()) == ONE // the Iterator produces the rexp // // SEQ(SEQ(SEQ(..., ONE | ONE) , ONE | ONE), ONE | ONE) // // where SEQ is nested 50 times. */ }
Write, Run & Share Scala code online using OneCompiler's Scala online compiler for free. It's one of the robust, feature-rich online compilers for Scala language, running on the latest version 2.13.8. Getting started with the OneCompiler's Scala compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as Scala
and start coding.
OneCompiler's Scala online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Scala program which takes name as input and prints hello message with your name.
object Hello {
def main(args: Array[String]): Unit = {
val name = scala.io.StdIn.readLine() // Read input from STDIN
println("Hello " + name )
}
}
Scala is both object-oriented and functional programming language by Martin Odersky in the year 2003.
Variable is a name given to the storage area in order to identify them in our programs.
var or val Variable-name [: Data-Type] = [Initial Value];
If, If-else, Nested-Ifs are used when you want to perform a certain set of operations based on conditional expressions.
if(conditional-expression){
//code
}
if(conditional-expression) {
//code if condition is true
} else {
//code if condition is false
}
if(condition-expression1) {
//code if above condition is true
} else if (condition-expression2) {
//code if above condition is true
}
else if(condition-expression3) {
//code if above condition is true
}
...
else {
//code if all the above conditions are false
}
For loop is used to iterate a set of statements based on a criteria.
for(index <- range){
// code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while(condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition)
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
def functionname(parameters : parameters-type) : returntype = { //code
}
You can either use =
or not in the function definition. If =
is not present, function will not return any value.