Fahad/Scala/POSIX.sc
changeset 31 238c98db6057
child 33 8b5ed306e961
equal deleted inserted replaced
30:197babb05539 31:238c98db6057
       
     1 package greeter
       
     2 
       
     3 object POSIX {
       
     4   println("Posix Algorithm")                      //> Posix Algorithm
       
     5 
       
     6   abstract class Rexp
       
     7   case object NULL extends Rexp
       
     8   case object EMPTY extends Rexp
       
     9   case class CHAR(c: Char) extends Rexp
       
    10   case class ALT(r1: Rexp, r2: Rexp) extends Rexp
       
    11   case class SEQ(r1: Rexp, r2: Rexp) extends Rexp
       
    12   case class STAR(r: Rexp) extends Rexp
       
    13   case class RECD(x: String, r: Rexp) extends Rexp
       
    14 
       
    15   abstract class Val
       
    16   case object Void extends Val
       
    17   case class Chr(c: Char) extends Val
       
    18   case class Sequ(v1: Val, v2: Val) extends Val
       
    19   case class Left(v: Val) extends Val
       
    20   case class Right(v: Val) extends Val
       
    21   case class Stars(vs: List[Val]) extends Val
       
    22   case class Rec(x: String, v: Val) extends Val
       
    23 
       
    24   def charlist2rexp(s: List[Char]): Rexp = s match {
       
    25     case Nil => EMPTY
       
    26     case c :: Nil => CHAR(c)
       
    27     case c :: s => SEQ(CHAR(c), charlist2rexp(s))
       
    28   }                                               //> charlist2rexp: (s: List[Char])greeter.POSIX.Rexp
       
    29   implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
       
    30                                                   //> string2rexp: (s: String)greeter.POSIX.Rexp
       
    31 
       
    32   implicit def RexpOps(r: Rexp) = new {
       
    33     def |(s: Rexp) = ALT(r, s)
       
    34     def % = STAR(r)
       
    35     def ~(s: Rexp) = SEQ(r, s)
       
    36   }                                               //> RexpOps: (r: greeter.POSIX.Rexp)AnyRef{def |(s: greeter.POSIX.Rexp): greete
       
    37                                                   //| r.POSIX.ALT; def %: greeter.POSIX.STAR; def ~(s: greeter.POSIX.Rexp): greet
       
    38                                                   //| er.POSIX.SEQ}
       
    39 
       
    40   implicit def stringOps(s: String) = new {
       
    41     def |(r: Rexp) = ALT(s, r)
       
    42     def |(r: String) = ALT(s, r)
       
    43     def % = STAR(s)
       
    44     def ~(r: Rexp) = SEQ(s, r)
       
    45     def ~(r: String) = SEQ(s, r)
       
    46     def $(r: Rexp) = RECD(s, r)
       
    47   }                                               //> stringOps: (s: String)AnyRef{def |(r: greeter.POSIX.Rexp): greeter.POSIX.AL
       
    48                                                   //| T; def |(r: String): greeter.POSIX.ALT; def %: greeter.POSIX.STAR; def ~(r:
       
    49                                                   //|  greeter.POSIX.Rexp): greeter.POSIX.SEQ; def ~(r: String): greeter.POSIX.SE
       
    50                                                   //| Q; def $(r: greeter.POSIX.Rexp): greeter.POSIX.RECD}
       
    51 
       
    52   // size of a regular expressions - for testing purposes
       
    53   def size(r: Rexp): Int = r match {
       
    54     case NULL => 1
       
    55     case EMPTY => 1
       
    56     case CHAR(_) => 1
       
    57     case ALT(r1, r2) => 1 + size(r1) + size(r2)
       
    58     case SEQ(r1, r2) => 1 + size(r1) + size(r2)
       
    59     case STAR(r) => 1 + size(r)
       
    60     case RECD(_, r) => 1 + size(r)
       
    61   }                                               //> size: (r: greeter.POSIX.Rexp)Int
       
    62 
       
    63   // nullable function: tests whether the regular
       
    64   // expression can recognise the empty string
       
    65   def nullable(r: Rexp): Boolean = r match {
       
    66     case NULL => false
       
    67     case EMPTY => true
       
    68     case CHAR(_) => false
       
    69     case ALT(r1, r2) => nullable(r1) || nullable(r2)
       
    70     case SEQ(r1, r2) => nullable(r1) && nullable(r2)
       
    71     case STAR(_) => true
       
    72     case RECD(_, r1) => nullable(r1)
       
    73   }                                               //> nullable: (r: greeter.POSIX.Rexp)Boolean
       
    74 
       
    75   // derivative of a regular expression w.r.t. a character
       
    76   def der(c: Char, r: Rexp): Rexp = r match {
       
    77     case NULL => NULL
       
    78     case EMPTY => NULL
       
    79     case CHAR(d) => if (c == d) EMPTY else NULL
       
    80     case ALT(r1, r2) => ALT(der(c, r1), der(c, r2))
       
    81     case SEQ(r1, r2) =>
       
    82       if (nullable(r1)) ALT(SEQ(der(c, r1), r2), der(c, r2))
       
    83       else SEQ(der(c, r1), r2)
       
    84     case STAR(r) => SEQ(der(c, r), STAR(r))
       
    85     case RECD(_, r1) => der(c, r1)
       
    86   }                                               //> der: (c: Char, r: greeter.POSIX.Rexp)greeter.POSIX.Rexp
       
    87 
       
    88   // derivative w.r.t. a string (iterates der)
       
    89   def ders(s: List[Char], r: Rexp): Rexp = s match {
       
    90     case Nil => r
       
    91     case c :: s => ders(s, der(c, r))
       
    92   }                                               //> ders: (s: List[Char], r: greeter.POSIX.Rexp)greeter.POSIX.Rexp
       
    93 
       
    94   // extracts a string from value
       
    95   def flatten(v: Val): String = v match {
       
    96     case Void => ""
       
    97     case Chr(c) => c.toString
       
    98     case Left(v) => flatten(v)
       
    99     case Right(v) => flatten(v)
       
   100     case Sequ(v1, v2) => flatten(v1) + flatten(v2)
       
   101     case Stars(vs) => vs.map(flatten).mkString
       
   102     case Rec(_, v) => flatten(v)
       
   103   }                                               //> flatten: (v: greeter.POSIX.Val)String
       
   104 
       
   105   // extracts an environment from a value
       
   106   def env(v: Val): List[(String, String)] = v match {
       
   107     case Void => Nil
       
   108     case Chr(c) => Nil
       
   109     case Left(v) => env(v)
       
   110     case Right(v) => env(v)
       
   111     case Sequ(v1, v2) => env(v1) ::: env(v2)
       
   112     case Stars(vs) => vs.flatMap(env)
       
   113     case Rec(x, v) => (x, flatten(v)) :: env(v)
       
   114   }                                               //> env: (v: greeter.POSIX.Val)List[(String, String)]
       
   115 
       
   116   def mkeps(r: Rexp): Val = r match {
       
   117     case EMPTY => Void
       
   118     case ALT(r1, r2) =>
       
   119       if (nullable(r1)) Left(mkeps(r1)) else Right(mkeps(r2))
       
   120     case SEQ(r1, r2) => Sequ(mkeps(r1), mkeps(r2))
       
   121     case STAR(r) => Stars(Nil)
       
   122     case RECD(x, r) => Rec(x, mkeps(r))
       
   123   }                                               //> mkeps: (r: greeter.POSIX.Rexp)greeter.POSIX.Val
       
   124 
       
   125   def inj(r: Rexp, c: Char, v: Val): Val = (r, v) match {
       
   126     case (STAR(r), Sequ(v1, Stars(vs))) => Stars(inj(r, c, v1) :: vs)
       
   127     case (SEQ(r1, r2), Sequ(v1, v2)) => Sequ(inj(r1, c, v1), v2)
       
   128     case (SEQ(r1, r2), Left(Sequ(v1, v2))) => Sequ(inj(r1, c, v1), v2)
       
   129     case (SEQ(r1, r2), Right(v2)) => Sequ(mkeps(r1), inj(r2, c, v2))
       
   130     case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
       
   131     case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
       
   132     case (CHAR(d), Void) => Chr(d)
       
   133     case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
       
   134   }                                               //> inj: (r: greeter.POSIX.Rexp, c: Char, v: greeter.POSIX.Val)greeter.POSIX.Va
       
   135                                                   //| l
       
   136 
       
   137   // main lexing function (produces a value)
       
   138   def lex(r: Rexp, s: List[Char]): Val = s match {
       
   139     case Nil => if (nullable(r)) mkeps(r) else throw new Exception("Not matched")
       
   140     case c :: cs => inj(r, c, lex(der(c, r), cs))
       
   141   }                                               //> lex: (r: greeter.POSIX.Rexp, s: List[Char])greeter.POSIX.Val
       
   142 
       
   143   def lexing(r: Rexp, s: String): Val = lex(r, s.toList)
       
   144                                                   //> lexing: (r: greeter.POSIX.Rexp, s: String)greeter.POSIX.Val
       
   145 
       
   146   val r = (("1" $ "a") | (("2" $ "b") | ("3" $ "ab"))).%
       
   147                                                   //> r  : greeter.POSIX.STAR = STAR(ALT(RECD(1,CHAR(a)),ALT(RECD(2,CHAR(b)),RECD
       
   148                                                   //| (3,SEQ(CHAR(a),CHAR(b))))))
       
   149   env(lexing(r, "ba"))                            //> res0: List[(String, String)] = List((2,b), (1,a))
       
   150 
       
   151   val r1 = "a" | "b"                              //> r1  : greeter.POSIX.ALT = ALT(CHAR(a),CHAR(b))
       
   152   lexing(r1, "a")                                 //> res1: greeter.POSIX.Val = Left(Chr(a))
       
   153 
       
   154   // Lexing Rules for a Small While Language
       
   155 
       
   156   def PLUS(r: Rexp) = r ~ r.%                     //> PLUS: (r: greeter.POSIX.Rexp)greeter.POSIX.SEQ
       
   157   val SYM = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
       
   158                                                   //> SYM  : greeter.POSIX.ALT = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(
       
   159                                                   //| ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(a),CHAR(b)),CHAR(c
       
   160                                                   //| )),CHAR(d)),CHAR(e)),CHAR(f)),CHAR(g)),CHAR(h)),CHAR(i)),CHAR(j)),CHAR(k)),
       
   161                                                   //| CHAR(l)),CHAR(m)),CHAR(n)),CHAR(o)),CHAR(p)),CHAR(q)),CHAR(r)),CHAR(s)),CHA
       
   162                                                   //| R(t)),CHAR(u)),CHAR(v)),CHAR(w)),CHAR(x)),CHAR(y)),CHAR(z))
       
   163   val DIGIT = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
       
   164                                                   //> DIGIT  : greeter.POSIX.ALT = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),CH
       
   165                                                   //| AR(1)),CHAR(2)),CHAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHAR(
       
   166                                                   //| 9))
       
   167   val ID = SYM ~ (SYM | DIGIT).%                  //> ID  : greeter.POSIX.SEQ = SEQ(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(A
       
   168                                                   //| LT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(a),CHAR(b)),CHA
       
   169                                                   //| R(c)),CHAR(d)),CHAR(e)),CHAR(f)),CHAR(g)),CHAR(h)),CHAR(i)),CHAR(j)),CHAR(k
       
   170                                                   //| )),CHAR(l)),CHAR(m)),CHAR(n)),CHAR(o)),CHAR(p)),CHAR(q)),CHAR(r)),CHAR(s)),
       
   171                                                   //| CHAR(t)),CHAR(u)),CHAR(v)),CHAR(w)),CHAR(x)),CHAR(y)),CHAR(z)),STAR(ALT(ALT
       
   172                                                   //| (ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(AL
       
   173                                                   //| T(ALT(ALT(ALT(ALT(ALT(CHAR(a),CHAR(b)),CHAR(c)),CHAR(d)),CHAR(e)),CHAR(f)),
       
   174                                                   //| CHAR(g)),CHAR(h)),CHAR(i)),CHAR(j)),CHAR(k)),CHAR(l)),CHAR(m)),CHAR(n)),CHA
       
   175                                                   //| R(o)),CHAR(p)),CHAR(q)),CHAR(r)),CHAR(s)),CHAR(t)),CHAR(u)),CHAR(v)),CHAR(w
       
   176                                                   //| )),CHAR(x)),CHAR(y)),CHAR(z)),ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),C
       
   177                                                   //| HAR(1)),CHAR(2)),CHAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHAR
       
   178                                                   //| (9)))))
       
   179   val NUM = PLUS(DIGIT)                           //> NUM  : greeter.POSIX.SEQ = SEQ(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),
       
   180                                                   //| CHAR(1)),CHAR(2)),CHAR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHA
       
   181                                                   //| R(9)),STAR(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(CHAR(0),CHAR(1)),CHAR(2)),CH
       
   182                                                   //| AR(3)),CHAR(4)),CHAR(5)),CHAR(6)),CHAR(7)),CHAR(8)),CHAR(9))))
       
   183   val KEYWORD: Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" | "true" | "false"
       
   184                                                   //> KEYWORD  : greeter.POSIX.Rexp = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(SEQ(CHA
       
   185                                                   //| R(s),SEQ(CHAR(k),SEQ(CHAR(i),CHAR(p)))),SEQ(CHAR(w),SEQ(CHAR(h),SEQ(CHAR(i)
       
   186                                                   //| ,SEQ(CHAR(l),CHAR(e)))))),SEQ(CHAR(d),CHAR(o))),SEQ(CHAR(i),CHAR(f))),SEQ(C
       
   187                                                   //| HAR(t),SEQ(CHAR(h),SEQ(CHAR(e),CHAR(n))))),SEQ(CHAR(e),SEQ(CHAR(l),SEQ(CHAR
       
   188                                                   //| (s),CHAR(e))))),SEQ(CHAR(r),SEQ(CHAR(e),SEQ(CHAR(a),CHAR(d))))),SEQ(CHAR(w)
       
   189                                                   //| ,SEQ(CHAR(r),SEQ(CHAR(i),SEQ(CHAR(t),CHAR(e)))))),SEQ(CHAR(t),SEQ(CHAR(r),S
       
   190                                                   //| EQ(CHAR(u),CHAR(e))))),SEQ(CHAR(f),SEQ(CHAR(a),SEQ(CHAR(l),SEQ(CHAR(s),CHAR
       
   191                                                   //| (e))))))
       
   192   val SEMI: Rexp = ";"                            //> SEMI  : greeter.POSIX.Rexp = CHAR(;)
       
   193   val OP: Rexp = ":=" | "==" | "-" | "+" | "*" | "!=" | "<" | ">" | "<=" | ">=" | "%" | "/"
       
   194                                                   //> OP  : greeter.POSIX.Rexp = ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(SEQ(
       
   195                                                   //| CHAR(:),CHAR(=)),SEQ(CHAR(=),CHAR(=))),CHAR(-)),CHAR(+)),CHAR(*)),SEQ(CHAR(
       
   196                                                   //| !),CHAR(=))),CHAR(<)),CHAR(>)),SEQ(CHAR(<),CHAR(=))),SEQ(CHAR(>),CHAR(=))),
       
   197                                                   //| CHAR(%)),CHAR(/))
       
   198   val WHITESPACE = PLUS(" " | "\n" | "\t")        //> WHITESPACE  : greeter.POSIX.SEQ = SEQ(ALT(ALT(CHAR( ),CHAR(
       
   199                                                   //| )),CHAR(	)),STAR(ALT(ALT(CHAR( ),CHAR(
       
   200                                                   //| )),CHAR(	))))
       
   201   val RPAREN: Rexp = ")"                          //> RPAREN  : greeter.POSIX.Rexp = CHAR())
       
   202   val LPAREN: Rexp = "("                          //> LPAREN  : greeter.POSIX.Rexp = CHAR(()
       
   203   val BEGIN: Rexp = "{"                           //> BEGIN  : greeter.POSIX.Rexp = CHAR({)
       
   204   val END: Rexp = "}"                             //> END  : greeter.POSIX.Rexp = CHAR(})
       
   205 
       
   206   /*
       
   207  * val WHILE_REGS = (("k" $ KEYWORD) |
       
   208                   ("i" $ ID) |
       
   209                   ("o" $ OP) |
       
   210                   ("n" $ NUM) |
       
   211                   ("s" $ SEMI) |
       
   212                   ("p" $ (LPAREN | RPAREN)) |
       
   213                   ("b" $ (BEGIN | END)) |
       
   214                   ("w" $ WHITESPACE)).%
       
   215 */
       
   216 
       
   217   val WHILE_REGS = (KEYWORD |
       
   218     ID |
       
   219     OP |
       
   220     NUM |
       
   221     SEMI |
       
   222     LPAREN | RPAREN |
       
   223     BEGIN | END |
       
   224     WHITESPACE).%                                 //> WHILE_REGS  : greeter.POSIX.STAR = STAR(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT
       
   225                                                   //| (ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(SEQ(CHAR(s),SEQ(CHAR(k),SEQ(CHAR(i),CH
       
   226                                                   //| AR(p)))),SEQ(CHAR(w),SEQ(CHAR(h),SEQ(CHAR(i),SEQ(CHAR(l),CHAR(e)))))),SEQ(C
       
   227                                                   //| HAR(d),CHAR(o))),SEQ(CHAR(i),CHAR(f))),SEQ(CHAR(t),SEQ(CHAR(h),SEQ(CHAR(e),
       
   228                                                   //| CHAR(n))))),SEQ(CHAR(e),SEQ(CHAR(l),SEQ(CHAR(s),CHAR(e))))),SEQ(CHAR(r),SEQ
       
   229                                                   //| (CHAR(e),SEQ(CHAR(a),CHAR(d))))),SEQ(CHAR(w),SEQ(CHAR(r),SEQ(CHAR(i),SEQ(CH
       
   230                                                   //| AR(t),CHAR(e)))))),SEQ(CHAR(t),SEQ(CHAR(r),SEQ(CHAR(u),CHAR(e))))),SEQ(CHAR
       
   231                                                   //| (f),SEQ(CHAR(a),SEQ(CHAR(l),SEQ(CHAR(s),CHAR(e)))))),SEQ(ALT(ALT(ALT(ALT(AL
       
   232                                                   //| T(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(ALT(A
       
   233                                                   //| LT(ALT(CHAR(a),CHAR(b)),CHAR(c)),CHAR(d)),CHAR(e)),CHAR(f)),CHAR(g)),CHAR(h
       
   234                                                   //| )),CHAR(i)),CHAR(j)),CHAR(k)),CHAR(l)),CHAR(m)),CHAR(n)),CHAR(o)),CHAR(p)),
       
   235                                                   //| CHAR(q)),CHAR(r)),CHAR(s)),CHAR(t)),CHAR(u)),CHAR(v)),CHAR(w)),CHAR(x)),CHA
       
   236                                                   //| R(y)),CHAR(z)),STAR(ALT
       
   237                                                   //| Output exceeds cutoff limit.
       
   238 
       
   239   // Some Tests
       
   240   //============
       
   241 
       
   242   def time[T](code: => T) = {
       
   243     val start = System.nanoTime()
       
   244     val result = code
       
   245     val end = System.nanoTime()
       
   246     println((end - start) / 1.0e9)
       
   247     result
       
   248   }                                               //> time: [T](code: => T)T
       
   249 
       
   250   val prog0 = """read n"""                        //> prog0  : String = read n
       
   251   //env(lexing_simp(WHILE_REGS, prog0))
       
   252 
       
   253   println("Next test")                            //> Next test
       
   254 }