Fix segfault with some syntax errors and improve error reporting