Off-side rule

From Wikipedia, the free encyclopedia
Jump to: navigation, search
This article is about the programming language syntax feature. For other uses, see Offside (disambiguation).

A computer programming language is said to adhere to the off-side rule if blocks in that language are expressed by their indentation. The term and the idea are attributed to Peter J. Landin, and the term can be seen as a pun on the offside law of football (soccer). This is contrasted with free-form languages, notably curly bracket programming languages, where indentation is not meaningful and indent style is only a matter of convention and code formatting.

Definition[edit]

Peter J. Landin, in an article called "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."[1]

Code examples[edit]

The following is an example of indentation blocks in Python (note also the colons – they are obligatory and not stylistic in Python):[2]

 def is_even(a):
     if a % 2 == 0:
         print('Even!')
         return True
     print('Odd!')
     return False

Implementation[edit]

The off-side rule can be implemented in the lexical analysis phase, as in Python, where increasing the indentation results in the lexer outputting an INDENT token, and decreasing the indentation results in the lexer outputting a DEDENT token.[3] 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. This requires that the lexer hold state, namely the current indentation level, and thus can detect changes in indentation when this changes, and thus the lexical grammar is not context-free – INDENT/DEDENT depend on the contextual information of previous indentation level.

Alternatives[edit]

The primary alternative to delimiting blocks, popularized by C, is to ignore whitespace and mark blocks explicitly with curly brackets (i.e. { and }) or some other delimiter. While this allows for more freedom – the developer might choose not to indent small pieces of code like the break and continue statements – sloppily indented code might lead the reader astray.

Lisp doesn't differentiate statements from expressions, and parentheses are enough to control the scoping of all statements within the language. As in curly bracket languages, the lengths of the whitespaces are ignored.

Another alternative is for each block to begin and end with explicit keywords. For example in Pascal blocks start with keyword begin and end with keyword end. In some languages (but not Pascal), this means that newlines are important[citation needed] (unlike in curly brace languages), but the indentation is not. In BASIC and FORTRAN, blocks begin with the block name (such as IF) and end with the block name prepended with END (e.g. END IF). The Bourne shell (sh, and bash) is similar, but the ending of the block is usually given by the name of the block written backward (e.g. case starts a conditional statement and it spans until the matching esac).

Off-side rule languages[edit]

See also[edit]

References[edit]

This article is based on material taken from the Free On-line Dictionary of Computing prior to 1 November 2008 and incorporated under the "relicensing" terms of the GFDL, version 1.3 or later.