Syntax Definition Formalisms 3 (SDF3) is an academic syntax definition, used for compiling languages. It is part of the Spoofax workbench.
Syntax and language basics
Formal language definitions are very much related to (context-free) grammars, as we know them from automata.
Programs are sentences, where a sentence consists of phrases. A phrase is either a terminal or a non-terminal. Sorts are the categorization of both terminals and non-terminals. By defining something as a sort, we can use the declaration in something called productions. A grammar of a language consists of rules aka productions to build a valid program structure. A production consists of a sort, or sort constructor, followed by a body. The body is a template and will be parsed. The body may also be a phrase or sub-phrase.
An example: <syntaxhighlight lang="cpp" line="1">
context-free syntax Expr = <Expr * Expr> // Everything after = is the body, also known as template
Sub-phrases are separated by layout, where layout is defined in the language as well. Layout is mostly whitespace, but also contains comments and other elements that should not be compiled. The constructor is used to construct abstract syntax tree nodes. Thus, the Bind constructor creates trees with two arguments trees for the identifier (ID) and expression (Exp) subtrees; in abstract syntax we leave out the literals and layout.
Expr.Bind = <<ID> = <Exp>;>