builds list of terminals and nonterminals