progs/comb2.scala
changeset 683 c6c79d21f8a8
parent 673 715b46eee102
child 684 1ee523c4f098
equal deleted inserted replaced
682:553b4d4e3719 683:c6c79d21f8a8
    12 
    12 
    13 abstract class Parser[I : IsSeq, T] {
    13 abstract class Parser[I : IsSeq, T] {
    14   def parse(ts: I): Set[(T, I)]
    14   def parse(ts: I): Set[(T, I)]
    15 
    15 
    16   def parse_all(ts: I) : Set[T] =
    16   def parse_all(ts: I) : Set[T] =
    17     for ((head, tail) <- parse(ts); if (tail.isEmpty)) yield head
    17     for ((head, tail) <- parse(ts); if tail.isEmpty) yield head
    18 }
    18 }
    19 
    19 
    20 class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
    20 class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
    21   def parse(sb: I) = 
    21   def parse(sb: I) = 
    22     for ((head1, tail1) <- p.parse(sb); 
    22     for ((head1, tail1) <- p.parse(sb); 
   121    (AExp ~ ">" ~ AExp) ==> { case x ~ y ~ z => Bop(">", x, z): BExp } ||
   121    (AExp ~ ">" ~ AExp) ==> { case x ~ y ~ z => Bop(">", x, z): BExp } ||
   122    ("(" ~ BExp ~ ")" ~ "&&" ~ BExp) ==> { case x ~ y ~ z ~ u ~ v => And(y, v): BExp } ||
   122    ("(" ~ BExp ~ ")" ~ "&&" ~ BExp) ==> { case x ~ y ~ z ~ u ~ v => And(y, v): BExp } ||
   123    ("(" ~ BExp ~ ")" ~ "||" ~ BExp) ==> { case x ~ y ~ z ~ u ~ v => Or(y, v): BExp } ||
   123    ("(" ~ BExp ~ ")" ~ "||" ~ BExp) ==> { case x ~ y ~ z ~ u ~ v => Or(y, v): BExp } ||
   124    ("true" ==> (_ => True: BExp )) || 
   124    ("true" ==> (_ => True: BExp )) || 
   125    ("false" ==> (_ => False: BExp )) ||
   125    ("false" ==> (_ => False: BExp )) ||
   126    ("(" ~ BExp ~ ")") ==> { case x ~ y ~ z => y}
   126    ("(" ~ BExp ~ ")") ==> { case _ ~ x ~ _ => x }
   127 
   127 
   128 // statement / statements
   128 // statement / statements
   129 lazy val Stmt: Parser[String, Stmt] =
   129 lazy val Stmt: Parser[String, Stmt] =
   130   (("skip" ==> (_ => Skip: Stmt)) ||
   130   (("skip" ==> (_ => Skip: Stmt)) ||
   131    (IdParser ~ ":=" ~ AExp) ==> { case x ~ y ~ z => Assign(x, z): Stmt } ||
   131    (IdParser ~ ":=" ~ AExp) ==> { case x ~ y ~ z => Assign(x, z): Stmt } ||