Recursive Descent Parser Error Handling
Create new accountRequest new password Navigation recent posts Home » forums » LtU ForumQuestion: Graceful error recovery during parsing I was hoping for a few pointers to good information on parsers that can recover from errors and continue to parse the remainder of their input, skipping portions of the syntax error handling in compiler text that are not valid in a clean way. Thanks! By Nathan Sobo at 2005-10-03 08:10 syntax error handling in compiler design | LtU Forum | previous forum topic | next forum topic | other blogs | 16474 reads Comment viewing options Flat list - collapsed Flat list - expanded Threaded list - collapsed Threaded list - expanded Date - newest first Date - oldest first 10 comments per page 30 comments per page 50 comments per page 70 comments per page 90 comments per page 150 comments per page 200 comments per page Select your preferred way to display the comments and click "Save settings" to activate your changes. JavaCC uses Java try/catch in BNF rules You can have a look at the brief explanation of this feature here. By Arnaud Clere at Mon, 2005-10-03 08:57 | login or register to post comments Here are some tips(from personal experience) If you mean that what you want is to ignore the error input and proceed with the rest, hopefully correct, input, here are some tips from personal experience: continue parsing from the token after then one that caused the error. For example, if token N caused the parser to fail, continue parsing from token N+1. This can be slow, especially if the parsing rules are big. use some tips from the grammar; for example, curly-bracket language compilers use curly brackets and keywords to find out the propable end of error region. If you mean that what you want is to undo the actions done during parsing due to an error found in the later part of the source code, then you have to rethink the way parsing is done: you don't have to do anything during parsing, except noting down what should take place after parsing finishes. For example, when parsing XML, you don't have to create a Node object for each successfully parsed node, because an error many levels deep may dim the node useless. You only have to store in an stack the event of node creation, along with the start and end token. Then, after parsing ends, all parsing events are executed in the order stored in the stack. You don't even have to use a real stack for this: an array, along with an index of the top element is more than enough. When parsing fails, the index is restored from a local variable, thus making 'undo' an O(1) operation. By Achi
no alternative of a production in either the parser or lexer, then a NoViableAltException is thrown. The methods in the lexer base class used to match characters (match et al) throw analogous exceptions. ANTLR will generate default error-handling code, or you may specify your own exception handlers. Either case results (where supported by the 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 exception handlers (default or otherwise) http://lambda-the-ultimate.org/node/1018 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 apply to ANTLR. ANTLR Exception Hierarchy ANTLR-generated http://www.antlr2.org/doc/err.html 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 by Parser.match() when it is looking for a token, but finds a different o
gets pretty complicated. I have a https://forums.bignerdranch.com/t/recursive-descent-parser/8664 very general idea what it is, but I'm trying to understand how the parser we made is a recursive descent parser. ibex10 2016-04-11 06:21:22 UTC #2 The following book is one of the books I ask my students to read.Compilers: Principles, error handling Techniques, and Tools - en.wikipedia.org/wiki/Compilers ... _and_Tools Chapter 2 (in the 1986 edition) deals briefly with recursive-descent parsers, with a complete example at the end. The example is written in C and is easy to understand. When I get some time, syntax error handling I might post a working example here. [Become a competent programmer faster than you can imagine: pretty-function.org] dcarver 2016-04-12 03:38:42 UTC #3 ibex10: The following book is one of the books I ask my students to read.Compilers: Principles, Techniques, and Tools - en.wikipedia.org/wiki/Compilers ... _and_Tools Chapter 2 (in the 1986 edition) deals briefly with recursive-descent parsers, with a complete example at the end. The example is written in C and is easy to understand. When I get some time, I might post a working example here. Thanks for the resource, I'm going to look into that. Any example you can post would be helpful and appreciated! Home Categories FAQ/Guidelines Terms of Service Privacy Policy Powered by Discourse, best viewed with JavaScript enabled