Search code examples
cobol

error: syntax error, unexpected MULTIPLY, expecting Identifier


I'm trying to complete a Kata in Codewars, my first program in COBOL. The assignment is to inform BMI based on weight and height. But when I run my program it shows me the error of the title. I'm using COBOL 3.1 IBM, the compiler in CodeKata.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. BMI.
       DATA DIVISION.
       LINKAGE SECTION.
       01 WEIGHT           PIC 9(8).
       01 HEIGHT           PIC 9(8)V9(2).
       01 HEIGHT_SQ        PIC 9(10)V9(2).
       01 SUBRESULT        PIC 9(10).
       01 RESULT           PIC A(11).
       PROCEDURE DIVISION USING WEIGHT HEIGHT RESULT HEIGHT_SQ SUBRESULT.
      
        MULTIPLY HEIGHT BY HEIGHT GIVING HEIGHT_SQ.
        DIVIDE WEIGHT BY HEIGHT GIVING SUBRESULT.
      
        IF SUBRESULT > 30
          MOVE "Obese" TO RESULT
        ELSE IF SUBRESULT <= 30
          MOVE "Overweight" TO RESULT
        ELSE IF SUBRESULT <= 25
          MOVE "Normal" TO RESULT
        ELSE IF SUBRESULT <= 185
          MOVE "Underweight" TO RESULT
        END-IF.

       END PROGRAM BMI.

If I delete the first two lines, I still get an error: error: syntax error, unexpected IF, expecting Identifier


Solution

  • The compiler gets confused because it only sees:

           PROCEDURE DIVISION USING WEIGHT HEIGHT RESULT HEIGHT_SQ SUBRESULT
    

    The period that ends the USING clause of the PROCEDURE DIVISION is on column 73 and this compiler obviously works with fixed-form reference-format. Just move that period to the next line and the program will get compiled.

    You possible want to check the COBOL draft standard to learn more about the reference-format - or check the compilers Language Reference / Programmer's Guide.

    Additional note: for "portable" COBOL you'd add a GOBACK statement at the end of the PROCEDURE DIVISION (often not needed, but doesn't harm and leads to "works on all environments").