Nested Syntax: Brackets vs Begin End Keywords vs Indentation

By Xah Lee. Date: . Last updated: .

Which Syntax Design is Best for Nested Syntax

Here's 3 programing language syntax for nesting.

Bracket style

[11, [21, 22], 31]

Begin End Keywords style

begin 11, begin 21, 22 end , 31 end

Indentation style

11
 21
 22
31

Bracket Style is Best

Of all characters, there's a unique property of brackets such as ( ) [ ] { }. Namely, they embed info on matching another char in text. This means, any editor, language, or parser, can trivially extract such info. Lisp uses this to advantage, and from it came widely praised lisp macros. Similar with JSON.

Begin End Keywords Style

languages that use {begin, end}, such as {Ruby, Pascal}, means there need to be semantic info added to such string, and more work for parser or text editor to understand the nesting structure.

Indentation Syntax is Worst

languages using indentation for blocks and nesting, such as {Python, Haskell, HAML, YAML, Slim} is non-trivial to parse. It requires a dedicated parser to understand the nesting.

Indentation syntax is also complex, prone to invalid syntax, because the nesting info must be embedded in each and every line of a nested structure.

Indentation syntax has another problem, which is most serious: Namely, it mixes semantic and syntactical info. The source code is hard-formatted in one fixed way, and must always be manually formatted. For a damage due to this, see: Why Python Lambda is Broken and Can't be Fixed.

See also:

computer language syntax for nesting