[antlr-interest] TYpe-checking and error reporting

Johannes Luber jaluber at gmx.de
Wed May 14 03:29:15 PDT 2008


Robin Hamilton-Pennell schrieb:
> 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.

This looks like a case for symbol tables. Effectively you collect all 
information about declarations and references in one place. You could 
check while storing a declaration, if it has been already declared. Or 
you could scan for duplicates after the parse phase, but before the tree 
grammar phase. None of these require sempreds.

Johannes
> 
> Thanks!
> Robin
> 



More information about the antlr-interest mailing list