declarative syntax definition - sdf and aterms

87
IN4303 2014/15 Compiler Construction Declarative Syntax Definition SDF3 and ATerms Guido Wachsmuth

Upload: guido-wachsmuth

Post on 28-Nov-2014

2.540 views

Category:

Education


1 download

DESCRIPTION

Presentation slides from lecture 4 of course IN4303 on Compiler Construction at TU Delft.

TRANSCRIPT

Page 1: Declarative Syntax Definition - SDF and ATerms

IN4303 2014/15 Compiler Construction

Declarative Syntax Definition SDF3 and ATerms

Guido Wachsmuth

Page 2: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 2

3 * +7 21

3 * +7 21

Exp ExpExp

Exp

Exp

parser

Page 3: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 3

3 * +7 21

parserConst

Mul

Const

213 7

Const

Add

Page 4: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 4

3 * +7 21

parser

Add( Mul( Const(3) , Const(7) ) , Const(21))

Page 5: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms

Stratego

NaBL TS

SDF3

ESV editor

SPT tests

5

syntax definition

concrete syntax

abstract syntax

static semantics

name binding

type system

dynamic semantics

translation

interpretation

Page 6: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 6

syntax definition

regular expressions

Backus-Naur Form

Extended BNF

SDF3

lexical syntax

context-free syntax

abstract syntax

disambiguation

Spoofax

architecture

testing

editors

syntax processing

pretty-printing

discovery

more editor services

Page 7: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 7

syntax definition

Page 8: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 8

software languages

Page 9: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 9

finite models

Page 10: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 10

Philosophy

Linguistics

lexicology

grammar

morphology

syntax

phonology

semantics

Interdisciplinary

Computer Science

syntax

semantics

Page 11: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 11

lexical syntax

words made from letters

irrelevant structure

literals 42 "foo" true

identifiers x fookeywords if while

operators + *whitespace // FIXME

regular expressions

context-free syntax

sentences made from words

relevant structure

context-free grammars

Backus-Naur Form

Extended Backus-Naur Form

computer science syntax

Page 12: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 12

syntax definition regular expressions

Page 13: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 13

basics

strings "nil"

character classes [a-zA-Z]

combinators

concatenation E1 E2

option E?

repetition (zero or more) E*

repetition (one or more) E+

alternative E1 | E2

regular expressions

Page 14: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 14

/* factorial function */!let ! var x := 0! function fact(n : int) : int = if n < 1 then 1 else (n * fact(n - 1))!in ! for i := 1 to 3 do ( x := x + fact(i); printint(x); print(" ") )!end

Tiger the lecture language

Page 15: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 15

An identifier is a sequence of letters, digits, and under-scores, starting with a letter. Uppercase letters are distinguished from lowercase.

[a-zA-Z][a-zA-Z0-9\_]*

A comment may appear between any two tokens. Comments start with /* and end with */ and may be nested.

Tiger lexical syntax

Pumping Lemma

Page 16: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 16

syntax definition Backus-Naur Form

Page 17: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 17

basics

strings nil

symbols <s>

combinators

concatenation E1 E2

production <s> ::= E1 | … | En

Backus-Naur Form

Page 18: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 18

<exp> ::= <num> | <exp> + <exp> | <exp> - <exp> | <exp> * <exp> | <exp> / <exp> | ( <exp> )

Tiger context-free syntax

Page 19: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 19

syntax definition Extended Backus-Naur Form

Page 20: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 20

basics

strings "nil"

symbols s

combinators

concatenation E1,E2

option [E]

repetition (zero or more) {E}

alternative E1 | E2 production s = E1 | … | En ;

Extended Backus-Naur Form

Page 21: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 21

exp = num | exp , "+" , exp | exp , "-" , exp | exp , "*" , exp | exp , "/" , exp | "(" , exp , ")" ;

Tiger context-free syntax

Page 22: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 22

SDF3 lexical syntax

Page 23: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 23

basics

strings "nil"

character classes [a-zA-Z]complements ~[a-zA-Z]sorts S

!

combinators

concatenation E1 E2

option E?

repetition (zero or more) E*

with separator {E1 E2}*

repetition (one or more) E+

with separator {E1 E2}+

alternative E1 | E2

production S = E

SDF3 lexical syntax

Page 24: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 24

module Literals!lexical syntax! ID = [a-zA-Z] [a-zA-Z0-9\_]* INT = "-"? [0-9]+ STRING = "\"" StringChar* "\"" StringChar = ~[\"\n] StringChar = "\\\"" StringChar = "\\"

Tiger lexical syntax

Page 25: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 25

module Whitespace!lexical syntax! LAYOUT = [\ \t\n\r] LAYOUT = "//" ~[\n\r]* NewLineEOF NewLineEOF = [\n\r] | EOF EOF =! LAYOUT = "/*" CommentPart* "*/" CommentPart = ~[\*] CommentPart = [\*]

Tiger lexical syntax

Page 26: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 26

SDF3 context-free syntax

Page 27: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 27

basics

strings "nil"

sorts S

!

combinators

concatenation E1 E2

option E?

repetition (zero or more) E*

with separator {E s}*

repetition (one or more) E+

with separator {E s}+

production S = E

SDF3 context-free syntax

Page 28: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 28

module Expressions!context-free syntax! Exp = LValue LValue = ID LValue = LValue "." ID LValue = LValue "[" Exp “]"! Exp = "nil" Exp = INT Exp = STRING Exp = ID "(" {Exp ","}* ")" Exp = TypeId "{" {InitField ","}* "}" Exp = TypeId "[" Exp "]" "of" Exp InitField = ID "=" Exp

Tiger context-free syntax

xx.f1x[i]!nil42"foo"!sqr(x)!aty [x] of 42rty {f1 = "foo", f2 = 42}

Page 29: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 29

context-free syntax! Exp = Exp "+" Exp Exp = Exp "-" Exp Exp = Exp "*" Exp Exp = Exp "/" Exp Exp = Exp “=" Exp Exp = Exp "<>" Exp Exp = Exp ">" Exp Exp = Exp "<" Exp Exp = Exp ">=" Exp Exp = Exp "<=" Exp! Exp = Exp “&" Exp Exp = Exp "|" Exp

Tiger context-free syntax

Page 30: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 30

SDF3 abstract syntax

Page 31: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 31

tree

leaf

parent node

children are trees

!

term

constant

constructor

arguments are terms

ATerms trees - terms

Const

Mul

Const

213 7

Const

Add

Add( Mul( Const(3) , Const(7) ) , Const(21))

Page 32: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 32

context-free syntax! Exp.Add = Exp "+" Exp Exp.Sub = Exp "-" Exp Exp.Mul = Exp "*" Exp Exp.Div = Exp "/" Exp Exp.Eq = Exp “=" Exp Exp.Neq = Exp "<>" Exp Exp.Gt = Exp ">" Exp Exp.Lt = Exp "<" Exp Exp.Gte = Exp ">=" Exp Exp.Lte = Exp "<=" Exp! Exp.And = Exp “&" Exp Exp.Or = Exp "|" Exp

Tiger context-free syntax

Page 33: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 33

SDF3 disambiguation

Page 34: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 34

lexical syntax! ID = [a-zA-Z] [a-zA-Z0-9\_]* ! ID = "nil" {reject} INT = "-"? [0-9]+ !lexical restrictions! ID -/- [a-zA-Z0-9\_] INT -/- [0-9]

Tiger lexical disambiguation

Page 35: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 35

lexical syntax! STRING = "\"" StringChar* "\"" StringChar = ~[\\\"\n] StringChar = "\\\"" StringChar = "\\" !lexical restrictions! "\\" -/- [\"]

Tiger lexical disambiguation

Page 36: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 36

lexical syntax! STRING = "\"" StringChar* "\"" StringChar = ~[\\\"\n] StringChar = "\\\"" StringChar = StringEscChar StringEscChar = "\\" !lexical restrictions! StringEscChar -/- [\"]

Tiger lexical disambiguation

Page 37: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 37

lexical syntax! LAYOUT = "//" ~[\n\r]* NewLineEOF NewLineEOF = [\n\r] | EOF EOF =! LAYOUT = "/*" CommentPart* "*/" CommentPart = ~[\*] CommentPart = "*"!lexical restrictions! EOF -/- ~[] "*" -/- ~[\/]

Tiger lexical disambiguation

Page 38: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 38

lexical syntax! LAYOUT = "//" ~[\n\r]* NewLineEOF NewLineEOF = [\n\r] | EOF EOF =! LAYOUT = "/*" CommentPart* "*/" CommentPart = ~[\*] CommentPart = CommentEndChar CommentEndChar = "*"!lexical restrictions EOF -/- ~[] CommentEndChar -/- ~[\/]

Tiger lexical disambiguation

Page 39: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 39

lexical syntax! LAYOUT = [\ \t\n\r] LAYOUT = "//" ~[\n\r]* NewLineEOF LAYOUT = "/*" CommentPart* "*/" context-free restrictions LAYOUT? -/- [\ \t\n\r] LAYOUT? -/- [\/].[\/] LAYOUT? -/- [\/].[\*]

Tiger context-free disambiguation

Page 40: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 40

context-free syntax! Exp.Add = Exp "+" Exp Exp.Sub = Exp "-" Exp Exp.Mul = Exp "*" Exp Exp.Div = Exp "/" Exp Exp.Eq = Exp “=" Exp Exp.Neq = Exp "<>" Exp Exp.Gt = Exp ">" Exp Exp.Lt = Exp "<" Exp Exp.Gte = Exp ">=" Exp Exp.Lte = Exp "<=" Exp! Exp.And = Exp “&" Exp Exp.Or = Exp "|" Exp

Tiger context-free syntax

Page 41: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 41

context-free syntax! Exp.Add = Exp "+" Exp {left} Exp.Sub = Exp "-" Exp {left} Exp.Mul = Exp "*" Exp {left} Exp.Div = Exp "/" Exp {left} Exp.Eq = Exp “=" Exp {non-assoc} Exp.Neq = Exp "<>" Exp {non-assoc} Exp.Gt = Exp ">" Exp {non-assoc} Exp.Lt = Exp "<" Exp {non-assoc} Exp.Gte = Exp ">=" Exp {non-assoc} Exp.Lte = Exp "<=" Exp {non-assoc}! Exp.And = Exp “&" Exp {left} Exp.Or = Exp "|" Exp {left}!

context-free priorities { left: Exp.Mul Exp.Div } > { left: Exp.Add Exp.Sub } > { non-assoc: Exp.Eq Exp.Neq Exp.Gt Exp.Lt Exp.Gte Exp.Lte } > Exp.And > Exp.Or

Tiger context-free disambiguation

Page 42: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 42

Spoofax architecture

Page 43: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 43

source code

parse generate

machine code

check

Page 44: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms

Data

Programs

Language Processors

44

Eclipse JDT

Fact.java

42

Java

Java

int

Page 45: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 45

language definition

parse generate

language implementation

check

Page 46: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms

syntax definition

46

parse generate

parse table

checkgeneric parser

Page 47: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms

Data

Programs

Language Processors

47

Java parser

Fact.java

42

Language Workbench Spoofax

SDF3

Java

int

SDF3

Page 48: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 48

context-free syntax! Grammar.Lexical = < lexical syntax <Production*> > Grammar.Contextfree = < context-free syntax <Production*> > Production.SdfProduction = <<Symbol> = <Symbol*> <Attributes>> Production.SdfProductionWithCons = <<SortCons> = <Symbol*> <Attributes>> SortCons.SortCons = <<Symbol>.<Constructor>>

context-free syntax SDF3 in SDF3

Page 49: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms

Stratego

NaBL TS

SDF3

ESV editor

SPT tests

49

syntax definition

concrete syntax

abstract syntax

static semantics

name binding

type system

dynamic semantics

translation

interpretation

Page 50: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms

Stratego

NaBL TS

SDF3

ESV editor

SPT tests

50

syntax definition

concrete syntax

abstract syntax

static semantics

name binding

type system

dynamic semantics

translation

interpretation

Page 51: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 51

Spoofax testing

Page 52: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 52

module example-suite!language Tigerstart symbol Start!test name [[...]] parse succeeds!test another name [[...]] parse fails

test suites

Page 53: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 53

test cases syntax

Language

valid program

test valid program [[...]] parse succeeds

Page 54: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 54

test cases syntax

Language

test invalid program [[...]] parse fails

invalid program

Page 55: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 55

module syntax/identifiers!language Tiger start symbol Id!test single lower case [[x]] parse succeedstest single upper case [[X]] parse succeedstest single digit [[1]] parse fails!test single lc digit [[x1]] parse succeedstest single digit lc [[1x]] parse failstest single uc digit [[X1]] parse succeedstest single digit uc [[1X]] parse failstest double digit [[11]] parse fails

test cases syntax

Page 56: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms

Language

56

test cases syntax

Page 57: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 57

module syntax/expressions/precedence!language Tiger start symbol Exp!test parentheses [[(42)]] parse to [[42]]!test left-associative addition [[21 + 14 + 7]] parse to [[(21 + 14) + 7]]!test precedence multiplication [[3 * 7 + 21]] parse to [[(3 * 7) + 21]]

test cases ambiguities

Page 58: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 58

Spoofax editors

Page 59: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms

syntax definition

59

parse generate

parse table

checkgeneric parser

Page 60: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 60

source code

parse generate

machine code

check

Page 61: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 61

module Tiger.main imports ...!language General properties name: Tiger id: org.metaborg.cube.tiger extends: Root description: "Spoofax-generated editor for the Tiger language" url: http://metaborg.org extensions: tig table: include/Tiger.tbl start symbols: Start provider: include/Tiger.ctree observer: editor-analyze (multifile) on save: editor-save

editor specification

Page 62: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 62

syntax processing

Page 63: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 63

3 * +7 213 * +7 21

3 * +7 21

Exp ExpExp

Exp

Exp

scanner parser

language processors scanners & parsers

Page 64: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 64

language processors scannerless parsers

3 * +7 21

3 * +7 21

Exp ExpExp

Exp

Exp

parser

Page 65: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 65

syntax processing pretty-printing

Page 66: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 66

language processors compilation by transformation

backend

frontend

parse desugar analyse normalise

generate optimise format

Page 67: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 67

language processors pretty-printers

3 * +7 21

3 * +7 21

Exp ExpExp

Exp

Exp

pretty- printer

Page 68: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 68

from ASTs to text

keywords

layout: spaces, line breaks, indentation

specification

partially defined in grammar

missing layout

language processors pretty-printing

Page 69: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 69

SDF3 production rules

context-free syntax! Exp.Nil = "nil" Exp.IntC = INT Exp.StringC = STRING! Exp.Call = ID "(" {Exp ","}* ")"! Exp.ArrayI = TypeId "[" Exp "]" "of" Exp! Exp.RecordI = TypeId "{" {InitField ","}* "}" InitField.FieldI = ID "=" Exp

Page 70: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 70

SDF3 templates

context-free syntax! Exp.Nil = <nil> Exp.IntC = INT Exp.StringC = STRING ! Exp.Call = <<ID> ( <{Exp ","}*> )> ! Exp.ArrayI = <<TypeId> [ <Exp> ] of <Exp>> ! Exp.RecordI = <<TypeId> { <{InitField ","}*> }> InitField.FieldI = <<ID> = <Exp>>

Page 71: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 71

SDF3 formatted templates

context-free syntax! Exp.Nil = <nil> Exp.IntC = INT Exp.StringC = STRING Exp.Call = <<ID>(<{Exp ", "}*>)> Exp.ArrayI = <<TypeId>[<Exp>] of <Exp>> Exp.RecordI = < <TypeId> { <{InitField ",\n"}*> }> InitField.FieldI = <<ID> = <Exp>>

Page 72: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 72

box layout basic boxes

_1

“foo”

KW [ “foo” ]

Page 73: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 73

box layout horizontal boxes

B B B

H hs=x [ ]B B Bhs=x

Page 74: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 74

box layout vertical boxes

V hs=x is=i [ ]B B Bvs=y is=i

B

B

B

Page 75: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 75

syntax processing discovery

Page 76: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 76

discovery syntactic code completion

Page 77: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 77

SDF3 formatted templates

context-free syntax! Exp.Nil = <nil> Exp.IntC = INT Exp.StringC = STRING Exp.Call = <<ID>(<{Exp ", "}*>)> Exp.ArrayI = <<TypeId>[<Exp>] of <Exp>> Exp.RecordI = < <TypeId> { <{InitField ",\n"}*> }> InitField.FieldI = <<ID> = <Exp>>

Page 78: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 78

SDF3 generated completion templates

module src-gen/completions/Expressions-esv!completions completion template Exp : "nil" = "nil" ! completion template Exp : "ID()" = <ID:ID> "(" <:Exp> ")" ! completion template Exp : "TypeId[Exp] of Exp" = <TypeId:TypeId> "[" <Exp:Exp> "] of " <Exp:Exp> ! completion template Exp : "TypeId { }" = <TypeId:TypeId> " {\n\t" (cursor) "\n}" (blank) ! completion template InitField : "ID = Exp" = <ID:ID> " = " <Exp:Exp>

Page 79: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 79

SDF3 improved templates

context-free syntax! Exp.Nil = <nil> Exp.IntC = INT Exp.StringC = STRING Exp.Call = <<ID; text="m">(<{Exp ", "}*>)> Exp.ArrayI = <<TypeId; text="type">[<Exp; text="size">] of <Exp; text="value">> Exp.RecordI = < <TypeId; text="type"> { <{InitField ",\n"}*> }> InitField.FieldI = <<ID; text="var"> = <Exp; text="value">>

Page 80: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 80

syntax processing more editor services

Page 81: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 81

editor services

creating full featured IDEs for domain-specific languages

The Spoofax Language Workbench

Page 82: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 82

Spoofax generated highlighting rules

module Tiger-Colorer.generated!colorer Default, token-based highlighting keyword : 127 0 85 bold identifier : default string : blue number : darkgreen var : 255 0 100 italic operator : 0 0 128 layout : 100 100 0 italic !colorer System colors darkred = 128 0 0 red = 255 0 0 darkgreen = 0 128 0 green = 0 255 0 darkblue = 0 0 128 …

Page 83: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 83

Spoofax customised highlighting rules

module Tiger-Colorer!imports Tiger-Colorer.generated!colorer TU Delft colours!TUDlavender = 123 160 201!colorer token-based highlighting layout : TUDlavender StrConst : darkgreen TypeId : blue

Page 84: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 84

SDF3 generated folding rules

module Tiger-Folding.generated!folding Default folding definitions Exp Dec.TypeDec Type.RecordTy

Page 85: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 85

Spoofax customised folding rules

module Tiger-Folding!imports Tiger-Folding.generated!folding! Dec* Dec.FunDec

Page 86: Declarative Syntax Definition - SDF and ATerms

SDF3 and ATerms 86

Except where otherwise noted, this work is licensed under