I start working at scala, working on S99 to learn scala. One problem is converting from a string to a tree data structure. I can do it "manually", I also want to see how to do it using the Scala combiner library.
Tree data structure
sealed abstract class Tree[+T] case class Node[+T](value: T, left: Tree[T], right: Tree[T]) extends Tree[T] { override def toString = "T(" + value.toString + " " + left.toString + " " + right.toString + ")" } case object End extends Tree[Nothing] { override def toString = "." } object Node { def apply[T](value: T): Node[T] = Node(value, End, End) }
And the input should be a string, for example: a(b(d,e),c(,f(g,)))
I can parse the string using something like
trait Tree extends JavaTokenParsers{ def leaf: Parser[Any] = ident def child: Parser[Any] = node | leaf | "" def node: Parser[Any] = ident~"("~child~","~child~")" | leaf }
But how can I use the parsing library to build a tree? I know that I can use ^^ to convert, for example, some string to an integer. My confusion arises from the need to โknowโ the left and right subtrees when creating an instance of Node . How can I do this, or is it a sign that I want to do something else?
I better take what the parser returns ( (((((a~()~(((((b~()~d)~,)~e)~)))~,)~(((((c~()~)~,)~(((((f~()~g)~,)~)~)))~)))~)) for the example above), and create a tree on it rather than using parsers like ^^ or ^^^ to directly build a tree