简体   繁体   中英

How do I translate a c program to python?

I'm trying to make a program that takes ac file and converts it into python code and was wondering if something already exists that is similar. Basically, I will input a simple file in c with normal c features and the program will have to output the same program but in python instead.

Thanks in advance, Ankit

The simplest answer to your question is just "no". Coding is a complicated process which requires creativity and nuance, and we're not really at the stage where a program can do that.

You could probably create something which you can input Python into and it outputs C for the equivalent command for basic things like "print", "for i in range(n):" etc but more complicated stuff is going to require a human at least for now.

I tried to do the same thing to turn Python into Java. It didn't go well and I concluded that it's basically impossible or at least much harder than I thought.

You have to make c compiler that generate python code. Try to see if you can learn how c is written to convert it to python. You could also use awk Unix command to make a basic interpreter line by line. The first thing to do may be to have clean formatted c code to enable easy translation :

  • { => add at end of code line and + increment indentation by 4 spaces
  • } => decrement indentation by 4 spaces
  • ; => end of line

It may be huge work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM