progs/comb2.scala
changeset 686 05cfce0fdef7
parent 685 75d9f9e5906f
equal deleted inserted replaced
685:75d9f9e5906f 686:05cfce0fdef7
    52 
    52 
    53 
    53 
    54 implicit def string2parser(s : String) = StringParser(s)
    54 implicit def string2parser(s : String) = StringParser(s)
    55 
    55 
    56 implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
    56 implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
    57   def || (q : => Parser[I, T]) = new AltParser[I, T](p, q)
    57   def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
    58   def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
    58   def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
    59   def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
    59   def ~[S](q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
    60 }
    60 }
    61 
    61 
    62 implicit def StringOps(s: String) = new {
    62 implicit def StringOps(s: String) = new {
    63   def || (q : => Parser[String, String]) = new AltParser[String, String](s, q)
    63   def ||(q : => Parser[String, String]) = new AltParser[String, String](s, q)
    64   def || (r: String) = new AltParser[String, String](s, r)
    64   def ||(r: String) = new AltParser[String, String](s, r)
    65   def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
    65   def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
    66   def ~[S](q : => Parser[String, S]) = 
    66   def ~[S](q : => Parser[String, S]) = 
    67     new SeqParser[String, String, S](s, q)
    67     new SeqParser[String, String, S](s, q)
    68   def ~ (r: String) = 
    68   def ~(r: String) = 
    69     new SeqParser[String, String, String](s, r)
    69     new SeqParser[String, String, String](s, r)
    70 }
    70 }
    71 
    71 
    72 
    72 
    73 // the abstract syntax trees for the WHILE language
    73 // the abstract syntax trees for the WHILE language
   100     case None => Set()
   100     case None => Set()
   101     case Some(s) => Set(sb.splitAt(s.length))
   101     case Some(s) => Set(sb.splitAt(s.length))
   102   }
   102   }
   103 }
   103 }
   104 
   104 
       
   105 // arithmetic expressions
   105 lazy val AExp: Parser[String, AExp] = 
   106 lazy val AExp: Parser[String, AExp] = 
   106   (Te ~ "+" ~ AExp) ==> { case x ~ _ ~ z => Aop("+", x, z): AExp } ||
   107   (Te ~ "+" ~ AExp) ==> { case x ~ _ ~ z => Aop("+", x, z): AExp } ||
   107   (Te ~ "-" ~ AExp) ==> { case x ~ _ ~ z => Aop("-", x, z): AExp } || Te 
   108   (Te ~ "-" ~ AExp) ==> { case x ~ _ ~ z => Aop("-", x, z): AExp } || Te 
   108 lazy val Te: Parser[String, AExp] = 
   109 lazy val Te: Parser[String, AExp] = 
   109   (Fa ~ "*" ~ Te) ==> { case x ~ _ ~ z => Aop("*", x, z): AExp } || 
   110   (Fa ~ "*" ~ Te) ==> { case x ~ _ ~ z => Aop("*", x, z): AExp } || 
   138   (Stmt ~ ";" ~ Stmts) ==> { case x ~ _ ~ z => x :: z : Block } ||
   139   (Stmt ~ ";" ~ Stmts) ==> { case x ~ _ ~ z => x :: z : Block } ||
   139   (Stmt ==> ( s => List(s) : Block))
   140   (Stmt ==> ( s => List(s) : Block))
   140 
   141 
   141 // blocks (enclosed in curly braces)
   142 // blocks (enclosed in curly braces)
   142 lazy val Block: Parser[String, Block] =
   143 lazy val Block: Parser[String, Block] =
   143   (("{" ~ Stmts ~ "}") ==> { case x ~ y ~ z => y} || 
   144   (("{" ~ Stmts ~ "}") ==> { case _ ~ y ~ _ => y } || 
   144    (Stmt ==> (s => List(s))))
   145    (Stmt ==> (s => List(s))))
   145 
   146 
   146 
   147 
   147 Stmts.parse_all("x2:=5+3;")
   148 Stmts.parse_all("x2:=5+3;")
   148 Block.parse_all("{x:=5;y:=8}")
   149 Block.parse_all("{x:=5;y:=8}")