P4041

Code

P4041

Message

Hex/binary/octal bit-string literal as a CASE label requires –allow-bit-string-case-labels flag

This error occurs when a hex, binary, or octal bit-string literal (e.g. 16#D012, 2#1010, 8#17) is used as a CASE label without the --allow-bit-string-case-labels compiler flag.

The IEC 61131-3 standard grammar for a case label (Annex B) is case_list_element ::= subrange | signed_integer | enumerated_value, where signed_integer is a decimal digit sequence. Radix-prefixed bit-string literals are separate productions that the standard does not include as case labels, so accepting them is a vendor extension used by TwinCAT and CODESYS.

A plain decimal label (5:) is standard syntax and is always allowed.

Example

The following code will generate error P4041:

FUNCTION_BLOCK FB_Example
VAR
    x : DINT;
    y : INT;
END_VAR
CASE x OF
    16#D012: y := 1;
END_CASE;
END_FUNCTION_BLOCK

To fix this error, either use a decimal label:

FUNCTION_BLOCK FB_Example
VAR
    x : DINT;
    y : INT;
END_VAR
CASE x OF
    53266: y := 1;
END_CASE;
END_FUNCTION_BLOCK

Or enable the vendor extension:

Pass the --allow-bit-string-case-labels flag to enable it, or select a dialect that includes it. See Enabling Dialects and Features for the dialects and flags reference.

Think IronPLC is wrong about this?

If you believe this diagnostic is incorrect, open an issue on GitHub with a small sample that demonstrates the problem.