Add function to detect invalid expressions