A computer programming language is said to adhere to the off-side rule of syntax if blocks in that language are expressed by their indentation. The term was coined by Peter Landin, possibly as a pun on the offside rule in association football. This is contrasted with free-form languages, notably curly-bracket programming languages, where indentation has no computational meaning and indent style is only a matter of coding conventions and formatting. Off-side-rule languages are also described as having significant indentation.
Peter Landin, in his 1966 article "The Next 700 Programming Languages", defined the off-side rule thus: "Any non-whitespace token to the left of the first such token on the previous line is taken to be the start of a new declaration."
The following is an example of indentation blocks in Python. The colons are part of the Python language syntax for readability; they are not needed to implement the off-side rule.
In Python, the rule is taken to define the boundaries of statements rather than declarations.
def is_even(a: int) -> bool:
"""Determine whether number 'a' is even."""
if a % 2 == 0:
print('Even!')
return True
print('Odd!')
return False
Python also suspends the off-side rule within brackets. A statement within brackets continues until its brackets match (or mismatch):
{
"this": True,
"that": False,
"them": [1, 2,
3, 4, 5,
6, 7, 8,
9, 10, 11, 12]
}
In this dictionary, the keys are indented, and a list is split between four lines.
The off-side rule can be implemented in the lexical analysis phase, as in Python, where increasing the indenting results in the lexer outputting an INDENT token, and decreasing the indenting results in the lexer outputting a DEDENT token. These tokens correspond to the opening brace { and closing brace } in languages that use braces for blocks, and means that the phrase grammar does not depend on whether braces or indentation are used.