Here is some gramma wich represents tree
<tree> -> *
<tree> -> (<tree><tree>)
where *
means leaf
and couple of trees in parentheses describes root that sons are trees in parentheses.
i Have to rewrite that gramma to dcg and add semantic actions which will create abstrac tree of that tree whith leaf/0
and node/2
example node(node(leaf,leaf),node(leaf,leaf))
for any idea or source i will be gratefull.
This works in my SWI-Prolog:
leaf_rule(leaf) --> "*".
tree_rule(X) --> leaf_rule(X), !.
tree_rule(node(X,Y)) --> "(", tree_rule(X), tree_rule(Y), ")".
In order to parse a string you need to call phrase
predicate, for example:
phrase(tree_rule(X), "((**)*)", Rest).
Prolog will unify X with abstract representation of tree from string.