progs/comb1a.scala
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 04 Jul 2020 16:58:12 +0100
changeset 732 c7bdd7eac4cb
parent 686 05cfce0fdef7
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
import scala.language.implicitConversions
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
import scala.language.reflectiveCalls
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
672
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 629
diff changeset
     4
// more convenience for the semantic actions later on
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 629
diff changeset
     5
case class ~[+A, +B](_1: A, _2: B)
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 629
diff changeset
     6
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 629
diff changeset
     7
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
/* Note, in the lectures I did not show the implicit type consraint
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
 * I => Seq[_], which means that the input type 'I' needs to be
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
 * a sequence. */
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
type IsSeq[A] = A => Seq[_]
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
abstract class Parser[I : IsSeq, T] {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
  def parse(ts: I): Set[(T, I)]
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
  def parse_all(ts: I) : Set[T] =
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
    for ((head, tail) <- parse(ts); 
683
c6c79d21f8a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 673
diff changeset
    19
        if tail.isEmpty) yield head
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
                                 q: => Parser[I, S]) extends Parser[I, ~[T, S]] {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
  def parse(sb: I) = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
    for ((head1, tail1) <- p.parse(sb); 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
         (head2, tail2) <- q.parse(tail1)) yield (new ~(head1, head2), tail2)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
class AltParser[I : IsSeq, T](p: => Parser[I, T], 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
                              q: => Parser[I, T]) extends Parser[I, T] {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
class FunParser[I : IsSeq, T, S](p: => Parser[I, T], 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
                                 f: T => S) extends Parser[I, S] {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
  def parse(sb: I) = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
// atomic parsers for characters, numbers and strings
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
case class CharParser(c: Char) extends Parser[String, Char] {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
  def parse(sb: String) = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
    if (sb != "" && sb.head == c) Set((c, sb.tail)) else Set()
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    46
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
import scala.util.matching.Regex
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
case class RegexParser(reg: Regex) extends Parser[String, String] {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
  def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
    case None => Set()
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
    case Some(m) => Set((m.matched, m.after.toString))  
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
  }
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
val NumParser = RegexParser("[0-9]+".r)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
def StringParser(s: String) = RegexParser(Regex.quote(s).r)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
// NumParserInt2 transforms a "string integer" into an Int;
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
// needs new, because FunParser is not a case class
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
val NumParserInt2 = new FunParser(NumParser, (s: String) => s.toInt)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
// convenience
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
implicit def string2parser(s: String) = StringParser(s)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
implicit def char2parser(c: Char) = CharParser(c)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
    68
implicit def ParserOps[I : IsSeq, T](p: Parser[I, T]) = new {
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    69
  def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
implicit def StringOps(s: String) = new {
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    75
  def ||(q : => Parser[String, String]) = new AltParser[String, String](s, q)
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    76
  def ||(r: String) = new AltParser[String, String](s, r)
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  def ==>[S] (f: => String => S) = new FunParser[String, String, S](s, f)
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    78
  def ~[S](q : => Parser[String, S]) = 
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
    new SeqParser[String, String, S](s, q)
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    80
  def ~(r: String) = 
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
    new SeqParser[String, String, String](s, r)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
}
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
// NumParserInt can now be written as
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
val NumParserInt = NumParser ==> (s => s.toInt)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
lazy val Pal : Parser[String, String] = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  (("a" ~ Pal ~ "a") ==> { case x ~ y ~ z => x + y + z } ||
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
   ("b" ~ Pal ~ "b") ==> { case x ~ y ~ z => x + y + z } || "a" || "b" || "")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    91
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
Pal.parse_all("abaaaba")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
Pal.parse("abaaaba")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
println("Palindrome: " + Pal.parse_all("abaaaba"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
// well-nested parentheses parser (transforms '(' -> '{' , ')' -> '}' )
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
lazy val P : Parser[String, String] = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  "(" ~ P ~ ")" ~ P ==> { case _ ~ x ~ _ ~ y => "{" + x + "}" + y } || ""
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
P.parse_all("(((()()))())")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
P.parse_all("(((()()))()))")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
P.parse_all(")(")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
P.parse_all("()")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   106
// Arithmetic Expressions (Terms and Factors)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   107
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   108
lazy val E: Parser[String, Int] = 
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   109
  (T ~ "+" ~ E) ==> { case x ~ _ ~ z => x + z } ||
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   110
  (T ~ "-" ~ E) ==> { case x ~ _ ~ z => x - z } || T 
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
lazy val T: Parser[String, Int] = 
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   112
  (F ~ "*" ~ T) ==> { case x ~ _ ~ z => x * z } || F
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
lazy val F: Parser[String, Int] = 
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   114
  ("(" ~ E ~ ")") ==> { case _ ~ y ~ _ => y } || NumParserInt
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
/* same parser but producing a string
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
lazy val E: Parser[String, String] = 
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   118
  (T ~ "+" ~ E) ==> { case x ~ y ~ z => "(" + x + ")+(" + z + ")"} || T 
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
lazy val T: Parser[String, String] = 
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   120
  (F ~ "*" ~ T) ==> { case x ~ y ~ z => "(" + x + ")*("+ z + ")"} || F
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
lazy val F: Parser[String, String] = 
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   122
  ("(" ~ E ~ ")") ==> { case x ~ y ~ z => y } || NumParser
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   123
*/
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   124
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   125
println(E.parse_all("1+3+4"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   126
println(E.parse("1+3+4"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
println(E.parse_all("4*2+3"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   128
println(E.parse_all("4*(2+3)"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   129
println(E.parse_all("(4)*((2+3))"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   130
println(E.parse_all("4/2+3"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   131
println(E.parse("1 + 2 * 3"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   132
println(E.parse_all("(1+2)+3"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   133
println(E.parse_all("1+2+3"))  
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   134
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
// no left-recursion allowed, otherwise will loop
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
lazy val EL: Parser[String, Int] = 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
  (EL ~ "+" ~ EL ==> { case x ~ y ~ z => x + z} || 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
   EL ~ "*" ~ EL ==> { case x ~ y ~ z => x * z} ||
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
   "(" ~ EL ~ ")" ==> { case x ~ y ~ z => y} ||
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
   NumParserInt)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
//println(EL.parse_all("1+2+3"))
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
// non-ambiguous vs ambiguous grammars
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   151
// ambiguous
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   152
lazy val S : Parser[String, String] =
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
  ("1" ~ S ~ S) ==> { case x ~ y ~ z => x + y + z } || ""
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   154
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   155
S.parse("1" * 10)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   156
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
// non-ambiguous
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   158
lazy val U : Parser[String, String] =
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   159
  ("1" ~ U) ==> { case x ~ y => x + y  } || ""
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   160
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
U.parse("1" * 25)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
U.parse("11")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
U.parse("11111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
U.parse("11011")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
U.parse_all("1" * 100)
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
U.parse_all("1" * 100 + "0")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
lazy val UCount : Parser[String, Int] =
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
  ("1" ~ UCount) ==> { case x ~ y => y + 1 } || "" ==> { x => 0 }
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
UCount.parse("11111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
UCount.parse_all("11111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   178
// Single Character parser
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
lazy val One : Parser[String, String] = "1"
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   180
lazy val Two : Parser[String, String] = "2"
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   181
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   182
One.parse("1")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   183
One.parse("111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   184
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   185
(One ~ One).parse("111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   186
(One ~ One ~ One).parse("111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
(One ~ One ~ One ~ One).parse("1111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   189
(One || Two).parse("111")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   190
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   191
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   192
672
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 629
diff changeset
   193
// a problem with the arithmetic expression parser -> gets 
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   194
// slow with deeply nested parentheses
672
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 629
diff changeset
   195
println("Runtime problem")
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   196
E.parse("1")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   197
E.parse("(1)")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
E.parse("((1))")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
E.parse("(((1)))")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   200
E.parse("((((1))))")
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   201
E.parse("((((((1))))))")
672
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 629
diff changeset
   202
E.parse("(((((((1)))))))")
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 629
diff changeset
   203
E.parse("((((((((1)))))))")