使用 ANTLR 解析 Java 代码“需求概念”
我正在尝试使用 ANTLR 进行程序编译,我使用 Java 编程语言作为目标,问题的核心是开发 Intent Regornizer 来纠正错误并改进源代码(如果源代码不符合)语法。 在关于 ANTLR 的教程和书籍上,我看到如何编译一个简单的代码,假设我已经制作了词法分析器和解析器,源代码如下:
int main(){
int a,b;
c=20;
}
how a program can detector error that the variable ' C ' is notknown to have之前声明过?
我尝试按照如何使用 ANTLR 进行编译的说明来应用它,但 ANTLR 生成器的代码被认为是有效的,因为它符合表达式的语法规则。但实际上变量 c 是未知的。
或者如何制作一个可以在其中实现面向对象概念的语法?我尝试过使用 ANTLR 语法,但结果仍然无法解释 OOP 的概念。
public class Hello {
}
public class HelloTwo {
Hello hl = new HelloWrong();
}
如果我编译代码,结果是有效的,因为按照语法。但是看看 HelloWrong 类确实没有。它还与我的第一个问题上的前一个变量的写入有关。
对不起我的英语。 我希望你能帮助我解决问题。 谢谢你
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
'c' 是否已被声明不是语法的一部分。
解析器输出一个 抽象语法树,编译器采用该 AST 并执行 语义对其进行分析。正是在这个阶段,生成了编译器错误,例如“变量 c 在该范围内不存在”。
ANTLR 为您生成 AST,然后就完成了。下一阶段(语义分析和编译以及生成可执行文件)由编译器的另一部分完成。
我用来产生您正在寻找的行为的方法是遍历 AST,在每个节点上进行“语义分析”。 AST 的外观完全取决于用于生成它的语法,但您的第一个程序可能如下所示:
并且语义分析可以执行如下操作:
1) 将“main”添加到符号表中作为全局可访问的函数
2) 将main函数作用域内的作用域1添加到符号表中
3) 将“a”和“b”添加到符号表中作为范围 1 内的局部变量
4) 在符号表中查找作用域 1 内的变量“c”,失败,在“main”的父作用域中查找,失败,在全局作用域中查找,失败,产生错误消息:找不到变量“c”。
据我所知,这是一个相当典型的过程。
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:
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.