4

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 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Okayyy task... now i got that. i know what the ANTLR for.thanks budie... so can you guve me solution, how to doing like that?or how to translate the message of exception to other language?
How to do semantic analysis? Okay, I've added a link and a bit of an overview for you, but it's a bit of a large topic. In general, you add things to the symbol table and then use the symbol table to look things up.
oke task.. thanks very much... do you know how to add to the Grammar from a program of java?for example i build a program with java to write some text to the grammar..s if i include a program on text area the the program will scan the source if the source have a variabel, then the variable will be include automatich in grammar.do you know how to do that?
Uh, you can't dynamically modify the grammar. The grammar is used to generate the parser and the lexer and then those are used to parse the input. Even if you did make changes to the grammar, that couldn't possibly change the currently running parser. You might want to read up a bit on compilers and then ask another question here. Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.