0

C code to (transform to) python code

I want to c source code[program] convert into python source code [program] by writing python program if it possible or not???

19th Feb 2020, 3:26 PM
Lakshmi Srinivas Rachumallu
Lakshmi Srinivas Rachumallu - avatar
1 Antwort
+ 1
That's called transpiling or a source-to-source translator https://en.wikipedia.org/wiki/Source-to-source_compiler Basically it involves taking the C source code and scanning through it in order to generate tokens. Also called lexing or lexical analysis if you want to be fancy. https://en.wikipedia.org/wiki/Lexical_analysis For example something simple like "int x = 4;" Might translate to the tokens "INT" "IDENTIFIER" "EQ_OP" "CONSTANT" "STRING_LITERAL" The C grammar rules are specified here: https://www.lysator.liu.se/c/ANSI-C-grammar-y.html Next, these tokens are fed to a parser. https://en.wikipedia.org/wiki/Parsing Which take these tokens and transforms them into an abstract syntax tree ( or AST ) using the aforementioned grammar rules. https://en.wikipedia.org/wiki/Abstract_syntax_tree Which is similar to a binary tree but with a variable number of nodes. Something like "74 + 4" could generate a tree like ( primary_expression( additive_expression( CONSTANT( 74 ), CONSTANT( 4 ) ) ) ) Maybe not this exact one, but something like this. ( And that is the basics to - how you make your own programming language ) The resulting syntax tree is then transpiled to another language where my knowledge ends. So not that trivial of a task to do. But at least you have something to google :D https://pypi.org/project/transpyle/ This project apparently implements a C to python transpiler already. In case anyone is interested/curious in the C++ grammar: http://www.externsoft.ch/download/cpp-iso.html
19th Feb 2020, 4:26 PM
Dennis
Dennis - avatar