Wednesday, April 18, 2012

Ocaml parsing string to make tree

I have a problem similar to this:

How to print a tree structure into a string fast in Ocaml?

But in an opposite way, that I already have a string and want to parse it back to be a tree.

For example, I have

type expr = 
Number of int
|Plus of expr*expr
|Minus of expr*expr

and I have a string like 1 2 + 3 4 + - (operator notations post-fixed, a little different from the link above)

Then I want my result to be a expr type Minus(Plus(1,2), Plus(3, 4))

I found another link that might talk about this, but not sure if it's a way of doing my problem:

Parsing grammars using OCaml

Please share some ideas, thank you.

No comments:

Post a Comment