Antlr Better Error Messages
Contents |
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
Antlr4 Error Handling
Us Learn more about Stack Overflow the company Business Learn more about hiring antlr error listener example developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the
Antlr Baseerrorlistener
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 minute: Sign up Handling Antlr Syntax Errors or how antlr4 lexer error handling to give a better message on unexpected token up vote 4 down vote favorite 1 We have the following sub-part of an Antlr grammar: signed_int : SIGN? INT ; INT : '0'..'9'+ ; When someone enters a numeric value everything is fine, but if they mistakenly type something like 1O (one and capital o) we get a cryptic error message like: error 1 : Missing token at antlr4 throw exception on error offset 14 near [Index: 0 (Start: 0-Stop: 0) ='
no alternative of a production in either the parser or lexer, then a NoViableAltException is thrown. The methods in the lexer base class used
Antlr Error Strategy
to match characters (match et al) throw analogous exceptions. ANTLR will generate antlrerrorlistener default error-handling code, or you may specify your own exception handlers. Either case results (where supported by the
Antlr Lexer Error Handling
language) in the creation of a try/catch block. Such try{} blocks surround the generated code for the grammar element of interest (rule, alternate, token reference, or rule reference). If no http://stackoverflow.com/questions/10386499/handling-antlr-syntax-errors-or-how-to-give-a-better-message-on-unexpected-token exception handlers (default or otherwise) are specified, then the exception will propagate all the way out of the parser to the calling program. ANTLR's default exception handling is good to get something working, but you will have more control over error-reporting and resynchronization if you write your own exception handlers. Note that the '@' exception specification of PCCTS 1.33 does not http://www.antlr2.org/doc/err.html apply to ANTLR. ANTLR Exception Hierarchy ANTLR-generated parsers throw exceptions to signal recognition errors or other stream problems. All exceptions derive from ANTLRException. The following diagram shows the hierarchy: Exception Description ANTLRException Root of the exception hiearchy. You can directly subclass this if you want to define your own exceptions unless they live more properly under one of the specific exceptions below. CharStreamException Something bad that happens on the character input stream. Most of the time it will be an IO problem, but you could define an exception for input coming from a dialog box or whatever. CharStreamIOException The character input stream had an IO exception (e.g., CharBuffer.fill() can throw this). If nextToken() sees this, it will convert it to a TokenStreamIOException. RecognitionException A generic recognition problem with the input. Use this as your "catch all" exception in your main() or other method that invokes a parser, lexer, or treeparser. All parser rules throw this exception. MismatchedCharException Thrown by CharScanner.match() when it is looking for a character, but finds a different one on the input stream. MismatchedTokenException Thrown
DefaultErrorStrategy java.lang.Object org.antlr.v4.runtime.DefaultErrorStrategy All Implemented Interfaces: ANTLRErrorStrategy Direct Known http://www.antlr3.org/pipermail/antlr-interest/2010-January/037433.html Subclasses: BailErrorStrategy public class DefaultErrorStrategy extends Object implements ANTLRErrorStrategy This is the default error handling implementation of ANTLRErrorStrategy used for error reporting and recovery in ANTLR parsers. Field Summary Fields Modifier and Type Field and Description protected boolean errorRecoveryMode Indicates whether the lexer error handling error strategy is currently "recovering from an error". protected int lastErrorIndex The index into the input stream where the last error occurred. protected IntervalSet lastErrorStates Constructor Summary Constructors Constructor and Description DefaultErrorStrategy() Method Summary Methods Modifier and Type Method and Description protected void beginErrorCondition(Parserrecognizer) This method is called to enter error recovery mode when a recognition exception is reported. protected void consumeUntil(Parserrecognizer, messages in tree parsers Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] ROTFL! Thanks for calling it as you see it. I feel a little less naïve now, knowing that you have "issues" with debugging. Thanks for the nice example too! Kyle Sent from my Verizon Wireless BlackBerry -----Original Message----- From: Terence Parr