Book • Software Language Engineering g g g g Generic Language Technology (2IS15) g g gy ( ) by Anneke Kleppe (Addison Wesley) Syntaxes Prof.dr. Mark van den Brand / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 1 Signatures and grammars Signatures and grammars • Definition of a (programming) language involves: ( g g) g g Grammar world Grammar world • abstract syntax, so-called signature • The 4-layer architecture • concrete syntax: • M3 (E)BNF/SDF grammar − textual syntax textual syntax − defines structure of the (E)BNF in (E)BNF − graphical syntax • M2 Java grammar • semantics: − defines the structure of Java in (E)BNF ( ) − static semantics − static semantics • M1 Java program − dynamic semantics − describes the manipulation (algorithm) of objects in the object layer • M0 Object layer − Objects we wish to manipulate / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 2 / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 3
Signatures and grammars Signatures and grammars • Abstract syntax: • Abstract syntax is a Abstract syntax definition of Booleans: y y • defines basic structure collection of “true”() -> BoolCon “false”() -> BoolCon of the language constructors/- “con”(BoolCon) con (BoolCon) > Bool -> Bool ( k l t (skeleton) ) functions “and”(Bool, Bool) -> Bool • is starting point for “or”(Bool, Bool) -> Bool • No information about “not”(Bool) -> Bool defining: defining: keywords, priorities, k d i iti − concrete syntax associativities, etc. − static semantics − dynamic semantics nonterminal nonterminal d i ti constructor / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 4 13-9-2011 PAGE 5 Signatures and grammars Signatures and grammars • There is no standardized way of defining abstract • SSL (grammar specification formalism of the y g (g syntax Synthesizer Generator) describes it as follows: • SSL (specification formalism of the Synthesizer Generator) • A collection of rules that define phyla and operators • Signature-like • Signature-like • A phylum is a nonempty set of terms • A phylum is a nonempty set of terms • (Meta-modeling) • A term is the application of a k-ary operator to k terms of the appropriate phylum • A k-ary operator is a constructor function mapping k terms to A k ary operator is a constructor function mapping k terms to a term • A phylum can be considered a nonterminal phyl 0 : op ( phyl 1 phyl 2 … phyl k ) / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 6 / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 7
Signatures and grammars Signatures and grammars • SSL notation of the definition of the abstract syntax • Signature describes it as follows: y g of Booleans: • A collection of functions that define sorts and operators • A sort represents a nonempty set of terms boolcon : True() | False() boolcon : True() | False() • A term is the application of a k-ary operator to k terms of the • A term is the application of a k-ary operator to k terms of the appropriate sort bool : Con(boolcon) • A k-ary operator is a constructor function mapping k terms to | And(bool bool) a term a term | Or(bool bool) • A sort can be considered a nonterminal op ( sort 1 , sort 2 , …, sort k ) sort 0 ( ) / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 8 13-9-2011 PAGE 9 Signatures and grammars Signatures and grammars • Signature notation of the definition of the abstract • Given signatures it is possible to generate APIs g g g syntax of Booleans: • Tooling for defining signatures and generating APIs: • GOM part of TOM “true”() true () > BoolCon -> BoolCon (http://tom loria fr/wiki/index php5/Documentation:Gom) (http://tom.loria.fr/wiki/index.php5/Documentation:Gom) “false”() -> BoolCon • ApiGen part of SDF (see later) “con”(BoolCon) -> Bool “and”(Bool, Bool) -> Bool “or”(Bool, Bool) -> Bool “not”(Bool) -> Bool / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 10 / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 11
Signatures and grammars Signatures and grammars • Definition of a (programming) language involves: • Goal: defining languages & manipulating programs g g g g g • lexical syntax, so-called tokens: • SDF: Syntax definition Formalism − identifiers, numbers, strings, “if”, “then”, “class” (keywords) • lexical & context-free syntax • context-free syntax, so-called production rules: • ASF+SDF Meta-Environment: IDE for ASF+SDF − Statement ::= “if” Expression “then” Statements “else” Statements “fi” ASF SDF M t E i t IDE f ASF SDF • static semantics: • manuals/documentation: www.meta-environment.org − identification and scope resolution • Spoofax/IMP: Eclipse plugin for SDF Spoofax/IMP: Eclipse plugin for SDF − type checking type checking • manuals/documentation: http://strategoxt.org/Spoofax • dynamic semantics: − operational semantics − interpretation interpretation − compilation / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 12 13-9-2011 PAGE 13 Signatures and grammars Signatures and grammars • Anatomy of SDF specifications • Anatomy of an SDF module y y module A module C Name of this module; may be followed by parameters ... ... Names of modules imported by this module; p y ; imports B C im ts B C module ModuleName May be followed by renamings ... ImportSection* Grammar elements that are visible from the Grammar elements that are visible from the ExportOrHiddenSection* E tO Hidd S ti * outside ( exports ) or only inside the module ( hiddens ). module B module D ... ... imports, aliases, sorts, imports D lexical syntax, ... context-free syntax, y priorities, variables / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 14 / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 15
Signatures and grammars Signatures and grammars • SDF by examples y Boolean Constants • Boolean language Sort of Boolean constants module basic/BoolCon Sorts should always start with a • Pico language capital letter p exports The constants true and false , literals sorts BoolCon should always be quoted context-free syntax "true" -> BoolCon {cons(“true”)} " " B l { (“ ”)} "false" -> BoolCon {cons(“false”)} / Faculteit Wiskunde en Informatica / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 16 13-9-2011 PAGE 17 Signatures and grammars Signatures and grammars The infix operators and & and or | . Booleans Both are left-associative (left) Both are left associative (left) module basic/Booleans Import Boolean constants imports basic/BoolCon The prefix function not exports exports Boolean "|" Boolean -> Boolean {cons( or”), left} B l n "|" B l n > B l n {c ns(“ r”) l ft} sorts Boolean The sort of Boolean expressions Boolean "&" Boolean -> Boolean {cons(“and”), left} context-free syntax “not” (Boolean) -> Boolean {cons(“not”)} BoolCon BoolCon -> Boolean {cons(“con”)} > Boolean {cons( con )} "(" Boolean ")" > Boolean {bracket} ( Boolean ) -> Boolean {bracket} ( and ) may be used as brackets in ( and ) may be used as brackets in Boolean expressions; they are Each Boolean constant is a Boolean context-free priorities ignored after parsing Expression, also called injection rule Boolean & Boolean -> Boolean > Boolean "&" Boolean -> Boolean > or chain rule h i l & h & has higher priority than | | hi h i it th Boolean "|" Boolean -> Boolean Example: Bool & Bool | Bool is interpreted as: (Bool & Bool) | Bool / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 18 / Faculteit Wiskunde en Informatica 13-9-2011 PAGE 19
Recommend
More recommend