如何使用antlr生成Java CFG(控制流图)? [英] How could I generate Java CFG(Control Flow Graph) using antlr?

查看:627
本文介绍了如何使用antlr生成Java CFG(控制流图)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试分析Java代码结构.

因此,我通过使用ANTLRv3和Java语法代码生成了Java解析器和词法分析器...

但是我不知道如何使用生成的解析器和词法分析器生成上下文流图.

我试图通过教程页面来学习如何做,但是教程页面已经消失了.

您能给我一种方法吗?或教程页面?

谢谢.

解决方案

AFAIK,ANTLR在构建控制流程图方面没有提供具体帮助.

您可以通过遍历AST来构建自己,并收集有关动作(无条件语句)和条件(表达式节点控制条件)的知识,并从该信息中组合图形.请注意,switch语句是一种复合条件,需要作为一组决策或特殊的N向条件来处理.

异常处理可能会改变您对无条件操作"的看法;每个方法调用都有可能将控制流重定向到异常处理程序(或函数的出口),并且您需要确定是否要在控制流图中包括这些内容.如果您想知道程序的功能(尤其是数据流),则需要对它们进行建模.

您将需要创建控制流节点(只是一种类),它可以引用AST的各个部分(这就是动作")和其他两个控制流节点(以处理if-then-else) ;这些出口中的出口"是真出口"和假出口",或者是继续"和陷阱到"出口.此类的子类将代表纯计算,IF语句,TRY块等.

Java是纯结构化"语言的事实意味着您可以自下而上"构建控制流程图.您可以在向上移动叶子时构建控制流程图的各个部分,并在爬树时合并来自子级的控制流程图.您需要做的是在树上传递一个迄今已组装好的控制流图(最初在叶子处为空),并引用该图中希望将控制权传递给异常处理程序的控制流节点列表.然后,当您爬树时,扩展控制流程图.

大多数工作发生在有条件的情况下,例如IF-THEN-ELSE节点;在这种情况下,具有两组异常的两个控制流子图将传递到该节点.然后,您创建一个控制流节点来表示条件,将其操作设置为指向条件表达式,将其两个子节点设置为指向传入的两个子图,并将其异常集设置为异常集的并集.

子例程调用获取其操作为方法调用的流节点,并具有以下语句/子表达式的出口,以及另一个出口(未填充),该出口最终将指向catch子句.将子例程调用节点添加到传递的异常列表中.

类似地处理THROW语句,尽管它们没有下一步"动作.

遇到TRY构造时,生成一个条件节点,其操作指向try主体,一个出口指向try的末尾或finally子例程调用.最后,修补例外列表中的所有控制流节点以指向catch子句.

您需要将catch子句链接在一起,作为一系列if语句.

您必须将最终"视为从try子句以及各种catch子句调用的子例程调用.

I am trying to analyse Java code sturcture.

So, I generated a Java parser and lexer by using ANTLRv3 and java grammar code...

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.

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. 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.

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); 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.

The fact that Java is a pure "structured" langauge means you can build the control flow graph "bottom up"; 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; 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. Add the subroutine call node to the list of exceptions passed up.

Treat THROW statment similarly, although they have have no "next" action.

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. Finally, patch all the control flow nodes in the exception list to point to the catch clause.

You'll need to chain the catch clauses together as a sequence of if statements.

You have to treat "finally" as a subroutine call, invoked from the try clause, and the various catch clauses.

这篇关于如何使用antlr生成Java CFG(控制流图)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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