SlideShare a Scribd company logo
1 of 94
Download to read offline
Eelco Visser
IN4303 Compiler Construction
TU Delft
September 2017
Declare Your Language
Chapter 2: Syntax Definition
Reading Material
2
The perspective of this lecture on
declarative syntax definition is
explained more elaborately in this
Onward! 2010 essay. It uses an on older
version of SDF than used in these
slides. Production rules have the form
X1 … Xn -> N {cons(“c”)}
instead of
N.c = X1 … Xn
http://swerl.tudelft.nl/twiki/pub/Main/TechnicalReports/TUD-SERG-2010-019.pdf
https://doi.org/10.1145/1932682.1869535
The SPoofax Testing (SPT) language used
in the section on testing syntax
definitions was introduced in this
OOPSLA 2011 paper.
http://swerl.tudelft.nl/twiki/pub/Main/TechnicalReports/TUD-SERG-2011-011.pdf
https://doi.org/10.1145/2076021.2048080
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
Lab Organization
6
Lab Organization
• Course website:

https://tudelft-in4303-2017.github.io
• Lab assignments distributed through GitHub
• Detailed guide for doing lab assignments:

https://tudelft-in4303-2017.github.io/documentation/
git.html
Lab assignment logistics
• Submit your netid and GitHub username on WebLab

(https://weblab.tudelft.nl/in4303/2017-2018/assignment/15274/view)
• Grades are published on WebLab
• You will get read access to our repository for you:

https://github.com/TUDelft-IN4303-2017/student-<netid>
• Fork the repository — you can work from this template
• Submit a pull-request (PR) to our repository to submit your solution
• On the deadline the PR gets merged automatically, then graded
Lab assignment logistics
• The branch of the PR should be called assignment1 for the first
assignment, assignment2 for the second, etc.
• We push new branches for new assignments to our repository
• Sometimes you are instructed to reuse a previous solution
• When a PR is opened or changes are pushed, you can get early feedback
• This feedback is based on an automated grading system
• Early feedback is given only a few times
• So be strategic about when you open the PR and when you push changes
Syntax
10
What is wrong with this program?
11
package org.metaborg.lang.tiger.interpreter.natives;
import org.metaborg.lang.tiger.interpreter.natives.addI_2NodeGen;
import org.metaborg.meta.lang.dynsem.interpreter.nodes.building.TermBuild;
import com.oracle.truffle.api.source.SourceSection;
public abstract class addI_2 extends TermBuild {
public addI_2(SourceSection source) {
super(source)
}
public int doInt(int left, int right) {
left + right
}
public static TermBuild create(SourceSection source, TermBuild left, TermBuild right) {
addI_2NodeGen.create(source, left, right)
}
}
What is wrong with this program?
12
let def int power(x: int, n: int) =
if n <= 0 then 1
else x * power(x, n - 1)
in power(3, 10)
end
Declarative Syntax
Definition
13
What is Syntax?
14
https://en.wikipedia.org/wiki/Syntax
In mathematics, syntax refers to the rules governing the behavior of
mathematical systems, such as formal languages used in logic. (See logical
syntax.)
In linguistics, syntax (/ˈsɪntæks/[1][2]) is the set of rules, principles, and processes
that govern the structure of sentences in a given language, specifically word
order and punctuation. 

The term syntax is also used to refer to the study of such principles and
processes.[3] 

The goal of many syntacticians is to discover the syntactic rules common to all
languages.
The word syntax comes from Ancient Greek: σύνταξις "coordination", which
consists of σύν syn, "together," and τάξις táxis, "an ordering".
Syntax (Programming Languages)
15
In computer science, the syntax of a computer language is the set of rules that
defines the combinations of symbols that are considered to be a correctly
structured document or fragment in that language.

This applies both to programming languages, where the document represents
source code, and markup languages, where the document represents data. 

The syntax of a language defines its surface form.[1] 

Text-based computer languages are based on sequences of characters, while
visual programming languages are based on the spatial layout and connections
between symbols (which may be textual or graphical). 

Documents that are syntactically invalid are said to have a syntax error.
https://en.wikipedia.org/wiki/Syntax_(programming_languages)
Syntax
- The set of rules, principles, and processes that govern
the structure of sentences in a given language

- The set of rules that defines the combinations of
symbols that are considered to be a correctly structured
document or fragment in that language

How to describe such a set of rules?
- An algorithm that recognizes (the structure of) well-
formed sentences
Syntax Definition
16
Parsing programs during compilation
Checking syntax in an editor
Coloring tokens in an editor
Formatting a program (after refactoring it)
Proposing completions in an editor
Randomly generating well-formed test programs
Explaining the syntax rules to a programmer
What is the Purpose of a Syntax Definition?
17
Parsing programs during compilation
- an algorithm that recognizes sentence and constructs structure

Checking syntax in an editor
- an algorithm that finds syntax errors in program text (and recovers from previous errors)

Coloring tokens in an editor
- assign colors to syntactic categories of tokens 

Formatting a program (after refactoring it)
- map a syntax tree to text

Proposing completions in an editor
- determine what syntactic constructs are valid here

Randomly generating well-formed test programs
- some probabilistic algorithm?

Explaining the syntax rules to a programmer
- let them read the parsing algorithm for the language?
How do you do that?
18
Language Design
- define the properties of a language

- done by a language designer

Language Implementation
- implement tools that satisfy properties of the language

- done by a language implementer

Can we automate the language implementer?
- that is what language workbenches attempt to do
Separation of Concerns
19
Objective
- A workbench supporting design and implementation of programming languages

Approach
- Declarative multi-purpose domain-specific meta-languages

Meta-Languages
- Languages for defining languages

Domain-Specific
- Linguistic abstractions for domain of language definition (syntax, names, types, …)

Multi-Purpose
- Derivation of interpreters, compilers, rich editors, documentation, and verification
from single source

Declarative
- Focus on what not how; avoid bias to particular purpose in language definition
Declarative Language Definition
20
Representation
- Standardized representation for <aspect> of programs

- Independent of specific object language

Specification Formalism
- Language-specific declarative rules

- Abstract from implementation concerns

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

- Multiple interpretations for different purposes

- Reuse between implementations of different languages
A Recipe for Separation of Concerns
21
Representation: (Abstract Syntax) Trees
• Standardized representation for structure of programs

• Basis for syntactic and semantic operations

Formalism: Syntax Definition
• Productions + Constructors + Templates + Disambiguation

• Language-specific 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 Definition
22
A meta-
language for
talking about
syntax
}
Formal Languages &
Grammars
23
The set of rules, principles, and processes that
govern the structure of sentences in a given language
Language = Set of Sentences
24
fun (x : Int) { x + 1 }
Text is a convenient interface for writing and reading programs
Vocabulary Σ
- finite, nonempty set of elements (words, letters)

- alphabet

String over Σ
- finite sequence of elements chosen from Σ

- word, sentence, utterance, program

Formal language λ
- set of strings over a vocabulary Σ

- λ ⊆ Σ*
Formal Language
25
Formal grammar G
Derivation relation G
Formal language L(G) ⊆ Σ*
- L(G) = {w∈Σ* | S G* w}
Formal Grammar
26
Decimal Numbers
27
G = (N, Σ, P, S)
Num = Digit Num
Num = Digit
Digit = “0”
Digit = “1”
Digit = “2”
Digit = “3”
Digit = “4”
Digit = “5”
Digit = “6”
Digit = “7”
Digit = “8”
Digit = “9”
Decimal Numbers
28
G = (N, Σ, P, S)
Num = Digit Num
Num = Digit
Digit = “0”
Digit = “1”
Digit = “2”
Digit = “3”
Digit = “4”
Digit = “5”
Digit = “6”
Digit = “7”
Digit = “8”
Digit = “9”
Σ: finite set of terminal symbols
Decimal Numbers
29
G = (N, Σ, P, S)
Num = Digit Num
Num = Digit
Digit = “0”
Digit = “1”
Digit = “2”
Digit = “3”
Digit = “4”
Digit = “5”
Digit = “6”
Digit = “7”
Digit = “8”
Digit = “9”
Σ: finite set of terminal symbols
N: finite set of non-terminal symbols
Decimal Numbers
30
G = (N, Σ, P, S)
Num = Digit Num
Num = Digit
Digit = “0”
Digit = “1”
Digit = “2”
Digit = “3”
Digit = “4”
Digit = “5”
Digit = “6”
Digit = “7”
Digit = “8”
Digit = “9”
Σ: finite set of terminal symbols
N: finite set of non-terminal symbols
S∈N: start symbol
Decimal Numbers
31
G = (N, Σ, P, S)
Num = Digit Num
Num = Digit
Digit = “0”
Digit = “1”
Digit = “2”
Digit = “3”
Digit = “4”
Digit = “5”
Digit = “6”
Digit = “7”
Digit = “8”
Digit = “9”
Σ: finite set of terminal symbols
N: finite set of non-terminal symbols
S∈N: start symbol
P⊆N×(N∪Σ)*
: set of production rules
Decimal Numbers: Production
32
Num
Digit Num
4 Num
4 Digit Num
4 3 Num
4 3 Digit Num
4 3 0 Num
4 3 0 Digit
4 3 0 3

leftmost derivation
G = (N, Σ, P, S)
Num = Digit Num
Num = Digit
Digit = “0”
Digit = “1”
Digit = “2”
Digit = “3”
Digit = “4”
Digit = “5”
Digit = “6”
Digit = “7”
Digit = “8”
Digit = “9”
Num = Digit Num
Digit = “4”
Num = Digit Num
Digit = “3”
Num = Digit Num
Digit = “0”
Num = Digit
Digit = “3”
Decimal Numbers: Production
33
Num
Digit Num
Digit Digit Num
Digit Digit Digit Num
Digit Digit Digit Digit
Digit Digit Digit 3
Digit Digit 0 3
Digit 3 0 3
4 3 0 3

Num = Digit Num
Num = Digit Num
Num = Digit Num
Num = Digit
Digit = “3”
Digit = “0”
Digit = “3”
Digit = “4”
rightmost derivation
G = (N, Σ, P, S)
Num = Digit Num
Num = Digit
Digit = “0”
Digit = “1”
Digit = “2”
Digit = “3”
Digit = “4”
Digit = “5”
Digit = “6”
Digit = “7”
Digit = “8”
Digit = “9”
Step
- replace non-terminal symbol in string by symbols in rhs of
production 

Derivation
- repeatedly apply steps starting with start symbol

- until string consists only of terminal symbols

Leftmost Derivation
- replace leftmost non-terminal symbol in string

Rightmost Derivation
- replace rightmost non-terminal in string
Leftmost vs Rightmost Derivation
34
Binary Expressions
35
G = (N, Σ, P, S)
Exp = Num
Exp = Exp “+” Exp
Exp = Exp “−” Exp
Exp = Exp “*” Exp
Exp = Exp “/” Exp
Exp = “(” Exp “)”
Binary Expressions
36
G = (N, Σ, P, S)
Exp = Num
Exp = Exp “+” Exp
Exp = Exp “−” Exp
Exp = Exp “*” Exp
Exp = Exp “/” Exp
Exp = “(” Exp “)”
Σ: finite set of terminal symbols
Binary Expressions
37
G = (N, Σ, P, S)
Exp = Num
Exp = Exp “+” Exp
Exp = Exp “−” Exp
Exp = Exp “*” Exp
Exp = Exp “/” Exp
Exp = “(” Exp “)”
Σ: finite set of terminal symbols
N: finite set of non-terminal symbols
Binary Expressions
38
G = (N, Σ, P, S)
Exp = Num
Exp = Exp “+” Exp
Exp = Exp “−” Exp
Exp = Exp “*” Exp
Exp = Exp “/” Exp
Exp = “(” Exp “)”
Σ: finite set of terminal symbols
N: finite set of non-terminal symbols
S∈N: start symbol
Binary Expressions
39
G = (N, Σ, P, S)
Exp = Num
Exp = Exp “+” Exp
Exp = Exp “−” Exp
Exp = Exp “*” Exp
Exp = Exp “/” Exp
Exp = “(” Exp “)”
Σ: finite set of terminal symbols
N: finite set of non-terminal symbols
S∈N: start symbol
P⊆N×(N∪Σ)*
: set of production rules
Binary Expressions
40
Exp
Exp + Exp
Exp + Exp * Exp
3 + Exp * Exp
3 + 4 * Exp
3 + 4 * 5

Exp = Exp “+” Exp
Exp = Exp “*” Exp
Exp = Num
Exp = Num
Exp = Num
Formal grammar G = (N, Σ, P, S)
- nonterminal symbols N 

- terminal symbols Σ

- production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)*

- start symbol S∈N
Generative Grammars
41
nonterminal symbol
context replacement
42
Chomsky Hierarchy
Type-0: Unrestricted
- P ⊆ (N∪Σ)* × (N∪Σ)*

- a = b

Type-1: Context-sensitive
- P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)*

- a A c = a b c

Type-2: Context-free
- P ⊆ N × (N∪Σ)*

- A = b

Type-3: Regular
- P ⊆ N × (Σ ∪ ΣN)

- A = x or A = xB
a, b, c range over (N∪Σ)*

A, B range over N
Chomsky Hierarchy
43
formal grammars
context-sensitive
context-free
regular
Formal grammar G = (N, Σ, P, S)
Derivation relation G ⊆ (N∪Σ)* × (N∪Σ)*
w G w’ 

∃(p, q)∈P: ∃u,v∈(N∪Σ)*: 

w=u p v ∧ w’=u q v

Formal language L(G) ⊆ Σ*
L(G) = {w∈Σ* | S *
G w}
Formal Grammars
44
Generation
- Given a grammar, generate a sentence in the language

Language
- The set of all sentences generated by the grammar

Recognition
- Is this sentence (word) in the language generated by the
grammar?

- Is there an algorithm for deciding that question?
Recognition: Word Problem
45
Word problem χL: Σ*→ {0,1}
- w → 1, if w∈L 

- w → 0, else

Decidability
- type-0: semi-decidable

- type-1, type-2, type-3: decidable

Complexity
- type-1: PSPACE-complete

- type-2, type-3: P
Recognition: Word Problem
46
Recognizing Binary Expressions
47
3 + 4 * 5
Exp + 4 * 5
Exp + Exp * 5
Exp + Exp * Exp
Exp + Exp
Exp
Exp = Num
Exp = Num
Exp = Num
Exp = Exp “*” Exp
Exp = Exp “+” Exp
Replace substring that matches right-hand side of production with left-hand-side
The Syntax Definition
Formalism SDF3
48
A human readable, machine processable
notation for declarative syntax definition
Productions
49
module Numbers
imports NumberConstants
context-free syntax
Exp = IntConst
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
1 + 3 <= 4 - 35 & 12 > 16
Encoding Sequences (Lists)
50
module Control-Flow
imports Identifiers
imports Variables
context-free syntax
Exp = "(" ExpList ")"
Exp = "if" Exp "then" Exp "else" Exp
Exp = "if" Exp "then" Exp
Exp = "while" Exp "do" Exp
Exp = "for" Var ":=" Exp "to" Exp "do" Exp
ExpList = // empty list
ExpList = ExpList ";" Exp
function printboard() = (
for i := 0 to N-1 do (
for j := 0 to N-1 do
print(if col[i]=j then " O" else " .");
print("n")
);
print("n")
)
Sugar for Sequences and Optionals
51
context-free syntax
Exp = "(" {Exp ";"}* ")"
context-free syntax
// automatically generated
{Exp ";"}* = // empty list
{Exp ";"}* = {Exp ";"}* ";" Exp
{Exp ";"}+ = Exp
{Exp ";"}+ = {Exp ";"}+ ";" Exp
Exp* = // empty list
Exp* = Exp* Exp
Exp+ = Exp
Exp+ = Exp+ Exp
Exp? = // no expression
Exp? = Exp // one expression
function printboard() = (
for i := 0 to N-1 do (
for j := 0 to N-1 do
print(if col[i]=j then " O" else " .");
print("n")
);
print("n")
)
Using Sugar for Sequences
52
module Functions
imports Identifiers
imports Types
context-free syntax
Dec = FunDec+
FunDec = "function" Id "(" {FArg ","}* ")" "=" Exp
FunDec = "function" Id "(" {FArg ","}* ")" ":" Type "=" Exp
FArg = Id ":" Type
Exp = Id "(" {Exp ","}* ")"
let function power(x: int, n: int): int =
if n <= 0 then 1
else x * power(x, n - 1)
in power(3, 10)
end
module Bindings
imports Control-Flow Identifiers Types Functions Variables
sorts Declarations
context-free syntax
Exp = "let" Dec* "in" {Exp ";"}* "end"
Declarations = "declarations" Dec*
Structure
53
The set of rules, principles, and processes that govern
the structure of sentences in a given language
Language = Set of Sentences
54
fun (x : Int) { x + 1 }
Text is a convenient interface for writing and reading programs
Language = Set of Trees
55
Fun
AddArgDecl
VarRefId Int
“1”
VarDeclId
“x”
TXINT
“x”
Tree is a convenient interface for transforming programs
Tree Transformation
Semantic
transform
translate
eval
analyze
refactor
type check
Syntactic
coloring
outline view
completion
Tree is a convenient interface for transforming programs
Tree Transformation
57
Add
VarRefId VarRefId
“y”“x”
Mul
Int
“3”
Add
VarRefId VarRefId
“y”“x”
Mul
Int
“3”
Mul
Int
“3”
Add(Mul(Int("3"),
VarRefId("x")),
Mul(Int("3"),
VarRefId("y")))
Mul(Int("3"),
Add(VarRefId("x"),
VarRefId("y")))
Mul(e1, Add(e2, e3)) -> Add(Mul(e1, e2), Mul(e1, e3))
Context-free Grammar = Tree Construction Rules
58
Exp = Num
Exp = Exp “+” Exp
Exp = Exp “−” Exp
Exp = Exp “*” Exp
Exp = Exp “/” Exp
Exp = “(” Exp “)”
Exp
Num
Exp
+Exp Exp
Exp
−Exp Exp
Exp
*Exp Exp
Exp
/Exp Exp
Exp
Exp( )
=
Tree Construction
59
Exp
Exp
Exp
3 + *4 5
Exp
Exp Exp
Num
Exp
+Exp Exp
Exp
−Exp Exp
Exp
*Exp Exp
Exp
/Exp Exp
Exp
Exp( )
Parse trees
- interior nodes: nonterminal symbol

- leaf nodes: terminal symbols

Abstract syntax trees (ASTs)
- abstract over terminal symbols

- convey information at nodes

- abstract over injective production rules
Parse Tree vs Abstract Syntax Tree
60
Parse Tree vs Abstract Syntax Tree
61
Const
Mul
Const
3 4 5
Const
Add
Exp
Exp
Exp Exp
Exp
(3 + *4 5 )
Exp
Language = Sentences and Trees
62
parse
format
Different representations convenient for different purposes
From Text to Tree and Back
Add
VarRefId VarRefId
“y”“x”
Mul
Int
“3”
Add
VarRefId VarRefId
“y”“x”
Mul
Int
“3”
Mul
Int
“3”
3 * (x + y) (3 * x) + (3 * y)
parse
transform
format
SDF3 Productions Define Trees and Sentences
64
parse(s) = t where format(t) == s (modulo layout)
Exp.Int = INT
Exp.Add = Exp "+" Exp
Exp.Mul = Exp "*" Exp
trees
(structure)
parse
(text to tree)
format
(tree to text)
+ =>
Term is defined inductively as
- If t1,…,tn is a term, 

then c(t1,…,tn) is a term, 

where c is a constructor symbol
Basic Terms
65
Term is defined inductively as
- A string literal is a term

- An integer constant is a term 

- If t1, …, tn are terms, then 

‣ c(t1, …, tn) is a term, where c is a constructor symbol 

‣ (t1, …, tn) is a term (tuple)

‣ [t1, …, tn] is a term (list)
Terms
66
Parse Tree vs Abstract Syntax Tree vs Term
67
Const
Mul
Const
3 4 5
Const
Add
Exp
Exp
Exp Exp
Exp
(3 + *4 5 )
Exp Add(
Const(3),
Mul(
Const(4),
Const(5)))
Productions with Constructors
68
context-free syntax
Exp.Int = IntConst
Exp.Uminus = "-" Exp
Exp.Times = Exp "*" Exp
Exp.Divide = Exp "/" Exp
Exp.Plus = Exp "+" Exp
Exp.Minus = Exp "-" Exp
Exp.Eq = Exp "=" Exp
Exp.Neq = Exp "<>" Exp
Exp.Gt = Exp ">" Exp
Exp.Lt = Exp "<" Exp
Exp.Geq = Exp ">=" Exp
Exp.Leq = Exp "<=" Exp
Exp.And = Exp "&" Exp
Exp.Or = Exp "|" Exp
1 + 3 <= 4 - 35 & 12 > 16
And(
Leq(
Plus(Int("1"), Int("3"))
, Minus(Int("4"), Int("35"))
)
, Gt(Int("12"), Int("16"))
)
List Terms
69
module Control-Flow
imports Identifiers
imports Variables
context-free syntax
Exp.Seq = "(" {Exp ";"}* ")"
Exp.If = "if" Exp "then" Exp "else" Exp
Exp.IfThen = "if" Exp "then" Exp
Exp.While = "while" Exp "do" Exp
Exp.For = "for" Var ":=" Exp "to" Exp "do" Exp
Exp.Break = "break"
(
for j := 0 to N-1 do
print(if col[i]=j then " O" else " .");
print("n")
)
Seq(
[ For(
Var("j")
, Int("0")
, Minus(Var("N"), Int("1"))
, Call(
"print"
, [ If(
Eq(Subscript(Var("col"), Var("i")), Var("j"))
, String("" O"")
, String("" ."")
)
]
)
)
, Call("print", [String(""n"")])
]
)
More List Terms
70
module Functions
imports Identifiers
imports Types
context-free syntax
Dec = FunDec+
FunDec = "function" Id "(" {FArg ","}* ")" "=" Exp
FunDec = "function" Id "(" {FArg ","}* ")" ":" Type "=" Exp
FArg = Id ":" Type
Exp = Id "(" {Exp ","}* ")"
function power(x: int, n: int): int =
if n <= 0 then 1
else x * power(x, n - 1)
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"))]
)
)
)
)
Testing Syntax Definitions
with SPT
71
Are you defining the right language?
Test Suites
72
module example-suite
language Tiger
start symbol Start
test name
[[...]]
parse succeeds
test another name
[[...]]
parse fails
SPT: SPoofax Testing language
Success is Failure, Failure is Success, …
73
module success-failure
language Tiger
test this test succeeds [[
1 + 3 * 4
]] parse succeeds
test this test fails [[
1 + 3 * 4
]] parse fails
test this test fails [[
1 + 3 *
]] parse succeeds
test this test succeeds [[
1 + 3 *
]] parse fails
Test Cases: Valid
74
Language
valid program
test valid program
[[...]]
parse succeeds
Test Cases: Invalid
75
test invalid program
[[...]]
parse fails
invalid program
Language
Test Cases
76
module syntax/identifiers
language Tiger start symbol Id
test single lower case [[x]] parse succeeds
test single upper case [[X]] parse succeeds
test single digit [[1]] parse fails
test single lc digit [[x1]] parse succeeds
test single digit lc [[1x]] parse fails
test single uc digit [[X1]] parse succeeds
test single digit uc [[1X]] parse fails
test double digit [[11]] parse fails
Test Corner Cases
77
Language
Testing Structure
78
module structure
language Tiger
test add times [[
21 + 14 + 7
]] parse to Mod(Plus(Int(“21"),Times(Int("14"),Int("7"))))
test times add [[
3 * 7 + 21
]] parse to Mod(Plus(Times(Int(“3"),Int("7")),Int("21")))
test if [[
if x then 3 else 4
]] parse to Mod(If(Var("x"),Int("3"),Int("4")))
The fragment did not parse to the expected ATerm. Parse result was:
Mod(Plus(Plus(Int("21"),Int("14")),Int("7"))) Expected result was:
Mod(Plus(Int(21), Times(Int(14), Int(7))))
Testing Ambiguity
79
Note : this does not actually work in Tiger, since () is a
sequencing construct; but works fine in Mini-Java
module 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]]
Testing Ambiguity
80
module precedence
language Tiger start symbol Exp
test plus/times priority [[
x + 4 * 5
]] parse to Plus(Var("x"), Times(Int("4"), Int("5")))
test plus/times sequence [[
(x + 4) * 5
]] parse to Times(
Seq([Plus(Var("x"), Int("4"))])
, Int("5")
)
Syntax Engineering in
Spoofax
81
Developing syntax definition
- Define syntax of language in multiple modules

- Syntax checking, colouring

- Checking for undefined non-terminals

Testing syntax definition
- Write example programs in editor for language under def

- Inspect abstract syntax terms

‣ Spoofax > Syntax > Show Parsed AST

- Write SPT test for success and failure cases

‣ Updated after build of syntax definition
Syntax Engineering in Spoofax
83
Ambiguity &
Disambiguation
84
Ambiguity
85
Exp
Exp
Exp
3 + *4 5
Exp
Exp
Exp
Exp
Exp
3 + *4 5
Exp
Exp
Exp
Num
Exp
+Exp Exp
Exp
−Exp Exp
Exp
*Exp Exp
Exp
/Exp Exp
Exp
Exp( )
Syntax trees
- different trees for same sentence

Derivations
- different leftmost derivations for same sentence

- different rightmost derivations for same sentence

- NOT just different derivations for same sentence
Ambiguity
86
Ambiguity
t1 != t2 / format(t1) = format(t2)
Add
VarRef VarRef
“y”“x”
Mul
Int
“3”
Add
VarRef
VarRef
“y”
“x”
Mul
Int
“3”
3 * x + y
Debugging Ambiguities
Declarative Disambiguation
parse filter
Disambiguation Filters [Klint & Visser; 1994], [Van den Brand, Scheerder, Vinju, Visser; CC 2002]
Priority and Associativity
context-free syntax
Expr.Int = INT
Expr.Add = <<Expr> + <Expr>> {left}
Expr.Mul = <<Expr> * <Expr>> {left}
context-free priorities
Expr.Mul > Expr.Add
Recent improvement: safe disambiguation of operator precedence [Afroozeh et al. SLE13, Onward15]
Add
VarRef VarRef
“y”“x”
Mul
Int
“3”
Add
VarRef
VarRef
“y”
“x”
Mul
Int
“3”
3 * x + y
Tiger Disambiguation
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
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 Disambiguation
Except where otherwise noted, this work is licensed under
Attribution
94
slide title author license
21-22, 38-40 Noam Chomsky Fellowsisters CC BY-NC-SA 2.0

More Related Content

What's hot

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

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
 
Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing Compiler Construction | Lecture 4 | Parsing
Compiler Construction | Lecture 4 | Parsing
 
Programming languages
Programming languagesProgramming languages
Programming languages
 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual Machines
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
 
Declarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term RewritingDeclarative Semantics Definition - Term Rewriting
Declarative Semantics Definition - Term Rewriting
 
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
 
Introduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented LanguagesIntroduction - Imperative and Object-Oriented Languages
Introduction - Imperative and Object-Oriented Languages
 
Static name resolution
Static name resolutionStatic name resolution
Static name resolution
 
Dynamic Semantics
Dynamic SemanticsDynamic Semantics
Dynamic Semantics
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical 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
 
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 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 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersCompiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | Interpreters
 
Dynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter GenerationDynamic Semantics Specification and Interpreter Generation
Dynamic Semantics Specification and Interpreter Generation
 
Lex (lexical analyzer)
Lex (lexical analyzer)Lex (lexical analyzer)
Lex (lexical analyzer)
 
Declare Your Language (at DLS)
Declare Your Language (at DLS)Declare Your Language (at DLS)
Declare Your Language (at DLS)
 
6 attributed grammars
6  attributed grammars6  attributed grammars
6 attributed grammars
 
Writing Parsers and Compilers with PLY
Writing Parsers and Compilers with PLYWriting Parsers and Compilers with PLY
Writing Parsers and Compilers with PLY
 

Similar to Declare Your Language: Syntax Definition

Compiler_Project_Srikanth_Vanama
Compiler_Project_Srikanth_VanamaCompiler_Project_Srikanth_Vanama
Compiler_Project_Srikanth_Vanama
Srikanth Vanama
 
Dsm as theory building
Dsm as theory buildingDsm as theory building
Dsm as theory building
ClarkTony
 
match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdf
arpitaeron555
 
Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#
Robert Pickering
 

Similar to Declare Your Language: Syntax Definition (20)

Plc part 2
Plc  part 2Plc  part 2
Plc part 2
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
Compiler_Project_Srikanth_Vanama
Compiler_Project_Srikanth_VanamaCompiler_Project_Srikanth_Vanama
Compiler_Project_Srikanth_Vanama
 
Language processors
Language processorsLanguage processors
Language processors
 
Using Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPUUsing Language Oriented Programming to Execute Computations on the GPU
Using Language Oriented Programming to Execute Computations on the GPU
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATORPSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
PSEUDOCODE TO SOURCE PROGRAMMING LANGUAGE TRANSLATOR
 
Dsm as theory building
Dsm as theory buildingDsm as theory building
Dsm as theory building
 
Computational model language and grammar bnf
Computational model language and grammar bnfComputational model language and grammar bnf
Computational model language and grammar bnf
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
C program compiler presentation
C program compiler presentationC program compiler presentation
C program compiler presentation
 
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGESOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
 
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGESOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
SOFTWARE TOOL FOR TRANSLATING PSEUDOCODE TO A PROGRAMMING LANGUAGE
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 
match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdf
 
role of lexical anaysis
role of lexical anaysisrole of lexical anaysis
role of lexical anaysis
 
Unit1.ppt
Unit1.pptUnit1.ppt
Unit1.ppt
 
lec00-Introduction.pdf
lec00-Introduction.pdflec00-Introduction.pdf
lec00-Introduction.pdf
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdf
 
Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#
 

More from Eelco Visser

More from Eelco Visser (14)

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
 
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 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code Generation
 
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 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

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Recently uploaded (20)

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 

Declare Your Language: Syntax Definition

  • 1. Eelco Visser IN4303 Compiler Construction TU Delft September 2017 Declare Your Language Chapter 2: Syntax Definition
  • 3. The perspective of this lecture on declarative syntax definition is explained more elaborately in this Onward! 2010 essay. It uses an on older version of SDF than used in these slides. Production rules have the form X1 … Xn -> N {cons(“c”)} instead of N.c = X1 … Xn http://swerl.tudelft.nl/twiki/pub/Main/TechnicalReports/TUD-SERG-2010-019.pdf https://doi.org/10.1145/1932682.1869535
  • 4. The SPoofax Testing (SPT) language used in the section on testing syntax definitions was introduced in this OOPSLA 2011 paper. http://swerl.tudelft.nl/twiki/pub/Main/TechnicalReports/TUD-SERG-2011-011.pdf https://doi.org/10.1145/2076021.2048080
  • 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
  • 7. Lab Organization • Course website:
 https://tudelft-in4303-2017.github.io • Lab assignments distributed through GitHub • Detailed guide for doing lab assignments:
 https://tudelft-in4303-2017.github.io/documentation/ git.html
  • 8. Lab assignment logistics • Submit your netid and GitHub username on WebLab
 (https://weblab.tudelft.nl/in4303/2017-2018/assignment/15274/view) • Grades are published on WebLab • You will get read access to our repository for you:
 https://github.com/TUDelft-IN4303-2017/student-<netid> • Fork the repository — you can work from this template • Submit a pull-request (PR) to our repository to submit your solution • On the deadline the PR gets merged automatically, then graded
  • 9. Lab assignment logistics • The branch of the PR should be called assignment1 for the first assignment, assignment2 for the second, etc. • We push new branches for new assignments to our repository • Sometimes you are instructed to reuse a previous solution • When a PR is opened or changes are pushed, you can get early feedback • This feedback is based on an automated grading system • Early feedback is given only a few times • So be strategic about when you open the PR and when you push changes
  • 11. What is wrong with this program? 11 package org.metaborg.lang.tiger.interpreter.natives; import org.metaborg.lang.tiger.interpreter.natives.addI_2NodeGen; import org.metaborg.meta.lang.dynsem.interpreter.nodes.building.TermBuild; import com.oracle.truffle.api.source.SourceSection; public abstract class addI_2 extends TermBuild { public addI_2(SourceSection source) { super(source) } public int doInt(int left, int right) { left + right } public static TermBuild create(SourceSection source, TermBuild left, TermBuild right) { addI_2NodeGen.create(source, left, right) } }
  • 12. What is wrong with this program? 12 let def int power(x: int, n: int) = if n <= 0 then 1 else x * power(x, n - 1) in power(3, 10) end
  • 14. What is Syntax? 14 https://en.wikipedia.org/wiki/Syntax In mathematics, syntax refers to the rules governing the behavior of mathematical systems, such as formal languages used in logic. (See logical syntax.) In linguistics, syntax (/ˈsɪntæks/[1][2]) is the set of rules, principles, and processes that govern the structure of sentences in a given language, specifically word order and punctuation. The term syntax is also used to refer to the study of such principles and processes.[3] The goal of many syntacticians is to discover the syntactic rules common to all languages. The word syntax comes from Ancient Greek: σύνταξις "coordination", which consists of σύν syn, "together," and τάξις táxis, "an ordering".
  • 15. Syntax (Programming Languages) 15 In computer science, the syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be a correctly structured document or fragment in that language. This applies both to programming languages, where the document represents source code, and markup languages, where the document represents data. The syntax of a language defines its surface form.[1] Text-based computer languages are based on sequences of characters, while visual programming languages are based on the spatial layout and connections between symbols (which may be textual or graphical). Documents that are syntactically invalid are said to have a syntax error. https://en.wikipedia.org/wiki/Syntax_(programming_languages)
  • 16. Syntax - The set of rules, principles, and processes that govern the structure of sentences in a given language - The set of rules that defines the combinations of symbols that are considered to be a correctly structured document or fragment in that language How to describe such a set of rules? - An algorithm that recognizes (the structure of) well- formed sentences Syntax Definition 16
  • 17. Parsing programs during compilation Checking syntax in an editor Coloring tokens in an editor Formatting a program (after refactoring it) Proposing completions in an editor Randomly generating well-formed test programs Explaining the syntax rules to a programmer What is the Purpose of a Syntax Definition? 17
  • 18. Parsing programs during compilation - an algorithm that recognizes sentence and constructs structure Checking syntax in an editor - an algorithm that finds syntax errors in program text (and recovers from previous errors) Coloring tokens in an editor - assign colors to syntactic categories of tokens Formatting a program (after refactoring it) - map a syntax tree to text Proposing completions in an editor - determine what syntactic constructs are valid here Randomly generating well-formed test programs - some probabilistic algorithm? Explaining the syntax rules to a programmer - let them read the parsing algorithm for the language? How do you do that? 18
  • 19. Language Design - define the properties of a language - done by a language designer Language Implementation - implement tools that satisfy properties of the language - done by a language implementer Can we automate the language implementer? - that is what language workbenches attempt to do Separation of Concerns 19
  • 20. Objective - A workbench supporting design and implementation of programming languages Approach - Declarative multi-purpose domain-specific meta-languages Meta-Languages - Languages for defining languages Domain-Specific - Linguistic abstractions for domain of language definition (syntax, names, types, …) Multi-Purpose - Derivation of interpreters, compilers, rich editors, documentation, and verification from single source Declarative - Focus on what not how; avoid bias to particular purpose in language definition Declarative Language Definition 20
  • 21. Representation - Standardized representation for <aspect> of programs - Independent of specific object language Specification Formalism - Language-specific declarative rules - Abstract from implementation concerns Language-Independent Interpretation - Formalism interpreted by language-independent algorithm - Multiple interpretations for different purposes - Reuse between implementations of different languages A Recipe for Separation of Concerns 21
  • 22. Representation: (Abstract Syntax) Trees • Standardized representation for structure of programs • Basis for syntactic and semantic operations Formalism: Syntax Definition • Productions + Constructors + Templates + Disambiguation • Language-specific 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 Definition 22 A meta- language for talking about syntax }
  • 23. Formal Languages & Grammars 23 The set of rules, principles, and processes that govern the structure of sentences in a given language
  • 24. Language = Set of Sentences 24 fun (x : Int) { x + 1 } Text is a convenient interface for writing and reading programs
  • 25. Vocabulary Σ - finite, nonempty set of elements (words, letters) - alphabet String over Σ - finite sequence of elements chosen from Σ - word, sentence, utterance, program Formal language λ - set of strings over a vocabulary Σ - λ ⊆ Σ* Formal Language 25
  • 26. Formal grammar G Derivation relation G Formal language L(G) ⊆ Σ* - L(G) = {w∈Σ* | S G* w} Formal Grammar 26
  • 27. Decimal Numbers 27 G = (N, Σ, P, S) Num = Digit Num Num = Digit Digit = “0” Digit = “1” Digit = “2” Digit = “3” Digit = “4” Digit = “5” Digit = “6” Digit = “7” Digit = “8” Digit = “9”
  • 28. Decimal Numbers 28 G = (N, Σ, P, S) Num = Digit Num Num = Digit Digit = “0” Digit = “1” Digit = “2” Digit = “3” Digit = “4” Digit = “5” Digit = “6” Digit = “7” Digit = “8” Digit = “9” Σ: finite set of terminal symbols
  • 29. Decimal Numbers 29 G = (N, Σ, P, S) Num = Digit Num Num = Digit Digit = “0” Digit = “1” Digit = “2” Digit = “3” Digit = “4” Digit = “5” Digit = “6” Digit = “7” Digit = “8” Digit = “9” Σ: finite set of terminal symbols N: finite set of non-terminal symbols
  • 30. Decimal Numbers 30 G = (N, Σ, P, S) Num = Digit Num Num = Digit Digit = “0” Digit = “1” Digit = “2” Digit = “3” Digit = “4” Digit = “5” Digit = “6” Digit = “7” Digit = “8” Digit = “9” Σ: finite set of terminal symbols N: finite set of non-terminal symbols S∈N: start symbol
  • 31. Decimal Numbers 31 G = (N, Σ, P, S) Num = Digit Num Num = Digit Digit = “0” Digit = “1” Digit = “2” Digit = “3” Digit = “4” Digit = “5” Digit = “6” Digit = “7” Digit = “8” Digit = “9” Σ: finite set of terminal symbols N: finite set of non-terminal symbols S∈N: start symbol P⊆N×(N∪Σ)* : set of production rules
  • 32. Decimal Numbers: Production 32 Num Digit Num 4 Num 4 Digit Num 4 3 Num 4 3 Digit Num 4 3 0 Num 4 3 0 Digit 4 3 0 3
 leftmost derivation G = (N, Σ, P, S) Num = Digit Num Num = Digit Digit = “0” Digit = “1” Digit = “2” Digit = “3” Digit = “4” Digit = “5” Digit = “6” Digit = “7” Digit = “8” Digit = “9” Num = Digit Num Digit = “4” Num = Digit Num Digit = “3” Num = Digit Num Digit = “0” Num = Digit Digit = “3”
  • 33. Decimal Numbers: Production 33 Num Digit Num Digit Digit Num Digit Digit Digit Num Digit Digit Digit Digit Digit Digit Digit 3 Digit Digit 0 3 Digit 3 0 3 4 3 0 3
 Num = Digit Num Num = Digit Num Num = Digit Num Num = Digit Digit = “3” Digit = “0” Digit = “3” Digit = “4” rightmost derivation G = (N, Σ, P, S) Num = Digit Num Num = Digit Digit = “0” Digit = “1” Digit = “2” Digit = “3” Digit = “4” Digit = “5” Digit = “6” Digit = “7” Digit = “8” Digit = “9”
  • 34. Step - replace non-terminal symbol in string by symbols in rhs of production Derivation - repeatedly apply steps starting with start symbol - until string consists only of terminal symbols Leftmost Derivation - replace leftmost non-terminal symbol in string Rightmost Derivation - replace rightmost non-terminal in string Leftmost vs Rightmost Derivation 34
  • 35. Binary Expressions 35 G = (N, Σ, P, S) Exp = Num Exp = Exp “+” Exp Exp = Exp “−” Exp Exp = Exp “*” Exp Exp = Exp “/” Exp Exp = “(” Exp “)”
  • 36. Binary Expressions 36 G = (N, Σ, P, S) Exp = Num Exp = Exp “+” Exp Exp = Exp “−” Exp Exp = Exp “*” Exp Exp = Exp “/” Exp Exp = “(” Exp “)” Σ: finite set of terminal symbols
  • 37. Binary Expressions 37 G = (N, Σ, P, S) Exp = Num Exp = Exp “+” Exp Exp = Exp “−” Exp Exp = Exp “*” Exp Exp = Exp “/” Exp Exp = “(” Exp “)” Σ: finite set of terminal symbols N: finite set of non-terminal symbols
  • 38. Binary Expressions 38 G = (N, Σ, P, S) Exp = Num Exp = Exp “+” Exp Exp = Exp “−” Exp Exp = Exp “*” Exp Exp = Exp “/” Exp Exp = “(” Exp “)” Σ: finite set of terminal symbols N: finite set of non-terminal symbols S∈N: start symbol
  • 39. Binary Expressions 39 G = (N, Σ, P, S) Exp = Num Exp = Exp “+” Exp Exp = Exp “−” Exp Exp = Exp “*” Exp Exp = Exp “/” Exp Exp = “(” Exp “)” Σ: finite set of terminal symbols N: finite set of non-terminal symbols S∈N: start symbol P⊆N×(N∪Σ)* : set of production rules
  • 40. Binary Expressions 40 Exp Exp + Exp Exp + Exp * Exp 3 + Exp * Exp 3 + 4 * Exp 3 + 4 * 5
 Exp = Exp “+” Exp Exp = Exp “*” Exp Exp = Num Exp = Num Exp = Num
  • 41. Formal grammar G = (N, Σ, P, S) - nonterminal symbols N - terminal symbols Σ - production rules P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* - start symbol S∈N Generative Grammars 41 nonterminal symbol context replacement
  • 42. 42 Chomsky Hierarchy Type-0: Unrestricted - P ⊆ (N∪Σ)* × (N∪Σ)* - a = b Type-1: Context-sensitive - P ⊆ (N∪Σ)* N (N∪Σ)* × (N∪Σ)* - a A c = a b c Type-2: Context-free - P ⊆ N × (N∪Σ)* - A = b Type-3: Regular - P ⊆ N × (Σ ∪ ΣN) - A = x or A = xB a, b, c range over (N∪Σ)* A, B range over N
  • 44. Formal grammar G = (N, Σ, P, S) Derivation relation G ⊆ (N∪Σ)* × (N∪Σ)* w G w’ ∃(p, q)∈P: ∃u,v∈(N∪Σ)*: w=u p v ∧ w’=u q v Formal language L(G) ⊆ Σ* L(G) = {w∈Σ* | S * G w} Formal Grammars 44
  • 45. Generation - Given a grammar, generate a sentence in the language Language - The set of all sentences generated by the grammar Recognition - Is this sentence (word) in the language generated by the grammar? - Is there an algorithm for deciding that question? Recognition: Word Problem 45
  • 46. Word problem χL: Σ*→ {0,1} - w → 1, if w∈L - w → 0, else Decidability - type-0: semi-decidable - type-1, type-2, type-3: decidable Complexity - type-1: PSPACE-complete - type-2, type-3: P Recognition: Word Problem 46
  • 47. Recognizing Binary Expressions 47 3 + 4 * 5 Exp + 4 * 5 Exp + Exp * 5 Exp + Exp * Exp Exp + Exp Exp Exp = Num Exp = Num Exp = Num Exp = Exp “*” Exp Exp = Exp “+” Exp Replace substring that matches right-hand side of production with left-hand-side
  • 48. The Syntax Definition Formalism SDF3 48 A human readable, machine processable notation for declarative syntax definition
  • 49. Productions 49 module Numbers imports NumberConstants context-free syntax Exp = IntConst 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 1 + 3 <= 4 - 35 & 12 > 16
  • 50. Encoding Sequences (Lists) 50 module Control-Flow imports Identifiers imports Variables context-free syntax Exp = "(" ExpList ")" Exp = "if" Exp "then" Exp "else" Exp Exp = "if" Exp "then" Exp Exp = "while" Exp "do" Exp Exp = "for" Var ":=" Exp "to" Exp "do" Exp ExpList = // empty list ExpList = ExpList ";" Exp function printboard() = ( for i := 0 to N-1 do ( for j := 0 to N-1 do print(if col[i]=j then " O" else " ."); print("n") ); print("n") )
  • 51. Sugar for Sequences and Optionals 51 context-free syntax Exp = "(" {Exp ";"}* ")" context-free syntax // automatically generated {Exp ";"}* = // empty list {Exp ";"}* = {Exp ";"}* ";" Exp {Exp ";"}+ = Exp {Exp ";"}+ = {Exp ";"}+ ";" Exp Exp* = // empty list Exp* = Exp* Exp Exp+ = Exp Exp+ = Exp+ Exp Exp? = // no expression Exp? = Exp // one expression function printboard() = ( for i := 0 to N-1 do ( for j := 0 to N-1 do print(if col[i]=j then " O" else " ."); print("n") ); print("n") )
  • 52. Using Sugar for Sequences 52 module Functions imports Identifiers imports Types context-free syntax Dec = FunDec+ FunDec = "function" Id "(" {FArg ","}* ")" "=" Exp FunDec = "function" Id "(" {FArg ","}* ")" ":" Type "=" Exp FArg = Id ":" Type Exp = Id "(" {Exp ","}* ")" let function power(x: int, n: int): int = if n <= 0 then 1 else x * power(x, n - 1) in power(3, 10) end module Bindings imports Control-Flow Identifiers Types Functions Variables sorts Declarations context-free syntax Exp = "let" Dec* "in" {Exp ";"}* "end" Declarations = "declarations" Dec*
  • 53. Structure 53 The set of rules, principles, and processes that govern the structure of sentences in a given language
  • 54. Language = Set of Sentences 54 fun (x : Int) { x + 1 } Text is a convenient interface for writing and reading programs
  • 55. Language = Set of Trees 55 Fun AddArgDecl VarRefId Int “1” VarDeclId “x” TXINT “x” Tree is a convenient interface for transforming programs
  • 56. Tree Transformation Semantic transform translate eval analyze refactor type check Syntactic coloring outline view completion Tree is a convenient interface for transforming programs
  • 57. Tree Transformation 57 Add VarRefId VarRefId “y”“x” Mul Int “3” Add VarRefId VarRefId “y”“x” Mul Int “3” Mul Int “3” Add(Mul(Int("3"), VarRefId("x")), Mul(Int("3"), VarRefId("y"))) Mul(Int("3"), Add(VarRefId("x"), VarRefId("y"))) Mul(e1, Add(e2, e3)) -> Add(Mul(e1, e2), Mul(e1, e3))
  • 58. Context-free Grammar = Tree Construction Rules 58 Exp = Num Exp = Exp “+” Exp Exp = Exp “−” Exp Exp = Exp “*” Exp Exp = Exp “/” Exp Exp = “(” Exp “)” Exp Num Exp +Exp Exp Exp −Exp Exp Exp *Exp Exp Exp /Exp Exp Exp Exp( ) =
  • 59. Tree Construction 59 Exp Exp Exp 3 + *4 5 Exp Exp Exp Num Exp +Exp Exp Exp −Exp Exp Exp *Exp Exp Exp /Exp Exp Exp Exp( )
  • 60. Parse trees - interior nodes: nonterminal symbol - leaf nodes: terminal symbols Abstract syntax trees (ASTs) - abstract over terminal symbols - convey information at nodes - abstract over injective production rules Parse Tree vs Abstract Syntax Tree 60
  • 61. Parse Tree vs Abstract Syntax Tree 61 Const Mul Const 3 4 5 Const Add Exp Exp Exp Exp Exp (3 + *4 5 ) Exp
  • 62. Language = Sentences and Trees 62 parse format Different representations convenient for different purposes
  • 63. From Text to Tree and Back Add VarRefId VarRefId “y”“x” Mul Int “3” Add VarRefId VarRefId “y”“x” Mul Int “3” Mul Int “3” 3 * (x + y) (3 * x) + (3 * y) parse transform format
  • 64. SDF3 Productions Define Trees and Sentences 64 parse(s) = t where format(t) == s (modulo layout) Exp.Int = INT Exp.Add = Exp "+" Exp Exp.Mul = Exp "*" Exp trees (structure) parse (text to tree) format (tree to text) + =>
  • 65. Term is defined inductively as - If t1,…,tn is a term, 
 then c(t1,…,tn) is a term, 
 where c is a constructor symbol Basic Terms 65
  • 66. Term is defined inductively as - A string literal is a term - An integer constant is a term - If t1, …, tn are terms, then ‣ c(t1, …, tn) is a term, where c is a constructor symbol ‣ (t1, …, tn) is a term (tuple) ‣ [t1, …, tn] is a term (list) Terms 66
  • 67. Parse Tree vs Abstract Syntax Tree vs Term 67 Const Mul Const 3 4 5 Const Add Exp Exp Exp Exp Exp (3 + *4 5 ) Exp Add( Const(3), Mul( Const(4), Const(5)))
  • 68. Productions with Constructors 68 context-free syntax Exp.Int = IntConst Exp.Uminus = "-" Exp Exp.Times = Exp "*" Exp Exp.Divide = Exp "/" Exp Exp.Plus = Exp "+" Exp Exp.Minus = Exp "-" Exp Exp.Eq = Exp "=" Exp Exp.Neq = Exp "<>" Exp Exp.Gt = Exp ">" Exp Exp.Lt = Exp "<" Exp Exp.Geq = Exp ">=" Exp Exp.Leq = Exp "<=" Exp Exp.And = Exp "&" Exp Exp.Or = Exp "|" Exp 1 + 3 <= 4 - 35 & 12 > 16 And( Leq( Plus(Int("1"), Int("3")) , Minus(Int("4"), Int("35")) ) , Gt(Int("12"), Int("16")) )
  • 69. List Terms 69 module Control-Flow imports Identifiers imports Variables context-free syntax Exp.Seq = "(" {Exp ";"}* ")" Exp.If = "if" Exp "then" Exp "else" Exp Exp.IfThen = "if" Exp "then" Exp Exp.While = "while" Exp "do" Exp Exp.For = "for" Var ":=" Exp "to" Exp "do" Exp Exp.Break = "break" ( for j := 0 to N-1 do print(if col[i]=j then " O" else " ."); print("n") ) Seq( [ For( Var("j") , Int("0") , Minus(Var("N"), Int("1")) , Call( "print" , [ If( Eq(Subscript(Var("col"), Var("i")), Var("j")) , String("" O"") , String("" ."") ) ] ) ) , Call("print", [String(""n"")]) ] )
  • 70. More List Terms 70 module Functions imports Identifiers imports Types context-free syntax Dec = FunDec+ FunDec = "function" Id "(" {FArg ","}* ")" "=" Exp FunDec = "function" Id "(" {FArg ","}* ")" ":" Type "=" Exp FArg = Id ":" Type Exp = Id "(" {Exp ","}* ")" function power(x: int, n: int): int = if n <= 0 then 1 else x * power(x, n - 1) 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"))] ) ) ) )
  • 71. Testing Syntax Definitions with SPT 71 Are you defining the right language?
  • 72. Test Suites 72 module example-suite language Tiger start symbol Start test name [[...]] parse succeeds test another name [[...]] parse fails SPT: SPoofax Testing language
  • 73. Success is Failure, Failure is Success, … 73 module success-failure language Tiger test this test succeeds [[ 1 + 3 * 4 ]] parse succeeds test this test fails [[ 1 + 3 * 4 ]] parse fails test this test fails [[ 1 + 3 * ]] parse succeeds test this test succeeds [[ 1 + 3 * ]] parse fails
  • 74. Test Cases: Valid 74 Language valid program test valid program [[...]] parse succeeds
  • 75. Test Cases: Invalid 75 test invalid program [[...]] parse fails invalid program Language
  • 76. Test Cases 76 module syntax/identifiers language Tiger start symbol Id test single lower case [[x]] parse succeeds test single upper case [[X]] parse succeeds test single digit [[1]] parse fails test single lc digit [[x1]] parse succeeds test single digit lc [[1x]] parse fails test single uc digit [[X1]] parse succeeds test single digit uc [[1X]] parse fails test double digit [[11]] parse fails
  • 78. Testing Structure 78 module structure language Tiger test add times [[ 21 + 14 + 7 ]] parse to Mod(Plus(Int(“21"),Times(Int("14"),Int("7")))) test times add [[ 3 * 7 + 21 ]] parse to Mod(Plus(Times(Int(“3"),Int("7")),Int("21"))) test if [[ if x then 3 else 4 ]] parse to Mod(If(Var("x"),Int("3"),Int("4"))) The fragment did not parse to the expected ATerm. Parse result was: Mod(Plus(Plus(Int("21"),Int("14")),Int("7"))) Expected result was: Mod(Plus(Int(21), Times(Int(14), Int(7))))
  • 79. Testing Ambiguity 79 Note : this does not actually work in Tiger, since () is a sequencing construct; but works fine in Mini-Java module 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]]
  • 80. Testing Ambiguity 80 module precedence language Tiger start symbol Exp test plus/times priority [[ x + 4 * 5 ]] parse to Plus(Var("x"), Times(Int("4"), Int("5"))) test plus/times sequence [[ (x + 4) * 5 ]] parse to Times( Seq([Plus(Var("x"), Int("4"))]) , Int("5") )
  • 82.
  • 83. Developing syntax definition - Define syntax of language in multiple modules - Syntax checking, colouring - Checking for undefined non-terminals Testing syntax definition - Write example programs in editor for language under def - Inspect abstract syntax terms ‣ Spoofax > Syntax > Show Parsed AST - Write SPT test for success and failure cases ‣ Updated after build of syntax definition Syntax Engineering in Spoofax 83
  • 85. Ambiguity 85 Exp Exp Exp 3 + *4 5 Exp Exp Exp Exp Exp 3 + *4 5 Exp Exp Exp Num Exp +Exp Exp Exp −Exp Exp Exp *Exp Exp Exp /Exp Exp Exp Exp( )
  • 86. Syntax trees - different trees for same sentence Derivations - different leftmost derivations for same sentence - different rightmost derivations for same sentence - NOT just different derivations for same sentence Ambiguity 86
  • 87. Ambiguity t1 != t2 / format(t1) = format(t2) Add VarRef VarRef “y”“x” Mul Int “3” Add VarRef VarRef “y” “x” Mul Int “3” 3 * x + y
  • 89. Declarative Disambiguation parse filter Disambiguation Filters [Klint & Visser; 1994], [Van den Brand, Scheerder, Vinju, Visser; CC 2002]
  • 90. Priority and Associativity context-free syntax Expr.Int = INT Expr.Add = <<Expr> + <Expr>> {left} Expr.Mul = <<Expr> * <Expr>> {left} context-free priorities Expr.Mul > Expr.Add Recent improvement: safe disambiguation of operator precedence [Afroozeh et al. SLE13, Onward15] Add VarRef VarRef “y”“x” Mul Int “3” Add VarRef VarRef “y” “x” Mul Int “3” 3 * x + y
  • 91. Tiger Disambiguation 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
  • 92. 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 Disambiguation
  • 93. Except where otherwise noted, this work is licensed under
  • 94. Attribution 94 slide title author license 21-22, 38-40 Noam Chomsky Fellowsisters CC BY-NC-SA 2.0