symtab with antlr4 for java

207 views
Skip to first unread message

Daniel Agota

unread,
May 3, 2017, 5:10:49 AM5/3/17
to antlr-discussion

Hi,

I am using Antlr4 for a while and became interested in building an app similar to the Cymbol example in the Definitive Antlr4 Refernce book:
8.4 Validating Program Symbol Usage only for Java8. 

I came across the the symtab project on github and tried to use it with Antlr4 together to build a symbol table in an antlr4 app (since Java8 is a statically scoped language, right?).
https://github.com/antlr/symtab

The project refers to one example:
https://github.com/parrt/cs652/tree/master/lectures/code/symtab/src

I tried to find the equivalent events in the Java8BaseListener generated from the Java8 grammar:
https://github.com/antlr/grammars-v4/blob/master/java8/Java8.g4

I started with rewriting Simple grammar event enterFile to enterCompilationUnit as:

Original example
public void enterFile(@NotNull SimpleParser.FileContext ctx) {
GlobalScope g = new GlobalScope(null);
ctx.scope = g;
pushScope(g);
}

my listener method:
        public void enterCompilationUnit(Java8Parser.CompilationUnitContext ctx) { 
            GlobalScope g = new GlobalScope(null);
            ctx.scope = g;
            pushScope(g);        
        }

But then the compiler complains about not havint a ctx.scope in the Java8Parser.CompilationUnitContext.

I then looked up the grammar of the Simple language:
https://github.com/parrt/cs652/blob/master/lectures/code/symtab/src/Simple.g4

and it contains definition like:
file returns [Scope scope]
  : (func|var)* EOF
  ;


My questions are:
What is the function of the returns statement in the grammar ?
Do I have to modify the Java8 grammar to accomplish a similar behaviour or is there an other option, which leaves the grammar untouched?

Is there any other symbol table implementation which out-of-the box support symbol tables for Java8 grammar?
I came across this blog entry:
https://shijinglu.wordpress.com/2015/01/22/common-design-patterns-in-antlr4-projects/
which mentions that Antlr4 supports symbol tables, but no further reference or example and I am a bit puzzled between the relationship of the symtab projects SymbolTable class and the one in the above example...

I have could follow the cymbol language example in the The Definitive ANTLR 4 Reference and have the language implementation patterns books as a reference, but I would like to avoid to reimplementing a symbol table for Java8 / rewrite the Java 8 grammar when it is possible...

Kind regards, Daniel 
Reply all
Reply to author
Forward
0 new messages