Menu

#829 plus/minus need a space before numbers to be recognized as arithmetic operator

invalid
not-our-bug
nobody
scanner (4)
8
3 days ago
2022-05-10
No

New member, TriAttack, has discovered a syntax compile error for the compute statement, see: [bd54dff202].

COMPUTE Num4 = (3/2)+3
Where Num4 is declared as PIC 9V9
Both the compiler and the parser in OCIDE say "Syntax Error, unexpected literal" on the line. However, when I input
COMPUTE Num4 = 3+(3/2)
The program works perfectly fine. Following the traditional order of operations, both lines should lead to the same value for Num4, 4.5, due to the parentheses.

The first compute statement will work if there are spaces on both sides of the plus sign.
Version 3.1.2.0

2 Attachments

Related

Discussion: bd54dff202
Discussion: About the Order of Operations of the COMPUTE Keyword

Discussion

  • Mickey White

    Mickey White - 2022-05-10

    C:\GnuCOBOL>cobc -x compute1.cbl
    compute1.cbl:18: error: syntax error, unexpected Literal

    C:\GnuCOBOL>cobc -x compute2.cbl

    C:\GnuCOBOL>compute2
    4.5
    3.5

     
  • Simon Sobisch

    Simon Sobisch - 2022-05-10
    • labels: --> scanner
     
  • Simon Sobisch

    Simon Sobisch - 2022-05-10

    OK, the issue is that this trips the part for signed numeric literals in the scanner, so the parser does not get

     TOK_OPEN_PAREN number TOK_SLASH number TOK_CLOSE_PAREN TOK_PLUS number
    

    but

     TOK_OPEN_PAREN number TOK_SLASH number TOK_CLOSE_PAREN signed-number
    

    and therefore bails out with the unexpected literal at the end.

    The workaround (also for old versions) is to add a space character after the + :

    -COMPUTE Num4 = (3/2)+3
    +COMPUTE Num4 = (3/2)+ 3
    

    Note: this also applies to simpler computations:

    COMPUTE Num4 = 2+3 --> syntax error
    COMPUTE Num4 = 2+ 3 --> fine

     
  • Simon Sobisch

    Simon Sobisch - 2022-05-10

    Rechecked: according to the standard that's fine:

    • an integer may only contain one sign which must be the very first character
    • arithmetic operators "are represented by specific COBOL characters that shall be preceded by a space and followed by a space except that no space is required between a left parenthesis and a unary operator or between a unary operator and a left parenthesis."

    Rechecked with MF: this recognizes the literal as verb; both this specific one and the 2+3 variant :)

        18     compute num-4 = (3/2)+3
    **    COBCH0301S Unrecognized verb  : compute1.cbl(18,34,33)
        18     compute num-4 = 2+3
    **    COBCH0301S Unrecognized verb  : compute1.cbl(18,30,29)
    
     
  • Simon Sobisch

    Simon Sobisch - 2022-05-10
    • summary: About the Order of Operations of the COMPUTE Keyword --> plus/minus need a space before numbers to be recognized as arithmetic operator
    • status: accepted --> pending
    • assigned_to: Simon Sobisch --> nobody
    • Priority: 3 --> 8
     
  • Simon Sobisch

    Simon Sobisch - 3 days ago

    Where do we stand with that?

    • MF errors on that with "recognizing" it as an unknown verb
    • GnuCOBOL says "error: syntax error, unexpected Literal"
    • gcobol says
    <source>:18:33: error: syntax error, unexpected numeric literal
       18 |            compute num-4 = (3/2)+3
          |                                 ^~ 
    cobol1: error: failed compiling <source>
    

    ... and my guess is that IBM will error similar.

    So - just close this as "not a bug"?

     
    • Mickey White

      Mickey White - 3 days ago

      On IBM the first program errors out the same.
      The second program compiles with warnings , but does not display the decimal point in the answer.
      If change 9v9 to 9.9 decimal is displayed.

       
      • Simon Sobisch

        Simon Sobisch - 3 days ago

        OK, then all compiler agree with the standard here (apart from MF reasoning that as a statement) and we can close this; note that cobc -std=ibm will likely display identical as IBM (as that dialect disables "pretty printing).

         
        👍
        1
  • Simon Sobisch

    Simon Sobisch - 3 days ago
    • status: pending --> not-our-bug
    • Group: GC 3.x --> invalid
     

Log in to post a comment.

MongoDB Logo MongoDB