moved functions for extracting nonterminals and terminals to ContextFreeGrammar