Found the problem. I misplaced the } it should have been placed after a .) then things started to work properly.
Code:
COMPILER Calculator
uses codegen;
var
gen: TCodeGenerator;
CHARACTERS
digit = "0123456789" .
hexdigit = digit + "ABCDEF" .
IGNORE CHR(1) .. CHR(31)
TOKENS
decNumber = digit { digit } .
hexNumber = "$" hexdigit { hexdigit } .
PRODUCTIONS
Calculator = { Expression "=" } (. gen.Emit(oWRITE); .)
(. gen.Emit(oRET); .)
.
Expression = Term { "+" Term (. gen.Emit(oAdd) .)
| "-" Term (. gen.Emit(oSub) .)
}
.
Term = Factor { "*" Factor (. gen.Emit(oMul) .)
| "/" Factor (. gen.Emit(oDiv) .)
}
.
Factor = decNumber (. gen.Emit(oCONST, StrToInt(LexString()) ); .)
| hexNumber (. gen.Emit(oCONST, StrToInt(LexString()) ); .) .
END Calculator.
Next i have to solve is how to get things in proper order as parsing is working a bit to well as i first get the values in order and then all the math operators in reverse order.
For:
10 + 10 / 4 =
i get:
CONST 10
CONST 10
CONST 4
DIV
ADD
WRITE
RET
[EDIT]I just realized i need to introduce () to priotize 10+10 so i can write (10 + 10) / 4 =[/EDIT]
Bookmarks