I want to translate a C application to .Net and thinking on how to automate the task. I don't need to "parse" and compile C files, rather I want to re-create a project structure, create a .Net classes corresponding the C files etc. Is there a sense to look at YACC/LEX tools and other parser generators? If I be able to create an AST from C code, maybe I can get a C# code from it?
Asked
Active
Viewed 1,613 times
2
-
6Even if there is a tool to do this, I think it would be a bad idea to use it: idiomatic C and idiomatic C# are almost entirely different and you would probably be far better off rewriting the code in C# and ensuring that it is written correctly (or if there is a lot of code, leave it written in C and just wrap it in C#). – James McNellis Apr 22 '11 at 05:36
-
(this is not meant to offend) when did idiomatic hit the scene? every time i see it used it seems to stifle someone. if you write idiomatically my guess is your probably traveled a road that included lots of twists like wondering about a c to c# compiler on the way there (again no offense intended). – Aaron Anodide Apr 22 '11 at 05:43
-
1@Gabriel - Idiomatic code is the one you recognize as written to the specific language. A straight translation is often just bad and unmaintainable. Just like when you translate from English to German, you have to consider the way things are expressed in programming languages as well. – Bo Persson Apr 22 '11 at 06:36
-
3The name for such a tool is "programmer" – David Heffernan Apr 22 '11 at 07:23
-
1@David - "Never send a human to do a machine's job". If something can be automated - it should be automated. Even with POwershell and regex I'm able to create c# source file to start from, and I'm sure that should exists more powerful tools. – Denis Mitropolskiy Apr 23 '11 at 02:55
-
@James - The main target now is to make it work in .Net and to have .Net code to work with. **Then** it can be refactored etc. Unmaintainability and procedural approach don't really scare me. – Denis Mitropolskiy Apr 23 '11 at 03:03
-
Have you thought about trying to compile it in managed C++? If you have an immediate need to graft .NET code into the application, this might be a quicker way to get started. – meklarian Apr 24 '11 at 05:27
-
@meklarian No, unfortunately in "cannot compile with the /clr option". Though anyway I want to have a fully management solution. – Denis Mitropolskiy Apr 26 '11 at 03:35
-
See my SO answer on how to translate between languages – Ira Baxter Apr 24 '11 at 04:56
1 Answers
3
Found code2code to convert from c++ from
On second thought:
In general, you are most likely to benifit from redesigning the implementation and rewriting the code in C#. Many things that used to be many lines in C could be sometimes just one line when you use the power of the .NET libraries. You would get a smaller, more efficient and easier to mange code base.

Ron Harlev
- 16,227
- 24
- 89
- 132
-
but this is only possibly good because c++ is object oriented. In C there are no objects as we know them in C#. – andrewjsaid Apr 22 '11 at 05:39
-
1+1 for redesing comment. IMO C and C# are so different, that any kind of conversion will end bad. – Euphoric Apr 22 '11 at 06:30
-
Seems like the link is not working (( but thanks anyway. Maybe it is what I'm looking for. – Denis Mitropolskiy Apr 23 '11 at 02:32
-
2I think that it can be easer to translate C to .Net first and redesign C# code after. – Denis Mitropolskiy Apr 23 '11 at 03:09