用ANTLR“需要概念"解析Java代码 [英] Parsing Java code with ANTLR "need concept"

查看:32
本文介绍了用ANTLR“需要概念"解析Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 ANTLR 编译程序,我使用 Java 编程语言作为目标,问题的核心是开发 Intent Regornizer 以纠正错误并改进源代码,如果源代码不是按照语法.在关于 ANTLR 的教程和书籍中,我看到了如何编译一个简单的代码,假设我已经制作了词法分析器和解析器以及像这样的源代码:

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;
}

程序如何检测变量C"之前不知道已声明的错误?

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

我已尝试按照有关如何使用 ANTLR 进行编译的说明来应用它,但 ANTLR 生成器的代码被认为是有效的,因为它符合表达式的语法规则.但实际上变量 c 是未知的.

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.

或者如何制作一个可以在其中实现面向对象概念的语法?我已经尝试使用ANTLR语法,但结果仍然没有解释OOP的概念.

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();
}

如果我编译代码,结果是有效的,因为按照语法.但是看起来HelloWrong这个类真的没有.它也与我在第一个problems上写入前一个变量有关.

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

推荐答案

是否声明了 'c' 不是语法的一部分.
解析器输出一个抽象语法树,编译器采用该AST并执行语义分析.正是在那个阶段产生了编译器错误,例如变量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 为您生成一个 AST,然后就完成了.下一阶段(语义分析和编译并生成可执行文件)由编译器的另一部分完成.

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.

我用来产生您正在寻找的行为的方法是遍历 AST,对每个节点进行语义分析".AST 的外观完全取决于生成它的语法,但您的第一个程序可能如下所示:

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

语义分析可以做这样的事情:
1) 将main"作为全局可访问函数添加到符号表中
2)将主函数作用域内的作用域1添加到符号表中
3) 将a"和b"作为作用域1内的局部变量添加到符号表中
4) 在作用域 1 内查找变量c"的符号表,失败,查找main"的父作用域,失败,查找全局作用域,失败,产生错误消息:未找到变量 'c'.

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.

这篇关于用ANTLR“需要概念"解析Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆