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
Pie.compile(String filePath, boolean showTree)
Needed JDK 12 or upper
The language has several rules:
- Contains boolean expressions separated by ';' (point with comma).
- Boolean expressions are composed of identifiers, hexadecimal numbers, assignment signs (: =), or, xor, and, not operation signs, and round brackets.
- Used comment type - #
Pie
has three parts:
- Lexical analyzer
- Syntactical analyzer
- Object Code Generator and Optimizer (Assembler)
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 | ; |
-
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)
-
The set of right and left symbols:
- The precedence table:
-
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]
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