[antlr-interest] No match
Randall R Schulz
rschulz at sonic.net
Sat May 26 19:21:46 PDT 2007
On Saturday 26 May 2007 18:37, Jason Hocker wrote:
> We have a rule like:
>
> code: (ruleA | ruleB | ruleC | ruleD)*;
>
> So if we try to parse code that is wrong... it won't match any of
> these rules, and the parser continues and end.
>
> How can we better implement error handling?
For one thing, when zero valid productions is explicitly acceptable,
match an explicit end-of-file (actually, match it regardless--otherwise
there can be garbage left over without any kind of diagnostic at all).
Secondly, you can often write productions for common error cases and
issue diagnostics. Then you can go on parsing. You have to watch out
for ambiguities that might be introduced and most of all don't preclude
a proper parse of an acceptable input (of course), but the technique
makes for much more "user friendly" parsers.
For example, in a Lisp-like grammar I have, I add productions that
accept unmatched close parentheses. I issue a diagnostic and then go on
parsing. From the user perspective, it's far preferable than bailing
out as soon as something that is not strictly grammatically acceptable
occurs.
Randall Schulz
More information about the antlr-interest
mailing list