thomas-siozos / Minimal-Compiler

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mininal++ Compiler

Στόχος του project είναι η μεταγλώττιση μιας αυτοσχέδιας συναρτησιακής προγραμματιστικής γλώσσας(Minimal++). Το αρχείο εξόδου θα είναι σε Assembly.

Minimal++'s Grammar

<program> ::= program id { }
<block> ::=
<declarations> ::= (declare ;)
<varlist> ::= ε | id (, id)

<subprograms> ::= ()
<subprogram> ::= function id | procedure id
<funcbody> ::= { }
<formalpars> ::= ( )
<formalparlist> ::= (, )
| ε
<formalparitem ::= in id | inout id
<statements> ::= | { (; )* }
<statement> ::= <assignment_stat>
| <if_stat>
|
| <forcase_stat>
| <call_stat>
| <return_stat>
| <input_stat>
| <print_stat>
<assignment_stat> ::= id :=
<if_stat> ::= if () then
<elsepart> ::= ε | else
<whilestat> ::= while ()
<forcase_stat> ::= forcase ( when () : )* default:
<return_stat> ::= return
<call_stat> ::= call id
<print_stat> ::= print ()
<input_stat> ::= input (id)
<actualpars> ::= ()
<actualparlist> ::= (, )* | ε
<actualparitem> ::= in | inouttk id
<condition> ::= (or )
<boolterm> ::= (and )

<boolfactor> ::= not [] | [] | <relational_oper>
<expression> ::= <optional_sign> (<add_oper> )
<term> ::= (<mul_oper> )

<factor> ::= constant | () | id <idtail
<idtail> ::= ε |
<relational_oper> ::= = | <= | >= | < | > | <>
<add_oper> ::= + | -
<mul_oper> ::= * | /
<optional_sign> ::= ε | <add_oper>

Comments:

  • Multiple lines: <</>>, <</>>
  • Single line: <<//>>

Running the project

  1. min_compiler.py
  2. Give a file
  3. Output files:
    1. semi_code.int
    2. semi_code.c
    3. final_code.asm

Output files

semi_code.int

Αποτελείται από αριθμημένες τετράδες.
Πράξεις:

  • +, a, b, t_1 (a+b). Εκχωρούμε την πράξη σε μια προσωρινή μεταβλητή.
  • -, *, /. Το ίδιο.
Εκχώρηση:
:=, x, _, z (z := x)
Jump(goto):
jump, _, _, 100. Θα πάει στην τετράδα 100.
Συγκρίσεις:
  • =, x, y, z (x=z). Αν ισχύει η σύγκριση θα μεταπηδήσει στο z(αριθμός τετράδας).
  • >=, <=, >, <, <>. To ίδιο.
Αρχή main ή συναρτήσεων:
begin_block, name, _, _
Τέλος main ή συναρτήσεων:
end_block, name, _, _
Τερματισμός προγράμματος:
halt, _, _, _
Παράμετροι:
par, x, m, _
Όπου x:
  • CV: Μετάδοση τιμής.
  • REF: Μετάδοση με αναφορά.
Κλήση συνάρτησης:
call, name, _, _
Επιστροφή τιμής συνάρτησης:
ret, x, _, _

semi_code.c

Μεταγγλωτισμένο σε C.

final_code.asm

Μεταγγλωτισμένο σε Assembly.

Testing

Αρχεία εισόδου υπάρχουν μέσα στον φάκελο "examples".

Copyright

MIT © 2020 Thomas Siozos, Georgia Kalitsi

About

License:MIT License


Languages

Language:Python 99.4%Language:Assembly 0.4%Language:C 0.2%