Antlr3 Error Handling
Gadgets About Confluence Log in ANTLR 3PagesBlogChild pagesArticlesCustom Syntax Error RecoveryBrowse pagesConfigureSpace tools Attachments (0) Page History Page Information Resolved comments Link to this antlr4 error handling Pageā¦ View in Hierarchy View Source Export to PDF Export to antlr error listener example Word Pages ANTLR 3 Wiki Home Articles Skip to end of banner JIRA links Go to start of banner Custom Syntax Error Recovery Skip to end of metadata Created by Unknown User (jimi@idle.ws), last modified on Oct 05, 2010 Go to start of metadata Custom Syntax Error Recovery An important part of a robust and useful parser is the behavior it exhibits when the next token in its incoming token stream is not one that the grammar specifies should be there. This is generally known as a syntax error. Here we look at how ANTLR recovers from the various mismatch cases and what techniques you can use to override or influence that recovery. The examples shown use the Java language, but the same techniques should apply to all ANTLR targets. ANTLR implements good error recovery mechanisms by default within the runtimes of all target languages but in some cases the way a grammar is structured impairs the ability of the algorithms to recover to exactly where you might expect or wish. Sometimes your grammar rules cause a loop in a parsing rule to be exited earlier than you would expect or want; sometimes when a certain construct is in error you want to skip everything up to the end of that construct instead of resuming at wherever ANTLR sees a token that looks like it is a valid recovery point. There are many reasons you may wish to influence or override the standard recovery behavior. However, before we can examine how to implement your own recovery, we need to know something about how ANTLR recovers from mismatch problems. Recognition Exception Once a mismatch is detected,
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a https://theantlrguy.atlassian.net/wiki/display/ANTLR3/Custom+Syntax+Error+Recovery minute: Sign up Issues of Error handling with ANTLR3 up vote 1 down vote favorite I tried error reporting in following manner. @members{ public String getErrorMessage(RecognitionException e,String[] tokenNames) { List stack=getRuleInvocationStack(e,this.getClass().getName()); String msg=null; if(e instanceof NoViableAltException){
ANTLRErrorStrategy http://www.antlr.org/api/Java/org/antlr/v4/runtime/ANTLRErrorStrategy.html All Known Implementing Classes: BailErrorStrategy, DefaultErrorStrategy public interface ANTLRErrorStrategy The interface for defining strategies to deal with syntax errors encountered during a parse by ANTLR-generated parsers. We distinguish between three different kinds of errors: error handling The parser could not figure out which path to take in the ATN (none of the available alternatives could possibly match) The current input does not match what we were looking for A predicate evaluated antlr3 error handling to false Implementations of this interface report syntax errors by calling Parser.notifyErrorListeners(java.lang.String). TODO: what to do about lexers Method Summary Methods Modifier and Type Method and Description boolean inErrorRecoveryMode(Parserrecognizer) Tests whether or not recognizer is in the process of recovering from an error. void recover(Parserrecognizer, RecognitionExceptione) This method is called to recover from exception e. Token recoverInline(Parserrecognizer) This method is called when an unexpected symbol is encountered during an inline match operation, such as Parser.match(int). void