Authors: Domenico de Gioia, Ivan Maddalena
Professor: Floriano Scioscia
This project is a simple implementation of a source-to-source compiler from C to Python for the course Formal Languages and Compiler at Polytechnic University of Bari.
The main goal of this project was to create a software that can translate source code written in the C programming language into equivalent Python code. This transpiler was designed to ease the migration of existing C applications to the Python development environment by providing an efficient way to convert code without having to completely rewrite it. The aim of this work is to make a one-way translation between two high-level programming languages to simulate the behavior of a compiler. The compiler was built in C language with the help of the automatic tools Flex scanner generator and the Bison parser generator.
Our transpiler works only on a subset of the C language. The restriction of source language, agreed for our project, is composed by the following:
- Comments: single-line, multi-line;
- Data types:
int
,float
,char*
; - Single and multiple declarations of variables;
- Single declarations of arrays;
- Arithmetic operators: addition (
+
), subtraction (-
), multiplication (*
), division (/
); - Comparison operators: greater than (
>
), greater than or equal (>=
), less than (<
), less than or equal (<=
), equal (==
), not equal (!=
); - Logical operators: and (
&&
), or (||
), not (!
); - Reference operator (
&
); - Assignment operator (
=
); - Expressions: the use of logical, arithmetic and comparison operators is allowed, and they can include also variables, array elements, and function calls;
- Assignment of an expression value to a variable or an array element;
- Branching:
if
-else
statement; - Loop/iteration:
for
statement; - Function: declarations, calls,
return
statement;; - Input: input from the user with built-in function
scanf
; - Output: standard output in console with built-in function
printf
.
The project consists of several files, listed below:
global.h
: global variables and function declarations used in multiple files;scanner.l
: Flex input file containing specifications and rules;parser.y
: Bison input file containing specifications and rules;ast.h
andast.c
: definitions of node structures of the Abstract Syntax Tree and functions for their creation and modification;symbol_table.h
andsymbol_table.c
: definitions of Symbol Table structures and functions for table and symbol management;semantic.h
andsemantic.c
: function definitions for semantic checks;translate.h
andtranslate.c
: function definitions for code generation;uthash.h
: support library for hash table implementation.
flex
(2.6.4 and up)bison
(3.8.2 and up)gcc
(11.4.0 and up)make
(4.3 and up)
To create the executable:
git clone https://github.com/domenicodegioia/transpiler_C2Python.git
cd transpiler_C2Python
make
For basic usage:
./compiler {input_file.c}
For a complete documentation, see here.
Each subfolder refers to a different kind of instruction to test (e.g. if, functions, classes etc.) and contains some .c
files for testing the functioning of the transpiler.