progs/comb1.scala
author Christian Urban <urbanc@in.tum.de>
Thu, 14 Nov 2019 13:17:02 +0000
changeset 687 8865f4f2be59
parent 686 05cfce0fdef7
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     1
import scala.language.implicitConversions
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
import scala.language.reflectiveCalls
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
     4
/* Note, in the lectures I did not show the implicit type 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
     5
 * constraint IsSeq, which means that the input type 'I' needs 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
     6
 * to be a sequence. */
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
     7
667
412556272333 updated
Christian Urban <urbanc@in.tum.de>
parents: 629
diff changeset
     8
type IsSeq[A] = A => Seq[_]
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
     9
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    10
abstract class Parser[I : IsSeq, T] {
461
890188804fb4 updated
Christian Urban <urbanc@in.tum.de>
parents: 458
diff changeset
    11
  def parse(ts: I): Set[(T, I)]
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    12
461
890188804fb4 updated
Christian Urban <urbanc@in.tum.de>
parents: 458
diff changeset
    13
  def parse_all(ts: I) : Set[T] =
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    14
    for ((head, tail) <- parse(ts); 
683
c6c79d21f8a8 updated
Christian Urban <urbanc@in.tum.de>
parents: 673
diff changeset
    15
        if tail.isEmpty) yield head
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    16
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    18
class SeqParser[I : IsSeq, T, S](p: => Parser[I, T], 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    19
                                 q: => Parser[I, S]) extends Parser[I, (T, S)] {
461
890188804fb4 updated
Christian Urban <urbanc@in.tum.de>
parents: 458
diff changeset
    20
  def parse(sb: I) = 
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
    for ((head1, tail1) <- p.parse(sb); 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
         (head2, tail2) <- q.parse(tail1)) yield ((head1, head2), tail2)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    25
class AltParser[I : IsSeq, T](p: => Parser[I, T], 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    26
                              q: => Parser[I, T]) extends Parser[I, T] {
461
890188804fb4 updated
Christian Urban <urbanc@in.tum.de>
parents: 458
diff changeset
    27
  def parse(sb: I) = p.parse(sb) ++ q.parse(sb)   
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
629
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    30
class FunParser[I : IsSeq, T, S](p: => Parser[I, T], 
1b718d6065c2 updated
Christian Urban <urbanc@in.tum.de>
parents: 628
diff changeset
    31
                                 f: T => S) extends Parser[I, S] {
461
890188804fb4 updated
Christian Urban <urbanc@in.tum.de>
parents: 458
diff changeset
    32
  def parse(sb: I) = 
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
    for ((head, tail) <- p.parse(sb)) yield (f(head), tail)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
    36
// atomic parsers for characters, numbers and strings
461
890188804fb4 updated
Christian Urban <urbanc@in.tum.de>
parents: 458
diff changeset
    37
case class CharParser(c: Char) extends Parser[String, Char] {
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
    38
  def parse(sb: String) = 
462
Christian Urban <urbanc@in.tum.de>
parents: 461
diff changeset
    39
    if (sb != "" && sb.head == c) Set((c, sb.tail)) else Set()
177
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
    40
}
53def1fbf472 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 172
diff changeset
    41
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    42
import scala.util.matching.Regex
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    43
case class RegexParser(reg: Regex) extends Parser[String, String] {
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    44
  def parse(sb: String) = reg.findPrefixMatchOf(sb) match {
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    45
    case None => Set()
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    46
    case Some(m) => Set((m.matched, m.after.toString))  
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
  }
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    50
val NumParser = RegexParser("[0-9]+".r)
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    51
def StringParser(s: String) = RegexParser(Regex.quote(s).r)
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    52
672
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 667
diff changeset
    53
// NumParserInt2 transforms a "string integer" into an actual Int;
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 599
diff changeset
    54
// needs new, because FunParser is not a case class
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
    55
val NumParserInt2 = new FunParser(NumParser, (s: String) => s.toInt)
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 599
diff changeset
    56
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
360
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 185
diff changeset
    58
// convenience
462
Christian Urban <urbanc@in.tum.de>
parents: 461
diff changeset
    59
implicit def string2parser(s: String) = StringParser(s)
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    60
implicit def char2parser(c: Char) = CharParser(c)
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    61
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 599
diff changeset
    62
implicit def ParserOps[I, T](p: Parser[I, T])(implicit ev: I => Seq[_]) = new {
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    63
  def ||(q : => Parser[I, T]) = new AltParser[I, T](p, q)
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    64
  def ==>[S] (f: => T => S) = new FunParser[I, T, S](p, f)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    65
  def ~[S] (q : => Parser[I, S]) = new SeqParser[I, T, S](p, q)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    66
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    67
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    68
implicit def StringOps(s: String) = new {
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    69
  def ||(q : => Parser[String, String]) = new AltParser[String, String](s, q)
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    70
  def ||(r: String) = new AltParser[String, String](s, r)
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    71
  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
    72
  def ~[S](q : => Parser[String, S]) = 
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    73
    new SeqParser[String, String, S](s, q)
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
    74
  def ~(r: String) = 
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    75
    new SeqParser[String, String, String](s, r)
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    76
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    77
672
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 667
diff changeset
    78
// NumParserInt can now be written as _ ===> _
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 667
diff changeset
    79
// the first part is the parser, and the second the 
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 667
diff changeset
    80
// semantic action
624
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 599
diff changeset
    81
val NumParserInt = NumParser ==> (s => s.toInt)
8d0af38389bc updated to Scala 2.13
Christian Urban <urbanc@in.tum.de>
parents: 599
diff changeset
    82
588
a4646557016d updated
Christian Urban <urbanc@in.tum.de>
parents: 586
diff changeset
    83
672
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 667
diff changeset
    84
// palindromes
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    85
lazy val Pal : Parser[String, String] = 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
    86
  (("a" ~ Pal ~ "a") ==> { case ((x, y), z) => x + y + z } ||
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
    87
   ("b" ~ Pal ~ "b") ==> { case ((x, y), z) => x + y + z } || "a" || "b" || "")
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    88
586
Christian Urban <urbanc@in.tum.de>
parents: 531
diff changeset
    89
Pal.parse_all("abaaaba")
667
412556272333 updated
Christian Urban <urbanc@in.tum.de>
parents: 629
diff changeset
    90
Pal.parse_all("abacba")
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
    91
Pal.parse("abaaaba")
586
Christian Urban <urbanc@in.tum.de>
parents: 531
diff changeset
    92
531
Christian Urban <urbanc@in.tum.de>
parents: 470
diff changeset
    93
println("Palindrome: " + Pal.parse_all("abaaaba"))
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    94
672
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 667
diff changeset
    95
// parser for well-nested parentheses (transforms '(' -> '{' , ')' -> '}' )
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    96
lazy val P : Parser[String, String] = 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
    97
  "(" ~ P ~ ")" ~ P ==> { case (((_, x), _), y) => "{" + x + "}" + y } || ""
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    99
P.parse_all("(((()()))())")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   100
P.parse_all("(((()()))()))")
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   101
P.parse_all(")(")
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   102
P.parse_all("()")
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   103
672
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 667
diff changeset
   104
// just counts parentheses
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   105
lazy val P2 : Parser[String, Int] = 
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   106
  ("(" ~ P2 ~ ")" ~ P2 ==> { case (((_, x), _), y) => x + y + 2 } || 
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   107
   "" ==> { _ => 0 })
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   108
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   109
P2.parse_all("(((()()))())")
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   110
P2.parse_all("(((()()))()))")
667
412556272333 updated
Christian Urban <urbanc@in.tum.de>
parents: 629
diff changeset
   111
673
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   112
// counts opening and closing parentheses
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   113
lazy val P3 : Parser[String, Int] = 
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   114
  ("(" ~ P3 ==> { case (_, x) => x + 1 } ||
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   115
   ")" ~ P3 ==> { case (_, x) => x - 1 } || 
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   116
   "" ==> { _ => 0 })
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   117
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   118
P3.parse_all("(((()()))())")
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   119
P3.parse_all("(((()()))()))")
715b46eee102 updated
Christian Urban <urbanc@in.tum.de>
parents: 672
diff changeset
   120
P3.parse_all(")(")
667
412556272333 updated
Christian Urban <urbanc@in.tum.de>
parents: 629
diff changeset
   121
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   122
// Arithmetic Expressions (Terms and Factors)
672
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 667
diff changeset
   123
// (because it is mutually recursive, you need :paste 
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 667
diff changeset
   124
//  for munching this definition in the REPL)
586
Christian Urban <urbanc@in.tum.de>
parents: 531
diff changeset
   125
367
04127a5aad23 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   126
lazy val E: Parser[String, Int] = 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   127
  (T ~ "+" ~ E) ==> { case ((x, y), z) => x + z } ||
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   128
  (T ~ "-" ~ E) ==> { case ((x, y), z) => x - z } || T 
470
8df654e9eb4e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 467
diff changeset
   129
lazy val T: Parser[String, Int] = 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   130
  (F ~ "*" ~ T) ==> { case ((x, y), z) => x * z } || F
367
04127a5aad23 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   131
lazy val F: Parser[String, Int] = 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   132
  ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParserInt
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   133
599
33c4b580092b updated
Christian Urban <urbanc@in.tum.de>
parents: 598
diff changeset
   134
println(E.parse_all("1+3+4"))
33c4b580092b updated
Christian Urban <urbanc@in.tum.de>
parents: 598
diff changeset
   135
println(E.parse("1+3+4"))
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   136
println(E.parse_all("4*2+3"))
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   137
println(E.parse_all("4*(2+3)"))
594
d40d7d7b85bc updated
Christian Urban <urbanc@in.tum.de>
parents: 593
diff changeset
   138
println(E.parse_all("(4)*((2+3))"))
586
Christian Urban <urbanc@in.tum.de>
parents: 531
diff changeset
   139
println(E.parse_all("4/2+3"))
367
04127a5aad23 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 366
diff changeset
   140
println(E.parse("1 + 2 * 3"))
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   141
println(E.parse_all("(1+2)+3"))
531
Christian Urban <urbanc@in.tum.de>
parents: 470
diff changeset
   142
println(E.parse_all("1+2+3"))  
Christian Urban <urbanc@in.tum.de>
parents: 470
diff changeset
   143
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   144
/* same parser but producing a string
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   145
lazy val E: Parser[String, String] = 
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   146
  (T ~ "+" ~ E) ==> { case ((x, y), z) => "(" + x + ")+(" + z + ")"} || T 
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   147
lazy val T: Parser[String, String] = 
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   148
  (F ~ "*" ~ T) ==> { case ((x, y), z) => "(" + x + ")*("+ z + ")"} || F
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   149
lazy val F: Parser[String, String] = 
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   150
  ("(" ~ E ~ ")") ==> { case ((x, y), z) => y } || NumParser
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   151
*/
586
Christian Urban <urbanc@in.tum.de>
parents: 531
diff changeset
   152
531
Christian Urban <urbanc@in.tum.de>
parents: 470
diff changeset
   153
// no left-recursion allowed, otherwise will loop
Christian Urban <urbanc@in.tum.de>
parents: 470
diff changeset
   154
lazy val EL: Parser[String, Int] = 
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   155
  (EL ~ "+" ~ EL ==> { case ((x, y), z) => x + z} || 
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   156
   EL ~ "*" ~ EL ==> { case ((x, y), z) => x * z} ||
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   157
   "(" ~ EL ~ ")" ==> { case ((x, y), z) => y} ||
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   158
   NumParserInt)
531
Christian Urban <urbanc@in.tum.de>
parents: 470
diff changeset
   159
593
bb24d4e207b6 updated
Christian Urban <urbanc@in.tum.de>
parents: 590
diff changeset
   160
//println(EL.parse_all("1+2+3"))
531
Christian Urban <urbanc@in.tum.de>
parents: 470
diff changeset
   161
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   162
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   163
// non-ambiguous vs ambiguous grammars
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   164
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   165
// ambiguous
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   166
lazy val S : Parser[String, String] =
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   167
  ("1" ~ S ~ S ~ S) ==> { case (((x, y), z), v) => x + y + z } || ""
172
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   168
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   169
S.parse("1" * 10)
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   170
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   171
// non-ambiguous
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   172
lazy val U : Parser[String, String] =
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   173
  ("1" ~ U) ==> { case (x, y) => x + y  } || ""
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   174
599
33c4b580092b updated
Christian Urban <urbanc@in.tum.de>
parents: 598
diff changeset
   175
U.parse("1" * 25)
531
Christian Urban <urbanc@in.tum.de>
parents: 470
diff changeset
   176
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   177
U.parse("11")
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   178
U.parse("11111")
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   179
U.parse("11011")
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   180
531
Christian Urban <urbanc@in.tum.de>
parents: 470
diff changeset
   181
U.parse_all("1" * 100)
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   182
U.parse_all("1" * 100 + "0")
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   183
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   184
lazy val UCount : Parser[String, Int] =
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   185
  ("1" ~ UCount) ==> { case (x, y) => y + 1 } || "" ==> { x => 0 }
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   186
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   187
UCount.parse("11111")
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   188
UCount.parse_all("11111")
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   189
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   190
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   191
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   192
// Single Character parser
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   193
lazy val One : Parser[String, String] = "1"
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   194
lazy val Two : Parser[String, String] = "2"
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   195
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   196
One.parse("1")
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   197
One.parse("111")
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   198
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   199
(One ~ One).parse("111")
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   200
(One ~ One ~ One).parse("1111")
366
5a83336a9690 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 362
diff changeset
   201
(One ~ One ~ One ~ One).parse("1111")
362
57ea439feaff updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 360
diff changeset
   202
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   203
(One || Two).parse("111")
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   204
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   205
672
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 667
diff changeset
   206
// a problem with the arithmetic expression parser -> gets 
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   207
// slow with deeply nested parentheses
628
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   208
E.parse("1")
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   209
E.parse("(1)")
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   210
E.parse("((1))")
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   211
E.parse("(((1)))")
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   212
E.parse("((((1))))")
8067d0a8ba04 updated
Christian Urban <urbanc@in.tum.de>
parents: 624
diff changeset
   213
E.parse("((((((1))))))")
672
e0d76f7f0688 updated
Christian Urban <urbanc@in.tum.de>
parents: 667
diff changeset
   214
E.parse("(((((((1)))))))")
686
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   215
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   216
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   217
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   218
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   219
/*
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   220
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   221
starting symbols
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   222
tokenise/detokenise
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   223
:paste
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   224
pairs in sequences
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   225
05cfce0fdef7 updated
Christian Urban <urbanc@in.tum.de>
parents: 683
diff changeset
   226
*/