progs/lexer/lex.sc
changeset 787 4b2496bc79b2
parent 786 da2488db453e
child 800 9eea6a801e10
equal deleted inserted replaced
786:da2488db453e 787:4b2496bc79b2
     9 case class CHAR(c: Char) extends Rexp
     9 case class CHAR(c: Char) extends Rexp
    10 case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
    10 case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
    11 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
    11 case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
    12 case class STAR(r: Rexp) extends Rexp 
    12 case class STAR(r: Rexp) extends Rexp 
    13 case class RECD(x: String, r: Rexp) extends Rexp  
    13 case class RECD(x: String, r: Rexp) extends Rexp  
    14                 // records for extracting strings or tokens
    14           // records for extracting strings or tokens
    15   
    15   
    16 // values  
    16 // values  
    17 abstract class Val
    17 abstract class Val
    18 case object Empty extends Val
    18 case object Empty extends Val
    19 case class Chr(c: Char) extends Val
    19 case class Chr(c: Char) extends Val
    31   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    31   case c::s => SEQ(CHAR(c), charlist2rexp(s))
    32 }
    32 }
    33 implicit def string2rexp(s : String) : Rexp = 
    33 implicit def string2rexp(s : String) : Rexp = 
    34   charlist2rexp(s.toList)
    34   charlist2rexp(s.toList)
    35 
    35 
       
    36 val HELLO : Rexp = "hello"
       
    37 
    36 implicit def RexpOps(r: Rexp) = new {
    38 implicit def RexpOps(r: Rexp) = new {
    37   def | (s: Rexp) = ALT(r, s)
    39   def | (s: Rexp) = ALT(r, s)
    38   def % = STAR(r)
    40   def % = STAR(r)
    39   def ~ (s: Rexp) = SEQ(r, s)
    41   def ~ (s: Rexp) = SEQ(r, s)
    40 }
    42 }
    45   def % = STAR(s)
    47   def % = STAR(s)
    46   def ~ (r: Rexp) = SEQ(s, r)
    48   def ~ (r: Rexp) = SEQ(s, r)
    47   def ~ (r: String) = SEQ(s, r)
    49   def ~ (r: String) = SEQ(s, r)
    48   def $ (r: Rexp) = RECD(s, r)
    50   def $ (r: Rexp) = RECD(s, r)
    49 }
    51 }
       
    52 
       
    53 val TEST = ("ab" | "ba").%
    50 
    54 
    51 def nullable(r: Rexp) : Boolean = r match {
    55 def nullable(r: Rexp) : Boolean = r match {
    52   case ZERO => false
    56   case ZERO => false
    53   case ONE => true
    57   case ONE => true
    54   case CHAR(_) => false
    58   case CHAR(_) => false
   147 val NUM = PLUS(DIGIT)
   151 val NUM = PLUS(DIGIT)
   148 val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" 
   152 val KEYWORD : Rexp = "skip" | "while" | "do" | "if" | "then" | "else" | "read" | "write" 
   149 val SEMI: Rexp = ";"
   153 val SEMI: Rexp = ";"
   150 val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
   154 val OP: Rexp = ":=" | "=" | "-" | "+" | "*" | "!=" | "<" | ">"
   151 val WHITESPACE = PLUS(" " | "\n" | "\t")
   155 val WHITESPACE = PLUS(" " | "\n" | "\t")
   152 val RPAREN: Rexp = "{"
   156 val RPAREN: Rexp = "}"
   153 val LPAREN: Rexp = "}"
   157 val LPAREN: Rexp = "{"
   154 val STRING: Rexp = "\"" ~ SYM.% ~ "\""
   158 val STRING: Rexp = "\"" ~ SYM.% ~ "\""
   155 
   159 
   156 
   160 
   157 val WHILE_REGS = (("k" $ KEYWORD) | 
   161 val WHILE_REGS = (("k" $ KEYWORD) | 
   158                   ("i" $ ID) | 
   162                   ("i" $ ID) |