progs/comb1.scala
changeset 594 d40d7d7b85bc
parent 593 bb24d4e207b6
child 598 e3ad67cd5123
equal deleted inserted replaced
593:bb24d4e207b6 594:d40d7d7b85bc
   100   ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } | NumParserInt
   100   ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } | NumParserInt
   101 
   101 
   102 
   102 
   103 println(E.parse_all("4*2+3"))
   103 println(E.parse_all("4*2+3"))
   104 println(E.parse_all("4*(2+3)"))
   104 println(E.parse_all("4*(2+3)"))
       
   105 println(E.parse_all("(4)*((2+3))"))
   105 println(E.parse_all("4/2+3"))
   106 println(E.parse_all("4/2+3"))
   106 println(E.parse("1 + 2 * 3"))
   107 println(E.parse("1 + 2 * 3"))
   107 println(E.parse_all("(1+2)+3"))
   108 println(E.parse_all("(1+2)+3"))
   108 println(E.parse_all("1+2+3"))  
   109 println(E.parse_all("1+2+3"))  
   109 
   110 
   116    "(" ~ EL ~ ")" ==> { case ((x, y), z) => y} |
   117    "(" ~ EL ~ ")" ==> { case ((x, y), z) => y} |
   117    NumParserInt)
   118    NumParserInt)
   118 
   119 
   119 //println(EL.parse_all("1+2+3"))
   120 //println(EL.parse_all("1+2+3"))
   120 
   121 
   121 
       
   122 // a repetition parser
       
   123 
       
   124 def RepParser[I  <% Seq[_], T](p: => Parser[I, T]): Parser[I, List[T]] = {
       
   125   p ==> { case x => x :: Nil } |
       
   126   p ~ RepParser(p) ==> { case (x, y) => x :: y }   
       
   127 }
       
   128 
       
   129 
       
   130 // a repetition parser
       
   131 lazy val R : Parser[String, List[Char]] = RepParser('a') 
       
   132 println(R.parse_all("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"))
       
   133 
   122 
   134 
   123 
   135 
   124 
   136 // non-ambiguous vs ambiguous grammars
   125 // non-ambiguous vs ambiguous grammars
   137 lazy val S : Parser[String, String] =
   126 lazy val S : Parser[String, String] =