Created
March 4, 2019 19:53
-
-
Save nicoabie/d23fa87eb16d02d9ee57b0b41fdc977f to your computer and use it in GitHub Desktop.
Pretty print a syntactic tree using prolog.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pptree(Term) :- | |
functor(Term, Name, Arity), | |
write(Name), | |
write('('), | |
pptree_children(Term, 1, Arity, 1). | |
% prints atomic siblings without last | |
pptree_children(Term, ChildN, Total, Level) :- | |
ChildN < Total, | |
arg(ChildN, Term, Child), | |
atomic(Child), | |
!, | |
write(Child), | |
write(','), | |
NextChildN is ChildN + 1, | |
pptree_children(Term, NextChildN, Total, Level). | |
% prints last atomic sibling | |
pptree_children(Term, ChildN, Total, _Level) :- | |
ChildN =:= Total, | |
arg(ChildN, Term, Child), | |
atomic(Child), | |
!, | |
write(Child), | |
write(')'). | |
% prints non atomic siblings without last | |
pptree_children(Term, ChildN, Total, Level) :- | |
ChildN < Total, | |
arg(ChildN, Term, Child), | |
functor(Child, Name, Arity), | |
nl, | |
Spaces is Level * 2, | |
NextLevel is Level + 1, | |
tab(Spaces), | |
write(Name), | |
write('('), | |
pptree_children(Child, 1, Arity, NextLevel), | |
NextChildN is ChildN + 1, | |
pptree_children(Term, NextChildN, Total, Level). | |
% prints last non atomic sibling | |
pptree_children(Term, ChildN, Total, Level) :- | |
ChildN =:= Total, | |
arg(ChildN, Term, Child), | |
functor(Child, Name, Arity), | |
nl, | |
Spaces is Level * 2, | |
NextLevel is Level + 1, | |
tab(Spaces), | |
write(Name), | |
write('('), | |
pptree_children(Child, 1, Arity, NextLevel), | |
write(')'). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment