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

查看:184
本文介绍了使用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类真的没有。它还与我在第一个问题上写上一个变量有关。

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并执行< a href =http://www.cs.virginia.edu/kim/courses/cs471/lec/cs471-09-ast.pdf =nofollow>语义分析。正是在那个阶段生成了编译器错误,例如变量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)将main函数范围内的Scope 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天全站免登陆