# ST language: phase 3 additions

> Exact Structured Text syntax eaxPLC added for its data model - TYPE (STRUCT, UNION, enumeration, subrange, alias type), VAR_GLOBAL [CONSTANT], VAR_EXTERNAL, aliases (AT path, bits), FUNCTION, VAR_IN_OUT, the access, write_range and was attributes, the PLC's own {gvl} and {project} lines, reserved keywords, constant expressions in bounds - and the compile errors and warnings with their exact messages.

Product: eaxPLC · Type: Reference · Editions: Free, Pro
Source: https://eaxware.com/docs/eaxplc/reference/st-language-phase-3/
Verified against commit 9828d73.

The Structured Text additions of the eaxPLC data model, with the rules the compiler applies. Messages are quoted as the compiler writes them; `X`, `T` and similar stand for your names. Parser messages start with `line N:`.

For what these features are for, see [Data model: global variables, data types and external access](/docs/eaxplc/explanation/data-model/).

## Reserved keywords

These words are keywords, in any letter case, and can no longer name a variable, type, POU or enumeration value:

`TYPE` `END_TYPE` `STRUCT` `END_STRUCT` `UNION` `END_UNION` `FUNCTION` `END_FUNCTION` `VAR_GLOBAL` `VAR_EXTERNAL`

A text that uses one as a name fails with a message naming it, for example *line 4: expected variable name, got "Type" (TYPE is a reserved keyword: rename the variable)*. At start and at every link, the PLC scans the project for such names, lists them as **Keyword clash** in the tag database and the project state (`keyword_clashes`), and audits `PROJECT_KEYWORD_CLASH`. The running program is not affected; its next test or deploy fails until you rename. Add `{attribute 'was' := 'OldName'}` on the new name to keep the value.

## Data types (`TYPE`)

A DUT POU holds exactly one `TYPE`, named like the POU, and nothing else.

```text
TYPE ST_Recipe :
STRUCT
    Temp_SP : REAL := 180.0;
    {attribute 'access' := 'read'}
    Time_s  : DINT := 600;
END_STRUCT
END_TYPE

TYPE U_Word :
UNION
    AsWord  : WORD;
    AsBytes : ARRAY[0..1] OF BYTE;
END_UNION
END_TYPE

TYPE E_State : (Idle, Run := 10, Fault) DINT;
END_TYPE

TYPE T_Speed : INT(0..MaxSpeed);
END_TYPE

TYPE T_Level : REAL;
END_TYPE
```

| Form | Rules |
|---|---|
| `STRUCT ... END_STRUCT` | At least one member. Members may have initial values and `access` and `write_range` attributes (they apply to the member in every instance; access can only narrow), not addresses. A member cannot be a function block instance (declare it in a FUNCTION_BLOCK). Members are naturally aligned, at most to 8 bytes. |
| `UNION ... END_UNION` | Members are elementary types (not STRING) or arrays of them, with no initial value. All start at offset 0. |
| Enumeration `(A, B := n, C) [base]` | Base type an integer type, default `INT`. A value without `:=` is the previous one plus 1 (the first is 0). Values and names are unique and fit the base type. |
| Subrange `base(lo..hi)` | Base an elementary integer type; bounds are constant expressions. |
| Alias type `T : type;` | Any type except a function block. |

A type may refer to a type declared later. A default value on the type itself (`TYPE T : INT := 5;`) is not supported: give the variables initial values. Subranges can also be written inline on a variable: `Speed : INT(0..3000);`.

### Enumeration values and conversions

- A value is `E_State.Run`, or `Run` when only one enumeration declares it and no variable is named so.
- Enumerations compare (`=`, `<>`, `<`, ...) only with values of their own type, and can be `CASE` selectors.
- Convert with `_TO_<int>` and `<int>_TO_`: `E_STATE_TO_INT(State)`, `INT_TO_E_STATE(n)`.

## Global variable lists

A GVL POU holds one or more `VAR_GLOBAL` blocks and nothing else.

```text
VAR_GLOBAL
    LineSpeed : REAL := 12.5;
    Lamp AT %QX0.1 : BOOL;
END_VAR
VAR_GLOBAL CONSTANT
    NumRecipes : INT := 20;
END_VAR
VAR_GLOBAL RETAIN
    Counter : DINT;
END_VAR
```

- `CONSTANT` and `RETAIN` qualify a block. Every CONSTANT needs a value. RETAIN is not persistent yet: a cold reset and a power cycle reset it.
- A global may be located (`AT %...`). An input-located variable (`AT %I...`) cannot have an initial value.
- Global names are unique across all lists, and differ from every POU name and every list name.
- `VAR_GLOBAL` is refused in a PROGRAM, FUNCTION_BLOCK or FUNCTION.

### `VAR_EXTERNAL`

Optional in PROGRAMs, FUNCTION_BLOCKs and FUNCTIONs: a POU can use a global by its name without it.

```text
VAR_EXTERNAL
    LineSpeed : REAL;
END_VAR
VAR_EXTERNAL CONSTANT
    NumRecipes : INT;
END_VAR
```

The type must equal the global's; no address or initial value. `VAR_EXTERNAL CONSTANT` makes the global read-only inside that POU.

### Name lookup

A POU looks a name up in its own declarations, then among the globals. A PROGRAM variable with a global's name is an error. In a FUNCTION_BLOCK or FUNCTION, a local may hide a global, with a warning.

## Aliases (`AT` a variable)

```text
PumpRun   AT Motors[3].Run   : BOOL;
Ready     AT Status.3        : BOOL;   (* bit 3 of Status *)
Oven2_SP  AT Recipes[2].Temp_SP : REAL;
```

- Declared in `VAR_GLOBAL` or in a PROGRAM's `VAR` only. Not in a FUNCTION_BLOCK, a FUNCTION, inputs, outputs, temporaries, or CONSTANT or RETAIN blocks.
- The target is a global or, in a PROGRAM, a variable of that program; a global alias targets a global. Targets may go through members (`.Member`), array elements with **constant** indexes (`[3]`, `[N+1]`) and one bit (`.n`) of an integer or bit-string variable (not TIME, BOOL or an enumeration).
- The alias's type is exactly the target's type, `BOOL` for a bit.
- No initial value and no storage of its own. An alias of an alias resolves, at most 4 aliases deep.
- Its external access is the narrower of its own and its target's, and applies to its name. Its write range is the narrower of its own and its target's (the leaf member's own range when it targets a member).
- A bit alias of an integer that has a write range is read-only from outside.

## FUNCTION

A FUN POU holds exactly one FUNCTION, named like the POU.

```text
FUNCTION Scale : REAL
VAR_INPUT
    IN, LO, HI : REAL;
END_VAR
VAR
    Span : REAL;
END_VAR
Span := HI - LO;
Scale := LO + IN * Span;
END_FUNCTION
```

- Sections: `VAR_INPUT`, `VAR_IN_OUT`, `VAR`, `VAR_TEMP`, `VAR_EXTERNAL`. No `VAR_OUTPUT`, no function block instances, no addresses, no RETAIN, no aliases.
- The result is set by assigning to the function's name; no variable may take that name.
- Called in expressions with formal (`Scale(IN := x, LO := 0.0, HI := 100.0)`) or positional (`Scale(x, 0.0, 100.0)`) arguments, not both in one call. Every in/out parameter must be passed.
- Locals start from their initial values at every call. A FUNCTION cannot be called in an initial value.
- No recursion, direct or through other functions.
- The name may not be a standard function's or a PROGRAM's.

## `VAR_IN_OUT`

In FUNCTION_BLOCKs and FUNCTIONs (not PROGRAMs):

```text
FUNCTION_BLOCK FB_Dose
VAR_IN_OUT
    Recipe : ST_Recipe;
END_VAR
...
END_FUNCTION_BLOCK

Dose1(Recipe := Recipes[Step]);
```

- Copy-in, copy-out: the actual is copied into the parameter before the body and back after it.
- The actual is a variable of exactly the parameter's type: no expression, no CONSTANT, no bit.
- A parameter is at most 4096 bytes.
- No CONSTANT and no initial value on the parameter.
- A FUNCTION_BLOCK's in/out is passed in the call (`Recipe := variable`), never read out with `=>`.

## Attributes and pragmas

A pragma is text in braces. An attribute pragma on the line(s) before a declaration attaches to it (with several names in one declaration, to each):

| Attribute | Value | Effect |
|---|---|---|
| `{attribute 'access' := 'none'}` | `none`, `read`, `read_write` | External access. Default without it: `read_write` (a CONSTANT is always `read` at most). |
| `{attribute 'write_range' := '0.0..120.0'}` | `'low..high'`, numbers, low ≤ high | Bounds external writes; narrowed further by a subrange type. |
| `{attribute 'was' := 'Line2.Speed'}` | an earlier storage name | Migrates the value from that variable at the next switch, while it exists in the running program. |
| `{attribute 'network' := ...}` | reserved | Accepted and kept as written, with the warning *reserved for networked tags (a later version): not applied*. |

Attributes are part of the text, so of the program hash: what the PLC enforces is signed. A value written `'it''s'` or `'it$'s'` holds a quote.

An attribute applies only where the PLC enforces it. Anywhere else it is a compile error, never silently dropped:

| Declared on | Attributes that apply |
|---|---|
| A global variable, a PROGRAM variable | `access`, `write_range`, `was`, `network` |
| A STRUCT or UNION member, a FUNCTION_BLOCK member | `access`, `write_range` |
| An alias | `access`, `write_range` |
| A FUNCTION's variables (a hidden frame), a `VAR_EXTERNAL`, a `TYPE` itself | none |

An attribute must stand directly before a declaration: one before `VAR_GLOBAL`, or before no declaration at all, is an error.

Two pragmas belong to the PLC alone:

- `{gvl Name}`: written by the linker before each list's `VAR_GLOBAL` blocks, so the compiler knows which list declares each global. A POU that holds one is refused: *line N: the {gvl ...} line is written by the PLC, never in a POU*.
- `{project key := value; ...}`: the project compile settings, written by the linker as the first line of the linked text when a setting differs from its default (`subrange_violation := 'clamp' | 'fault'`, `migrate_cap_bytes := 1..16777216`). A POU, an import or a restored tree that holds one is refused. See [Set the project compile settings](/docs/eaxplc/how-to/set-the-project-compile-settings/).

Any other pragma is a warning and is ignored, never an error.

## Constant expressions

Array bounds, STRING lengths, subrange bounds and enumeration values accept constant integer expressions: integer literals, CONSTANT names (the POU's own first, then global ones), unary minus, `+ - * /` and `MOD`.

```text
VAR_GLOBAL CONSTANT
    N : INT := 20;
END_VAR
...
Buffer : ARRAY[1..N*2+1] OF INT;
Name   : STRING[N];
```

A STRING length is 1 to 65535.

## Compile errors

### Pragmas and keywords

| Message | Cause |
|---|---|
| `line N: attribute 'access' := 'X': the access is none, read or read_write` | bad access value |
| `line N: attribute 'write_range' := 'X': expected 'low..high' (numbers, low <= high)` | bad range |
| `line N: attribute 'was' needs the earlier name` | empty `was` |
| `line N: attribute 'X' given twice` | the same attribute twice on one declaration |
| `X: attribute 'Y' is not applied on Z (only 'access' and 'write_range' apply there)` | an attribute such as `was` or `network` on a member or an alias; Z is `TYPE T (a member)`, `FUNCTION_BLOCK F (a member)` or `an alias (it has no storage of its own)` |
| `X: attribute 'Y' is not applied on FUNCTION F (its variables are a hidden frame) (no attribute applies there)` | an attribute on a FUNCTION's variable |
| `X: attribute 'Y' is not applied on a VAR_EXTERNAL (put it on the global) (no attribute applies there)` | an attribute on a `VAR_EXTERNAL` |
| `TYPE T: attribute 'Y' is not applied on a TYPE (put it on a member or a variable) (no attribute applies there)` | an attribute on the type itself |
| `line N: attribute 'X' before VAR_GLOBAL is not applied: put it on the declaration` | an attribute before the block keyword |
| `line N: attribute 'X' is not before a declaration: it is not applied` | an attribute followed by no declaration |
| `line N: expected variable name, got "X" (X is a reserved keyword: rename the variable)` | a keyword used as a name (also *expected a type name ...*, *expected an enumeration value ...*) |
| `line N: {project} must be the first line of the text ...`, `line N: a second {project} pragma ...` | a hand-written `{project}` line |

### Types

| Message |
|---|
| `TYPE T: the name is an elementary type` / `the name is a library block` |
| `TYPE T declared twice` |
| `TYPE T: by-value cycle T -> U -> T (a type cannot hold itself)` |
| `TYPE T: M: a member cannot have an address` |
| `TYPE T: M: a function block instance cannot be a member of a STRUCT (declare it in a FUNCTION_BLOCK)` |
| `TYPE T: M: a UNION member is an elementary type or an ARRAY of one, not X` |
| `TYPE T: M: a UNION member cannot have an initial value` |
| `TYPE T: a STRUCT needs at least one member` (or `a UNION`) |
| `TYPE T: M declared twice` |
| `TYPE T: the base type of an enumeration is an integer type, not X` |
| `TYPE T: A and B have the same value n` |
| `TYPE T: V = n is outside SINT` |
| `TYPE T: a subrange needs an elementary integer base type, not X` |
| `TYPE T: an alias of a function block is not supported (declare an instance of X)` |
| `line N: TYPE T: a default value on a type is not supported (give the variables initial values)` |
| `line N: enumeration T has no values` |
| `cannot compare E_A and E_B: different enumerations` |
| `cannot assign INT to E_STATE (an enumeration holds only its own values: convert with INT_TO_E_STATE)` |
| `X is a value of several enumerations: write E_A.X or E_B.X` |
| `E_STATE has no value X` |
| `CASE on E_STATE: X is a value of the enumeration E_OTHER` |
| `+ is not defined for an enumeration (only comparisons are)` |
| `FOR variable I is a subrange (T): use a plain integer` |
| `T: the fault mode supports subrange bounds within DINT` |

### Constant expressions

| Message |
|---|
| `X: N: X is not a CONSTANT with a value` |
| `X: ...: not a constant integer expression` |
| `X: ...: 2.5 is not an integer` |
| `X: ...: division by zero in a constant` |
| `X: ...: the constant depends on itself` |
| `X: STRING length n out of range 1..65535` |
| `X: subrange bound ...: ...` |

### Globals

| Message |
|---|
| `global variable X declared twice (in GVL_A and GVL_B)` |
| `global variable list X: the name is also a global variable` |
| `global variable X (GVL_Line): the name is a PROGRAM of the project` (or a FUNCTION_BLOCK, a TYPE, a FUNCTION) |
| `X is a global variable (GVL_Line): rename the local or use the global` |
| `CONSTANT X needs a value` |
| `VAR_EXTERNAL X: there is no global variable X (declare it in a global variable list)` |
| `VAR_EXTERNAL X: no address or initial value here (they belong to the global)` |
| `VAR_EXTERNAL X : INT, but the global variable (GVL_Line) is DINT` |
| `line N: VAR_GLOBAL belongs in a global variable list, not in a PROGRAM or FUNCTION_BLOCK (declare the global there and use VAR_EXTERNAL)` |
| `X: an input (%I) cannot have an initial value: the inputs set it every cycle, and a new set's first cycle would show the initial value instead of the input (its initialisation runs after the inputs are read)` |

### Aliases

| Message |
|---|
| `alias X: an alias is declared in VAR_GLOBAL or a PROGRAM's VAR, not in VAR_INPUT` (or another section) |
| `FUNCTION_BLOCK F: X: an alias is declared in VAR_GLOBAL or a PROGRAM (a block takes in/out parameters)` |
| `alias X: an alias cannot be CONSTANT (narrow its access with {attribute 'access' := 'read'})` |
| `alias X: an alias has no storage of its own: RETAIN does not apply` |
| `alias X: an alias has no initial value (its target has)` |
| `alias X AT T: T is not a declared variable` |
| `alias X AT T: a global alias targets a global variable` |
| `alias X AT T: an alias's index is a constant (an alias names one fixed element)` |
| `alias X AT T: index n is outside [lo..hi]` |
| `alias X AT T: bit n of X: only an integer or bit string has bits` |
| `alias X AT T: bit n is outside INT (bits 0..15)` |
| `alias X AT T: Y has no members (it is a INT)` |
| `alias X AT T: Y is not an ARRAY` |
| `alias X : INT, but T is DINT (an alias has exactly its target's type)` |
| `alias X : INT: a bit (T) is a BOOL` |
| `alias X: cycle A -> B -> A (an alias must lead to a variable)` |
| `alias X: more than 4 aliases in a chain (point it at the variable)` |

### FUNCTION and VAR_IN_OUT

| Message |
|---|
| `FUNCTION F: the name is a standard function or a PROGRAM` |
| `FUNCTION F: a FUNCTION returns a value, not a function block instance` |
| `FUNCTION F: X: VAR_OUTPUT is not supported in a FUNCTION (return the value, or use VAR_IN_OUT)` |
| `FUNCTION F: X: a variable cannot be named like its FUNCTION (the name is the result)` |
| `FUNCTION F: X: a FUNCTION cannot declare function block instances (use a FUNCTION_BLOCK)` |
| `FUNCTION F: X: direct addresses are not allowed inside a FUNCTION` |
| `FUNCTION F: X: a FUNCTION's variables do not persist between calls: RETAIN does not apply` |
| `FUNCTION F: recursive call F -> G -> F (a FUNCTION cannot call itself, directly or through others)` |
| `FUNCTION F calls itself (a FUNCTION cannot be recursive)` |
| `FUNCTION F has no parameter X` / `F.X is not a VAR_INPUT or VAR_IN_OUT` / `F: parameter X given twice` |
| `F takes n argument(s), got m` |
| `F: the in/out parameter X must be passed` |
| `the call of F mixes formal (name := value) and positional arguments` |
| `F(X => ...): a FUNCTION has no outputs (use its result, or a VAR_IN_OUT)` |
| `F: a FUNCTION cannot be called here (an initial value is set before any code runs)` |
| `F: the in/out parameter X needs a variable of type T, not an expression` |
| `F: the in/out parameter X is T, Y is U (an in/out actual has exactly the parameter's type)` |
| `F: Y is a CONSTANT: it cannot be passed to the in/out parameter X` |
| `F: Y is a bit: it cannot be passed to the in/out parameter X` |
| `VAR_IN_OUT X is n bytes: in/out parameters are copied in and out, at most 4096 bytes (pass a global variable instead)` |
| `... X: a VAR_IN_OUT has no value of its own (no CONSTANT, no initial value): the call passes it` |
| `X: VAR_IN_OUT is for function blocks and functions, not for a PROGRAM` |
| `FB.X is a VAR_IN_OUT: pass it with X := variable` |

### POU texts (refused at save)

| Message |
|---|
| `the DUT POU T must hold exactly one type, TYPE T : ... END_TYPE` |
| `the DUT POU T holds TYPE T and nothing else` |
| `the GVL POU G holds only VAR_GLOBAL blocks, not VAR` (or `no FUNCTION_BLOCK`, ...) |
| `the GVL POU G holds at least one VAR_GLOBAL ... END_VAR block` |
| `the FUN POU F must hold exactly one unit, FUNCTION F : <type> ... END_FUNCTION` |
| `a FUNCTION declares no VAR_GLOBAL (globals live in a GVL POU)` |
| `line N: the {gvl ...} line is written by the PLC, never in a POU` |
| `P line N: {project ...} is written by the PLC only (the project's compile settings, first line of the linked text): remove it, and set the settings with PUT /api/project/settings` |

## Warnings and information

| Message | Meaning |
|---|---|
| `FUNCTION_BLOCK F: X hides the global variable X (GVL_Line)` (also `FUNCTION F: ...`) | a local wins over a global inside the block |
| `F: Y is passed to the in/out parameter X and the body of F also uses the global Y: ...` | copy-in/out means the body sees the value from before the call |
| `line N: unknown pragma {X}: ignored`, `line N: unknown attribute 'X': ignored` | not understood, ignored |
| `X: attribute 'network' is reserved for networked tags (a later version): not applied` | accepted (kept for exchange), not enforced yet |
| `X moved to a new offset: a located variable or direct address now uses its memory; its value is reset` | storage taken by a located variable |
| `X: migrated to its new type; dropped (their values are lost): ...` | members removed from a migrated type |
| `X: not migrated, initialised: its old storage (bytes a..b) is covered by a located variable or direct address of the new program (bytes c..d), so its values cannot be copied` | see [Online changes to data structures](/docs/eaxplc/explanation/online-changes-to-data-structures/#when-a-value-is-initialised-instead) |
| `info: X: attribute 'was' := 'Y': no variable Y in the running program: nothing to migrate (the attribute can be removed)` | `was` has nothing to do |
| `info: X: attribute 'was' := 'Y': Y is still declared: ignored` | the old name still exists |
| `info: X: attribute 'was' := 'Y': a located variable keeps its address: ignored` | located variables are not migrated |

## Cross-reference checks (eaxPLC Standard and Performance)

| Check | Finding |
|---|---|
| `global-written-several-tasks` | a global written from POUs run by different tasks |
| `global-unused` | a global declared but never read or written |
| `alias-target-written-directly` | an alias and its target are both written (information) |
| `fb-uses-global` | a FUNCTION_BLOCK or FUNCTION body uses a global (information: it hurts reuse) |

They appear in the cross-reference's **Checks**, in the impact report, and as row warnings in the tag database.

## Related

- [Data model: global variables, data types and external access](/docs/eaxplc/explanation/data-model/)
- [Online changes to data structures](/docs/eaxplc/explanation/online-changes-to-data-structures/)
- [Constants, outside writes and forces](/docs/eaxplc/reference/constants-writes-and-forces/)
