[antlr-interest] TYpe-checking and error reporting

Robin Hamilton-Pennell robin at pozytron.com
Wed May 14 14:39:28 PDT 2008


Thanks for the information, guys.

Johannes: I agree with you (in fact, as I said, I already am using
symbol tables, and already can successfully detect collisions). The
actual problem I'm facing (sorry if it wasn't clear) is that I don't
know the best way to report the error.

Pete: Good to see I'm not the only one to run into this! You are
right, it's a bit smelly, and it's also pretty similar to what I was
doing (minus the exit() call). I think the best bet at this point,
unless someone has a better suggestion, will be to throw a special
SemanticAnalysisException that stores a message about the type of
error. I will try that next. It still seems a bit smelly, since one of
the goals of the built-in RecognitionException & friends is that they
don't store English strings, isn't it?

I would think that this kind of basic analysis & error reporting is
pretty common in compilers, so I find it a bit surprising there's so
little information about the best technique!

Thanks again for the help,
Robin

On Wed, May 14, 2008 at 10:53 AM,  <siemsen at ucar.edu> wrote:
> 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
>
>


More information about the antlr-interest mailing list