Difference between revisions of "SDF3"

From MyInfoRepo
Jump to navigation Jump to search
Line 19: Line 19:
     Expr = <Expr * Expr> // Everything after = is the body, also known as template
     Expr = <Expr * Expr> // Everything after = is the body, also known as template
</syntaxhighlight>
</syntaxhighlight>
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.
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.
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.

Revision as of 18:54, 8 September 2020

Syntax Definition Formalisms 3 (SDF3) is an academic syntax definition, used for compiling languages. It is part of the Spoofax workbench.

Intro

none yet

Syntax and language basics

Formal language definitions are very much related to (context-free) grammars, as we know them from automata.

Definitions

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">

 sorts Expr
 context-free syntax
   Expr = <Expr * Expr> // Everything after = is the body, also known as template

</syntaxhighlight>

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>;>


Lexical vs Context free

An example program