Skip to content

Repository files navigation

Greek++ Compiler

A compiler for the Greek++ programming language implemented in Python.

The compiler includes:

  • lexical analysis
  • recursive descent parsing
  • semantic analysis
  • symbol table management and visualization
  • intermediate code generation
  • RISC-V assembly generation

Project Structure

  • lexical.py → lexical analyzer
  • syntax.py → recursive descent parser
  • intermediate.py → intermediate code generation
  • final.py → RISC-V generation
  • symbol_table.py → scope and symbol management

Run

There is a dedicated python script file for each section of the compiler. To test each individual part of the compiler use the python script in the appropriate directory. To run the compiler from start to finish use the python script in the "final code tests" file.

python final.py TEST1.gr

Example Outputs

Intermediate Code

0: (begin_block, αύξηση, _, _)
1: (+, α, 1, T@0)
2: (:=, T@0, _, β)
3: (+, α, 1, T@1)
4: (:=, T@1, _, αύξηση)
5: (end_block, αύξηση, _, _)
6: (begin_block, τύπωσε_συν_1, _, _)
7: (+, χ, 1, T@2)
8: (out, T@2, _, _)
9: (end_block, τύπωσε_συν_1, _, _)
10: (begin_block, τεστ2, _, _)
11: (:=, 1, _, α)
12: (*, α, -α, T@3)
13: (-, 2, α, T@4)
14: (*, 2, α, T@5)
15: (-, T@4, T@5, T@6)
16: (/, T@3, T@6, T@7)
17: (+, 2, -T@7, T@8)
18: (:=, T@8, _, β)
19: (par, α, cv, _)
20: (par, β, ref, _)
21: (par, T@9, ret, _)
22: (call, αύξηση, _, _)
23: (:=, T@9, _, γ)
53: (jump, _, _, 50)
54: (halt, _, _, _)
55: (end_block, τεστ2, _, _)

Symbol Table

-------------------------------------------------------------
Symbol Table:

Scope Level 0:
  α: Variable (name=α, data_type=int, offset=12)
  β: Variable (name=β, data_type=int, offset=16)
  γ: Variable (name=γ, data_type=int, offset=20)
  n: Variable (name=n, data_type=int, offset=24)
  x: Variable (name=x, data_type=int, offset=28)

-------------------------------------------------------------
-------------------------------------------------------------
Symbol Table:

Scope Level 0:
  α: Variable (name=α, data_type=int, offset=12)
  β: Variable (name=β, data_type=int, offset=16)
  γ: Variable (name=γ, data_type=int, offset=20)
  n: Variable (name=n, data_type=int, offset=24)
  x: Variable (name=x, data_type=int, offset=28)
  αύξηση: Function (name=αύξηση, start_quad=0, frame_length=28, data_type=int)

Scope Level 1:
  α: FormalParameter (name=α, data_type=int, passing_mode=cv, offset=12)
  β: FormalParameter (name=β, data_type=int, passing_mode=ref, offset=16)
  T@0: TemporaryVariable (name=T@0, data_type=int, offset=20)
  T@1: TemporaryVariable (name=T@1, data_type=int, offset=24)

-------------------------------------------------------------

Final Assembly Code

.data 
str_nl: .asciz "\n" 
.text 
 
Lmain:
	j main 
 
L0: 
	sw ra (sp)
L1: 
	lw t1, -12(sp)
	li t2, 1
	add t1, t2, t1
	sw t1, -20(sp)
L2: 
	lw t0, -20(sp)
	lw t0, -16(sp)
	sw t0, (t0)
L3: 
	lw t1, -12(sp)
	li t2, 1
	add t1, t2, t1
	sw t1, -24(sp)
L4: 
	lw t0, -8(sp)
	lw t1, -24(sp)
	sw t1, (t0)
L5: 
	lw ra (sp)
	jr ra
L6: 
	sw ra (sp)
L7: 
	lw t1, -12(sp)
	li t2, 1
	add t1, t2, t1
	sw t1, -16(sp)
L8: 
	lw a0, -16(sp)
	li a7, 1
	ecall
	la a0, str_nl
	li a7, 4
	ecall
L9: 
	lw ra (sp)
	jr ra
L10: 
main: 
	addi sp, sp, 84
	mv gp, sp
L11: 
	li t0, 1
	sw t0, -68(sp)