Parsing Java code with ANTLR “need concept”

2019-02-25 09:39发布

问题:

I'm trying to make a compilation of programs using ANTLR and I use the Java programming language as the target and the core of the issue is developing the Intent Regornizer to correct errors and improve the source code if the source code is not in accordance with the Grammar. on tutorials and books on ANTLR I see how to compile a simple code with the assumption that the lexer and parser I've made and the source code like this:

int main(){
     int a,b;
     c=20;
}

how a program can detect errors that the variable ' C ' is not known to have declared before?

I've tried to apply it by following the instructions on how to compile using ANTLR but code for ANTLR generator is considered valid because it is according to the grammar rules of expression. but in fact the variable c is not known.

or how to make a grammar that can implement object-oriented concepts in it? I've tried using the ANTLR grammar but the result still doesn't explain the concept of OOP.

public class Hello {
}

public class HelloTwo {
    Hello hl = new HelloWrong();
}

If I compile the code, the result is valid because in accordance with the Grammar.but look that class HelloWrong is really no. It is also associated with the writing of the previous variable on my first probelms.

Sorry with my English. I hope you can help my problems. thanks to you

回答1:

Whether or not 'c' has been declared is not part of the grammar.
The parser outputs an Abstract Syntax Tree, the compiler takes that AST and does semantic analysis on it. It's at that stage that compiler errors are generated, such as "variable c does not exist in that scope".

ANTLR produces an AST for you, and then it's done. The next stage (semantic analysis and compilation and generating an executable) is done by another part of the compiler.


The method I've used to produce the behaviour you're looking for is to traverse the AST doing "semantic analysis" on each node. What the AST looks like will depend entirely on the grammar used to produce it, but your first program could look like this:

PROGRAM
|- FUNCTION_DEC "main"
   |- ARGS <none>
   |- SCOPE 1
      |- LOCAL_DEC "a", "b"
      |- EXPRESSION_STMT
         |- ASSIGNMENT
            |- VARIABLE "c"
            |- LITERAL 20

And the semantic analysis could do something like this:
1) Add "main" to the symbol table as a globally accessible function
2) Add Scope 1 inside the scope of the main function to the symbol table
3) Add "a" and "b" to the symbol table as local variables inside scope 1
4) Look in the symbol table for variable "c" inside scope 1, fail, look in the parent scope of "main", fail, look in the global scope, fail, produce an error message: Variable 'c' not found.

That's a fairly typical process as far as I know.



标签: java oop antlr