Jialong Wu, Yishujie Zhao and Shuwei Huang
A C to Python translator 🤖 based on Lex/Yacc.
Course project for Principles of Assembly and Compilation, instructed by Prof. Wang.
For details, see our report and slides (in Chinese).
Support:
- Variable and literal
- Arithmetic and logical expression
- Conditional and loop statements
- One-dimensional array
- Function
Not support:
- Pointer and reference
continueandbreakdo-whilestatement- Conditions like
if (a)(must beif (a!=0))
Standard library functions, including:
scanfprintfatoistrlen
Their Python equivalents are implemented in utils.py.
Detect and report semantic errors, including:
- Redefinition of variables, arrays and functions
- Undefined identifiers
All semantic errors will be accumulated and reported in the end of compilation.
We maintain a symbol table in order to do scope check and renaming of variables. For details, have a try to compile examples/test.c.
Ubuntu 16.04+ with Lex, Yacc, and Python 3.7
make clean
make
./parser [Input C File]
python out.py
The default input file is in.c and results will be written to out.py.
There are more examples in src/examples:
calc/calc.c: Expression evaluationaplusb.c: A+B Problem & HelloWorldfactorial.c: Solving factorialpalindrome.c: Palindrome Validationspecial.c: Example for semantic errorstest.c: Example for variables renaming
For details, please refer to our report.