0

How to start with creating a program to format C and their derived code into .net style formatted code so that if I input any program. This program can recognize and reformat by properly adding indentation and other things.

1 Answer 1

1

This is an extremely complex task because programming languages have complex grammar. If you want to format not just C#, but C and C++ too, it’s even more complex — possibly impossible because there may be syntax in one language that is not valid (or means something different) in another.

If you want to do it just for C#, you need a C# parser. There are a few free-software C# parsers available:

Once you have the parse tree, you will have to walk the tree and progressively output it properly formatted. Some of the C# parsers may already have this functionality.

Regarding the grammar ambiguity: Consider the following line of code:

Method(a<b,c>(d+1)); 

In C# the correct formatting would be:

Method(a<b, c>(d + 1)); // “a<T1, T2>” is generic; Method has one argument 

In C and C++ I believe the correct formatting would be:

Method(a < b, c > (d + 1)); // Method has two arguments with binary operators 
Sign up to request clarification or add additional context in comments.

3 Comments

I do understand the complexity. I have no problem using any other free resource. I have got this link codeproject.com/KB/macros/makecodenicer.aspx where macro is available. But I dont know anything about it.
@Shantanu: What is your question?
this is tagged 'homework'. OP is tasked with recreating VS's "Format Selected Code feature" for C# afai can tell.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.