The predicate mark(Tree,Set) marks the generated tree Tree given the trees Set found by the parser. The third argument M of the terms t(Label,Ds,M) representing derivation trees indicates whether the current node is marked (in that case the value is y) or not (using the value n). Subtrees of marked nodes have no instantiated value for this variable.
mark(t(L,Ds,n),Set):- root_same(L,Set),!, get_ds(Set,DsSet), mark_ds(Ds,DsSet). mark(t(L,Ds,y),Set). root_same(L,[]). root_same(L,[t(L,_,_)|T]):- root_same(L,T). mark_ds([],[]). mark_ds([H|T],[Hs|Ts]):- mark(H,Hs), mark_ds(T,Ts). get_ds([t(_,[],_)|_],[]). get_ds(Set,[H|T]):- get_f(Set,Set2,H), get_ds(Set2,T). get_f([],[],[]). get_f([t(_,[H3|B],_)|T], [t(_,B,_)|T2],[H3|T3]):- get_f(T,T2,T3).