noeska
10-02-2008, 03:30 PM
System: Windows XP
Compiler: Delphi 2005 and COCO/R
Libraries: Coco/R
Hello,
Currently i am trying to get a simple calculator to workt with coco/r and delphi. Only the the coco/r part does not seem to behave like i expected:
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.
The problem seems to be that even if there is not Factor a line is written.
For my input:
10 + 10 =
The following is generated:
CONST10
DIV
CONST10
DIV
ADD
SUB
WRITE
RET
But i expected it to be:
CONST 10
ADD
CONST 10
WRITE
RET
Thanks for your answers in advance.
Also is there a COCO/R forum or mailinglist somewhere?
Compiler: Delphi 2005 and COCO/R
Libraries: Coco/R
Hello,
Currently i am trying to get a simple calculator to workt with coco/r and delphi. Only the the coco/r part does not seem to behave like i expected:
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.
The problem seems to be that even if there is not Factor a line is written.
For my input:
10 + 10 =
The following is generated:
CONST10
DIV
CONST10
DIV
ADD
SUB
WRITE
RET
But i expected it to be:
CONST 10
ADD
CONST 10
WRITE
RET
Thanks for your answers in advance.
Also is there a COCO/R forum or mailinglist somewhere?