progs/lexer/lexer.sc
changeset 947 dc31a099dc98
parent 945 5070392a1bd7
child 955 47acfd7f9096
equal deleted inserted replaced
946:bee7c57c18c3 947:dc31a099dc98
    37 def charlist2rexp(s : List[Char]): Rexp = s match {
    37 def charlist2rexp(s : List[Char]): Rexp = s match {
    38   case Nil => ONE
    38   case Nil => ONE
    39   case c::Nil => CHAR(c)
    39   case c::Nil => CHAR(c)
    40   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    40   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    41 }
    41 }
       
    42 
    42 implicit def string2rexp(s : String) : Rexp = 
    43 implicit def string2rexp(s : String) : Rexp = 
    43   charlist2rexp(s.toList)
    44   charlist2rexp(s.toList)
    44 
    45 
       
    46 // to use & for records, instead of $ which had
       
    47 // its precedence be changed in Scala 3
    45 extension (s: String) {
    48 extension (s: String) {
    46   def | (r: Rexp) = ALT(s, r)
    49   def & (r: Rexp) = RECD(s, r)
    47   def | (r: String) = ALT(s, r)
       
    48   def % = STAR(s)
       
    49   def ~ (r: Rexp) = SEQ(s, r)
       
    50   def ~ (r: String) = SEQ(s, r)
       
    51   def $ (r: Rexp) = RECD(s, r)
       
    52 }
    50 }
    53 
    51 
    54 extension (r: Rexp) {
    52 extension (r: Rexp) {
    55   def | (s: Rexp) = ALT(r, s)
    53   def | (s: Rexp) = ALT(r, s)
    56   def % = STAR(r)
    54   def % = STAR(r)
    57   def ~ (s: Rexp) = SEQ(r, s)
    55   def ~ (s: Rexp) = SEQ(r, s)
    58 }
    56 }
    59 
       
    60 
       
    61 
    57 
    62 def nullable(r: Rexp) : Boolean = r match {
    58 def nullable(r: Rexp) : Boolean = r match {
    63   case ZERO => false
    59   case ZERO => false
    64   case ONE => true
    60   case ONE => true
    65   case CHAR(_) => false
    61   case CHAR(_) => false
   211 val RPAREN: Rexp = "{"
   207 val RPAREN: Rexp = "{"
   212 val LPAREN: Rexp = "}"
   208 val LPAREN: Rexp = "}"
   213 val STRING: Rexp = "\"" ~ SYM.% ~ "\""
   209 val STRING: Rexp = "\"" ~ SYM.% ~ "\""
   214 
   210 
   215 
   211 
   216 val WHILE_REGS = (("k" $ KEYWORD) | 
   212 val WHILE_REGS = (("k" & KEYWORD) | 
   217                   ("i" $ ID) | 
   213                   ("i" & ID) | 
   218                   ("o" $ OP) | 
   214                   ("o" & OP) | 
   219                   ("n" $ NUM) | 
   215                   ("n" & NUM) | 
   220                   ("s" $ SEMI) | 
   216                   ("s" & SEMI) | 
   221                   ("str" $ STRING) |
   217                   ("str" & STRING) |
   222                   ("p" $ (LPAREN | RPAREN)) | 
   218                   ("p" & (LPAREN | RPAREN)) | 
   223                   ("w" $ WHITESPACE)).%
   219                   ("w" & WHITESPACE)).%
   224 
   220 
   225 
   221 
   226 // Two Simple While Tests
   222 // Two Simple While Tests
   227 //========================
   223 //========================
   228 
   224