Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Since Python itself is written in C, is it theoretically possible to "decompile" any Python program into C, for whatever reason? Not translate, (which would be taking the semantics of the program and writing another program in C that does the same thing) but truly decompile (use a program to find the appropriate C functions for each Python operation and implement them in a syntactically correct manner).
Any programming language can theoretically be translated to any other programming language. This theoretical possibility says nothing about how easy it is, or about whether any existing tools allow you to do it.
It's also ambiguous what counts as "decompiling". For example, I can use boost::python and embed a python program as a string in a C++ program. Now I have a C++ program completely equivalent to that python code. That hardly counts as a proper translation, though.
There are some things no translater will be able to do (well):
if ask_user():
a = 1
else:
a = "hi"
print(a)
Because of the compile-time type ambiguity, any equivalent c program will have to have some elaborate data structures with run-time type information.
Yes. Of course you could translate python to c. Parts of what the interpreter does would end up being in your c program. If you restrict your python to RPython it gets a lot easier. As some things in full python don't translate well. Mostly I don't see much point though.
Check out https://code.google.com/p/py2c/ to convert python to c.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How is it different from scripting languages?
An extension language is just what it sounds like. A language that is used to extend other applications. For example, when you write a macro in Excel using Visual Basic, that's using VB as a extension language. When you write a plugin for your browser with Javascript, that is using JS as an extension language.
Extension Language is a language used to write extensions.
This means you can "easily" connect Python with other languages.
One example would be to have a main program in C, and use external Python scripts inside.
To illustrate, imagine a program in C that computes a labyrinth, and a Python script that gives a strategy to walk through the labyrinth. A user could define his/her own strategies in Python instead of diving in the C code.
The user would execute the C code, giving the Python script filepath as an argument, and the C code would execute the Python code as the strategy to use.
One nice property is that you can change the Python script and never recompile the C code.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Say I want to parse a C function in a Python script and want to get a list with all the types of the arguments and only the arguments for the C function. How would I go about doing this efficiently?
E.g.:
With this function
uint32_t foo(char a, int b, double* c, uint64_t d);
I want output ['char', 'int', 'double*', 'uint64_t']
I think what you want is to parse the "prototype of a C function". When you say parse a "C function" ordinarily one would assume that you are doing what a compiler might do with the source implementation.
In your example, you only show a prototype. In the real world, you will encounter many complications that might make you choose another approach. Your killer is the C preprocessor. In an insane piece of code the real type of 'a' could actually be double* due to a really bad macro. You also might have pointers to structs, typedefs for function pointers, etc.
There is a complication that absolutely defies a solution to your problem. The compilation of the prototype will depend on command line arguments to a C compiler describing where to find header files. Without that metadata, you are hosed. With it, you would really want to run the C preprocessor first - check your compiler documentation for how to do so. Then your python program could read the output.
That line might not even be in it - LOL - if it happened to be surrounded by a #if construct that eliminated it.
These features of the C language are the features that make it really nasty to do things like write programs that operate on C, such as a refactoring engine.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to build a full C Parser using pyparsing.
Actually what I want for my project is to identify certain lines of code in a C Program of interest to me. Eg. Complex Assignment instructions with typecasting, pointer dereference etc.
I thought, since I am investing the effort, I will implement the Full C Grammar in pyparsing, and use just what I need.
I referred to this C Grammar for YACC and wrote it according to pyparsing (to the best of my limited understanding of pyparsing).
http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#translation-unit
What I get however is that pyparsing gets stuck in an infinite loop. I have uploaded the python code here.
https://gist.github.com/gkernel/18cd1d38376d07db989a
I need help in this. Please also tell me an alternative approach to solve my problem if you know any.
EDIT:
To be clear, there could be a bug in the code, but I have already invested effort in checking that I have written the correct grammar. I basically want to ask if pyparsing can be used for something as complicated as this.
One of the things I have done is Forward() declare all the non-terminals in the grammar, and I want to know if this is the right approach. I did this because Python would complain of some names being undefined.
As far as I know, pyparsing creates recursive-descent grammars. Recursive-descent grammars will go into an infinite loop if presented with a left-recursive grammar, and it is most likely that the rather ancient C grammar you unearthed (and any more modern C grammar) will be left-recursive, since such grammars are easier to write and are acceptable input to LALR(1) and GLR parser generators, like bison.
C is not an easy language to parse, and more so if you don't understand the basics of parsing theory. If your goal is to learn parsing theory, I'd suggest that you try a simpler language. If your only goal is to parse C, as indicated in your question, then I'd suggest you use one of the available tools; both gcc and clang come with (unfortunately underdocumented) mechanisms to access the parse tree for a C program, and there are commercial products as well if you have a budget.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have been looking around but haven't found an example of this. I'd like to write out a few long/tedious python scripts using Clojure. Just because I happen to enjoy Clojure a bit more and they are not full on programs.
This site makes me think it is possible:
http://jkkramer.com/sudoku.html
For example if I have script.clj, I'd like to be able to convert it to script.py - not by hand of course.
Is it possible to do this? If so, what tool/library/script should I use? If its not possible not, why not?
[Edit] I edited this because the wording mistakenly gave the impression I was looking for a detailed lesson on writing my own solution. I was just curious if the tools were out there to answer my question and if not then why not.
Yes. Write a compiler that takes Clojure syntax and outputs valid Python syntax.
How to do that is well outside of the ability/scope of a StackOverflow answer.
Also note that if you do this for the general case of compiling any piece of Clojure code to Python you will have implemented quite a bit of Clojure in Python (especially when you implement defmacro and generic methods).
You actually don't have to do a source to source translation in order to write Clojure that will interact with python libraries. Just see clojure-py which allows you to write regular Clojure syntax and run it under the Python interpreter and call Python libraries.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
When I ctrl click a builtin function in my IDE I noticed I usually get sended to an init file which holds the function but it just returns the function again.. It states a vague doc string like 'original footprint unknown'
Where do I find the real functions ?
For example where is print_function
the specific example, print is defined in C, in the bltinmodule.c: specifically.
http://hg.python.org/cpython/file/3.3/Python/bltinmodule.c#l1518
More generally, functions implemented in C have no equivalent to the source file you would read in python; the C code is compiled into binary machine code, and no reference to where that bit of code might have come from is (usually) retained in the result; and even if there was, it's unlikely that you happen to have the source code installed in a place your IDE is likely to find it, unless you built it yourself, with debug symbols, and are running the C executable process in that ide's debugger.
Usually in the same directory where that file is. (Which I can't possibly know.)