Skip to content

adelaidekangaroo/Pie

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pie - compiler for a simple pseudo language.

    pseudo code: 
            a := x and (0x09 xor z or 0xAF); #mycode
    
    asm code:
            MOV AX, 0x09
            MOV BX, z
            XOR AX, BX
    
            MOV BX, 0xAF
            OR AX, BX
    
            MOVE BX, AX
            MOV AX, x
            AND AX, BX
    
            MOV a, AX
How to start?
Pie.compile(String filePath, boolean showTree)

Needed JDK 12 or upper

The language has several rules:

  1. Contains boolean expressions separated by ';' (point with comma).
  2. Boolean expressions are composed of identifiers, hexadecimal numbers, assignment signs (: =), or, xor, and, not operation signs, and round brackets.
  3. Used comment type - #

Pie has three parts:

  1. Lexical analyzer
  2. Syntactical analyzer
  3. Object Code Generator and Optimizer (Assembler)

Lexical analyzer

  The lexical analyzer is based on a finite - state machine:

  G = ({N, S, C, I, A, Z, X, H, E}, {0...9, a...F,(,), ′ # ′ , ; , =, :}, 𝛿, N, {S})
  N - start state
  S - end state
  C - comment input state
  I - identifier input state
  A - assignment statement input state
  Z - 0 input state
  X - x input state
  H - hexadecimal input state
  E - error state (entered an invalid character)

Test

---- Input ----

a := x and (0x09 xor z or 0xAF);

---- Debugging ----

currentStateN a
currentStateI
currentStateN :
currentStateA =
currentStateN
currentStateN x
currentStateI
currentStateN a
currentStateI n
currentStateI d
currentStateI
currentStateN (
currentStateN 0
currentStateZ x
currentStateX 0
currentStateH 9
currentStateH
currentStateN x
currentStateI o
currentStateI r
currentStateI
currentStateN z
currentStateI
currentStateN o
currentStateI r
currentStateI
currentStateN 0
currentStateZ x
currentStateX A
currentStateH F
currentStateH )
currentStateN ;

---- Token table ----

ID a
ASSIGNMENT :=
ID x
KEYWORD and
BRACE (
HEX 0x09
KEYWORD xor
ID z
KEYWORD or
HEX 0xAF
BRACE )
END_STATEMENT ;

Syntactical analyzer

  1. There are preset language rules:

    S -> a := F; (Rule 1)    
    F -> F or T | F xor T | T (Rules 2,3,4)   
    T -> T and E | E (Rules 5,6)  	
    E -> (F) | not (F) | a (Rules 7,8,9)  
    G({S,F,T,E},{a, := , ; , or, xor, and, not, (, )},P,S)  
    
  2. The set of right and left symbols:

  1. The precedence table:

  1. Minimizing rules:

    E -> a := E; (Rule 1)   
    E -> E or E | E xor E | E (Rules 2,3,4)   
    E -> E and E | E (Rules 5,6)  
    E -> (E) | not (E) | a (Rules 7,8,9)
    
Test

---- Input ----

a := x and (0x09 xor z or 0xAF);

---- Building the output tree ----

---- Debugging ----

Line - [a := a and ( a xor a or a ) ;]
Memory - []
Action - Transfer

Line - [:= a and ( a xor a or a ) ;]
Memory - [a]
Compare... a = := Action - Transfer

Line - [a and ( a xor a or a ) ;]
Memory - [a :=]
Compare... := < a Action - Transfer

Line - [and ( a xor a or a ) ;]
Memory - [a := a]
Compare... a > and Action - Convolution 9

Line - [and ( a xor a or a ) ;]

Memory - [a := E]
Compare... := < and Action - Transfer

Line - [( a xor a or a ) ;]

Memory - [a := E and]
Compare... and < ( Action - Transfer

Line - [a xor a or a ) ;]

Memory - [a := E and (]
Compare... ( < a Action - Transfer

Line - [xor a or a ) ;]

Memory - [a := E and ( a]
Compare... a > xor Action - Convolution 9

Line - [xor a or a ) ;]

Memory - [a := E and ( E]
Compare... ( < xor Action - Transfer

Line - [a or a ) ;]

Memory - [a := E and ( E xor]
Compare... xor < a Action - Transfer

Line - [or a ) ;]

Memory - [a := E and ( E xor a]
Compare... a > or Action - Convolution 9

Line - [or a ) ;]

Memory - [a := E and ( E xor E]
Compare... xor > or Action - Convolution 3

Line - [or a ) ;]

Memory - [a := E and ( E]
Compare... ( < or Action - Transfer

Line - [a ) ;]

Memory - [a := E and ( E or]
Compare... or < a Action - Transfer

Line - [) ;]

Memory - [a := E and ( E or a]
Compare... a > ) Action - Convolution 9

Line - [) ;]

Memory - [a := E and ( E or E]
Compare... or > ) Action - Convolution 2

Line - [) ;]

Memory - [a := E and ( E]
Compare... ( = ) Action - Transfer

Line - [;]

Memory - [a := E and ( E )]
Compare... ) > ; Action - Convolution 7

Line - [;]

Memory - [a := E and E]
Compare... and > ; Action - Convolution 5

Line - [;]

Memory - [a := E]
Compare... := = ; Action - Transfer

Line - []

Memory - [a := E ;]
Action - Convolution 1

Line - []

Memory - [E]

Object Code Generator and Optimizer

Generation based on triads.

Test

Triads:

1: xor (0x09, z)
2: or (^1, 0xAF)
3: and (x, ^2)
4: := (a, ^3)

Code:

MOV AX, 0x09
MOV BX, z
XOR AX, BX
PUSH AX

POP AX
MOV BX, 0xAF
OR AX, BX
PUSH AX

POP BX
MOV AX, x
AND AX, BX
PUSH AX

POP AX
MOV a, AX

Collapsing triads:

Step 1:

1: xor (0x09, z)
2: or (^1, 0xAF)
3: and (x, ^2)
4: := (a, ^3)

Step 2:

1: xor (0x09, z)
2: or (^1, 0xAF)
3: and (x, ^2)
4: := (a, ^3)

Optimized code:

MOV AX, 0x09
MOV BX, z
XOR AX, BX

MOV BX, 0xAF
OR AX, BX

MOVE BX, AX
MOV AX, x
AND AX, BX

MOV a, AX

About

Compiler for a simple pseudo language

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages