Results 1 to 5 of 5

Thread: Creating a simple calculator with COCO/R and delphi

  1. #1

    Creating a simple calculator with COCO/R and delphi

    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:

    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.
    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?
    http://3das.noeska.com - create adventure games without programming

  2. #2

    Creating a simple calculator with COCO/R and delphi

    I'm not sure what is wrong.

    But a good thing with Coco/R compared to other compiler generators is that the generated code is actually readable. So examine the generated code and you will probably see how you should change your input file.

    Also take a look at sample calculator grammars to verify that your input is correct.
    ZGameEditor - Develop 64kb games for Windows.
    Thrust for Vectrex - ROM-file and 6809 source code.

  3. #3

    Creating a simple calculator with COCO/R and delphi

    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]
    http://3das.noeska.com - create adventure games without programming

  4. #4

    Creating a simple calculator with COCO/R and delphi

    Now things work nicely:
    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()) ); .)
                      | '(' Expression Factor ')'
                      .
      END Calculator.
    I now also can do (10 + 10) / 4 =

    But i also get an error:
    Code:
    │    1  (10 + 10) /4 =
    │*****          ^ invalid Factor
    │
    │    1 error
    It is complaining about the ) as it seems.

    Though the end result seem to be right:
    CONST 10
    CONST 10
    ADD
    CONST 4
    DIV
    WRITE
    RET
    http://3das.noeska.com - create adventure games without programming

  5. #5

    Creating a simple calculator with COCO/R and delphi

    Ok, actually it was not complaining about the last ), but on the word Factor.
    So instead of using:
    | '(' Expression Factor ')'
    I had to use:
    | '(' Expression ')'
    And no error anymore.
    http://3das.noeska.com - create adventure games without programming

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •