/C-Assembly_Translator

C to Assembly translator

Primary LanguagePython

C-Assembly_Translator

    Final work of the subject Language Processors.

    We are two students in the 3th grade of computer science at the University of Cádiz, Luis de Celis Muñoz and Rubén Montero Domínguez, and we are going to make a traductor to translate code in C to assembly in Python3 using Sly (Lex Yacc).

What does support

  • Just the Integer type
  • Assignment, arithmetic, relational and logical expressions
  • Printf and scanf functions (with strings and parameters)
  • Senteces:
    • If-Else statement
    • While loop
    • Compoud statement
  • Function calls and definition
  • Local and global variables

What does not support

  • Void function
  • if-else statement and while loop without curly braces
  • More than one operation parameter in a function call (but, you can use as many ID and NUM as you want)
  • Pointers neither vectors

How to use

    You have to give as a parameter the name of the file in C that you want to translate, like this:

$ python traductor.py myfile.c

Example

C code:

int fact(int n)
{
    if (n <= 1){
        return 1;
    }
    else{
        return n * fact(n-1);
    }
}

int numb;

int main()
{
    printf("Enter a number: ");
    scanf("%d", &numb);
    printf("Factorial = %d\n", fact(numb));
   
    return 0;
} 

Equivalent Assembly code:

.section .rodata
.LC0:
	.string "Enter a number: "
.LC1:
	.string "%d"
.LC2:
	.string "Factorial = %d\n"

.text
.globl fact
.type fact, @function

fact:
	# PROLOGO 
	pushl %ebp
	movl %esp, %ebp

	movl 8(%ebp), %eax
	pushl %eax
	movl $(1), %eax
	movl %eax, %ebx
	popl %eax
	cmpl %eax, %ebx
	jle lessEqual1
	movl $(0), %eax
	jmp fin_lessEqual1

lessEqual1:
	movl $(1), %eax

fin_lessEqual1:
	cmpl $0, %eax
	je false1
	
	movl $(1), %eax
	jmp return1
	jmp final1

false1:
	movl 8(%ebp), %eax
	pushl %eax
	movl 8(%ebp), %eax
	pushl %eax
	movl $(1), %eax
	movl %eax, %ebx
	popl %eax
	subl %ebx, %eax
	movl %eax, %ebx
	pushl %ebx
	call fact
	addl $(4), %esp
	movl %eax, %ebx
	popl %eax
	imull %ebx, %eax
	jmp return1

final1:

return1:
	# EPILOGO 
	movl %ebp, %esp
	popl %ebp
	ret

.text
.globl main
.type main, @function

main:
	# PROLOGO 
	pushl %ebp
	movl %esp, %ebp

	pushl $s0		# $s0 = Enter a number: 
	call printf
	addl $(4), %esp
	pushl $numb, %eax
	pushl $s1		# $s1 = %d
	call scanf
	addl $(8), %esp
	movl numb, %eax
	movl %eax, %ebx
	pushl %ebx
	call fact
	addl $(4), %esp
	movl %eax, %ebx
	pushl %ebx
	pushl $s2		# $s2 = Factorial = %d\n
	call printf
	addl $(8), %esp
	movl $(0), %eax
	jmp return2

return2:
	# EPILOGO 
	movl %ebp, %esp
	popl %ebp
	ret

Our grammar

entrada    ➞ sentencia entrada

                  ↳ functiondef entrada

                  ↳ ε

sentencia ➞ definicion ";"

                  ↳ asignacion ";"

                  ↳ operacion ";"

entradaInFunc ➞ sentenciaInFunc entradaInFunc

                          ↳ funcionIf entradaInFunc

                          ↳ bucleWhile entradaInFunc

                          ↳ ε

sentenciaInFunc ➞ sentencia

                          ↳ funcionIf

                          ↳ bucleWhile

                          ↳ funcioncall

                          ↳ devolver


definicion ➞ tipo lista

                   ↳ ε

tipo           ➞ INT

lista           ➞ elto resto

elto           ➞ ID

                   ↳ ID "=" operacion

resto         ➞ "," elto resto

                   ↳ ε


asignacion ➞ ID "=" operacion

                    ↳ ID "+=" operacion

                    ↳ ID "-=" operacion

                    ↳ ID "*=" operacion

                    ↳ ID "/=" operacion

                    ↳ ID "%=" operacion


operacion ➞ operacion "||" bopand

                   ↳ bopand

bopand     ➞ bopand "&&" bopeq

                   ↳ bopeq

bopeq       ➞ bopeq "==" bopcomp

                   ↳ bopeq "!=" bopcomp

                   ↳ bopcomp

bopcomp  ➞ bopcomp "<" exprar

                   ↳ bopcomp ">" exprar

                   ↳ bopcomp "<=" exprar

                   ↳ bopcomp ">=" exprar

                   ↳ exprar

exprar       ➞ exprar "+" exprprod

                   ↳ exprar "-" exprprod

                   ↳ exprprod

exprprod  ➞ exprprod "*" uar

                   ↳ exprprod "/" uar

                   ↳ exprprod "%" uar

                   ↳ uar

uar            ➞ "-" brack

                   ↳ "+" brack

                   ↳ "!" brack

                   ↳ brack

brack        ➞ "(" operacion ")"

                   ↳ NUM

                   ↳ ID

                   ↳ funcioncall


functiondef ➞ tipo ID "(" tiposInp ")" "{" entradaInFunc "}"

tiposInp       ➞ tipo ID tiposInpRe

                      ↳ ε

devolver       ➞ RETURN operacion ";"

tiposInpRe  ➞ "," tipo ID tiposInpRe

                      ↳ ε

funcioncall  ➞ ID "(" paramlist ")"

                      ↳ PRINTF "(" STR restoF ")"

                      ↳ SCANF "(" STR "," "&" ID restoScan ")"

paramlist     ➞ elm restoF

                      ↳ ε

elm               ➞ ID

                      ↳ NUM

                      ↳ operacion

restoF          ➞ "," elm restoF

                      ↳ ε

restoScan    ➞ "," "&" ID restoScan

                      ↳ ε


funcionIf    ➞ IF "(" operacion ")" "{" entradaInFunc "} funcionElse"

funcionElse  ➞ ELSE "{" entradaInFunc "}"

                      ↳ ε

bucleWhile ➞ WHILE "(" operacion ")" "{" entradaInFunc "}"