[antlr-interest] TYpe-checking and error reporting

siemsen at ucar.edu siemsen at ucar.edu
Wed May 14 09:53:13 PDT 2008


I agree - we need better documentation about how to report semantic  
errors in ANTLR.  The book covers how to deal with lexer and parser  
errors, but not semantic errors like the one you describe.  Such  
errors should follow the standard style, with a file name, line number  
and character number, so I use these methods to report them:

	public void errorMessage (TokenStream inp, String msg) {
		String introString = "";
		if (!currentInputFileName.equals("")) {
			int lineNbr = inp.LT(1).getLine();
			int charPosition = inp.LT(1).getCharPositionInLine();
			introString = currentInputFileName + "(" + lineNbr + ":" +  
charPosition + "): ";
		}
		System.err.println(introString + msg);
	}
	
	public void dieWithMessage (TokenStream inp, String msg) {
		errorMessage(inp, msg);
	        System.exit(1);
	}

which I call with, like,

	dieWithMessage(input, "class " + classNam + " undefined");

This "works" but smells bad:

     1. It needs a global "currentFileName", which changes when there  
are includstream.
     2. The methods take a TokenStream to get the line number and  
character position.  From reading ANTLR-generated code, I figured out  
that "input" is the token stream.  Perhaps I should pass in the token  
itself (input.LT(1)) instead of the whole stream.
     3. It doesn't use exceptions, which seems wrong.
     4. Related to #3, it calls System.exit(), which makes unit  
testing difficult.

Hopefully, this code will provoke enough disgust in better minds that  
they'll post superior approaches :-)

-- Pete

On May 13, 2008, at 6:38 PM, Robin Hamilton-Pennell wrote:

> Howdy all,
>
> I've been using ANTLR for a little while now and it has been mostly
> smooth sailing. For my target DSL I've successfully built a lexer,
> parser which generates ASTs, and tree grammar to pretty-print the
> ASTs. So, my app works great as nothing more than a pretty printer at
> this point, but it's a step, at least.
>
> I started implementing code to perform some basic static type
> checking. I build symbol tables in the parser, and in the tree grammar
> I am able to access the type information. This I got working as
> expected.
>
> I ran into a snag when attempting to do some error reporting. For the
> purposes of this question, I'll show you a snippet of target code
> (it's in a C-like syntax):
>
> int foo;
> int bar;
> {
>    int foo; // This is valid, as it's in a different scope than the  
> foo above
> }
> int foo; // This is illegal, as the variable "foo" in the same scope
> is being redeclared.
>
> I have a method, boolean isDefined(String identifier), which works as
> expected to catch this error (for example, if I embed a
> System.err.println() action). However, I cannot figure out the best
> approach to report the error!
>
> I've perused the book and website to find a solution, and searched the
> mailing list briefly, to no avail. I can use semantic predicates, but
> the resulting error messages are next-to-useless. Has anyone tackled
> this problem before, and if so, what's the recommended path for
> reporting semantic errors (without sem preds, if possible)? I really
> appreciate any help that can be provided.
>
> Thanks!
> Robin

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080514/8439eddc/attachment.html 


More information about the antlr-interest mailing list