## 11.3 Extra-grammatical constraints

### Type constraints

A well-formed Stan program must satisfy the type constraints imposed by functions and distributions. For example, the binomial distribution requires an integer total count parameter and integer variate and when truncated would require integer truncation points. If these constraints are violated, the program will be rejected during compilation with an error message indicating the location of the problem.

### Operator precedence and associativity

In the Stan grammar provided in this chapter, the expression 1 + 2 * 3 has two parses. As described in the operator precedence table, Stan disambiguates between the meaning $$1 + (2 \times 3)$$ and the meaning $$(1 + 2) \times 3$$ based on operator precedences and associativities.

### Typing of compound declaration and definition

In a compound variable declaration and definition, the type of the right-hand side expression must be assignable to the variable being declared. The assignability constraint restricts compound declarations and definitions to local variables and variables declared in the transformed data, transformed parameters, and generated quantities blocks.

### Typing of array expressions

The types of expressions used for elements in array expressions ('{' expressions '}') must all be of the same type or a mixture of scalar (int, real and complex) types (in which case the result is promoted to be of the highest type on the int -> real -> complex hierarchy).

### Forms of numbers

Integer literals longer than one digit may not start with 0 and real literals cannot consist of only a period or only an exponent.

### Conditional arguments

Both the conditional if-then-else statement and while-loop statement require the expression denoting the condition to be a primitive type, integer or real.

### For loop containers

The for loop statement requires that we specify in addition to the loop identifier, either a range consisting of two expressions denoting an integer, separated by ‘:’ or a single expression denoting a container. The loop variable will be of type integer in the former case and of the contained type in the latter case. Furthermore, the loop variable must not be in scope (i.e., there is no masking of variables).

### Only break and continue in loops

The break and continue statements may only be used within the body of a for-loop or while-loop.

### 11.3.1 Block-specific restrictions

Some constructs in the Stan language are only allowed in certain blocks or in certain kinds of user-defiend functions.

#### 11.3.1.1 PRNG functions

Functions ending in _rng may only be called in the transformed data and generated quantities block, and within the bodies of user-defined functions with names ending in _rng.

#### 11.3.1.2 Unnormalized distributions

Unnormalized distributions (with suffixes _lupmf or _lupdf) may only be called in the model block, user-defined probability functions, or within the bodies of user defined functions which end in _lp.

#### 11.3.1.3 Incrementing and accessing target

target += statements can only be used inside of the model block or user-defined functions which end in _lp.

User defined functions which end in _lp and the target() function can only be used in the model block, transformed parameters block, and in the bodies of other user defined functions which end in _lp.

Sampling statements (using ~) can only be used in the model block or in the bodies of user-defined functions which end in _lp.

### Probability function naming

A probability function literal must have one of the following suffixes: _lpdf, _lpmf, _lcdf, or _lccdf.

### Indexes

Standalone expressions used as indexes must denote either an integer (int) or an integer array (array[] int). Expressions participating in range indexes (e.g., a and b in a : b) must denote integers (int).

A second condition is that there not be more indexes provided than dimensions of the underlying expression (in general) or variable (on the left side of assignments) being indexed. A vector or row vector adds 1 to the array dimension and a matrix adds 2. That is, the type matrix[ , , ], a three-dimensional array of matrices, has five index positions: three for the array, one for the row of the matrix and one for the column.