问题
I have been asked to write a program to construct a data flow graph of a input program code, given the abstract syntax tree. I search for the definition of data flow graph online and found there are a lot of things that goes on in data flow analysis of a code segment. I want to know what exactly I have to draw to construct a data flow graph for a given code. Any help is very much appreciated!
回答1:
Given an AST, to produce a data flow graph, you must:
build up symbol tables, so that every identifier used is mapped to its explicitly or implicitly defined type, also allowing you distinguish an identifier in one scope from the same identifier in another scope
construct a control flow graph, showing the order in which the program code is executed, and the conditional branches. (Bonus points for constructing a call graph between functions!)
determine how data flows along the control flow graph, usually using some kind of data flow analysis framework, building up references to variable lifetimes, and capturing all this as a graph.
You can draw the final graph using some kind of external graph drawing package.
All of these steps are pretty complicated, and are likely to be a lot more work than you might think. I get the impression you don't have much background here. You can get that background by studying a standard compiler text (Aho/Sethi/Ullman "Compilers") is pretty classic and very good. But you need to do that before you start, or you won't really understand the steps and they link together.
来源:https://stackoverflow.com/questions/15087195/data-flow-graph-construction