Antlr C Target Error Handling
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 antlr lexer error handling Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs antlr exception handling 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 minute: Sign up What is the Antlr3 C runtime equivalent of throwing an exception for error reporting up vote 2 down vote favorite I'm pretty familiar with Antlr 2.X and the Antlr 3.1.X CSharp and python targets. However I'm now forced to use the Antlr 3 C target for a project. My issue is how do I report errors in my grammar, or tree grammar. Consider a rule that matches a token and we put it into a map. We want to ensure the token is unique. Normally I'd throw an exception if the token was already in the map and catch the exception outside of hte parser to report the error. What is Antlr C runtime equivalent of the following rule? token_match: ID { if(mp.find($ID.Text)) throw std::exception("Non unique token found"); } c error-handling antlr3 share|improve this question asked Jan 20 '11 at 19:37 chollida 5,23043471 add a comment| 1 Answer 1 active oldest votes up vote 1 down vote accepted I'd recommend using your own methods to display semantic errors. But if you need to stay in ANTLR for this: First you have to create your own error handler if you want to create custom exception types. Look at antlr3baserecognizer.c:1000 for the original. static void displayRecognitionErrorNew (pANTLR3_BASE_RECOGNIZER recognizer, pANTLR3_UINT8 * tokenNames) { ... switch (ex->type) { case ANTLR3_UNWANTED_TOKEN_EXCEPTION: ... case NUTF_EXCEPTION: printf("Non unique token found"); break; Then code some function or rule code to process an exception structure. Look at antlr3baserecognizer.c:325 to setup your exception. ex = antlr3ExceptionNew(ANTLR3_RECOGNITION_EXCEPTION, (void *)ANTLR3_RECOGNITION_EX_NAME, NULL, ANTLR3_FALSE); ex->type = NUTF_EXCEPTION ex->line = ins->getLine (ins); ex->charPositionInLine = ins->getCharPositionInLine (ins); ex->index = is->index (is); ex->streamName = ins->fileName; ex->message = "That was totally unexpected"; Next you need to actually throw your exception when you detect the error. I think the only way to do this is to override mismatch() adding in your code and calling it from your rule. static void mismatchNew(pANTLR3_BASE_RECOGNIZER recognizer, ANTLR3_UINT32 ttype, pANTLR3_BITSET_LIST foll
Gadgets About Confluence Log in ANTLR 3PagesBlogChild pagesANTLR v3 FAQFAQ - C TargetBrowse pagesConfigureSpace tools Attachments (0) Page History Page Information Resolved comments Link to this Pageā¦ View in Hierarchy View Source Export to PDF Export to Word Pages ANTLR 3 Wiki Home ANTLR v3 FAQ Skip to end of banner JIRA links Go to start of banner FAQ - C Target http://stackoverflow.com/questions/4751699/what-is-the-antlr3-c-runtime-equivalent-of-throwing-an-exception-for-error-repor Skip to end of metadata Created by Terence Parr [Administrator], last modified by Unknown User (sachutan@yahoo.com) on Apr 18, 2012 Go to start of metadata How can Antlr Parser actions know the file names and line numbers from a C preprocessed file?http://antlr.markmail.org/search/?q=How+can+Antlr+Parser+actions+know+the+file+names+and+line+numbers+from+a+C+preprocessed+file%3F#query:How%20can%20Antlr%20Parser%20actions%20know%20the%20file%20names%20and%20line%20numbers%20from%20a%20C%20preprocessed%20file%3F+page:1+mid:xf3ap74bgjnhwfz4+state:resultsTerryArnoldOne way is to follow this path:Derive https://theantlrguy.atlassian.net/wiki/display/ANTLR3/FAQ+-+C+Target your own token from CommonToken and add file number field to it;Get the lexer to produce those tokens and the parser to accept them;Build a file table in the lexer and refer to it in error messages;Keep track of current file in the lexer in case you need error messages from thelexer;Keep track of current line number in the inferred file by setting it in your PPLINE rule then incrementing it in your NEWLINE rule;Set the line and file number at the end of each rule (or override the nextToken stuff to set this automatically);Start down that path and you will see the best way for your requirements.In the C target there are user fields for storing such additional information so you can do the same thing as deriving a token.ASTWhat is the ASTLabelType to be specified for tree grammar in C target?http://antlr.markmail.org/search/?q=Problem+with+AST+type+in+tree+grammar+in+C+target#query:Problem%20with%20AST%20type%20in%20tree%20grammar%20in%20C%20target+page:1+mid:cl
covered in this manual);How ANTLR works (not covered in this manual);How to use the @sections with the C targetInteroperation with the runtime within rule actions;Implementing custom versions of the standard library methods; If http://www.antlr3.org/api/C/using.html you are as yet unfamiliar with how ANTLR works in general, then it is suggested that you read the various wiki pages concerned with getting started. However there are a few things that you should note: The lexer is independent of the parser. You cannot control the lexer from within the parser;The tree parser is independent of the parser. You cannot control the parser from within the error handling tree parser(s);Each tree parser is independent of other tree parsers. This means that your lexer runs first and consumes all the input stream until you stop it programmatically, or it reaches the end of the input stream. It produces a complete stream of tokens, which the parser then consumes. @sections in a C Targeted Grammar Within a grammar file there are a number of special sections you antlr c target can add that cause the code within them to be placed at strategic points in the generated code such as before or after the include statements in the .c file, within the generated header file or within the constructor for the recognizer. Many of the @sections used within a Java targeted grammar have some equivalent function within a C targeted grammar, but their use may well be subtly different. There are also additional sections that have meaning only within a grammar targeted for the C runtime. Detailed documentation of these sections is given here: Using Sections Within Grammar Files Interacting with the Generated Code Rule actions have a limited number of elements they can access by name, independently of the target language generated. These are elements such as $line, $pos, $text and so on. Where the $xxx returns a basic type such as int, then you can use these in C as you would in the Java target, but where a reference returns a string, you will get a pointer to the C runtime string implementation pANTLR3_STRING. This will give you access to things like token text but also provides some convenience methods such as pANTLR3_STRING->substring() and pANTLR3_STRING->toUTF8(). The genera