Converting C# Code to Flowchart

May 3, 2008

Introduction:

Expressing the program logic in a diagram give it the advantage of making it more understandable. This comes from the fact that the human brain is thinking in graphical way. Most of developers document their code with a flowchart. It will be better it we create a tool doing this job. We use a recursive decent parser parses a C# code and draw the corresponding flowchart.

C# flow control:

In C# language there are a lot of flow-control statements:

  1. IF-Else.
  2. While.
  3. Do-while.
  4. For.
  5. Foreach.
  6. Switch.

Each of these statements affects the flow of events in C# code. Some of these statements are considered decision statement (If-Else and switch). The others are considered loop statements (while, do-while, for and foreach).

Decision statements put a condition to go through a block of code. If the condition occurred the block of code will be executed. Otherwise, the block of code will be skipped. If-else statement exactly does this job CFG:

If-statement := if
(condition)
{ statements1 } [else { statements2 }]

The if part is necessary put the else part is optional. So the flowchart of the if-else statement takes two forms:



The switch statement guides the program to a path from multiple paths. The CFG of switch statement is:

Switch_statement := switch
(expression) { { case value: statements break; } }

As the switch statement has multi-paths the flowchart will be in the form:


Loop statements repeat a block of statements according to a condition. Only the expression and the expression place changes from a statement to another. The do-while loop which have the following CFG:

Do-while := do
{ statements } while
(condition );

Takes the form:

The other statements have the same form in flowchart. There CFG’s are:

While-statement:= while
(condition )
{statements}

For-statement:= for
(for-Exp)
{ statements }

Foreach := foreach(foreach-exp)
{statements }

The flowchart form of these statements is:

Process

The process starts when a new code entered. This code enters a scanner that output tokens. The tokens enter a recursive decent parser gives a parse tree represents the entered code. The parse tree then enters the drawing module to be displayed as a model.

Scanner

The scanner takes the code and tries to split the code into a set of tokens each token belongs to one of these types:

  1. Flow-control keyword: keywords represent one of the six flow-control statements discussed in C# flow control section.
  2. Block start: like “{” symbol.
  3. Block end: like “}” symbol.
  4. While spaces.
  5. Separators: set of symbols separate two tokens.

According to these types tokens will take there places in parse tree discussed in next section.

Parse Tree

The parse tree structure used in this tool must have two criteria. First it can represent the sequential statements. Second, it must represent the nested statements. So we use a tree structure represented by these classes:

Stake current=new TreeNode(“main block”);

Class TreeNode

{

Block current

Public Relation[] relation;

}

Class Relation

{

String Name;

treeNode from;

treeNode to;

}

TreeNode is a class represents a node in the parse tree. Each node have object from Block class represents a specified block in flowchart may be decision, loop start, loop end, process start state or end state.
In TreeNode
there is array of relations represents the relations from this TreeNode to its child nodes.

Parser

The function of parser is to take the tokens and place them in their places in a parse tree. The parser specifies the token place in parse tree according to token types. If the token is new flow control token it will be added as a new node in the node existing in the top of stack and this new node will be pushed in the current stack. If token is not a flow control token, the token will be added in the “current” block in the current node. Finally if the token is block termination token (like: } ) pop the first node in current stack.

Drawer

After the parser produced the parse tree, the Drawer takes the parse tree and draws a flowchart representing it. The Algorithm used in the Drawer as follow:

An algorithm is needed to determine the place where the drawer should place the shapes it draws. The y-position is the same for nodes have the same parent. While the x-position should be incremented for a child than the previous one.

Conclusion

To Convert a C# code to flowchart you need to parse the code searching for the flow control statements and code blocks then its very simple to draw the flowchart if you have a well structured parse tree.


A Simple Algorithm for High Curvature Point Detection in 2D Graph

May 3, 2008

Introduction:

A new algorithm is proposed to detect corners and high curvature points in a 2D graph. A corner is defined as a location where a triangle with specified opening angle and size can be inscribed in the curve. Detecting a corner in a 2D graph is related to detecting corners in a gray scale image.

Applications:

This algorithm is developed to be used in shape analysis problems. It cans measure important features in a graph like: number of corners, number of lines and the slope of lines. All of these features can be measured from the output of this algorithm. These features can be used in a pattern recognition technique to classify a given graph.

Technique:

We have a set of points belongs to a curve some internal points and two end points. We will follow a path from one end point to the second end point. We will start from one of the end points of the curve (figure 1).

In each step we will add a point to a list Q. PF: is the first point in the set Q. PL: is the last point of the set Q.PM is the middle point of the set Q. Also two lines will be defined L1 and L2. L1: is the line connecting the PF and PM. L2: is the line connecting the PM and PL. The angle between the two lines L1 and L2 called N. (figure 2)


We continue adding points to L and calculate the angle N until the angle is more than the threshold value T. If N is higher than T, then there is a corner in the set Q initially the corner is PM. (figure 3)

To make our choice more accurate, define two angles NL and NR. The angle NL is the angle between the left neighbor of PM, PF and PL. the angle NR is the angle between the right neighbor of PM, PF and PM. If the value of NL greater than the value of NR and the value of N we will continue moving to the left direction. If the value of NR is greater than N and greater than or equal NL then move to right direction. Keep move to the next point in the specified direction until the angle between the new point, PF and PL is less than the previous angle or the current point is PL. So the corner will be the previous point. (figure 4)


Algorithm:

PF= start_point

Q={PF};

FOREAH (point p in Points)

{

PL=p;

Add p to q;

PM= mid point of Q;

N=angle(PF,PM,PL);

If(N>threshold)

{

corner=PM;

NL=angle(PF,Q[PM_index-1],PL);

NR=angle(PF,Q[PM_index+1],PL);

IF(NL>N&&NL>NR)

Step=-1;

ELSE if(NR>N&&NR>=NL)

Step=1;

Else

Return corner;

Index=PM_index+step;

Max=0;

While(index within Q range)

{

A=angle(PF,Q[index],PL);

If(A>Max)

{

Max=A;

Courner=Q[index];

}

Else

Return max

}

Return max

}

}

Results:

The set of shapes used to test the algorithm listed in (figure 5).