Converting C# Code to Flowchart

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.

Advertisements

2 Responses to Converting C# Code to Flowchart

  1. wilsonlarg says:

    I am looking an implementation for this.
    C# like would be nice.
    Thanks

  2. amrox says:

    I’m trying to finalize the code and make it ready to be used to be integrated with a drawing component.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: