Antlr Override Emit Error Message
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 Us Learn more about Stack Overflow the company Business Learn more about hiring developers or antlr4 error handling posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss antlr error listener example 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
Antlr4 Error Handling Example
a minute: Sign up antlr length of token and error handling up vote 0 down vote favorite I'm using altlr version 3.4. First question, please see grammar: request: 'C' DELIM source DELIM target { System.out.println("Hi"); } ; source: ID ;
Antlr4 Throw Exception On Error
target: ID ; DELIM: '|' ; fragment ALPHA: 'a'..'z' | 'A'..'Z' ; fragment NUM: '0'..'9' ; ID: ALPHA (ALPHA | NUM)* ; "source" and "target" cannot be empty. But my test shows the following: for input "C|n1|n2" : normal case, no problem. for input "C||n2" : syntax error, and "Hi" not printed. Expected. Ok for input "C|n1|" : syntax error, but "Hi" is printed. Not good. I do need to set other things if "request" token is reached. But from above even antlr error handling for syntax error the code still reaches "request" token. Why? Second question: how do I specify a rule for fixed length token, for example, a token of exact 10 digits? Third question is about error handling. I override emitErrorMessage() in parser to set an error flag, but I found another emitErrorMessage() in lexer. I don't want to share the error flag between the parser and lexer objects. Can I override emitErrorMessage() in lexer to do nothing, and totally rely on the parser to report error? Or put another way, if there is an error, will the parser capture it for sure? And if the error flag is set for one error, can the parser actually recovers and matches anther rule, so the previous error is false alarm? Thanks for any help! antlr share|improve this question edited Jun 26 '14 at 21:01 Jus12 6,8091460109 asked Apr 30 '12 at 3:25 Michael Chen 913279 add a comment| 2 Answers 2 active oldest votes up vote 1 down vote ... for input "C|n1|" : syntax error, but "Hi" is printed. Not good. I do need to set other things if "request" token is reached. But from above even for syntax error the code still reaches "request" token. Why? Because the parser tries to recover from this. If you don't want the parser to (try to) recover from mis-matched tokens, simply throw an exception like this: grammar T; // options... @members { @Override public void
ConsoleErrorListener java.lang.Object antlr parser org.antlr.v4.runtime.BaseErrorListener org.antlr.v4.runtime.ConsoleErrorListener All Implemented Interfaces: ANTLRErrorListener public class ConsoleErrorListener extends BaseErrorListener http://stackoverflow.com/questions/10378123/antlr-length-of-token-and-error-handling Author: Sam Harwell Field Summary Fields Modifier and Type Field and Description static ConsoleErrorListener INSTANCE Provides a default instance http://www.antlr.org/api/Java/org/antlr/v4/runtime/ConsoleErrorListener.html of ConsoleErrorListener. Constructor Summary Constructors Constructor and Description ConsoleErrorListener() Method Summary Methods Modifier and Type Method and Description void syntaxError(Recognizer,?>recognizer, ObjectoffendingSymbol, intline, intcharPositionInLine, Stringmsg, RecognitionExceptione) Upon syntax error, notify any interested parties. Methods inherited from classorg.antlr.v4.runtime.BaseErrorListener reportAmbiguity, reportAttemptingFullContext, reportContextSensitivity Methods inherited from cl
please see grammar: request: 'C' DELIM source DELIM target { System.out.println("Hi"); } ; source: ID ; target: ID ; DELIM: '|' ; fragment ALPHA: 'a'..'z' | 'A'..'Z' ; fragment NUM: '0'..'9' ; ID: ALPHA (ALPHA | NUM)* ; "source" and "target" cannot be empty. But http://just-developers.com/q/10392536/antlr-length-of-token-and-error-handling my test shows the following: for input "C|n1|n2" : normal case, no problem. for input "C||n2" : syntax error, and "Hi" not printed. Expected. Ok for input "C|n1|" : syntax error, but "Hi" is printed. Not good. I do need to set other things if "request" token is reached. But from above even for syntax error the code still reaches "request" token. Why? Second question: how do I specify a rule for fixed length token, for example, error handling a token of exact 10 digits? Third question is about error handling. I override emitErrorMessage() in parser to set an error flag, but I found another emitErrorMessage() in lexer. I don't want to share the error flag between the parser and lexer objects. Can I override emitErrorMessage() in lexer to do nothing, and totally rely on the parser to report error? Or put another way, if there is an error, will the parser capture it for sure? antlr4 error handling And if the error flag is set for one error, can the parser actually recovers and matches anther rule, so the previous error is false alarm? Thanks for any help! antlr 2016-08-18 08:34:44 | 2 Answers | 70 Views SOLVED ... for input "C|n1|" : syntax error, but "Hi" is printed. Not good. I do need to set other things if "request" token is reached. But from above even for syntax error the code still reaches "request" token. Why? Because the parser tries to recover from this. If you don't want the parser to (try to) recover from mis-matched tokens, simply throw an exception like this: grammar T; // options... @members { @Override public void emitErrorMessage(String message) { throw new RuntimeException(message); } } request : 'C' DELIM source DELIM target { System.out.println("Hi"); } ; // more rules... Note that @members is short for @parser::members, it will only cause the emitErrorMessage(...) to be overridden in the parser, not the lexer. For lexer-members, you need to do @lexer::members. Second question: how do I specify a rule for fixed length token, for example, a token of exact 10 digits? See: ANTR3 set the number of accepted characters for a token Third question is about error handling. ... See the first part of my answer: simply override emitErrorMessage() and do nothing in it (the default action is to print on the std.err). Ca