How to transform a flow chart into an implementation? [closed]

回眸只為那壹抹淺笑 提交于 2019-12-01 06:41:05

Quoting OP: Preferably, and quite hopefully, I am looking for an algorithmic way to implement ALGORITHM WHATEVER

Well, the obvious answer is to define a label in the target language for each step of the algorithm, write the code for each step at that label, and insert GOTO statements exactly as ALGORITHM WHATEVER describes. That will give you a working program that most would call "spaghetti code".

OP has a long-winded introduction that suggests he (or maybe Tom) would prefer to write a goto-free version in a way that convincingly matches ALGORITHM WHATEVER.

OK, so the good news is that Bohm and Jocopini long ago showed that you can implement any computation with just the three basic control flow operations of sequence, if-then-else, and while loops, with the nice property of being single-entry, single-exit. So we know there is a "structured program" implementation.

The not so good news is that their constructive proof (procedure for building such a program from a gotoful/flowchart version) introduces a set of boolean variables and additional tests to control the loops. These variables are used to skip the remainder of a loop iteration, to force exit from the loop, and tell the loop-invoker that the loop exited. This extra code, for me, makes the resulting program somewhat worse to read even if you don't object to the storage and execution time needed to manage those variables. (See the wikipedia link for an algorithm that does this for goto-ful COBOL programs).

Better news is that S. Rao Kosaraju demonstrated that you build such programs with NO additional control variables if you can break out of loops of arbitrary nesting depth. Many programming languages offer such a feature; C offers a fragile version with its "break N;" statement that breaks out of N nested loops [using this famously crashed AT&T's telephone system for the East Coast when somebody inserted an extra loop in existing code and didn't notice the break statements]. Ada and other languages allow one to label the loops, and essentially "leave " to leave the loop with specified label name. For me, this is a pretty solution. [I prefer a variant in which one has a labelled begin-end block that can be "leave"d. If your language does not have such a labelled-leave construct, but you are willing to use GOTO statements in a stylistically approved (rather than ad hoc manner), you can simulate leave statements by placing a label after each loop and writing "goto " instead of leave.

So, we know understand it is possible to build a structured program out of an arbitrary flowchart/gotoful program that is clean.

An algorithm to do this works by reducing the original flowchart into structured sub elements, along the lines of Sue Graham's 1975 paper, A fast and usually linear algorithm for global flow analysis.

The essence of this algorithm is to stepwise reduce the original flowchart into the Bohm/Jacopini primitives (sequence/if-then-else/loop) constructs where possible, and reducing "non-reducible" constructs (think of a small knot in the flowchart) that don't look like these in a special way. At each step, a part of the flowchart is reduced to a single summary node for that part. Eventually, this process reduces the original flowchart of arbitrary complexity into a single node. At this point, the reduction process can be run in reverse, with each summary node expanded back to the original.

For OP's purpose, expansion of the summary node is the opportunity to write code for the reduced subchart in a structured way. Repeating until the original flowchart is produced then causes the entire program to be written in a structured way.

[That's all for today. Let's see if I can produce an algorithm. Watch this space].

If we treat each of the "Do something" statements in ALGORITHM WHATEVER as a function returning TRUE or FALSE, you can do something like the following (in pseudocode):

Let N be an int curr_state be an int T[1..N] be an array of ints F[1..N] be an array of ints Func[1..N] be an array of functions returning booleans

1. curr_state := 1 2. while curr_state != N+1 do /* end state */ 3. if (Func[curr_state] == TRUE) then 4. curr_state := T[curr_state] 5. else 6. curr_state := F[curr_state] 7. fi 8. od

One might ask, "But what if the functions take arguments or need to modify some shared state?" In principle, function arguments and shared state can all be stored in global variables (within the scope of your algorithm). In practice, you'd likely do something slightly different.

The embedded software community has been using such tools with varying degrees of success for quite a while. I don't develop that kind of software anymore, but as of 10 yrs ago there was Rational Rose RT (I think IBM bought them out), MATLAB Simulink, MATLAB Real-time Workshop, and others. They would take various types of graphical input (whether it be class diagrams, state diagrams or flow charts) and generate C++ code.

The way you're describing your example it's not entirely certain whether you are describing a finite state machine where the state transition atoms only depend on the observed state (which is how a flow chart would work), or if you're describing more of an expert system where the transition items care about not only the observed state, but also what transitions occurred prior to getting to that state.

If you're describing the former, then all you need to do is note that your goto labels are states in and of themselves. Each decision block in your flow chart becomes a state transition function that acts on some universe of information and sets a new state when it has completed. The new state may be "END" or something else. The universe of information is usually shared (i.e., shared memory) between all of the state transition functions. The software implementation is usually some variation of the command design pattern (GoF).

If you're describing the latter, then check out the Rete algorithm. The Rete algorithm is an optimized generic algorithm for implementing any expert system's rule book.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!