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.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 22 days ago.
Improve this question
Recently I had to convert a module written in pure python to pyx in in order to compile with cython.
The procedure for converting from py to pyx was very straight forward since the all the variables and functions were type hinted. So it was a just a matter of looking up the cython static type for each types in python.
What are the current options available for automating the conversion of a module written in pure Python to Cython, specifically for converting .py files to .pyx files, taking into account the use of Python type hints in the original code? Are there any existing modules or tools that can facilitate this process? if NO, is it theoretically possible to develop a module that can automatically convert Python type hints to Cython static types, and if so, what challenges may arise in the development of such a module?
Cython already uses Python type annotations so the chances are you didn't need to do anything.
There's a few small notes:
the most recent Cython 3 alpha versions have better support for this than the 0.29.x branch (so upgrade if you can),
int is assumed to to be a Python object rather than a C int to avoid overflow for large ints and keep the semantics the same as Python. Use cython.int as the annotation for a C int.
"A bit more clarification:" Cython is generally pretty conservative with type annotations. It assumes the annotations are correct but aren't for its benefit. Therefore it doesn't do things where the annotation might change the answer. int is the main example - Python ints can be infinitely large while C ints have a fixed maximum value. Therefore, Cython doesn't change an int annotation to a C int because the result could change. Obviously if you're happy with the potentially different behaviour you can make this change yourself.
If you want to automate a fully conversion then the builtin ast module is probably the easiest way to do it. I'd still recommend sticking with "pure Python mode" for simplicity, just because the ast module can generate Python code. But essentially what you'd do is create a visitor that looks for annotations (i.e. ast.AnnAssign, ast.Arg) and replaces it will an appropriate Cython-friendly annotation. You then use ast.dump to rewrite the modified 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 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 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.
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.)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 8 years ago.
Improve this question
I'm preparing to make a little IDE that allows to expand function/macro calls to their definitions right in the place they are called (and do that recursively if needed, so that if funciton A calls B, I expand B, then B calls C - I expand C in the code of B etc.):
Question 1: do you know of existing IDEs with this feature?
Question2: How would you implement that? My approach is as follows: I'd like to have the IDE being able to understand various programming languages. In order to extract function definitions, I'll seemingly have to use parsers. Is there any collection of parsers for various languages? I've looked into Ctags and Pygments lexers, but their output is insufficient to accomplish this task.
Preferable language for IDE is Python (although Java and C suggestions are ok, too), graphial library - gtk+2. Thanks.
Question 1: do you know of existing IDEs with this feature?
Not perhaps quite what you describe, but Visual Studio's Code Definition Window shows the definition of any symbol at the current cursor position. It differs of course in that it shows the definition in a separate window to the editor window, but to be honest I would find your proposition somewhat jarring and counter-intuitive since the calls are not semantically in-line.
What you want is called "goto definition" in most IDEs. Granted, they don't expand inline, but you have to put the definition somewhere.
As a general rules, many IDEs have split windows so the definition can be seen at the same time with the original call. It seems to me that you essentially want to automate the setup of splitting the windows, and going the definition point.
Many IDEs contain internally programmable interfaces. You can probably do with with Eclipse; you could certainly do this with Emacs, if M-x goto-definition-in-new-window isn't already built in :-}