简体   繁体   English

如何使用antlr生成Java CFG(控制流图)?

[英]How could I generate Java CFG(Control Flow Graph) using antlr?

I am trying to analyse Java code sturcture. 我正在尝试分析Java代码结构。

So, I generated a Java parser and lexer by using ANTLRv3 and java grammar code... 因此,我使用ANTLRv3和Java语法代码生成了Java解析器和词法分析器...

but I don't know how could I generate Context Flow Graph using the generated parser and lexer. 但是我不知道如何使用生成的解析器和词法分析器生成上下文流图。

I tried to learn how to do this through tutorial page, but the tutorial page has already disappeared. 我试图通过教程页面学习如何执行此操作,但是教程页面已经消失了。

Could you give me a way how to do this? 您能给我一种方法吗? or tutorial page? 还是教程页面?

Thank you. 谢谢。

AFAIK, ANTLR provides no specific help in building a control flow graph. AFAIK,ANTLR在构建控制流程图方面没有提供特定帮助。

You can build one yourself by walking the AST, and collecting knowledge of actions (unconditional statements) and conditions (expression nodes control conditionals), and assembling the graph from that information. 您可以通过遍历AST来构建自己,并收集有关动作(无条件语句)和条件(表达式节点控制条件)的知识,并从该信息中组合图。 Note that switch statements are a kind of compound conditional and need to be handled as a set of decisions, or a special N-way conditional. 请注意,switch语句是一种复合条件,需要作为一组决策或特殊的N向条件进行处理。

Exception handling may change what you think of as "unconditional actions"; 异常处理可能会更改您认为的“无条件操作”; every method call has the potential to redirect control flow to the exception handler (or to the exit of the function) and you need to decide if you are going to include these in your control flow graph. 每个方法调用都有可能将控制流重定向到异常处理程序(或函数的出口),并且您需要确定是否要在控制流图中包括这些内容。 If you want to reason about what the program does (especially dataflow) you will need to model these. 如果您想知道程序的功能(尤其是数据流),则需要对它们进行建模。

You will need to create control flow nodes (just a kind of class) that can refer to pieces of the AST ("this is the action") and to two other control flow nodes (to handle if-then-else); 您将需要创建控制流节点(只是一种类),该节点可以引用AST的各个部分(“这就是动作”)和其他两个控制流节点(以处理if-then-else); this of these exits as "true exit" and "false exit" or alternatively "continue to" and "trap to" exits. 这些出口中的这个作为“真实出口”和“错误出口”,或者“继续”和“陷阱”出口。 Subclasses of this will represent pure computation, IF statements, TRY blocks, etc. 它的子类将代表纯计算,IF语句,TRY块等。

The fact that Java is a pure "structured" langauge means you can build the control flow graph "bottom up"; Java是纯粹的“结构化”语言,这一事实意味着您可以“自下而上”构建控制流程图。 you can build bits of the control flow graph as you traverse leaf-upwards, and combine control flow graphs from children as you climb the tree. 您可以在向上移动叶子时构建控制流程图的各个部分,并在爬树时合并来自子级的控制流程图。 What you need to do is pass a so-far-assembled control flow graph (initially empty at the leaves) up the tree with a reference to a list of control flow nodes in that graph that want to pass control to an exception handler. 您需要做的是在树上传递到目前为止组装好的控制流图(最初在叶子处是空的),并引用该图中希望将控制权传递给异常处理程序的控制流节点列表。 Then as you climb the tree, extend the control flow graph. 然后,当您爬树时,扩展控制流程图。

Most of the work happens at a conditional, such as an IF-THEN-ELSE node; 大部分工作发生在有条件的情况下,例如IF-THEN-ELSE节点。 in this case, two control flow subgraphs with two sets of exceptions are passed to that node. 在这种情况下,具有两组异常的两个控制流子图将传递到该节点。 You then create a control flow node to represent the conditional, set its action to point to the conditional expression, set its two children to point to the two subgraphs passed in, and set its exception set to the union of the exception set. 然后,您创建一个控制流节点来表示条件,将其操作设置为指向条件表达式,将其两个子节点设置为指向传入的两个子图,并将其异常集设置为异常集的并集。

Subroutine calls get flow nodes whose action is the method call, with an exit to the following statement/subexpression, and another exit (unfilled) that will eventually point to the catch clause. 子例程调用获取其操作为方法调用的流节点,并带有以下语句/子表达式的出口,以及另一个出口(未填充),该出口最终将指向catch子句。 Add the subroutine call node to the list of exceptions passed up. 将子例程调用节点添加到传递的异常列表中。

Treat THROW statment similarly, although they have have no "next" action. 尽管THROW语句没有“下一步”动作,但它们的处理方式类似。

When you encounter a TRY construct, generate a conditional node with the action pointing to the try body, one exit pointing to the end of try or to a finally subroutine call. 当您遇到TRY构造时,生成一个条件节点,其操作指向try主体,一个出口指向try的末尾或finally子例程调用。 Finally, patch all the control flow nodes in the exception list to point to the catch clause. 最后,修补例外列表中的所有控制流节点以指向catch子句。

You'll need to chain the catch clauses together as a sequence of if statements. 您需要将catch子句链接在一起,作为一系列if语句。

You have to treat "finally" as a subroutine call, invoked from the try clause, and the various catch clauses. 您必须将“最终”视为从try子句以及各种catch子句调用的子例程调用。

These days, I found an Eclipse plug-in generating Java Control Flow Graph: Control Flow Graph Factory . 这些天来,我发现了一个Eclipse插件生成Java Control Flow Graph: Control Flow Graph Factory This plug-in generates CFG perfectly and store it by XML file. 该插件完美生成CFG并通过XML文件存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM