memoize non-terminals