[antlr-interest] Re: changing a token type at the parser level

Chris Black cblack0 at yahoo.com
Fri May 14 15:06:45 PDT 2004


--- In antlr-interest at yahoogroups.com, Mark Lentczner <markl at g...> wrote:
[stuff deleted]
> However, there is a much cleaner way to do what you want:
> 
> 	token {
> 	    ABSENT;
> 	    // a token to use as a place holder when optional elements are 
> absent
> 	}
> 
> 	line:
> 	    FIELD^ DELIM!
> 	    optionalField DELIM!
> 	    FIELD
> 	    (DELIM! FIELD)*
> 	    NEWLINE!
> 	    ;
> 
> 	optionalField:
> 	      FIELD
> 	    | empty { ## = #[ABSENT]; }
> 	    ;
> 
> 	empty: ;
> 
> The optional field is explicit in the grammar, and it always returns an 
> AST, either a FIELD or ABSENT.

Thanks! That is much cleaner...

Unfortunately, whenever I have an empty rule like:

empty: ;

in my parser, the generated java does not compile since it comlains
about an unreachable catch block since an empty rule will not throw a
RecognitionException:

	public final void empty() throws RecognitionException,
TokenStreamException {
		
		returnAST = null;
		ASTPair currentAST = new ASTPair();
		AST empty_AST = null;
		
		try {      // for error handling
			empty_AST = (AST)currentAST.root;
		}
// cb comment: this catch block is unreachable
		catch (RecognitionException ex) {
			reportError(ex);
			consume();
			consumeUntil(_tokenSet_5);
		}
		returnAST = empty_AST;
	}


Any ideas?



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list