[antlr-interest] Parsing erroneous input

Jim Idle jimi at temporal-wave.com
Fri May 14 08:56:57 PDT 2010


You have to be careful how you implement your grammar rules such that you can recover sensibly from errors. Generally you build a tree or partial tree then analyze that. You may also need to specifically code for some potential missing elements, but again you have to be careful not to introduce ambiguities that break the normal grammar.

For hints on how to code rules that recover well from errors (especially in loops), see:

http://www.antlr.org/wiki/display/ANTLR3/Custom+Syntax+Error+Recovery

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Andreas Heck
> Sent: Friday, May 14, 2010 6:59 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Parsing erroneous input
> 
> Hello everybody,
> 
> I have a parser based on the C target that I want to use to check for
> correct syntax in one program but I also want to use it to parse
> erroneous input to do autocompletion in another program.
> 
> If I just parse some input where a semicolon is missing on one line the
> parser recovers since the following statements are transformed into
> correct AST nodes but the code from the line which caused the error
> gets
> represented by two "Tree Node Error" nodes in the AST.
> 
> Unfortunately you can't count on perfectly valid input if you want to
> provide some form of autocompletion.
> 
> What is the best approach to parse erroneous input? Do I have to create
> a second grammar that also accepts input with common errors like a
> missing semicolon?
> 
> Or is there a better way where I can just use the parser which only
> accepts correct input? Maybe I could somehow get the code line which
> caused the error and use handwritten code for common error cases to
> extract the information I need? Or maybe there is a way to get the raw
> tokens that caused the problem from antlr or make it put the best
> partial derivation it can create into the AST?
> 
> Are there any other approaches?
> 
> 
> Best regards,
> 
> Andreas
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address





More information about the antlr-interest mailing list