SlideShare a Scribd company logo
Eelco Visser
IN4303 Compiler Construction
TU Delft
September 2017
Declare Your Language
Chapter 3: Syntactic (Editor) Services
2
Source
Code
Type Check
Machine
Code
Parse CodeGenOptimize
Declarative syntax definition
Syntax
Definition
ParserGen
Parse
Table
Derive parser from syntax definition
Lexical syntax
- deļ¬ning the syntax of tokens / terminals including layout

- making lexical syntax explicit

Formatting speciļ¬cation
- how to map (abstract syntax) trees to text

Syntactic completion
- proposing valid syntactic completions in an editor

Grammar transformation
- disambiguation by transformation

Parsing
- interpreting a syntax deļ¬nition to map text to trees
This Lecture
3
Reading Material
4
5
The SDF3 syntax definition
formalism is documented at
the metaborg.org website.
http://www.metaborg.org/en/latest/source/langdev/meta/lang/sdf3/index.html
6
The inverse of parsing is unparsing or
pretty-printing or formatting, i.e.
mapping a tree representation of a
program to a textual representation. A
plain context-free grammar can be used
as specification of an unparser.
However, then it is unclear where the
whitespace should go.
This paper extends context-free
grammars with templates that provide
hints for layout of program text when
formatting.
https://doi.org/10.1145/2427048.2427056
7
Syntax definitions cannot be used just
for parsing, but for many other
operations. This paper shows how
syntactic completion can be provided
generically given a syntax definition.
open access!
https://doi.org/10.1145/2427048.2427056
8
Chapter 2: Lexical Analysis
Chapter 3: Parsing
Chapter 4: Abstract Syntax
Next week: LR Parsing
Lexical Syntax
9
Context-Free Syntax vs Lexical Syntax
10
let function power(x: int, n: int): int =
if n <= 0 then 1
else x * power(x, n - 1)
in power(3, 10)
end
Mod(
Let(
[ FunDecs(
[ FunDec(
"power"
, [FArg("x", Tid("int")), FArg("n", Tid("int"))]
, Tid("int")
, If(
Leq(Var("n"), Int("0"))
, Int("1")
, Times(
Var("x")
, Call(
"power"
, [Var("x"), Minus(Var("n"), Int("1"))]
)
)
)
)
]
)
]
, [Call("power", [Int("3"), Int("10")])]
)
)
phrase structure
lexeme / token
separated by layout
structure not relevant
not separated by layout
Character Classes
11
lexical syntax // character codes
Character = [65]
Range = [65-90]
Union = [65-90] / [97-122]
Difference = [0-127] / [1013]
Union = [0-911-1214-255]
Character class represents choice from a set of characters
Sugar for Character Classes
12
lexical syntax // sugar
CharSugar = [a]
= [97]
CharClass = [abcdefghijklmnopqrstuvwxyz]
= [97-122]
SugarRange = [a-z]
= [97-122]
Union = [a-z] / [A-Z] / [0-9]
= [48-5765-9097-122]
RangeCombi = [a-z0-9_]
= [48-579597-122]
Complement = ~[nr]
= [0-255] / [1013]
= [0-911-1214-255]
Literals are Sequences of Characters
13
lexical syntax // literals
Literal = "then" // case sensitive sequence of characters
CaseInsensitive = 'then' // case insensitive sequence of characters
syntax
"then" = [116] [104] [101] [110]
'then' = [84116] [72104] [69101] [78110]
syntax
"then" = [t] [h] [e] [n]
'then' = [Tt] [Hh] [Ee] [Nn]
Identiļ¬ers
14
lexical syntax
Id = [a-zA-Z] [a-zA-Z0-9_]*
Id = a
Id = B
Id = cD
Id = xyz10
Id = internal_
Id = CamelCase
Id = lower_case
Id = ...
Lexical Ambiguity: Longest Match
15
lexical syntax
Id = [a-zA-Z] [a-zA-Z0-9_]*
context-free syntax
Exp.Var = Id
Exp.Call = Exp Exp {left} // curried function call
ab
Mod(
amb(
[Var("ab"),
Call(Var("a"), Var("b"))]
)
)
abc
Lexical Ambiguity: Longest Match
16
lexical syntax
Id = [a-zA-Z] [a-zA-Z0-9_]*
context-free syntax
Exp.Var = Id
Exp.Call = Exp Exp {left} // curried function call
Mod(
amb(
[ amb(
[ Var("abc")
, Call(
amb(
[Var("ab"), Call(Var("a"), Var("b"))]
)
, Var("c")
)
]
)
, Call(Var("a"), Var("bc"))
]
)
)
abc def ghi
Lexical Restriction => Longest Match
17
lexical syntax
Id = [a-zA-Z] [a-zA-Z0-9_]*
lexical restrictions
Id -/- [a-zA-Z0-9_] // longest match for identifiers
context-free syntax
Exp.Var = Id
Exp.Call = Exp Exp {left} // curried function call
Call(Call(Var("abc"), Var("def")), Var("ghi"))
Lexical restriction: phrase cannot be followed by character in character class
if def then ghi
Lexical Ambiguity: Keywords overlap with Identiļ¬ers
18
lexical syntax
Id = [a-zA-Z] [a-zA-Z0-9_]*
lexical restrictions
Id -/- [a-zA-Z0-9_] // longest match for identifiers
context-free syntax
Exp.Var = Id
Exp.Call = Exp Exp {left}
Exp.IfThen = "if" Exp "then" Exp
amb(
[ Mod(
Call(
Call(Call(Var("if"), Var("def")), Var("then"))
, Var("ghi")
)
)
, Mod(IfThen(Var("def"), Var("ghi")))
]
)
ifdef then ghi
Lexical Ambiguity: Keywords overlap with Identiļ¬ers
19
amb(
[ Mod(
Call(Call(Var("ifdef"), Var("then")), Var("ghi"))
)
, Mod(IfThen(Var("def"), Var("ghi")))
]
)
lexical syntax
Id = [a-zA-Z] [a-zA-Z0-9_]*
lexical restrictions
Id -/- [a-zA-Z0-9_] // longest match for identifiers
context-free syntax
Exp.Var = Id
Exp.Call = Exp Exp {left}
Exp.IfThen = "if" Exp "then" Exp
Reject Productions => Reserved Words
20
lexical syntax
Id = [a-zA-Z] [a-zA-Z0-9_]*
Id = ā€œif" {reject}
Id = "then" {reject}
lexical restrictions
Id -/- [a-zA-Z0-9_] // longest match for identifiers
"if" "then" -/- [a-zA-Z0-9_]
context-free syntax
Exp.Var = Id
Exp.Call = Exp Exp {left}
Exp.IfThen = "if" Exp "then" Exp
if def then ghi IfThen(Var("def"), Var("ghi"))
ifdef then ghi
Reject Productions => Reserved Words
21
parse error
lexical syntax
Id = [a-zA-Z] [a-zA-Z0-9_]*
Id = ā€œif" {reject}
Id = "then" {reject}
lexical restrictions
Id -/- [a-zA-Z0-9_] // longest match for identifiers
"if" "then" -/- [a-zA-Z0-9_]
context-free syntax
Exp.Var = Id
Exp.Call = Exp Exp {left}
Exp.IfThen = "if" Exp "then" Exp
Real Numbers
22
lexical syntax
RealConst.RealConstNoExp = IntConst "." IntConst
RealConst.RealConst = IntConst "." IntConst "e" Sign IntConst
Sign = "+"
Sign = "-"
Tiger Lexical Syntax
23
Tiger Lexical Syntax: Identiļ¬ers
24
module Identifiers
lexical syntax
Id = [a-zA-Z] [a-zA-Z0-9_]*
lexical restrictions
Id -/- [a-zA-Z0-9_]
Tiger Lexical Syntax: Number Literals
25
module Numbers
lexical syntax
IntConst = [0-9]+
lexical syntax
RealConst.RealConstNoExp = IntConst "." IntConst
RealConst.RealConst = IntConst "." IntConst "e" Sign IntConst
Sign = "+"
Sign = "-"
context-free syntax
Exp.Int = IntConst
Tiger Lexical Syntax: String Literals
26
module Strings
sorts StrConst
lexical syntax
StrConst = """ StrChar* """
StrChar = ~["n]
StrChar = [] [n]
StrChar = [] [t]
StrChar = [] [^] [A-Z]
StrChar = [] [0-9] [0-9] [0-9]
StrChar = [] ["]
StrChar = [] []
StrChar = [] [ tn]+ []
context-free syntax // records
Exp.String = StrConst
Tiger Lexical Syntax: Whitespace
27
module Whitespace
lexical syntax
LAYOUT = [ tnr]
context-free restrictions
// Ensure greedy matching for comments
LAYOUT? -/- [ tnr]
LAYOUT? -/- [/].[/]
LAYOUT? -/- [/].[*]
syntax
LAYOUT-CF = LAYOUT-LEX
LAYOUT-CF = LAYOUT-CF LAYOUT-CF {left}
Implicit composition of layout
Tiger Lexical Syntax: Comment
28
lexical syntax
CommentChar = [*]
LAYOUT = "/*" InsideComment* "*/"
InsideComment = ~[*]
InsideComment = CommentChar
lexical restrictions
CommentChar -/- [/]
context-free restrictions
LAYOUT? -/- [/].[*]
lexical syntax
LAYOUT = SingleLineComment
SingleLineComment = "//" ~[nr]* NewLineEOF
NewLineEOF = [nr]
NewLineEOF = EOF
EOF =
lexical restrictions
EOF -/- ~[]
context-free restrictions
LAYOUT? -/- [/].[/]
Desugaring Lexical Syntax
29
Core language
- context-free grammar productions

- with constructors

- only character classes as terminals

- explicit deļ¬nition of layout

Desugaring
- express lexical syntax in terms of character classes

- explicate layout between context-free syntax symbols

- separate lexical and context-free syntax non-terminals
Explication of Lexical Syntax
30
Explication of Layout by Transformation
31
context-free syntax
Exp.Int = IntConst
Exp.Uminus = "-" Exp
Exp.Times = Exp "*" Exp {left}
Exp.Divide = Exp "/" Exp {left}
Exp.Plus = Exp "+" Exp {left}
syntax
Exp-CF.Int = IntConst-CF
Exp-CF.Uminus = "-" LAYOUT?-CF Exp-CF
Exp-CF.Times = Exp-CF LAYOUT?-CF "*" LAYOUT?-CF Exp-CF {left}
Exp-CF.Divide = Exp-CF LAYOUT?-CF "/" LAYOUT?-CF Exp-CF {left}
Exp-CF.Plus = Exp-CF LAYOUT?-CF "+" LAYOUT?-CF Exp-CF {left}
Symbols in context-free
syntax are implicitly
separated by optional
layout
Separation of Lexical and Context-free Syntax
32
syntax
Id-LEX = [65-9097-122] [48-5765-909597-122]*-LEX
Id-LEX = "if" {reject}
Id-LEX = "then" {reject}
Id-CF = Id-LEX
Exp-CF.Var = Id-CF
lexical syntax
Id = [a-zA-Z] [a-zA-Z0-9_]*
Id = ā€œif" {reject}
Id = "then" {reject}
context-free syntax
Exp.Var = Id
33
lexical syntax
Id = [a-zA-Z] [a-zA-Z0-9_]*
Id = ā€œif" {reject}
Id = "then" {reject}
lexical restrictions
Id -/- [a-zA-Z0-9_]
"if" "then" -/- [a-zA-Z0-9_]
context-free syntax
Exp.Var = Id
Exp.Call = Exp Exp {left}
Exp.IfThen = "if" Exp "then" Exp
syntax
"if" = [105] [102]
"then" = [116] [104] [101] [110]
[48-5765-909597-122]+-LEX = [48-5765-909597-122]
[48-5765-909597-122]+-LEX = [48-5765-909597-122]+-LEX [48-5765-909597-122]
[48-5765-909597-122]*-LEX =
[48-5765-909597-122]*-LEX = [48-5765-909597-122]+-LEX
Id-LEX = [65-9097-122] [48-5765-909597-122]*-LEX
Id-LEX = "if" {reject}
Id-LEX = "then" {reject}
Id-CF = Id-LEX
Exp-CF.Var = Id-CF
Exp-CF.Call = Exp-CF LAYOUT?-CF Exp-CF {left}
Exp-CF.IfThen = "if" LAYOUT?-CF Exp-CF LAYOUT?-CF "then" LAYOUT?-CF Exp-CF
LAYOUT-CF = LAYOUT-CF LAYOUT-CF {left}
LAYOUT?-CF = LAYOUT-CF
LAYOUT?-CF =
restrictions
Id-LEX -/- [48-5765-909597-122]
"if" -/- [48-5765-909597-122]
"then" -/- [48-5765-909597-122]
priorities
Exp-CF.Call left Exp-CF.Call,
LAYOUT-CF = LAYOUT-CF LAYOUT-CF left LAYOUT-CF = LAYOUT-CF LAYOUT-CF
separate lexical
and context-free
syntax
separate context-
free symbols by
optional layout
character classes
as only terminals
Give an example syntax deļ¬nition that
demonstrates the need for separating lexical and
context-free syntax
Explain what goes wrong in the absence of that
separation
Exam Question
34
Syntactic (Editor) Services
35
What can we do with a syntax deļ¬nition?
Editor Services
36
The Spoofax Language Workbench
Editor Services
37
Source
Code
Editor
Parse
Feedback
& Operations
Abstract
Syntax
Tree
Feedback

- syntax coloring

- syntax checking

- outline view
Operations

- syntactic completion

- formatting

- abstract syntax tree
Language Project Conļ¬guration (ESV)
38
module Main
imports
Syntax
Analysis
language
extensions : tig
//provider : target/metaborg/stratego.ctree
provider : target/metaborg/stratego.jar
provider : target/metaborg/stratego-javastrat.jar
menus
menu: "Transform" (openeditor) (realtime)
action: "Desugar" = editor-desugar (source)
action: "Desugar AST" = editor-desugar-ast (source)
Conļ¬guration of Syntactic Services (ESV)
39
module Syntax
imports
libspoofax/color/default
completion/colorer/Tiger-cc-esv
language
table : target/metaborg/sdf-new.tbl
//table : target/metaborg/sdf.tbl
start symbols : Module
line comment : "//"
block comment : "/*" * "*/"
fences : [ ] ( ) { }
menus
menu: "Syntax" (openeditor)
action: "Format" = editor-format (source)
action: "Show parsed AST" = debug-show-aterm (source)
views
outline view: editor-outline (source)
expand to level: 3
Syntax Coloring
40
Generated Syntax Coloring
41
module libspoofax/color/default
imports
libspoofax/color/colors
colorer // Default, token-based highlighting
keyword : 127 0 85 bold
identifier : default
string : blue
number : darkgreen
var : 139 69 19 italic
operator : 0 0 128
layout : 63 127 95 italic
// compute the n-th fibonacci number
let function fib(n: int): int =
if n <= 1 then 1
else fib(n - 1) + fib(n - 2)
in fib(10)
end
Customized Syntax Coloring
42
module Tiger-Colorer
colorer
red = 255 0 0
green = 0 255 0
blue = 0 0 255
TUDlavender = 123 160 201
colorer token-based highlighting
keyword : red
Id : TUDlavender
StrConst : darkgreen
TypeId : blue
layout : green
// compute the n-th fibonacci number
let function fib(n: int): int =
if n <= 1 then 1
else fib(n - 1) + fib(n - 2)
in fib(10)
end
From Unparsers
to Pretty-Printers
with Templates
43
Language = Sentences and Trees
44
parse
format
Unparsing: From Abstract Syntax Term to Text
45
rules
unparse :
Int(x) -> x
unparse :
Plus(e1, e2) -> $[[<unparse> e1] + [<unparse> e2]]
unparse :
Times(e1, e2) -> $[[<unparse> e1] * [<unparse> e2]]
Mod(
Plus(
Int("1")
, Times(
Int("2")
, Plus(Int("3"), Int("4"))
)
)
)
1 + 2 * 3 + 4
take priorities into account!
context-free syntax
Exp.Int = IntConst
Exp.Times = [[Exp] * [Exp]] {left}
Exp.Plus = [[Exp] + [Exp]] {left}
From ASTs to text
- insert keywords

- insert layout: spaces, line breaks, indentation

- insert parentheses to preserve tree structure

Unparser
- derive transformation rules from context-free grammar

- keywords, literals deļ¬ned in grammar productions

- parentheses determined by priority, associativity rules

- separate all symbols by a space => not pretty, or even readable

Pretty-printer
- introduce spaces, line breaks, and indentation to produce readable text

- doing that manually is tedious
Pretty-Printing
46
Specifying Formatting Layout with Templates
47
context-free syntax
Exp.Seq = <
(
<{Exp ";n"}*>
)
>
Exp.If = <
if <Exp> then
<Exp>
else
<Exp>
>
Exp.IfThen = <
if <Exp> then
<Exp>
>
Exp.While = <
while <Exp> do
<Exp>
>
Inverse quotation

- template quotes literal text with <>

- anti-quotations insert non-terminals with <>
Layout directives

- whitespace (linebreaks, indentation, spaces)
in template guides formatting

- is interpreted as LAYOUT? for parsing
Formatter generation

- generate rules for mapping AST to text (via
box expressions)
Applications

- code generation; pretty-printing generated
AST

- syntactic completions

- formatting
Templates for Tiger: Binary Expressions
48
context-free syntax
Exp.Int = IntConst
Exp.Uminus = [- [Exp]]
Exp.Times = [[Exp] * [Exp]] {left}
Exp.Divide = [[Exp] / [Exp]] {left}
Exp.Plus = [[Exp] + [Exp]] {left}
Exp.Minus = [[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.Geq = [[Exp] >= [Exp]] {non-assoc}
Exp.Leq = [[Exp] <= [Exp]] {non-assoc}
Exp.And = [[Exp] & [Exp]] {left}
Exp.Or = [[Exp] | [Exp]] {left}
Use [] quotes instead of
<> to avoid clash with
comparison operators
Templates for Tiger: Functions
49
context-free syntax
Dec.FunDecs = <<{FunDec "n"}+>> {longest-match}
FunDec.ProcDec = <
function <Id>(<{FArg ", "}*>) =
<Exp>
>
FunDec.FunDec = <
function <Id>(<{FArg ", "}*>) : <Type> =
<Exp>
>
FArg.FArg = <<Id> : <Type>>
Exp.Call = <<Id>(<{Exp ", "}*>)>
No space after function name in call
Space after comma!
Function declarations
separated by newline
Indent body
of function
Templates for Tiger: Bindings and Records
50
context-free syntax
Exp.Let = <
let
<{Dec "n"}*>
in
<{Exp ";n"}*>
end
>
context-free syntax // records
Type.RecordTy = <
{
<{Field ", n"}*>
}
>
Field.Field = <<Id> : <TypeId>>
Exp.NilExp = <nil>
Exp.Record = <<TypeId>{ <{InitField ", "}*> }>
InitField.InitField = <<Id> = <Exp>>
LValue.FieldVar = <<LValue>.<Id>>
Note spacing / layout in separators
Generating Pretty-Print Rules from Template Productions
51
context-free syntax
FunDec.FunDec = <
function <Id>(<{FArg ", "}*>) : <Type> =
<Exp>
>
rules
prettyprint-Tiger-FunDec :
ProcDec(t1__, t2__, t3__) -> [ H(
[SOpt(HS(), "0")]
, [ S("function ")
, t1__
, S("(")
, t2__'
, S(") =")
]
)
, t3__'
]
with t1__' := <pp-one-Z(prettyprint-Tiger-Id) <+ pp-one-Z(prettyprint-completion-aux)> t1__
with t2__' := <pp-H-list(prettyprint-Tiger-FArg|", ")
<+ pp-one-Z(prettyprint-completion-aux)> t2__
with t3__' := <pp-indent(|"2")> [ <pp-one-Z(prettyprint-Tiger-Exp) <+ pp-one-Z(prettyprint-completion-aux)> t3__
]
Separation of concerns: 

- generated formatter transforms AST to Box

- Box formatter produces text
Boxes for Formatting
52
_1
ā€œfooā€
KW [ ā€œfooā€ ]
literal text, keywords, parameters
Horizontal Layout
53
B B B
H hs=x [ ]B B Bhs=x
hs: horizontal space between boxes
Vertical Layout
54
V hs=x is=i [ ]B B Bvs=y is=i
B
B
B
vs: vertical space between boxes; is: indentation space
Tiger Syntax Deļ¬nition
with Templates
55
Tiger Syntax: Composition
module Tiger
imports Whitespace
imports Comments
imports Types
imports Identifiers
imports Bindings
imports Variables
imports Functions
imports Numbers
imports Strings
imports Records
imports Arrays
imports Control-Flow
context-free start-symbols Module
context-free syntax
Module.Mod = Exp
context-free priorities
Exp.Or > Exp.Array > Exp.Assign ,
{Exp.Uminus LValue.FieldVar LValue.Subscript}
> {left : Exp.Times Exp.Divide}
Tiger Syntax: Identiļ¬ers and Strings
module Identifiers
lexical syntax
Id = [a-zA-Z] [a-zA-Z0-9_]*
lexical restrictions
Id -/- [a-zA-Z0-9_]
lexical syntax
Id = "nil" {reject}
Id = "let" {reject}
Id = ā€¦ {reject}
module Strings
sorts StrConst
lexical syntax
StrConst = """ StrChar* """
StrChar = ~["n]
StrChar = [] [n]
StrChar = [] [t]
StrChar = [] [^] [A-Z]
StrChar = [] [0-9] [0-9] [0-9]
StrChar = [] ["]
StrChar = [] []
StrChar = [] [ tn]+ []
context-free syntax // records
Exp.String = StrConst
Tiger Syntax: Whitespace
module Whitespace
lexical syntax
LAYOUT = [ tnr]
context-free restrictions
LAYOUT? -/- [ tnr]
Tiger Syntax: Comments
module Comments
lexical syntax // multiline comments
CommentChar = [*]
LAYOUT = "/*" InsideComment* "*/"
InsideComment = ~[*]
InsideComment = CommentChar
lexical restrictions
CommentChar -/- [/]
context-free restrictions
LAYOUT? -/- [/].[/]
lexical syntax // single line comments
LAYOUT = "//" ~[nr]* NewLineEOF
NewLineEOF = [nr]
NewLineEOF = EOF
EOF =
// end of file since it cannot be followed by any character
// avoids the need for a newline to close a single line comment
// at the last line of a file
lexical restrictions
EOF -/- ~[]
context-free restrictions
LAYOUT? -/- [/].[*]
Tiger Syntax: Numbers
module Numbers
lexical syntax
IntConst = [0-9]+
context-free syntax
Exp.Int = IntConst
Exp.Uminus = [- [Exp]]
Exp.Times = [[Exp] * [Exp]] {left}
Exp.Divide = [[Exp] / [Exp]] {left}
Exp.Plus = [[Exp] + [Exp]] {left}
Exp.Minus = [[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.Geq = [[Exp] >= [Exp]] {non-assoc}
Exp.Leq = [[Exp] <= [Exp]] {non-assoc}
Exp.And = [[Exp] & [Exp]] {left}
Exp.Or = [[Exp] | [Exp]] {left}
context-free priorities
{Exp.Uminus}
> {left :
Exp.Times
Exp.Divide}
> {left :
Exp.Plus
Exp.Minus}
> {non-assoc :
Exp.Eq
Exp.Neq
Exp.Gt
Exp.Lt
Exp.Geq
Exp.Leq}
> Exp.And
> Exp.Or
Tiger Syntax: Variables and Functions
module Bindings
imports Control-Flow
imports Identifiers
imports Types
imports Functions
imports Variables
sorts Declarations
context-free syntax
Exp.Let = <
let
<{Dec "n"}*>
in
<{Exp ";n"}*>
end
>
Declarations.Declarations = <
declarations <{Dec "n"}*>
>
module Variables
imports Identifiers
imports Types
sorts Var
context-free syntax
Dec.VarDec = <var <Id> : <Type> := <Exp>>
Dec.VarDecNoType = <var <Id> := <Exp>>
Var.Var = Id
LValue = Var
Exp = LValue
Exp.Assign = <<LValue> := <Exp>>
module Functions
imports Identifiers
imports Types
context-free syntax
Dec.FunDecs = <<{FunDec "n"}+>> {longest-match}
FunDec.ProcDec = <
function <Id>(<{FArg ", "}*>) =
<Exp>
>
FunDec.FunDec = <
function <Id>(<{FArg ", "}*>) : <Type> =
<Exp>
>
FArg.FArg = <<Id> : <Type>>
Exp.Call = <<Id>(<{Exp ", "}*>)>
Tiger Syntax: Records, Arrays, Types
module Records
imports Base
imports Identifiers
imports Types
context-free syntax // records
Type.RecordTy = <
{
<{Field ", n"}*>
}
>
Field.Field = <<Id> : <TypeId>>
Exp.NilExp = <nil>
Exp.Record = <<TypeId>{ <{InitField ", "}*> }>
InitField.InitField = <<Id> = <Exp>>
LValue.FieldVar = <<LValue>.<Id>>
module Arrays
imports Types
context-free syntax // arrays
Type.ArrayTy = <array of <TypeId>>
Exp.Array = <<TypeId>[<Exp>] of <Exp>>
LValue.Subscript = <<LValue>[<Index>]>
Index = Exp
module Types
imports Identifiers
imports Bindings
sorts Type
context-free syntax // type declarations
Dec.TypeDecs = <<{TypeDec "n"}+>> {longest-match}
TypeDec.TypeDec = <type <Id> = <Type>>
context-free syntax // type expressions
Type = TypeId
TypeId.Tid = Id
sorts Ty
context-free syntax // semantic types
Ty.INT = <INT>
Ty.STRING = <STRING>
Ty.NIL = <NIL>
Ty.UNIT = <UNIT>
Ty.NAME = <NAME <Id>>
Ty.RECORD = <RECORD <Id>>
Ty.ARRAY = <ARRAY <Ty> <Id>>
Ty.FUN = <FUN ( <{Ty ","}*> ) <Ty>>
Syntactic Completion
63
Amorim, Erdweg, Wachsmuth, Visser

Principled syntactic code completion using placeholders.
In ACM SIGPLAN International Conference on Software Language Engineering [SLE 2016].
64
*S. Amann, S. Proksch, S. Nadi, and M. Mezini. A study
of visual studio usage in practice. In SANER, 2016.
65
Syntactic Completion
Semantic Completion
Problems
66
Ad-hoc: re-implement for each language / IDE
Incomplete: not all programs reachableUnsound: propose invalid constructs
Goal
67
Sound and Complete
Syntactic Code Completion
from Syntax Deļ¬nition
Idea 1
68
Explicit
Placeholders
69
Incomplete
programs
70
Incomplete
programs
Make incompleteness valid using placeholders!
71
72
73
74
75
4
2
3
1
Deriving Syntactic Completion from Syntax Deļ¬nition
76
context-free syntax // regular production
Statement.If = "if" "(" Exp ")" Statement "else" Statement
Placeholders as Language Constructs
77
context-free syntax // placeholder rule
Statement.Statement-Plhdr = ā€œ$Statement"
context-free syntax // regular production
Statement.If = "if" "(" Exp ")" Statement "else" Statement
Calculate Placeholder Expansions
78
context-free syntax // regular production
Statement.If = "if" "(" Exp ")" Statement "else" Statement
Stm-Plhdr
If
Exp-Plhdr Stm-Plhdr
Stm-Plhdr
context-free syntax // placeholder rule
Statement.Statement-Plhdr = ā€œ$Statement"
rules
rewrite-placeholder:
Statement-Plhdr() -> If(Exp-Plhdr(), Statement-Plhdr(),
Statement-Plhdr())
Calculate Placeholder Expansions
79
context-free syntax // regular production
Statement.If = "if" "(" Exp ")" Statement "else" Statement
context-free syntax // placeholder rule
Statement.Statement-Plhdr = ā€œ$Statement"
rules
rewrite-placeholder:
Statement-Plhdr() -> If(Exp-Plhdr(), Statement-Plhdr(),
Statement-Plhdr())
80
Incomplete
programs
Complete
programs
Expand
placeholder
Expand/overwrite
placeholders
Correct
programs
81
Complete
programs
How to expand a complete program?
82
Insert a placeholder?
How to expand a complete program?
Idea 2
83
Inferring
Placeholders
Placeholder Inference
84
Optional Parent
List of Statements
Add Optional Element
No source region
Add ļ¬rst or last element
Add Element to List
Placeholder Inference: Optional
ClassDecl
class ā€œAā€
{NoParent ConsMethod
[ā€¦]
}NilField
85
Placeholder Inference: Optional
86
ClassDecl
class ā€œAā€
{NoParent ConsMethod
[ā€¦]
}NilField
Placeholder Inference: Optional
87
ClassDecl
class ā€œAā€
{NoParent ConsMethod
[ā€¦]
}NilField
Placeholder Inference: Optional
88
89
Placeholder Inference - Lists
[ā€¦]
[ā€¦]
Method
Cons
VarDecl
AssignInt ā€œxā€
VarRef
ā€œxā€
Add
Int
21
Cons
Nil
{ return
[ā€¦]
int ;
Int
=
21
90
Placeholder Inference - Lists
[ā€¦]
[ā€¦]
Method
Cons
VarDecl
AssignInt ā€œxā€
VarRef
ā€œxā€
Add
Int
21
Cons
Nil
{ return
[ā€¦]
int ;
Int
=
21
91
Placeholder Inference - Lists
[ā€¦]
[ā€¦]
Method
Cons
VarDecl
AssignInt ā€œxā€
VarRef
ā€œxā€
Add
Int
21
Cons
Nil
{ return
[ā€¦]
int ;
Int
=
21
92
Placeholder Inference - Lists
[ā€¦]
[ā€¦]
Method
Cons
VarDecl
AssignInt ā€œxā€
VarRef
ā€œxā€
Add
Int
21
Cons
Nil
{ return
[ā€¦]
int ;
Int
=
21
93
94
Incomplete
programs
Complete
programs
Expand
placeholder
Infer
placeholder
Correct
programs
Infer
placeholder
Expand/overwrite
placeholders
95
Incorrect
programs
Idea 3
96
Error Recovery
97
Insertion Rules
98
context-free syntax //regular syntax rules
Statement.VarDecl = <<Type> <ID>;>
Statement.Assign = <<VarRef> = <Exp>;>
// derived insertion rules for placeholders
context-free syntax
Type.Type-Plhdr = {symbol-insertion}
ID.ID-Plhdr = {symbol-insertion}
Statement.Statement-Plhdr = {symbol-insertion}
VarRef.VarRef-Plhdr = {symbol-insertion}
Exp.Exp-Plhdr = {symbol-insertion}
// derived insertion rules for literals
lexical syntax
"=" = {symbol-completion}
";" = {symbol-completion}
Empty productions
Apply Insertion Rules at Cursor
99
Proposal nodes
Insertion nodes
[ā€¦]
[ā€¦]
Stmt*
amb
VarDecl
ClassType
ā€œxā€
VarDecl
[ā€¦]
Assign
ID-Plhdr ;
Assign
VarRef
ā€œxā€
Exp-Plhdr ;
=
Limit Search Space
100
Exp-Plhdr Exp-Plhdr
+
Assign
VarRef
ā€œxā€
;
=
[ā€¦]
Add
Assign
VarRef
ā€œxā€
;=
[ā€¦]
Exp-Plhdr
Use the simplest possible expansions
Greedy Recovery
101
[ā€¦]
[ā€¦]
Stmt*
amb
VarDecl
ClassType
ā€œxā€
VarDecl
[ā€¦]
Assign
ID-Plhdr ;
Assign
VarRef
ā€œxā€
Exp-Plhdr ;
=
Include postļ¬x in recovery proposal
Nested Proposal Nodes
102
IntValue Exp-Plhdr+
Assign
VarRef
ā€œxā€
;=
[ā€¦]
Add
1
103
Incorrect
programs
Incomplete
programs
Complete
programs
Correct
programs
104
Incorrect
programs
Incomplete
programs
Complete
programs
Expand
placeholder
Correct
programs
Expand/overwrite
placeholders
105
Incorrect
programs
Incomplete
programs
Complete
programs
Expand
placeholder
Infer
placeholder
Correct
programs
Infer
placeholder
Expand/overwrite
placeholders
106
Incorrect
programs
Incomplete
programs
Complete
programs
Expand
placeholder
Infer
placeholder
Correct
programs
Infer
placeholder
Expand/overwrite
placeholders
Recover
incomplete
program
Recover
complete
program
107
Syntax Deļ¬nition: Summary
108
Representation
- Standardized representation for <aspect> of programs

- Independent of speciļ¬c object language

Speciļ¬cation Formalism
- Language-speciļ¬c declarative rules

- Abstract from implementation concerns

Language-Independent Interpretation
- Formalism interpreted by language-independent algorithm

- Multiple interpretations for diļ¬€erent purposes

- Reuse between implementations of diļ¬€erent languages
Separation of Concerns in Declarative Language Deļ¬nition
109
Context-free grammars
- well-understood mathematical basis

- generation of valid sentences of language

- derivation corresponds to phrase structure

Character-level grammars
- terminals are characters

- lexical syntax deļ¬ned using same (CFG) formalism

Declarative disambiguation
- associativity and priority declarations for phrase structure ambiguities

- follow restrictions and reject productions for lexical disambiguation

- not discussed: layout constraints for modeling layout sensitive languages

Structure
- abstract syntax tree schema deļ¬ned in grammar using constructors

Formatting
- mapping from abstract syntax to text deļ¬ned using template production

Services
- automatically derived: coloring, formatting, completion

- to do: parsing
Declarative Syntax Deļ¬nition
110
Representation: (Abstract Syntax) Trees
- Standardized representation for structure of programs

- Basis for syntactic and semantic operations

Formalism: Syntax Deļ¬nition
- Productions + Constructors + Templates + Disambiguation

- Language-speciļ¬c rules: structure of each language construct

Language-Independent Interpretation
- Well-formedness of abstract syntax trees

ā€£ provides declarative correctness criterion for parsing

- Parsing algorithm

ā€£ No need to understand parsing algorithm

ā€£ Debugging in terms of representation

- Formatting based on layout hints in grammar

- Syntactic completion
Declarative Syntax Deļ¬nition
111
A meta-
language for
talking about
syntax
}
Syntax of a Syntax
Deļ¬nition Formalism
112
Bootstrapping
Bootstrapping: SDF3 in SDF3
113
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>>
Exam Question
114
Exam Question
115
Consider the following SDF3 syntax deļ¬nition and assume a standard deļ¬nition for identiļ¬ers ID: 

context-free syntax
E.Var = ID
E.Add = E "+" E
E.App = E E
E.Fun = "" ID "." "{" E "}"
E = "(" E ")" {bracket}
(a) Demonstrate that this grammar is ambiguous by giving two diļ¬€erent abstract syntax trees
using term notation based on the constructors in the grammar for the sentence f x + x and the
left-most derivations that give rise to these trees.

(b) Disambiguate the syntax deļ¬nition by means of priority and associativity declarations, i.e.
without (otherwise) changing the productions. Motivate your choice of disambiguation rules. Is
your disambiguation complete? 

(c) Translate the disambiguated syntax deļ¬nition into an unambiguous one without separate
disambiguation rules. The syntax deļ¬nition should accept the same language and produce the
same abstract syntax trees.
116
Except where otherwise noted, this work is licensed under

More Related Content

What's hot

Term Rewriting
Term RewritingTerm Rewriting
Term Rewriting
Eelco Visser
Ā 
Dynamic Semantics
Dynamic SemanticsDynamic Semantics
Dynamic Semantics
Eelco Visser
Ā 
Declare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionDeclare Your Language: Syntax Definition
Declare Your Language: Syntax Definition
Eelco Visser
Ā 
Static name resolution
Static name resolutionStatic name resolution
Static name resolution
Eelco Visser
Ā 
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error Checking
Guido Wachsmuth
Ā 
Formal Grammars
Formal GrammarsFormal Grammars
Formal Grammars
Eelco Visser
Ā 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Guido Wachsmuth
Ā 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
Eelco Visser
Ā 
Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing
Eelco Visser
Ā 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and Trees
Guido Wachsmuth
Ā 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Eelco Visser
Ā 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type Constraints
Eelco Visser
Ā 
LL Parsing
LL ParsingLL Parsing
LL Parsing
Eelco Visser
Ā 
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingCS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
Eelco Visser
Ā 
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingCS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | Parsing
Eelco Visser
Ā 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
Guido Wachsmuth
Ā 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint Resolution
Eelco Visser
Ā 
Compiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical AnalysisCompiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical Analysis
Guido Wachsmuth
Ā 
Compiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsCompiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing Algorithms
Guido Wachsmuth
Ā 
Declare Your Language (at DLS)
Declare Your Language (at DLS)Declare Your Language (at DLS)
Declare Your Language (at DLS)
Eelco Visser
Ā 

What's hot (20)

Term Rewriting
Term RewritingTerm Rewriting
Term Rewriting
Ā 
Dynamic Semantics
Dynamic SemanticsDynamic Semantics
Dynamic Semantics
Ā 
Declare Your Language: Syntax Definition
Declare Your Language: Syntax DefinitionDeclare Your Language: Syntax Definition
Declare Your Language: Syntax Definition
Ā 
Static name resolution
Static name resolutionStatic name resolution
Static name resolution
Ā 
Declarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error CheckingDeclarative Semantics Definition - Static Analysis and Error Checking
Declarative Semantics Definition - Static Analysis and Error Checking
Ā 
Formal Grammars
Formal GrammarsFormal Grammars
Formal Grammars
Ā 
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and RegainedPure and Declarative Syntax Definition: Paradise Lost and Regained
Pure and Declarative Syntax Definition: Paradise Lost and Regained
Ā 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
Ā 
Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing
Ā 
Declarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and TreesDeclarative Syntax Definition - Grammars and Trees
Declarative Syntax Definition - Grammars and Trees
Ā 
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term RewritingCompiler Construction | Lecture 5 | Transformation by Term Rewriting
Compiler Construction | Lecture 5 | Transformation by Term Rewriting
Ā 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type Constraints
Ā 
LL Parsing
LL ParsingLL Parsing
LL Parsing
Ā 
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term RewritingCS4200 2019 | Lecture 5 | Transformation by Term Rewriting
CS4200 2019 | Lecture 5 | Transformation by Term Rewriting
Ā 
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingCS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | Parsing
Ā 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
Ā 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint Resolution
Ā 
Compiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical AnalysisCompiler Components and their Generators - Lexical Analysis
Compiler Components and their Generators - Lexical Analysis
Ā 
Compiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing AlgorithmsCompiler Components and their Generators - Traditional Parsing Algorithms
Compiler Components and their Generators - Traditional Parsing Algorithms
Ā 
Declare Your Language (at DLS)
Declare Your Language (at DLS)Declare Your Language (at DLS)
Declare Your Language (at DLS)
Ā 

Similar to Declare Your Language: Syntactic (Editor) Services

Compiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor ServicesCompiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor Services
Eelco Visser
Ā 
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic Services
Eelco Visser
Ā 
Advanced Regular Expressions Redux
Advanced Regular Expressions ReduxAdvanced Regular Expressions Redux
Advanced Regular Expressions Redux
Jakub Nesetril
Ā 
CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptx
Obsa2
Ā 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
Ben Brumfield
Ā 
Bound
BoundBound
Bound
Edward Kmett
Ā 
Mikhail Khristophorov "Introduction to Regular Expressions"
Mikhail Khristophorov "Introduction to Regular Expressions"Mikhail Khristophorov "Introduction to Regular Expressions"
Mikhail Khristophorov "Introduction to Regular Expressions"
LogeekNightUkraine
Ā 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentationarnolambert
Ā 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentationarnolambert
Ā 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
sana mateen
Ā 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
sana mateen
Ā 
4. languages and grammars
4. languages and grammars4. languages and grammars
4. languages and grammars
Saeed Parsa
Ā 
2016 bioinformatics i_python_part_3_io_and_strings_wim_vancriekinge
2016 bioinformatics i_python_part_3_io_and_strings_wim_vancriekinge2016 bioinformatics i_python_part_3_io_and_strings_wim_vancriekinge
2016 bioinformatics i_python_part_3_io_and_strings_wim_vancriekinge
Prof. Wim Van Criekinge
Ā 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Brij Kishore
Ā 

Similar to Declare Your Language: Syntactic (Editor) Services (20)

Compiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor ServicesCompiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor Services
Ā 
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic Services
Ā 
Regexp
RegexpRegexp
Regexp
Ā 
Advanced Regular Expressions Redux
Advanced Regular Expressions ReduxAdvanced Regular Expressions Redux
Advanced Regular Expressions Redux
Ā 
CH 2.pptx
CH 2.pptxCH 2.pptx
CH 2.pptx
Ā 
Introduction to regular expressions
Introduction to regular expressionsIntroduction to regular expressions
Introduction to regular expressions
Ā 
Bound
BoundBound
Bound
Ā 
Mikhail Khristophorov "Introduction to Regular Expressions"
Mikhail Khristophorov "Introduction to Regular Expressions"Mikhail Khristophorov "Introduction to Regular Expressions"
Mikhail Khristophorov "Introduction to Regular Expressions"
Ā 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
Ā 
Regex Presentation
Regex PresentationRegex Presentation
Regex Presentation
Ā 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
Ā 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
Ā 
Reg EX
Reg EXReg EX
Reg EX
Ā 
4. languages and grammars
4. languages and grammars4. languages and grammars
4. languages and grammars
Ā 
perl-pocket
perl-pocketperl-pocket
perl-pocket
Ā 
perl-pocket
perl-pocketperl-pocket
perl-pocket
Ā 
perl-pocket
perl-pocketperl-pocket
perl-pocket
Ā 
perl-pocket
perl-pocketperl-pocket
perl-pocket
Ā 
2016 bioinformatics i_python_part_3_io_and_strings_wim_vancriekinge
2016 bioinformatics i_python_part_3_io_and_strings_wim_vancriekinge2016 bioinformatics i_python_part_3_io_and_strings_wim_vancriekinge
2016 bioinformatics i_python_part_3_io_and_strings_wim_vancriekinge
Ā 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Ā 

More from Eelco Visser

CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
Eelco Visser
Ā 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: Introduction
Eelco Visser
Ā 
A Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation RulesA Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation Rules
Eelco Visser
Ā 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
Eelco Visser
Ā 
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler ConstructionCompiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Eelco Visser
Ā 
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Eelco Visser
Ā 
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementCompiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory Management
Eelco Visser
Ā 
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersCompiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | Interpreters
Eelco Visser
Ā 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code Generation
Eelco Visser
Ā 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual Machines
Eelco Visser
Ā 
Compiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone FrameworksCompiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone Frameworks
Eelco Visser
Ā 
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisCompiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow Analysis
Eelco Visser
Ā 
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type Checking
Eelco Visser
Ā 
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static AnalysisCompiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Eelco Visser
Ā 
Compiler Construction | Lecture 2 | Declarative Syntax Definition
Compiler Construction | Lecture 2 | Declarative Syntax DefinitionCompiler Construction | Lecture 2 | Declarative Syntax Definition
Compiler Construction | Lecture 2 | Declarative Syntax Definition
Eelco Visser
Ā 
Compiler Construction | Lecture 1 | What is a compiler?
Compiler Construction | Lecture 1 | What is a compiler?Compiler Construction | Lecture 1 | What is a compiler?
Compiler Construction | Lecture 1 | What is a compiler?
Eelco Visser
Ā 
Declare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code GenerationDeclare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code Generation
Eelco Visser
Ā 
Declare Your Language: Dynamic Semantics
Declare Your Language: Dynamic SemanticsDeclare Your Language: Dynamic Semantics
Declare Your Language: Dynamic Semantics
Eelco Visser
Ā 
Declare Your Language: Constraint Resolution 2
Declare Your Language: Constraint Resolution 2Declare Your Language: Constraint Resolution 2
Declare Your Language: Constraint Resolution 2
Eelco Visser
Ā 
Declare Your Language: Constraint Resolution 1
Declare Your Language: Constraint Resolution 1Declare Your Language: Constraint Resolution 1
Declare Your Language: Constraint Resolution 1
Eelco Visser
Ā 

More from Eelco Visser (20)

CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
Ā 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: Introduction
Ā 
A Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation RulesA Direct Semantics of Declarative Disambiguation Rules
A Direct Semantics of Declarative Disambiguation Rules
Ā 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with Statix
Ā 
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler ConstructionCompiler Construction | Lecture 17 | Beyond Compiler Construction
Compiler Construction | Lecture 17 | Beyond Compiler Construction
Ā 
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Domain Specific Languages for Parallel Graph AnalytiX (PGX)
Ā 
Compiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory ManagementCompiler Construction | Lecture 15 | Memory Management
Compiler Construction | Lecture 15 | Memory Management
Ā 
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersCompiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | Interpreters
Ā 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code Generation
Ā 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual Machines
Ā 
Compiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone FrameworksCompiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone Frameworks
Ā 
Compiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow AnalysisCompiler Construction | Lecture 10 | Data-Flow Analysis
Compiler Construction | Lecture 10 | Data-Flow Analysis
Ā 
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type Checking
Ā 
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static AnalysisCompiler Construction | Lecture 6 | Introduction to Static Analysis
Compiler Construction | Lecture 6 | Introduction to Static Analysis
Ā 
Compiler Construction | Lecture 2 | Declarative Syntax Definition
Compiler Construction | Lecture 2 | Declarative Syntax DefinitionCompiler Construction | Lecture 2 | Declarative Syntax Definition
Compiler Construction | Lecture 2 | Declarative Syntax Definition
Ā 
Compiler Construction | Lecture 1 | What is a compiler?
Compiler Construction | Lecture 1 | What is a compiler?Compiler Construction | Lecture 1 | What is a compiler?
Compiler Construction | Lecture 1 | What is a compiler?
Ā 
Declare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code GenerationDeclare Your Language: Virtual Machines & Code Generation
Declare Your Language: Virtual Machines & Code Generation
Ā 
Declare Your Language: Dynamic Semantics
Declare Your Language: Dynamic SemanticsDeclare Your Language: Dynamic Semantics
Declare Your Language: Dynamic Semantics
Ā 
Declare Your Language: Constraint Resolution 2
Declare Your Language: Constraint Resolution 2Declare Your Language: Constraint Resolution 2
Declare Your Language: Constraint Resolution 2
Ā 
Declare Your Language: Constraint Resolution 1
Declare Your Language: Constraint Resolution 1Declare Your Language: Constraint Resolution 1
Declare Your Language: Constraint Resolution 1
Ā 

Recently uploaded

Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
Ā 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
Ā 
Atelier - Innover avec lā€™IA GĆ©nĆ©rative et les graphes de connaissances
Atelier - Innover avec lā€™IA GĆ©nĆ©rative et les graphes de connaissancesAtelier - Innover avec lā€™IA GĆ©nĆ©rative et les graphes de connaissances
Atelier - Innover avec lā€™IA GĆ©nĆ©rative et les graphes de connaissances
Neo4j
Ā 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
vrstrong314
Ā 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
Ā 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
Ā 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
Ā 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
Ā 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
Ā 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
Ā 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
Ā 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
Ā 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
Ā 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
Ā 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni GarcĆ­a
Ā 
AI Pilot Review: The Worldā€™s First Virtual Assistant Marketing Suite
AI Pilot Review: The Worldā€™s First Virtual Assistant Marketing SuiteAI Pilot Review: The Worldā€™s First Virtual Assistant Marketing Suite
AI Pilot Review: The Worldā€™s First Virtual Assistant Marketing Suite
Google
Ā 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
Ā 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
Ā 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
Ā 
Need for Speed: Removing speed bumps from your Symfony projects āš”ļø
Need for Speed: Removing speed bumps from your Symfony projects āš”ļøNeed for Speed: Removing speed bumps from your Symfony projects āš”ļø
Need for Speed: Removing speed bumps from your Symfony projects āš”ļø
Łukasz Chruściel
Ā 

Recently uploaded (20)

Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
Ā 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ā 
Atelier - Innover avec lā€™IA GĆ©nĆ©rative et les graphes de connaissances
Atelier - Innover avec lā€™IA GĆ©nĆ©rative et les graphes de connaissancesAtelier - Innover avec lā€™IA GĆ©nĆ©rative et les graphes de connaissances
Atelier - Innover avec lā€™IA GĆ©nĆ©rative et les graphes de connaissances
Ā 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
Ā 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Ā 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Ā 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Ā 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Ā 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
Ā 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Ā 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Ā 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Ā 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Ā 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Ā 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Ā 
AI Pilot Review: The Worldā€™s First Virtual Assistant Marketing Suite
AI Pilot Review: The Worldā€™s First Virtual Assistant Marketing SuiteAI Pilot Review: The Worldā€™s First Virtual Assistant Marketing Suite
AI Pilot Review: The Worldā€™s First Virtual Assistant Marketing Suite
Ā 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Ā 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
Ā 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Ā 
Need for Speed: Removing speed bumps from your Symfony projects āš”ļø
Need for Speed: Removing speed bumps from your Symfony projects āš”ļøNeed for Speed: Removing speed bumps from your Symfony projects āš”ļø
Need for Speed: Removing speed bumps from your Symfony projects āš”ļø
Ā 

Declare Your Language: Syntactic (Editor) Services

  • 1. Eelco Visser IN4303 Compiler Construction TU Delft September 2017 Declare Your Language Chapter 3: Syntactic (Editor) Services
  • 2. 2 Source Code Type Check Machine Code Parse CodeGenOptimize Declarative syntax definition Syntax Definition ParserGen Parse Table Derive parser from syntax definition
  • 3. Lexical syntax - deļ¬ning the syntax of tokens / terminals including layout - making lexical syntax explicit Formatting speciļ¬cation - how to map (abstract syntax) trees to text Syntactic completion - proposing valid syntactic completions in an editor Grammar transformation - disambiguation by transformation Parsing - interpreting a syntax deļ¬nition to map text to trees This Lecture 3
  • 5. 5 The SDF3 syntax definition formalism is documented at the metaborg.org website. http://www.metaborg.org/en/latest/source/langdev/meta/lang/sdf3/index.html
  • 6. 6 The inverse of parsing is unparsing or pretty-printing or formatting, i.e. mapping a tree representation of a program to a textual representation. A plain context-free grammar can be used as specification of an unparser. However, then it is unclear where the whitespace should go. This paper extends context-free grammars with templates that provide hints for layout of program text when formatting. https://doi.org/10.1145/2427048.2427056
  • 7. 7 Syntax definitions cannot be used just for parsing, but for many other operations. This paper shows how syntactic completion can be provided generically given a syntax definition. open access! https://doi.org/10.1145/2427048.2427056
  • 8. 8 Chapter 2: Lexical Analysis Chapter 3: Parsing Chapter 4: Abstract Syntax Next week: LR Parsing
  • 10. Context-Free Syntax vs Lexical Syntax 10 let function power(x: int, n: int): int = if n <= 0 then 1 else x * power(x, n - 1) in power(3, 10) end Mod( Let( [ FunDecs( [ FunDec( "power" , [FArg("x", Tid("int")), FArg("n", Tid("int"))] , Tid("int") , If( Leq(Var("n"), Int("0")) , Int("1") , Times( Var("x") , Call( "power" , [Var("x"), Minus(Var("n"), Int("1"))] ) ) ) ) ] ) ] , [Call("power", [Int("3"), Int("10")])] ) ) phrase structure lexeme / token separated by layout structure not relevant not separated by layout
  • 11. Character Classes 11 lexical syntax // character codes Character = [65] Range = [65-90] Union = [65-90] / [97-122] Difference = [0-127] / [1013] Union = [0-911-1214-255] Character class represents choice from a set of characters
  • 12. Sugar for Character Classes 12 lexical syntax // sugar CharSugar = [a] = [97] CharClass = [abcdefghijklmnopqrstuvwxyz] = [97-122] SugarRange = [a-z] = [97-122] Union = [a-z] / [A-Z] / [0-9] = [48-5765-9097-122] RangeCombi = [a-z0-9_] = [48-579597-122] Complement = ~[nr] = [0-255] / [1013] = [0-911-1214-255]
  • 13. Literals are Sequences of Characters 13 lexical syntax // literals Literal = "then" // case sensitive sequence of characters CaseInsensitive = 'then' // case insensitive sequence of characters syntax "then" = [116] [104] [101] [110] 'then' = [84116] [72104] [69101] [78110] syntax "then" = [t] [h] [e] [n] 'then' = [Tt] [Hh] [Ee] [Nn]
  • 14. Identiļ¬ers 14 lexical syntax Id = [a-zA-Z] [a-zA-Z0-9_]* Id = a Id = B Id = cD Id = xyz10 Id = internal_ Id = CamelCase Id = lower_case Id = ...
  • 15. Lexical Ambiguity: Longest Match 15 lexical syntax Id = [a-zA-Z] [a-zA-Z0-9_]* context-free syntax Exp.Var = Id Exp.Call = Exp Exp {left} // curried function call ab Mod( amb( [Var("ab"), Call(Var("a"), Var("b"))] ) )
  • 16. abc Lexical Ambiguity: Longest Match 16 lexical syntax Id = [a-zA-Z] [a-zA-Z0-9_]* context-free syntax Exp.Var = Id Exp.Call = Exp Exp {left} // curried function call Mod( amb( [ amb( [ Var("abc") , Call( amb( [Var("ab"), Call(Var("a"), Var("b"))] ) , Var("c") ) ] ) , Call(Var("a"), Var("bc")) ] ) )
  • 17. abc def ghi Lexical Restriction => Longest Match 17 lexical syntax Id = [a-zA-Z] [a-zA-Z0-9_]* lexical restrictions Id -/- [a-zA-Z0-9_] // longest match for identifiers context-free syntax Exp.Var = Id Exp.Call = Exp Exp {left} // curried function call Call(Call(Var("abc"), Var("def")), Var("ghi")) Lexical restriction: phrase cannot be followed by character in character class
  • 18. if def then ghi Lexical Ambiguity: Keywords overlap with Identiļ¬ers 18 lexical syntax Id = [a-zA-Z] [a-zA-Z0-9_]* lexical restrictions Id -/- [a-zA-Z0-9_] // longest match for identifiers context-free syntax Exp.Var = Id Exp.Call = Exp Exp {left} Exp.IfThen = "if" Exp "then" Exp amb( [ Mod( Call( Call(Call(Var("if"), Var("def")), Var("then")) , Var("ghi") ) ) , Mod(IfThen(Var("def"), Var("ghi"))) ] )
  • 19. ifdef then ghi Lexical Ambiguity: Keywords overlap with Identiļ¬ers 19 amb( [ Mod( Call(Call(Var("ifdef"), Var("then")), Var("ghi")) ) , Mod(IfThen(Var("def"), Var("ghi"))) ] ) lexical syntax Id = [a-zA-Z] [a-zA-Z0-9_]* lexical restrictions Id -/- [a-zA-Z0-9_] // longest match for identifiers context-free syntax Exp.Var = Id Exp.Call = Exp Exp {left} Exp.IfThen = "if" Exp "then" Exp
  • 20. Reject Productions => Reserved Words 20 lexical syntax Id = [a-zA-Z] [a-zA-Z0-9_]* Id = ā€œif" {reject} Id = "then" {reject} lexical restrictions Id -/- [a-zA-Z0-9_] // longest match for identifiers "if" "then" -/- [a-zA-Z0-9_] context-free syntax Exp.Var = Id Exp.Call = Exp Exp {left} Exp.IfThen = "if" Exp "then" Exp if def then ghi IfThen(Var("def"), Var("ghi"))
  • 21. ifdef then ghi Reject Productions => Reserved Words 21 parse error lexical syntax Id = [a-zA-Z] [a-zA-Z0-9_]* Id = ā€œif" {reject} Id = "then" {reject} lexical restrictions Id -/- [a-zA-Z0-9_] // longest match for identifiers "if" "then" -/- [a-zA-Z0-9_] context-free syntax Exp.Var = Id Exp.Call = Exp Exp {left} Exp.IfThen = "if" Exp "then" Exp
  • 22. Real Numbers 22 lexical syntax RealConst.RealConstNoExp = IntConst "." IntConst RealConst.RealConst = IntConst "." IntConst "e" Sign IntConst Sign = "+" Sign = "-"
  • 24. Tiger Lexical Syntax: Identiļ¬ers 24 module Identifiers lexical syntax Id = [a-zA-Z] [a-zA-Z0-9_]* lexical restrictions Id -/- [a-zA-Z0-9_]
  • 25. Tiger Lexical Syntax: Number Literals 25 module Numbers lexical syntax IntConst = [0-9]+ lexical syntax RealConst.RealConstNoExp = IntConst "." IntConst RealConst.RealConst = IntConst "." IntConst "e" Sign IntConst Sign = "+" Sign = "-" context-free syntax Exp.Int = IntConst
  • 26. Tiger Lexical Syntax: String Literals 26 module Strings sorts StrConst lexical syntax StrConst = """ StrChar* """ StrChar = ~["n] StrChar = [] [n] StrChar = [] [t] StrChar = [] [^] [A-Z] StrChar = [] [0-9] [0-9] [0-9] StrChar = [] ["] StrChar = [] [] StrChar = [] [ tn]+ [] context-free syntax // records Exp.String = StrConst
  • 27. Tiger Lexical Syntax: Whitespace 27 module Whitespace lexical syntax LAYOUT = [ tnr] context-free restrictions // Ensure greedy matching for comments LAYOUT? -/- [ tnr] LAYOUT? -/- [/].[/] LAYOUT? -/- [/].[*] syntax LAYOUT-CF = LAYOUT-LEX LAYOUT-CF = LAYOUT-CF LAYOUT-CF {left} Implicit composition of layout
  • 28. Tiger Lexical Syntax: Comment 28 lexical syntax CommentChar = [*] LAYOUT = "/*" InsideComment* "*/" InsideComment = ~[*] InsideComment = CommentChar lexical restrictions CommentChar -/- [/] context-free restrictions LAYOUT? -/- [/].[*] lexical syntax LAYOUT = SingleLineComment SingleLineComment = "//" ~[nr]* NewLineEOF NewLineEOF = [nr] NewLineEOF = EOF EOF = lexical restrictions EOF -/- ~[] context-free restrictions LAYOUT? -/- [/].[/]
  • 30. Core language - context-free grammar productions - with constructors - only character classes as terminals - explicit deļ¬nition of layout Desugaring - express lexical syntax in terms of character classes - explicate layout between context-free syntax symbols - separate lexical and context-free syntax non-terminals Explication of Lexical Syntax 30
  • 31. Explication of Layout by Transformation 31 context-free syntax Exp.Int = IntConst Exp.Uminus = "-" Exp Exp.Times = Exp "*" Exp {left} Exp.Divide = Exp "/" Exp {left} Exp.Plus = Exp "+" Exp {left} syntax Exp-CF.Int = IntConst-CF Exp-CF.Uminus = "-" LAYOUT?-CF Exp-CF Exp-CF.Times = Exp-CF LAYOUT?-CF "*" LAYOUT?-CF Exp-CF {left} Exp-CF.Divide = Exp-CF LAYOUT?-CF "/" LAYOUT?-CF Exp-CF {left} Exp-CF.Plus = Exp-CF LAYOUT?-CF "+" LAYOUT?-CF Exp-CF {left} Symbols in context-free syntax are implicitly separated by optional layout
  • 32. Separation of Lexical and Context-free Syntax 32 syntax Id-LEX = [65-9097-122] [48-5765-909597-122]*-LEX Id-LEX = "if" {reject} Id-LEX = "then" {reject} Id-CF = Id-LEX Exp-CF.Var = Id-CF lexical syntax Id = [a-zA-Z] [a-zA-Z0-9_]* Id = ā€œif" {reject} Id = "then" {reject} context-free syntax Exp.Var = Id
  • 33. 33 lexical syntax Id = [a-zA-Z] [a-zA-Z0-9_]* Id = ā€œif" {reject} Id = "then" {reject} lexical restrictions Id -/- [a-zA-Z0-9_] "if" "then" -/- [a-zA-Z0-9_] context-free syntax Exp.Var = Id Exp.Call = Exp Exp {left} Exp.IfThen = "if" Exp "then" Exp syntax "if" = [105] [102] "then" = [116] [104] [101] [110] [48-5765-909597-122]+-LEX = [48-5765-909597-122] [48-5765-909597-122]+-LEX = [48-5765-909597-122]+-LEX [48-5765-909597-122] [48-5765-909597-122]*-LEX = [48-5765-909597-122]*-LEX = [48-5765-909597-122]+-LEX Id-LEX = [65-9097-122] [48-5765-909597-122]*-LEX Id-LEX = "if" {reject} Id-LEX = "then" {reject} Id-CF = Id-LEX Exp-CF.Var = Id-CF Exp-CF.Call = Exp-CF LAYOUT?-CF Exp-CF {left} Exp-CF.IfThen = "if" LAYOUT?-CF Exp-CF LAYOUT?-CF "then" LAYOUT?-CF Exp-CF LAYOUT-CF = LAYOUT-CF LAYOUT-CF {left} LAYOUT?-CF = LAYOUT-CF LAYOUT?-CF = restrictions Id-LEX -/- [48-5765-909597-122] "if" -/- [48-5765-909597-122] "then" -/- [48-5765-909597-122] priorities Exp-CF.Call left Exp-CF.Call, LAYOUT-CF = LAYOUT-CF LAYOUT-CF left LAYOUT-CF = LAYOUT-CF LAYOUT-CF separate lexical and context-free syntax separate context- free symbols by optional layout character classes as only terminals
  • 34. Give an example syntax deļ¬nition that demonstrates the need for separating lexical and context-free syntax Explain what goes wrong in the absence of that separation Exam Question 34
  • 35. Syntactic (Editor) Services 35 What can we do with a syntax deļ¬nition?
  • 36. Editor Services 36 The Spoofax Language Workbench
  • 37. Editor Services 37 Source Code Editor Parse Feedback & Operations Abstract Syntax Tree Feedback - syntax coloring - syntax checking - outline view Operations - syntactic completion - formatting - abstract syntax tree
  • 38. Language Project Conļ¬guration (ESV) 38 module Main imports Syntax Analysis language extensions : tig //provider : target/metaborg/stratego.ctree provider : target/metaborg/stratego.jar provider : target/metaborg/stratego-javastrat.jar menus menu: "Transform" (openeditor) (realtime) action: "Desugar" = editor-desugar (source) action: "Desugar AST" = editor-desugar-ast (source)
  • 39. Conļ¬guration of Syntactic Services (ESV) 39 module Syntax imports libspoofax/color/default completion/colorer/Tiger-cc-esv language table : target/metaborg/sdf-new.tbl //table : target/metaborg/sdf.tbl start symbols : Module line comment : "//" block comment : "/*" * "*/" fences : [ ] ( ) { } menus menu: "Syntax" (openeditor) action: "Format" = editor-format (source) action: "Show parsed AST" = debug-show-aterm (source) views outline view: editor-outline (source) expand to level: 3
  • 41. Generated Syntax Coloring 41 module libspoofax/color/default imports libspoofax/color/colors colorer // Default, token-based highlighting keyword : 127 0 85 bold identifier : default string : blue number : darkgreen var : 139 69 19 italic operator : 0 0 128 layout : 63 127 95 italic // compute the n-th fibonacci number let function fib(n: int): int = if n <= 1 then 1 else fib(n - 1) + fib(n - 2) in fib(10) end
  • 42. Customized Syntax Coloring 42 module Tiger-Colorer colorer red = 255 0 0 green = 0 255 0 blue = 0 0 255 TUDlavender = 123 160 201 colorer token-based highlighting keyword : red Id : TUDlavender StrConst : darkgreen TypeId : blue layout : green // compute the n-th fibonacci number let function fib(n: int): int = if n <= 1 then 1 else fib(n - 1) + fib(n - 2) in fib(10) end
  • 44. Language = Sentences and Trees 44 parse format
  • 45. Unparsing: From Abstract Syntax Term to Text 45 rules unparse : Int(x) -> x unparse : Plus(e1, e2) -> $[[<unparse> e1] + [<unparse> e2]] unparse : Times(e1, e2) -> $[[<unparse> e1] * [<unparse> e2]] Mod( Plus( Int("1") , Times( Int("2") , Plus(Int("3"), Int("4")) ) ) ) 1 + 2 * 3 + 4 take priorities into account! context-free syntax Exp.Int = IntConst Exp.Times = [[Exp] * [Exp]] {left} Exp.Plus = [[Exp] + [Exp]] {left}
  • 46. From ASTs to text - insert keywords - insert layout: spaces, line breaks, indentation - insert parentheses to preserve tree structure Unparser - derive transformation rules from context-free grammar - keywords, literals deļ¬ned in grammar productions - parentheses determined by priority, associativity rules - separate all symbols by a space => not pretty, or even readable Pretty-printer - introduce spaces, line breaks, and indentation to produce readable text - doing that manually is tedious Pretty-Printing 46
  • 47. Specifying Formatting Layout with Templates 47 context-free syntax Exp.Seq = < ( <{Exp ";n"}*> ) > Exp.If = < if <Exp> then <Exp> else <Exp> > Exp.IfThen = < if <Exp> then <Exp> > Exp.While = < while <Exp> do <Exp> > Inverse quotation - template quotes literal text with <> - anti-quotations insert non-terminals with <> Layout directives - whitespace (linebreaks, indentation, spaces) in template guides formatting - is interpreted as LAYOUT? for parsing Formatter generation - generate rules for mapping AST to text (via box expressions) Applications - code generation; pretty-printing generated AST - syntactic completions - formatting
  • 48. Templates for Tiger: Binary Expressions 48 context-free syntax Exp.Int = IntConst Exp.Uminus = [- [Exp]] Exp.Times = [[Exp] * [Exp]] {left} Exp.Divide = [[Exp] / [Exp]] {left} Exp.Plus = [[Exp] + [Exp]] {left} Exp.Minus = [[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.Geq = [[Exp] >= [Exp]] {non-assoc} Exp.Leq = [[Exp] <= [Exp]] {non-assoc} Exp.And = [[Exp] & [Exp]] {left} Exp.Or = [[Exp] | [Exp]] {left} Use [] quotes instead of <> to avoid clash with comparison operators
  • 49. Templates for Tiger: Functions 49 context-free syntax Dec.FunDecs = <<{FunDec "n"}+>> {longest-match} FunDec.ProcDec = < function <Id>(<{FArg ", "}*>) = <Exp> > FunDec.FunDec = < function <Id>(<{FArg ", "}*>) : <Type> = <Exp> > FArg.FArg = <<Id> : <Type>> Exp.Call = <<Id>(<{Exp ", "}*>)> No space after function name in call Space after comma! Function declarations separated by newline Indent body of function
  • 50. Templates for Tiger: Bindings and Records 50 context-free syntax Exp.Let = < let <{Dec "n"}*> in <{Exp ";n"}*> end > context-free syntax // records Type.RecordTy = < { <{Field ", n"}*> } > Field.Field = <<Id> : <TypeId>> Exp.NilExp = <nil> Exp.Record = <<TypeId>{ <{InitField ", "}*> }> InitField.InitField = <<Id> = <Exp>> LValue.FieldVar = <<LValue>.<Id>> Note spacing / layout in separators
  • 51. Generating Pretty-Print Rules from Template Productions 51 context-free syntax FunDec.FunDec = < function <Id>(<{FArg ", "}*>) : <Type> = <Exp> > rules prettyprint-Tiger-FunDec : ProcDec(t1__, t2__, t3__) -> [ H( [SOpt(HS(), "0")] , [ S("function ") , t1__ , S("(") , t2__' , S(") =") ] ) , t3__' ] with t1__' := <pp-one-Z(prettyprint-Tiger-Id) <+ pp-one-Z(prettyprint-completion-aux)> t1__ with t2__' := <pp-H-list(prettyprint-Tiger-FArg|", ") <+ pp-one-Z(prettyprint-completion-aux)> t2__ with t3__' := <pp-indent(|"2")> [ <pp-one-Z(prettyprint-Tiger-Exp) <+ pp-one-Z(prettyprint-completion-aux)> t3__ ] Separation of concerns: - generated formatter transforms AST to Box - Box formatter produces text
  • 52. Boxes for Formatting 52 _1 ā€œfooā€ KW [ ā€œfooā€ ] literal text, keywords, parameters
  • 53. Horizontal Layout 53 B B B H hs=x [ ]B B Bhs=x hs: horizontal space between boxes
  • 54. Vertical Layout 54 V hs=x is=i [ ]B B Bvs=y is=i B B B vs: vertical space between boxes; is: indentation space
  • 56. Tiger Syntax: Composition module Tiger imports Whitespace imports Comments imports Types imports Identifiers imports Bindings imports Variables imports Functions imports Numbers imports Strings imports Records imports Arrays imports Control-Flow context-free start-symbols Module context-free syntax Module.Mod = Exp context-free priorities Exp.Or > Exp.Array > Exp.Assign , {Exp.Uminus LValue.FieldVar LValue.Subscript} > {left : Exp.Times Exp.Divide}
  • 57. Tiger Syntax: Identiļ¬ers and Strings module Identifiers lexical syntax Id = [a-zA-Z] [a-zA-Z0-9_]* lexical restrictions Id -/- [a-zA-Z0-9_] lexical syntax Id = "nil" {reject} Id = "let" {reject} Id = ā€¦ {reject} module Strings sorts StrConst lexical syntax StrConst = """ StrChar* """ StrChar = ~["n] StrChar = [] [n] StrChar = [] [t] StrChar = [] [^] [A-Z] StrChar = [] [0-9] [0-9] [0-9] StrChar = [] ["] StrChar = [] [] StrChar = [] [ tn]+ [] context-free syntax // records Exp.String = StrConst
  • 58. Tiger Syntax: Whitespace module Whitespace lexical syntax LAYOUT = [ tnr] context-free restrictions LAYOUT? -/- [ tnr]
  • 59. Tiger Syntax: Comments module Comments lexical syntax // multiline comments CommentChar = [*] LAYOUT = "/*" InsideComment* "*/" InsideComment = ~[*] InsideComment = CommentChar lexical restrictions CommentChar -/- [/] context-free restrictions LAYOUT? -/- [/].[/] lexical syntax // single line comments LAYOUT = "//" ~[nr]* NewLineEOF NewLineEOF = [nr] NewLineEOF = EOF EOF = // end of file since it cannot be followed by any character // avoids the need for a newline to close a single line comment // at the last line of a file lexical restrictions EOF -/- ~[] context-free restrictions LAYOUT? -/- [/].[*]
  • 60. Tiger Syntax: Numbers module Numbers lexical syntax IntConst = [0-9]+ context-free syntax Exp.Int = IntConst Exp.Uminus = [- [Exp]] Exp.Times = [[Exp] * [Exp]] {left} Exp.Divide = [[Exp] / [Exp]] {left} Exp.Plus = [[Exp] + [Exp]] {left} Exp.Minus = [[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.Geq = [[Exp] >= [Exp]] {non-assoc} Exp.Leq = [[Exp] <= [Exp]] {non-assoc} Exp.And = [[Exp] & [Exp]] {left} Exp.Or = [[Exp] | [Exp]] {left} context-free priorities {Exp.Uminus} > {left : Exp.Times Exp.Divide} > {left : Exp.Plus Exp.Minus} > {non-assoc : Exp.Eq Exp.Neq Exp.Gt Exp.Lt Exp.Geq Exp.Leq} > Exp.And > Exp.Or
  • 61. Tiger Syntax: Variables and Functions module Bindings imports Control-Flow imports Identifiers imports Types imports Functions imports Variables sorts Declarations context-free syntax Exp.Let = < let <{Dec "n"}*> in <{Exp ";n"}*> end > Declarations.Declarations = < declarations <{Dec "n"}*> > module Variables imports Identifiers imports Types sorts Var context-free syntax Dec.VarDec = <var <Id> : <Type> := <Exp>> Dec.VarDecNoType = <var <Id> := <Exp>> Var.Var = Id LValue = Var Exp = LValue Exp.Assign = <<LValue> := <Exp>> module Functions imports Identifiers imports Types context-free syntax Dec.FunDecs = <<{FunDec "n"}+>> {longest-match} FunDec.ProcDec = < function <Id>(<{FArg ", "}*>) = <Exp> > FunDec.FunDec = < function <Id>(<{FArg ", "}*>) : <Type> = <Exp> > FArg.FArg = <<Id> : <Type>> Exp.Call = <<Id>(<{Exp ", "}*>)>
  • 62. Tiger Syntax: Records, Arrays, Types module Records imports Base imports Identifiers imports Types context-free syntax // records Type.RecordTy = < { <{Field ", n"}*> } > Field.Field = <<Id> : <TypeId>> Exp.NilExp = <nil> Exp.Record = <<TypeId>{ <{InitField ", "}*> }> InitField.InitField = <<Id> = <Exp>> LValue.FieldVar = <<LValue>.<Id>> module Arrays imports Types context-free syntax // arrays Type.ArrayTy = <array of <TypeId>> Exp.Array = <<TypeId>[<Exp>] of <Exp>> LValue.Subscript = <<LValue>[<Index>]> Index = Exp module Types imports Identifiers imports Bindings sorts Type context-free syntax // type declarations Dec.TypeDecs = <<{TypeDec "n"}+>> {longest-match} TypeDec.TypeDec = <type <Id> = <Type>> context-free syntax // type expressions Type = TypeId TypeId.Tid = Id sorts Ty context-free syntax // semantic types Ty.INT = <INT> Ty.STRING = <STRING> Ty.NIL = <NIL> Ty.UNIT = <UNIT> Ty.NAME = <NAME <Id>> Ty.RECORD = <RECORD <Id>> Ty.ARRAY = <ARRAY <Ty> <Id>> Ty.FUN = <FUN ( <{Ty ","}*> ) <Ty>>
  • 63. Syntactic Completion 63 Amorim, Erdweg, Wachsmuth, Visser Principled syntactic code completion using placeholders. In ACM SIGPLAN International Conference on Software Language Engineering [SLE 2016].
  • 64. 64 *S. Amann, S. Proksch, S. Nadi, and M. Mezini. A study of visual studio usage in practice. In SANER, 2016.
  • 66. Problems 66 Ad-hoc: re-implement for each language / IDE Incomplete: not all programs reachableUnsound: propose invalid constructs
  • 67. Goal 67 Sound and Complete Syntactic Code Completion from Syntax Deļ¬nition
  • 71. 71
  • 72. 72
  • 73. 73
  • 74. 74
  • 76. Deriving Syntactic Completion from Syntax Deļ¬nition 76 context-free syntax // regular production Statement.If = "if" "(" Exp ")" Statement "else" Statement
  • 77. Placeholders as Language Constructs 77 context-free syntax // placeholder rule Statement.Statement-Plhdr = ā€œ$Statement" context-free syntax // regular production Statement.If = "if" "(" Exp ")" Statement "else" Statement
  • 78. Calculate Placeholder Expansions 78 context-free syntax // regular production Statement.If = "if" "(" Exp ")" Statement "else" Statement Stm-Plhdr If Exp-Plhdr Stm-Plhdr Stm-Plhdr context-free syntax // placeholder rule Statement.Statement-Plhdr = ā€œ$Statement" rules rewrite-placeholder: Statement-Plhdr() -> If(Exp-Plhdr(), Statement-Plhdr(), Statement-Plhdr())
  • 79. Calculate Placeholder Expansions 79 context-free syntax // regular production Statement.If = "if" "(" Exp ")" Statement "else" Statement context-free syntax // placeholder rule Statement.Statement-Plhdr = ā€œ$Statement" rules rewrite-placeholder: Statement-Plhdr() -> If(Exp-Plhdr(), Statement-Plhdr(), Statement-Plhdr())
  • 81. 81 Complete programs How to expand a complete program?
  • 82. 82 Insert a placeholder? How to expand a complete program?
  • 84. Placeholder Inference 84 Optional Parent List of Statements Add Optional Element No source region Add ļ¬rst or last element Add Element to List
  • 85. Placeholder Inference: Optional ClassDecl class ā€œAā€ {NoParent ConsMethod [ā€¦] }NilField 85
  • 86. Placeholder Inference: Optional 86 ClassDecl class ā€œAā€ {NoParent ConsMethod [ā€¦] }NilField
  • 87. Placeholder Inference: Optional 87 ClassDecl class ā€œAā€ {NoParent ConsMethod [ā€¦] }NilField
  • 89. 89 Placeholder Inference - Lists [ā€¦] [ā€¦] Method Cons VarDecl AssignInt ā€œxā€ VarRef ā€œxā€ Add Int 21 Cons Nil { return [ā€¦] int ; Int = 21
  • 90. 90 Placeholder Inference - Lists [ā€¦] [ā€¦] Method Cons VarDecl AssignInt ā€œxā€ VarRef ā€œxā€ Add Int 21 Cons Nil { return [ā€¦] int ; Int = 21
  • 91. 91 Placeholder Inference - Lists [ā€¦] [ā€¦] Method Cons VarDecl AssignInt ā€œxā€ VarRef ā€œxā€ Add Int 21 Cons Nil { return [ā€¦] int ; Int = 21
  • 92. 92 Placeholder Inference - Lists [ā€¦] [ā€¦] Method Cons VarDecl AssignInt ā€œxā€ VarRef ā€œxā€ Add Int 21 Cons Nil { return [ā€¦] int ; Int = 21
  • 93. 93
  • 97. 97
  • 98. Insertion Rules 98 context-free syntax //regular syntax rules Statement.VarDecl = <<Type> <ID>;> Statement.Assign = <<VarRef> = <Exp>;> // derived insertion rules for placeholders context-free syntax Type.Type-Plhdr = {symbol-insertion} ID.ID-Plhdr = {symbol-insertion} Statement.Statement-Plhdr = {symbol-insertion} VarRef.VarRef-Plhdr = {symbol-insertion} Exp.Exp-Plhdr = {symbol-insertion} // derived insertion rules for literals lexical syntax "=" = {symbol-completion} ";" = {symbol-completion} Empty productions
  • 99. Apply Insertion Rules at Cursor 99 Proposal nodes Insertion nodes [ā€¦] [ā€¦] Stmt* amb VarDecl ClassType ā€œxā€ VarDecl [ā€¦] Assign ID-Plhdr ; Assign VarRef ā€œxā€ Exp-Plhdr ; =
  • 100. Limit Search Space 100 Exp-Plhdr Exp-Plhdr + Assign VarRef ā€œxā€ ; = [ā€¦] Add Assign VarRef ā€œxā€ ;= [ā€¦] Exp-Plhdr Use the simplest possible expansions
  • 102. Nested Proposal Nodes 102 IntValue Exp-Plhdr+ Assign VarRef ā€œxā€ ;= [ā€¦] Add 1
  • 103. 103
  • 109. Representation - Standardized representation for <aspect> of programs - Independent of speciļ¬c object language Speciļ¬cation Formalism - Language-speciļ¬c declarative rules - Abstract from implementation concerns Language-Independent Interpretation - Formalism interpreted by language-independent algorithm - Multiple interpretations for diļ¬€erent purposes - Reuse between implementations of diļ¬€erent languages Separation of Concerns in Declarative Language Deļ¬nition 109
  • 110. Context-free grammars - well-understood mathematical basis - generation of valid sentences of language - derivation corresponds to phrase structure Character-level grammars - terminals are characters - lexical syntax deļ¬ned using same (CFG) formalism Declarative disambiguation - associativity and priority declarations for phrase structure ambiguities - follow restrictions and reject productions for lexical disambiguation - not discussed: layout constraints for modeling layout sensitive languages Structure - abstract syntax tree schema deļ¬ned in grammar using constructors Formatting - mapping from abstract syntax to text deļ¬ned using template production Services - automatically derived: coloring, formatting, completion - to do: parsing Declarative Syntax Deļ¬nition 110
  • 111. Representation: (Abstract Syntax) Trees - Standardized representation for structure of programs - Basis for syntactic and semantic operations Formalism: Syntax Deļ¬nition - Productions + Constructors + Templates + Disambiguation - Language-speciļ¬c rules: structure of each language construct Language-Independent Interpretation - Well-formedness of abstract syntax trees ā€£ provides declarative correctness criterion for parsing - Parsing algorithm ā€£ No need to understand parsing algorithm ā€£ Debugging in terms of representation - Formatting based on layout hints in grammar - Syntactic completion Declarative Syntax Deļ¬nition 111 A meta- language for talking about syntax }
  • 112. Syntax of a Syntax Deļ¬nition Formalism 112 Bootstrapping
  • 113. Bootstrapping: SDF3 in SDF3 113 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>>
  • 115. Exam Question 115 Consider the following SDF3 syntax deļ¬nition and assume a standard deļ¬nition for identiļ¬ers ID: context-free syntax E.Var = ID E.Add = E "+" E E.App = E E E.Fun = "" ID "." "{" E "}" E = "(" E ")" {bracket} (a) Demonstrate that this grammar is ambiguous by giving two diļ¬€erent abstract syntax trees using term notation based on the constructors in the grammar for the sentence f x + x and the left-most derivations that give rise to these trees. (b) Disambiguate the syntax deļ¬nition by means of priority and associativity declarations, i.e. without (otherwise) changing the productions. Motivate your choice of disambiguation rules. Is your disambiguation complete? (c) Translate the disambiguated syntax deļ¬nition into an unambiguous one without separate disambiguation rules. The syntax deļ¬nition should accept the same language and produce the same abstract syntax trees.
  • 116. 116 Except where otherwise noted, this work is licensed under