Learning context-free grammars from structural data in polynomial time