Growing a Syntax Eric Allen 1 , Ryan Culpepper 2 , Janus Dam Nielsen - - PowerPoint PPT Presentation

growing a syntax
SMART_READER_LITE
LIVE PREVIEW

Growing a Syntax Eric Allen 1 , Ryan Culpepper 2 , Janus Dam Nielsen - - PowerPoint PPT Presentation

Growing a Language Objectives and Example Syntax Normalization Summary Growing a Syntax Eric Allen 1 , Ryan Culpepper 2 , Janus Dam Nielsen 3 , Jon Rafkind 4 , and Sukyoung Ryu 1 1 Sun Microsystems 2 Northeastern University 3 Aarhus University 4


slide-1
SLIDE 1

Growing a Language Objectives and Example Syntax Normalization Summary

Growing a Syntax

Eric Allen1, Ryan Culpepper2, Janus Dam Nielsen3, Jon Rafkind4, and Sukyoung Ryu1

1Sun Microsystems 2Northeastern University 3Aarhus University 4University of Utah

Foundations of Object-Oriented Languages, 2009

slide-2
SLIDE 2

Growing a Language Objectives and Example Syntax Normalization Summary

Outline

1

Growing a Language Fortress Introduction Growing a Language XML Example Introduction

2

Objectives and Example Syntactic Abstraction Goals XML as an Example

3

Syntax Normalization Parsing and Transformation

slide-3
SLIDE 3

Growing a Language Objectives and Example Syntax Normalization Summary Fortress Introduction

Outline

1

Growing a Language Fortress Introduction Growing a Language XML Example Introduction

2

Objectives and Example Syntactic Abstraction Goals XML as an Example

3

Syntax Normalization Parsing and Transformation

slide-4
SLIDE 4

Growing a Language Objectives and Example Syntax Normalization Summary Fortress Introduction

The Fortress Programming Language

A multicore language for scientists and engineers

slide-5
SLIDE 5

Growing a Language Objectives and Example Syntax Normalization Summary Fortress Introduction

The Fortress Programming Language

A multicore language for scientists and engineers Run your whiteboard in parallel!

slide-6
SLIDE 6

Growing a Language Objectives and Example Syntax Normalization Summary Fortress Introduction

The Fortress Programming Language

A multicore language for scientists and engineers Run your whiteboard in parallel! vnorm = v/ v || cij =

  • k←0:n

aik bkj C = A ∪ B y = 3x sin x cos 2x log log x

slide-7
SLIDE 7

Growing a Language Objectives and Example Syntax Normalization Summary Fortress Introduction

The Fortress Programming Language

A multicore language for scientists and engineers Run your whiteboard in parallel! vnorm = v/ v || cij =

  • k←0:n

aik bkj C = A ∪ B y = 3x sin x cos 2x log log x

slide-8
SLIDE 8

Growing a Language Objectives and Example Syntax Normalization Summary Fortress Introduction

The Fortress Programming Language

A multicore language for scientists and engineers Run your whiteboard in parallel! vnorm = v/ v || cij =

  • k←0:n

aik bkj C = A ∪ B y = 3x sin x cos 2x log log x Growing a Language Guy L. Steele Jr., keynote talk OOPSLA 1998

Higher-Order and Symbolic Computation 12, 221-236 (1999)

slide-9
SLIDE 9

Growing a Language Objectives and Example Syntax Normalization Summary Growing a Language

Outline

1

Growing a Language Fortress Introduction Growing a Language XML Example Introduction

2

Objectives and Example Syntactic Abstraction Goals XML as an Example

3

Syntax Normalization Parsing and Transformation

slide-10
SLIDE 10

Growing a Language Objectives and Example Syntax Normalization Summary Growing a Language

Growing a Language

“So I think the sole way to win is to plan for growth with help from the users.”

Guy L. Steele Jr.

keynote talk, OOPSLA 1998; Higher-Order and Symbolic Computation 12, 221-236 (1999)

slide-11
SLIDE 11

Growing a Language Objectives and Example Syntax Normalization Summary Growing a Language

Design Strategy

Consider how a proposed language feature might be provided by a library rather than building features directly into the compiler.

This requires control over both syntax and semantics, not just the ability to add new functions and methods.

slide-12
SLIDE 12

Growing a Language Objectives and Example Syntax Normalization Summary XML Example Introduction

Outline

1

Growing a Language Fortress Introduction Growing a Language XML Example Introduction

2

Objectives and Example Syntactic Abstraction Goals XML as an Example

3

Syntax Normalization Parsing and Transformation

slide-13
SLIDE 13

Growing a Language Objectives and Example Syntax Normalization Summary XML Example Introduction

XML in Fortress

x = <html xmlns = “http://www.w3.org/1999/xhtml”> <title> Project Fortress </title> <body/> </html> <body/>.hasElements x.children x.attributes XML literals in Fortress Seamless integration DOM operations on values

slide-14
SLIDE 14

Growing a Language Objectives and Example Syntax Normalization Summary XML Example Introduction

XML in Fortress

x = <html xmlns = “http://www.w3.org/1999/xhtml”> <title> Project Fortress </title> <body/> </html> <body/>.hasElements x.children x.attributes XML literals in Fortress Seamless integration DOM operations on values

slide-15
SLIDE 15

Growing a Language Objectives and Example Syntax Normalization Summary XML Example Introduction

XML in Fortress

x = <html xmlns = “http://www.w3.org/1999/xhtml”> <title> Project Fortress </title> <body/> </html> <body/>.hasElements x.children x.attributes XML literals in Fortress Seamless integration DOM operations on values

slide-16
SLIDE 16

Growing a Language Objectives and Example Syntax Normalization Summary XML Example Introduction

Desugaring XML

The XML literal: <html xmlns = “http://www.w3.org/1999/xhtml”> <title> Project Fortress </title> <body/> </html> Desugars to: Element( Header(“html”, Attribute(“xmlns”, “http://www.w3.org/1999/xhtml”) ), Element( Header(“title”, ), CData(“Project Fortress”) , “title”), Element(Header(“body”, ), , “body”) , “html”)

slide-17
SLIDE 17

Growing a Language Objectives and Example Syntax Normalization Summary XML Example Introduction

Basic XML DOM Structure in Fortress

  • bject Element(info : Header, contents : ListContent,

endTag : String) extends Content getter name() : String getter hasElements() : Boolean getter children() : ListElement getter cdata() : CData getter attributes() : ListAttribute getter toXml() : String end trait Content end

  • bject CData(c : String) extends Content end
  • bject Header(startTag : String, attributes : ListAttribute) end
  • bject Attribute(key : String, val : String) end
slide-18
SLIDE 18

Growing a Language Objectives and Example Syntax Normalization Summary XML Example Introduction

Desugaring XML

Design Strategy Consider how a proposed language feature might be provided by a library rather than building features directly into the compiler XML desugaring is provided by syntactic abstraction in a library

slide-19
SLIDE 19

Growing a Language Objectives and Example Syntax Normalization Summary Syntactic Abstraction Goals

Outline

1

Growing a Language Fortress Introduction Growing a Language XML Example Introduction

2

Objectives and Example Syntactic Abstraction Goals XML as an Example

3

Syntax Normalization Parsing and Transformation

slide-20
SLIDE 20

Growing a Language Objectives and Example Syntax Normalization Summary Syntactic Abstraction Goals

Syntactic Abstraction Goals

New syntax indistinguishable from core syntax Similar syntax for definition/use of a language extension Composition of independent language extensions Expansion into other language extensions Mutually recursive definition of language extensions

slide-21
SLIDE 21

Growing a Language Objectives and Example Syntax Normalization Summary XML as an Example

Outline

1

Growing a Language Fortress Introduction Growing a Language XML Example Introduction

2

Objectives and Example Syntactic Abstraction Goals XML as an Example

3

Syntax Normalization Parsing and Transformation

slide-22
SLIDE 22

Growing a Language Objectives and Example Syntax Normalization Summary XML as an Example

Grammar of XML Literals in Fortress

grammar xml extends { Expression, Symbols } Expr |:= x : XExpr ⇒ < [ x ] > XExpr ::= b : startTag c : XmlContent e : endTag ⇒ < [ Element(b, c, e) ] > | b : startTag e : endTag ⇒ < [ Element(b, , e) ] > XmlContent ::= s : XmlIdentifier ⇒ < [ CData(s) ] > | {x : XExpr SPACE}+ ⇒ < [ x** ] > startTag ::= <s : XmlIdentifier {a : Attribute SPACE}∗> ⇒ < [ Header(s, a** ] > endTag ::= </ s : XmlIdentifier> ⇒ < [ s ] > end

slide-23
SLIDE 23

Growing a Language Objectives and Example Syntax Normalization Summary XML as an Example

Examples of Goals

New syntax indistinguishable from core syntax <body /> .hasElements Similar syntax for definition/use of a language extension < s : XmlIdentifier {a : Attribute SPACE}∗ /> ⇒ . . . <html xmlns = “http://www.w3.org/1999/xhtml”/>

slide-24
SLIDE 24

Growing a Language Objectives and Example Syntax Normalization Summary XML as an Example

Examples of Goals

Composition of independent language extensions grammar xml extends { Expression, Symbols } Expansion into other language extensions . . . ⇒< [<body />] > Mutually recursive definition of language extensions

slide-25
SLIDE 25

Growing a Language Objectives and Example Syntax Normalization Summary Parsing and Transformation

Outline

1

Growing a Language Fortress Introduction Growing a Language XML Example Introduction

2

Objectives and Example Syntactic Abstraction Goals XML as an Example

3

Syntax Normalization Parsing and Transformation

slide-26
SLIDE 26

Growing a Language Objectives and Example Syntax Normalization Summary Parsing and Transformation

Syntax Normalization

Parsing stage - transforms a source program (in text) into a parsed program (in node expression) Transformation stage - transforms the parsed program into a program in executable core Fortress AST

slide-27
SLIDE 27

Growing a Language Objectives and Example Syntax Normalization Summary Parsing and Transformation

Syntax Normalization

Parsing stage - transforms a source program (in text) into a parsed program (in node expression)

First step parses the macro definition into an intermediate form to generate a parser that recognizes the new syntax Second step uses the generated parser to parse a source program using the new syntax

Transformation stage - transforms the parsed program into a program in executable core Fortress AST

slide-28
SLIDE 28

Growing a Language Objectives and Example Syntax Normalization Summary Parsing and Transformation

Node Expressions - Intermediate Representation

NodeExpr ::= PatternVar | Transformer (NodeExpr) | NodeConstructor(NodeExpr) | Ellipses (NodeExpr, NodeExpr) | case PatternVar of Empty ⇒ NodeExpr Cons(PatternVar, PatternVar) ⇒ NodeExpr end

slide-29
SLIDE 29

Growing a Language Objectives and Example Syntax Normalization Summary Parsing and Transformation

Transformation: Evaluation of Node Expressions

Pattern variables are substituted by the corresponding values Transformers are replaced with their bodies, substituting pattern variables along the way Core Fortress AST nodes transform their arguments Case expression match its input to constructors Empty and Cons, and invokes the corresponding transformer

slide-30
SLIDE 30

Growing a Language Objectives and Example Syntax Normalization Summary

Summary

Fortress is a growable language Syntactic abstraction supports language growth Implementation is available as part of the open-source Fortress reference implementation at: http://projectfortress.sun.com

slide-31
SLIDE 31

Growing a Language Objectives and Example Syntax Normalization Summary

Evaluation of Node Expressions (1)

[Pattern Variable] Γ(x) = v Υ, Γ ⊢ x → Υ, Γ ⊢ v [Node Constructor] Υ, Γ ⊢ n → Υ, Γ ⊢ n′ Υ, Γ ⊢ c(n) → Υ, Γ ⊢ c(n′)

slide-32
SLIDE 32

Growing a Language Objectives and Example Syntax Normalization Summary

Evaluation of Node Expressions (2)

[Macro Invocation Arguments] Υ, Γ ⊢ n → Υ, Γ ⊢ n′ Υ, Γ ⊢ t(n) → Υ, Γ ⊢ t(n′) [Macro Invocation] Υ(t) = t x.n Υ, Γ ⊢ t(v) → Υ, Γ [x → v] ⊢ n

slide-33
SLIDE 33

Growing a Language Objectives and Example Syntax Normalization Summary

Evaluation of Node Expressions (3)

[Case Empty] Υ, Γ ⊢ x → Υ, Γ ⊢ Empty Υ, Γ ⊢ case x of → Υ, Γ ⊢ n1 Empty ⇒ n1 Cons(x1, x2) ⇒ n2 end [Case Cons] Υ, Γ ⊢ x → Υ, Γ ⊢ Cons(v1, v2) Υ, Γ ⊢ case x of → Υ, Γ [x1 → v1] [x2 → v2] ⊢ n2 Empty ⇒ n1 Cons(x1, x2) ⇒ n2 end

slide-34
SLIDE 34

Growing a Language Objectives and Example Syntax Normalization Summary

Evaluation of Node Expressions (4)

[Ellipses First] |v′| + 1 = i ≤ size(n) xj ∈ PV(n) |Γ(xj)| > 1 Γ′ = Γ

  • xj → Γ(xj).nth(i)
  • Υ, Γ′ ⊢ n′′ → Υ, Γ′ ⊢ n′′′

Υ, Γ ⊢ Ellipses(n, v′n′′) → Υ, Γ ⊢ Ellipses(n, v′n′′′) [Ellipses Middle] |v′| + 1 = i − 1 < size(n) xj ∈ PV(n) |Γ(xj)| > 1 Γ′ = Γ

  • xj → Γ(xj).nth(i)
  • Υ, Γ′ ⊢ n → Υ, Γ′ ⊢ n′′′

Υ, Γ ⊢ Ellipses(n, v′v′′) → Υ, Γ ⊢ Ellipses(n, v′v′′n′′′) [Ellipses Last] |v′| + 1 = size(n) Υ, Γ ⊢ Ellipses(n, v′v′′) → Υ, Γ ⊢ v′v′′