progs/lexer/lexer.sc
changeset 955 47acfd7f9096
parent 947 dc31a099dc98
child 960 c7009356ddd8
equal deleted inserted replaced
954:eda0ccf56c72 955:47acfd7f9096
     1 // A simple lexer inspired by work of Sulzmann & Lu
     1 // A simple lexer inspired by work of Sulzmann $ Lu
     2 //==================================================
     2 //==================================================
     3 //
     3 //
     4 // Call the test cases with 
     4 // Call the test cases with 
     5 //
     5 //
     6 //   amm lexer.sc small
     6 //   amm lexer.sc small
    41 }
    41 }
    42 
    42 
    43 implicit def string2rexp(s : String) : Rexp = 
    43 implicit def string2rexp(s : String) : Rexp = 
    44   charlist2rexp(s.toList)
    44   charlist2rexp(s.toList)
    45 
    45 
    46 // to use & for records, instead of $ which had
    46 
    47 // its precedence be changed in Scala 3
       
    48 extension (s: String) {
    47 extension (s: String) {
    49   def & (r: Rexp) = RECD(s, r)
    48   def $ (r: Rexp) = RECD(s, r)
    50 }
    49 }
    51 
    50 
    52 extension (r: Rexp) {
    51 extension (r: Rexp) {
    53   def | (s: Rexp) = ALT(r, s)
    52   def | (s: Rexp) = ALT(r, s)
    54   def % = STAR(r)
    53   def % = STAR(r)
    58 def nullable(r: Rexp) : Boolean = r match {
    57 def nullable(r: Rexp) : Boolean = r match {
    59   case ZERO => false
    58   case ZERO => false
    60   case ONE => true
    59   case ONE => true
    61   case CHAR(_) => false
    60   case CHAR(_) => false
    62   case ALT(r1, r2) => nullable(r1) || nullable(r2)
    61   case ALT(r1, r2) => nullable(r1) || nullable(r2)
    63   case SEQ(r1, r2) => nullable(r1) && nullable(r2)
    62   case SEQ(r1, r2) => nullable(r1) $$ nullable(r2)
    64   case STAR(_) => true
    63   case STAR(_) => true
    65   case RECD(_, r1) => nullable(r1)
    64   case RECD(_, r1) => nullable(r1)
    66 }
    65 }
    67 
    66 
    68 def der(c: Char, r: Rexp) : Rexp = r match {
    67 def der(c: Char, r: Rexp) : Rexp = r match {
   207 val RPAREN: Rexp = "{"
   206 val RPAREN: Rexp = "{"
   208 val LPAREN: Rexp = "}"
   207 val LPAREN: Rexp = "}"
   209 val STRING: Rexp = "\"" ~ SYM.% ~ "\""
   208 val STRING: Rexp = "\"" ~ SYM.% ~ "\""
   210 
   209 
   211 
   210 
   212 val WHILE_REGS = (("k" & KEYWORD) | 
   211 val WHILE_REGS = (("k" $ KEYWORD) | 
   213                   ("i" & ID) | 
   212                   ("i" $ ID) | 
   214                   ("o" & OP) | 
   213                   ("o" $ OP) | 
   215                   ("n" & NUM) | 
   214                   ("n" $ NUM) | 
   216                   ("s" & SEMI) | 
   215                   ("s" $ SEMI) | 
   217                   ("str" & STRING) |
   216                   ("str" $ STRING) |
   218                   ("p" & (LPAREN | RPAREN)) | 
   217                   ("p" $ (LPAREN | RPAREN)) | 
   219                   ("w" & WHITESPACE)).%
   218                   ("w" $ WHITESPACE)).%
   220 
   219 
   221 
   220 
   222 // Two Simple While Tests
   221 // Two Simple While Tests
   223 //========================
   222 //========================
   224 
   223