progs/lexer/lexer.sc
changeset 943 5365ef60707e
parent 936 0b5f06539a84
child 944 f5d2453c5640
equal deleted inserted replaced
942:c82a45f48bfc 943:5365ef60707e
    40   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    40   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    41 }
    41 }
    42 implicit def string2rexp(s : String) : Rexp = 
    42 implicit def string2rexp(s : String) : Rexp = 
    43   charlist2rexp(s.toList)
    43   charlist2rexp(s.toList)
    44 
    44 
    45 extension (r: Rexp) {
       
    46   def | (s: Rexp) = ALT(r, s)
       
    47   def % = STAR(r)
       
    48   def ~ (s: Rexp) = SEQ(r, s)
       
    49 }
       
    50 
       
    51 extension (s: String) {
    45 extension (s: String) {
    52   def | (r: Rexp) = ALT(s, r)
    46   def | (r: Rexp) = ALT(s, r)
    53   def | (r: String) = ALT(s, r)
    47   def | (r: String) = ALT(s, r)
    54   def % = STAR(s)
    48   def % = STAR(s)
    55   def ~ (r: Rexp) = SEQ(s, r)
    49   def ~ (r: Rexp) = SEQ(s, r)
    56   def ~ (r: String) = SEQ(s, r)
    50   def ~ (r: String) = SEQ(s, r)
    57   def $ (r: Rexp) = RECD(s, r)
    51   def $ (r: Rexp) = RECD(s, r)
    58 }
    52 }
       
    53 
       
    54 extension (r: Rexp) {
       
    55   def | (s: Rexp) = ALT(r, s)
       
    56   def % = STAR(r)
       
    57   def ~ (s: Rexp) = SEQ(r, s)
       
    58 }
       
    59 
       
    60 
       
    61 
       
    62 val r : Rexp = ("a" | "b").% 
       
    63 println(r) 
    59 
    64 
    60 def nullable(r: Rexp) : Boolean = r match {
    65 def nullable(r: Rexp) : Boolean = r match {
    61   case ZERO => false
    66   case ZERO => false
    62   case ONE => true
    67   case ONE => true
    63   case CHAR(_) => false
    68   case CHAR(_) => false
    77     else SEQ(der(c, r1), r2)
    82     else SEQ(der(c, r1), r2)
    78   case STAR(r) => SEQ(der(c, r), STAR(r))
    83   case STAR(r) => SEQ(der(c, r), STAR(r))
    79   case RECD(_, r1) => der(c, r1)
    84   case RECD(_, r1) => der(c, r1)
    80 }
    85 }
    81 
    86 
       
    87 println(der('a', ALT(STAR("a"), "b")))
    82 
    88 
    83 // extracts a string from a value
    89 // extracts a string from a value
    84 def flatten(v: Val) : String = v match {
    90 def flatten(v: Val) : String = v match {
    85   case Empty => ""
    91   case Empty => ""
    86   case Chr(c) => c.toString
    92   case Chr(c) => c.toString
   125   case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
   131   case (ALT(r1, r2), Left(v1)) => Left(inj(r1, c, v1))
   126   case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
   132   case (ALT(r1, r2), Right(v2)) => Right(inj(r2, c, v2))
   127   case (CHAR(d), Empty) => Chr(c) 
   133   case (CHAR(d), Empty) => Chr(c) 
   128   case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
   134   case (RECD(x, r1), _) => Rec(x, inj(r1, c, v))
   129 }
   135 }
       
   136 
       
   137 
   130 
   138 
   131 // some "rectification" functions for simplification
   139 // some "rectification" functions for simplification
   132 def F_ID(v: Val): Val = v
   140 def F_ID(v: Val): Val = v
   133 def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
   141 def F_RIGHT(f: Val => Val) = (v:Val) => Right(f(v))
   134 def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))
   142 def F_LEFT(f: Val => Val) = (v:Val) => Left(f(v))