SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Downloaden Sie, um offline zu lesen
Language-Parametric Name
         Resolution Based on
       Declarative Name Binding
           and Scope Rules

Gabriël Konat, Lennart Kats, Guido Wachsmuth, Eelco Visser
Language-Parametric Name
         Resolution Based on
       Declarative Name Binding
           and Scope Rules

Gabriël Konat, Lennart Kats, Guido Wachsmuth, Eelco Visser
Name Binding and Scope



function power(x: Int, n: Int): Int {
  if(n == 0) {
    return 1;
  } else {
    return x * power(x, n - 1);
  }
}
Fundamentalist Domain-Specific
       Programming
Populist Domain-Specific Programming

Internal DSL

Programmatic encoding

Limited abstraction from implementation strategy

Bound to specific host language

Captures implementation, not understanding
Context-free Grammars

"return" Exp ";"                                    -> Stm {"Return"}
Type ID ";"                                         -> Stm {"VarDef"}
Type ID "=" Exp ";"                                 -> Stm {"VarDef"}
ID "=" Exp ";"	 	 	 	 	 	                           -> Stm {"Assign"}
"if" "(" Exp ")" Block                              -> Stm {"If"}
"if" "(" Exp ")" Block "else" Block                 -> Stm {"IfElse"}
"for" "(" Type ID "=" Exp ";" Exp ";" Stm ")" Block -> Stm {"For"}
"{" Stm* "}"                                        -> Block {"Block"}
Block                                               -> Stm



                            for (int i = i; i <10 ; i = i + 1;) {
                              int i;
                              i = i + 1;
                            }



 Pure and declarative syntax definition: paradise lost and regained. Lennart C. L. Kats, Eelco Visser, Guido Wachsmuth.
 Onward 2010: 918-932
Fundamentalist Domain-Specific Programming


    External DSL

    Abstraction from implementation strategy

    Not bound to specific host language

    Captures understanding
    (and room for many implementations)
Name Binding and Scope
Name Binding and Scope


static checking

editor services

transformation

  refactoring

code generation
Reference Resolution (Navigation)
Code Completion
Classical Approaches
Type Systems




Source: Luca Cardelli. Type Systems. In Handbook of Computer Science and Engineering,
Chapter 103. CRC Press, 1997.
Modular Structural Operational Semantics




Source: Peter Mosses. Modular SOS: Differences from SOS.
First APPSEM-II Workshop, Nottingham, March 2003
Reference Attribute Grammars
Source: Görel Hedin


                                                                                   eq	
  Program.getBlock().lookup(String	
  id)	
  =	
  
                     Program                                                       	
  	
  null;
                  eq	
  lookup	
  =	
  …	
  


                                                                                   syn	
  Block.localLookup(String	
  id)	
  {
                                                                                   	
  	
  for	
  (Decl	
  d	
  :	
  getDecls())	
  {
                                           lookup(String)                          	
  	
  	
  	
  if	
  (d.getID().equals(id))
                        Block             localLookup(String)                      	
  	
  	
  	
  	
  	
  return	
  d;
                                                                                   	
  	
  }
               eq	
  lookup	
  =	
  …	
  
                                                                                   	
  	
  return	
  null;
                                                                                   }


  Decl                                                            lookup(String)
                                            Block            localLookup(String)
                                     eq	
  lookup	
  =	
  …	
  
                                                                                   eq	
  Block.getChild(int	
  i).lookup(String	
  id)	
  {
                                                                                   	
  	
  Decl	
  d	
  =	
  localLookup(id);
                                                                                   	
  	
  if	
  (d	
  !=	
  null)	
  return	
  d;
                                                                                   	
  	
  return	
  lookup(id);
           Decl                                                                    }

                         lookup(String)
                                                        Use                        syn	
  Decl	
  Use.decl	
  =	
  lookup(getID());
                                               decl
                                                                                   inh	
  Decl	
  Use.lookup(String);
Rewriting Strategies with Dynamic Rules
    rename-top = alltd(rename)

    rename :
      |[ var x : srt ]| -> |[ var y : srt ]|
      where y := <add-naming-key(|srt)> x

    rename :
      |[ define page x (farg1*) { elem1* } ]| ->
      |[ define page x (farg2*) { elem2* } ]|
      where {| Rename
             : farg2* := <map(rename-page-arg)> farg1*
             ; elem2* := <rename-top> elem1*
             |}

    rename = Rename

    add-naming-key(|srt) : x -> y
      where y := x{<newname> x}
          ; rules (
              Rename : Var(x) -> Var(y)
              TypeOf : y -> srt
            )
What is the Problem?
What is the Problem?

Mental model:

Name binding is defined in terms of
programmatic encoding of name resolution

Rules encoded in many language operations

Abstractions try to reduce overhead
of the programmatic encoding
Name Binding in Xtext

grammar DomainModel with Terminals

Domainmodel :
  elements += Type*
;
Type:
  DataType | Entity
;
DataType:
  'datatype' name = ID
;
Entity:
  'entity' name = ID ('extends' superType = [Entity])? '{'
     features += Feature*
  '}'
;
Feature:
  many?='many'? name = ID ':' type = [Type]
;
Name Binding Language
Definitions and Reference


                           class C {
Class(_, c, _, _):         }
  defines Class c
                           class D {
ClassType(c) :               C x;
  refers to Class c        }

Base(c) :                  class E : D {
  refers to Class c          C x;
                           }
Design Choice: Grammar Annotations


"class" Type@=ID "{" ClassBodyDecl* "}" -> Definition {"Class"}

Type@ID                                   -> Type {"ClassType"}




                      Class(_, c, _) :
                        defines Class c

                      ClassType(c) :
                        refers to Class c
Unique and Non-Unique Definitions

                                 class D {
                                   C1 x;
Class(NonPartial(), c, _, _) :   }
  defines unique Class c         class C1 {
                                   D d;
Class(Partial(), c, _, _) :      }
  defines non-unique Class c     class C2: C1 {
                                   Int i;
Base(c) :                        }
  refers to Class c              partial class C3: C2 {
                                   Int j;
ClassType(c) :                   }
  refers to Class c              partial class C3 {
                                   Int k;
                                 }
Namespaces
namespaces
  Class Method Field Variable

Class(NonPartial(), c, _, _):
  defines unique Class c

Field(_, f) :
                                    class x {
  defines unique Field f
                                      int x;
                                      void x() {
Method(_, m, _, _):
                                        int x; x = x + 1;
  defines unique Method m
                                      }
Call(m, _) :
                                    }
  refers to Method m

VarDef(_, v):
  defines unique Variable v
VarRef(x):
   refers to Variable x
   otherwise refers to Field x
Scope

                                       class C {
Class(NonPartial(), c, _, _):            int y;
  defines unique Class c                 void m() {
  scopes Field, Method                     int x;
                                           x = y + 1;
Class(Partial(), c, _, _):               }
  defines non-unique Class c           }
  scopes Field, Method
                                       class D {
Method(_, m, _, _):                      void m() {
  defines unique Method m                  int x;
  scopes Variable                          int y;
                                           { int x; x = y + 1; }
Block(_):                                  x = y + 1;
  scopes Variable                        }
                                       }
C# Namespaces are Scopes




                            namespace N {
                              class N {}
Namespace(n, _):
                              namespace N {
  defines Namespace n
                                class N {}
  scopes Namespace, Class
                              }
                            }
Imports



                                        using N;
Using(qn):
  imports Class from Namespace ns       namespace M {
  where qn refers to Namespace ns         class C { int f; }
                                        }
Base(c):
  imports   Field                       namespace O {
     from   Class c {transitive}          class E: M.C {
  imports   Method                          void m() {}
     from   Class c {transitive}          }
                                          class F:E { }
                                        }
Definition Context




                            class C {
Foreach(t, v, exp, body):     void m(int[] x) {
  defines Variable v            foreach (int x in x)
       of type t                  WriteLine(x);
       in body                }
                            }
Interaction with Type System (1)


                                  class C {
                                    int i;
                                  }

FieldAccess(e, f):                class D {
  refers to Field f in c            C c;
  where e has type ClassType(c)     int i;
                                    void init() {
                                      i = c.i;
                                    }
                                  }
Interaction with Type System (2)


                                  class C {
                                    int i;
                                    int get() { return i; }
Method(t, m, ps, _) :
                                  }
  defines Method m of type t

                                  class D {
MethodCall(e, m, _):
                                    C c;
  refers to Method m
                                    int i;
      of type t in c
                                    void init() {
  where e has type ClassType(c)
                                      i = c.get();
                                    }
                                  }
Interaction with Type System (3)

Method(t, m, ps, _) :             class C {
  defines unique Method m           void m() {}
       of type (ts, t)              void m(int x) {}
  where ps has type ts              void m(bool x) {}
                                    void m(int x, int y) {}
Param(t, p):                        void m(bool x, bool y) {}
  defines unique Variable p         void x() {
       of type t                      m();
                                      m(42);
MethodCall(e, m, es):                 m(true);
  refers to Method m                  m(21, 21);
      of type (ts, t) in c            m(true, false);
  where e has type ClassType(c)     }
  where es has type ts            }
Name Binding
                               class
 defines                    partial class
                               type
  refers                    inheritance

namespaces
                            namespace
                              using
  scopes
                             method
                               field
 imports                     variable
                            parameter
                              block
Name Binding Language in Spoofax (*)
Name Binding Language in Spoofax (*)


         derivation of editor services
                          checking
                      code completion
                    reference resolution


                   multi-file analysis

                  parallel evaluation

              incremental evaluation


      (*) alpha; NBL is compiled to Stratego implementation of
      name resolution algorithm (ask me for pointers)
Outlook

Language definition = CFG + NBD + TS + DS

           Single source for

        Language reference manual

      Efficient (incremental) compiler

      Execution engine (interpreter)

   Integrated development environment

Weitere ähnliche Inhalte

Was ist angesagt?

Creating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonCreating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonSiddhi
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Sanjeev_Knoldus
 
Constructor&method
Constructor&methodConstructor&method
Constructor&methodJani Harsh
 
C# v8 new features - raimundas banevicius
C# v8 new features - raimundas baneviciusC# v8 new features - raimundas banevicius
C# v8 new features - raimundas baneviciusRaimundas Banevičius
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3mohamedsamyali
 
Java 2 chapter 10 - basic oop in java
Java 2   chapter 10 - basic oop in javaJava 2   chapter 10 - basic oop in java
Java 2 chapter 10 - basic oop in javalet's go to study
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharpsarfarazali
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulationIntro C# Book
 
purely_functional_play_framework_application
purely_functional_play_framework_applicationpurely_functional_play_framework_application
purely_functional_play_framework_applicationNaoki Aoyama
 
The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181Mahmoud Samir Fayed
 

Was ist angesagt? (20)

Groovy intro for OUDL
Groovy intro for OUDLGroovy intro for OUDL
Groovy intro for OUDL
 
PDBC
PDBCPDBC
PDBC
 
Creating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonCreating Domain Specific Languages in Python
Creating Domain Specific Languages in Python
 
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
Scala traits training by Sanjeev Kumar @Kick Start Scala traits & Play, organ...
 
Scala 2013 review
Scala 2013 reviewScala 2013 review
Scala 2013 review
 
Constructor&method
Constructor&methodConstructor&method
Constructor&method
 
C# v8 new features - raimundas banevicius
C# v8 new features - raimundas baneviciusC# v8 new features - raimundas banevicius
C# v8 new features - raimundas banevicius
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
 
Algorithm and Programming (Array)
Algorithm and Programming (Array)Algorithm and Programming (Array)
Algorithm and Programming (Array)
 
Java 2 chapter 10 - basic oop in java
Java 2   chapter 10 - basic oop in javaJava 2   chapter 10 - basic oop in java
Java 2 chapter 10 - basic oop in java
 
Java Basics - Part1
Java Basics - Part1Java Basics - Part1
Java Basics - Part1
 
Scala best practices
Scala best practicesScala best practices
Scala best practices
 
Introduction To Csharp
Introduction To CsharpIntroduction To Csharp
Introduction To Csharp
 
Java Basics - Part2
Java Basics - Part2Java Basics - Part2
Java Basics - Part2
 
C# - What's next
C# - What's nextC# - What's next
C# - What's next
 
20.3 Java encapsulation
20.3 Java encapsulation20.3 Java encapsulation
20.3 Java encapsulation
 
OOP v3
OOP v3OOP v3
OOP v3
 
purely_functional_play_framework_application
purely_functional_play_framework_applicationpurely_functional_play_framework_application
purely_functional_play_framework_application
 
Parte II Objective C
Parte II   Objective CParte II   Objective C
Parte II Objective C
 
The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181The Ring programming language version 1.5.2 book - Part 37 of 181
The Ring programming language version 1.5.2 book - Part 37 of 181
 

Andere mochten auch

Andere mochten auch (20)

Grammarware Memes
Grammarware MemesGrammarware Memes
Grammarware Memes
 
I
II
I
 
Dayana lizeth gallego y july gallon
Dayana lizeth gallego y july gallonDayana lizeth gallego y july gallon
Dayana lizeth gallego y july gallon
 
Me acuerdo de_ti_(osingh)
Me acuerdo de_ti_(osingh)Me acuerdo de_ti_(osingh)
Me acuerdo de_ti_(osingh)
 
O netmath53
O netmath53O netmath53
O netmath53
 
Onet m6 52 math
Onet m6 52  mathOnet m6 52  math
Onet m6 52 math
 
Estructura pag 2
Estructura pag 2Estructura pag 2
Estructura pag 2
 
Estructura pag 2
Estructura pag 2Estructura pag 2
Estructura pag 2
 
Plan territorial de Empleo de La Palma 2008-2013
Plan territorial de Empleo de La Palma 2008-2013Plan territorial de Empleo de La Palma 2008-2013
Plan territorial de Empleo de La Palma 2008-2013
 
Auzoetako jaiak
Auzoetako jaiakAuzoetako jaiak
Auzoetako jaiak
 
Playstation 3 - Balão da Informática!
Playstation 3 - Balão da Informática!Playstation 3 - Balão da Informática!
Playstation 3 - Balão da Informática!
 
Projeto: Eu - Romero Britto
Projeto: Eu - Romero BrittoProjeto: Eu - Romero Britto
Projeto: Eu - Romero Britto
 
Plan territorial de Empleo Tenerife 2008-2013
Plan territorial de Empleo Tenerife 2008-2013Plan territorial de Empleo Tenerife 2008-2013
Plan territorial de Empleo Tenerife 2008-2013
 
Plan territorial de Empleo de Fuerteventura 2008-2013
Plan territorial de Empleo de Fuerteventura 2008-2013Plan territorial de Empleo de Fuerteventura 2008-2013
Plan territorial de Empleo de Fuerteventura 2008-2013
 
ใบงานที่ 11
ใบงานที่ 11ใบงานที่ 11
ใบงานที่ 11
 
Hábitos de aseo por Ana Maria Simbaña
Hábitos de aseo por Ana Maria Simbaña Hábitos de aseo por Ana Maria Simbaña
Hábitos de aseo por Ana Maria Simbaña
 
ใบงานที่ 5
ใบงานที่ 5ใบงานที่ 5
ใบงานที่ 5
 
Pwp prestecni
Pwp prestecniPwp prestecni
Pwp prestecni
 
Colegio nacional nicolas esguerra 2
Colegio nacional nicolas esguerra 2Colegio nacional nicolas esguerra 2
Colegio nacional nicolas esguerra 2
 
Colegio nacional nicolas esguerra 2
Colegio nacional nicolas esguerra 2Colegio nacional nicolas esguerra 2
Colegio nacional nicolas esguerra 2
 

Ähnlich wie Declarative Name Binding and Scope Rules

Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)gekiaruj
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systemsleague
 
Pointcuts and Analysis
Pointcuts and AnalysisPointcuts and Analysis
Pointcuts and AnalysisWiwat Ruengmee
 
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM LanguageCodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM LanguageCodeFest
 
Testing for share
Testing for share Testing for share
Testing for share Rajeev Mehta
 
Type Classes in Scala and Haskell
Type Classes in Scala and HaskellType Classes in Scala and Haskell
Type Classes in Scala and HaskellHermann Hueck
 
Csharp In Detail Part2
Csharp In Detail Part2Csharp In Detail Part2
Csharp In Detail Part2Mohamed Krar
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation streamRuslan Shevchenko
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)HamletDRC
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsJohn De Goes
 

Ähnlich wie Declarative Name Binding and Scope Rules (20)

Python 2.5 reference card (2009)
Python 2.5 reference card (2009)Python 2.5 reference card (2009)
Python 2.5 reference card (2009)
 
What's New In C# 7
What's New In C# 7What's New In C# 7
What's New In C# 7
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
 
Object-oriented Basics
Object-oriented BasicsObject-oriented Basics
Object-oriented Basics
 
An introduction to scala
An introduction to scalaAn introduction to scala
An introduction to scala
 
Pointcuts and Analysis
Pointcuts and AnalysisPointcuts and Analysis
Pointcuts and Analysis
 
C++11
C++11C++11
C++11
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM LanguageCodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
CodeFest 2010. Иноземцев И. — Fantom. Cross-VM Language
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
C# programming
C# programming C# programming
C# programming
 
Testing for share
Testing for share Testing for share
Testing for share
 
Type Classes in Scala and Haskell
Type Classes in Scala and HaskellType Classes in Scala and Haskell
Type Classes in Scala and Haskell
 
Interpreter Case Study - Design Patterns
Interpreter Case Study - Design PatternsInterpreter Case Study - Design Patterns
Interpreter Case Study - Design Patterns
 
Basic c#
Basic c#Basic c#
Basic c#
 
Csharp In Detail Part2
Csharp In Detail Part2Csharp In Detail Part2
Csharp In Detail Part2
 
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
JDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation streamJDays Lviv 2014:  Java8 vs Scala:  Difference points & innovation stream
JDays Lviv 2014: Java8 vs Scala: Difference points & innovation stream
 
Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
 
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & AnalyticsQuark: A Purely-Functional Scala DSL for Data Processing & Analytics
Quark: A Purely-Functional Scala DSL for Data Processing & Analytics
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 

Mehr von 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 RewritingEelco Visser
 
CS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesEelco Visser
 
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingCS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingEelco Visser
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionEelco Visser
 
CS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionCS4200 2019 Lecture 1: Introduction
CS4200 2019 Lecture 1: IntroductionEelco 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 RulesEelco Visser
 
Declarative Type System Specification with Statix
Declarative Type System Specification with StatixDeclarative Type System Specification with Statix
Declarative Type System Specification with StatixEelco 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 ConstructionEelco 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 ManagementEelco Visser
 
Compiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersCompiler Construction | Lecture 14 | Interpreters
Compiler Construction | Lecture 14 | InterpretersEelco Visser
 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationEelco Visser
 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesEelco Visser
 
Compiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone FrameworksCompiler Construction | Lecture 11 | Monotone Frameworks
Compiler Construction | Lecture 11 | Monotone FrameworksEelco 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 AnalysisEelco Visser
 
Compiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionEelco Visser
 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsEelco Visser
 
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingEelco 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 AnalysisEelco Visser
 
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 RewritingEelco Visser
 

Mehr von Eelco Visser (20)

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 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic ServicesCS4200 2019 | Lecture 4 | Syntactic Services
CS4200 2019 | Lecture 4 | Syntactic Services
 
CS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | ParsingCS4200 2019 | Lecture 3 | Parsing
CS4200 2019 | Lecture 3 | Parsing
 
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 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint ResolutionCompiler Construction | Lecture 9 | Constraint Resolution
Compiler Construction | Lecture 9 | Constraint Resolution
 
Compiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type ConstraintsCompiler Construction | Lecture 8 | Type Constraints
Compiler Construction | Lecture 8 | Type Constraints
 
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 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
 

Declarative Name Binding and Scope Rules

  • 1. Language-Parametric Name Resolution Based on Declarative Name Binding and Scope Rules Gabriël Konat, Lennart Kats, Guido Wachsmuth, Eelco Visser
  • 2. Language-Parametric Name Resolution Based on Declarative Name Binding and Scope Rules Gabriël Konat, Lennart Kats, Guido Wachsmuth, Eelco Visser
  • 3. Name Binding and Scope function power(x: Int, n: Int): Int { if(n == 0) { return 1; } else { return x * power(x, n - 1); } }
  • 5. Populist Domain-Specific Programming Internal DSL Programmatic encoding Limited abstraction from implementation strategy Bound to specific host language Captures implementation, not understanding
  • 6. Context-free Grammars "return" Exp ";" -> Stm {"Return"} Type ID ";" -> Stm {"VarDef"} Type ID "=" Exp ";" -> Stm {"VarDef"} ID "=" Exp ";" -> Stm {"Assign"} "if" "(" Exp ")" Block -> Stm {"If"} "if" "(" Exp ")" Block "else" Block -> Stm {"IfElse"} "for" "(" Type ID "=" Exp ";" Exp ";" Stm ")" Block -> Stm {"For"} "{" Stm* "}" -> Block {"Block"} Block -> Stm for (int i = i; i <10 ; i = i + 1;) { int i; i = i + 1; } Pure and declarative syntax definition: paradise lost and regained. Lennart C. L. Kats, Eelco Visser, Guido Wachsmuth. Onward 2010: 918-932
  • 7. Fundamentalist Domain-Specific Programming External DSL Abstraction from implementation strategy Not bound to specific host language Captures understanding (and room for many implementations)
  • 9. Name Binding and Scope static checking editor services transformation refactoring code generation
  • 13. Type Systems Source: Luca Cardelli. Type Systems. In Handbook of Computer Science and Engineering, Chapter 103. CRC Press, 1997.
  • 14. Modular Structural Operational Semantics Source: Peter Mosses. Modular SOS: Differences from SOS. First APPSEM-II Workshop, Nottingham, March 2003
  • 15. Reference Attribute Grammars Source: Görel Hedin eq  Program.getBlock().lookup(String  id)  =   Program    null; eq  lookup  =  …   syn  Block.localLookup(String  id)  {    for  (Decl  d  :  getDecls())  { lookup(String)        if  (d.getID().equals(id)) Block localLookup(String)            return  d;    } eq  lookup  =  …      return  null; } Decl lookup(String) Block localLookup(String) eq  lookup  =  …   eq  Block.getChild(int  i).lookup(String  id)  {    Decl  d  =  localLookup(id);    if  (d  !=  null)  return  d;    return  lookup(id); Decl } lookup(String) Use syn  Decl  Use.decl  =  lookup(getID()); decl inh  Decl  Use.lookup(String);
  • 16. Rewriting Strategies with Dynamic Rules rename-top = alltd(rename) rename : |[ var x : srt ]| -> |[ var y : srt ]| where y := <add-naming-key(|srt)> x rename : |[ define page x (farg1*) { elem1* } ]| -> |[ define page x (farg2*) { elem2* } ]| where {| Rename : farg2* := <map(rename-page-arg)> farg1* ; elem2* := <rename-top> elem1* |} rename = Rename add-naming-key(|srt) : x -> y where y := x{<newname> x} ; rules ( Rename : Var(x) -> Var(y) TypeOf : y -> srt )
  • 17. What is the Problem?
  • 18. What is the Problem? Mental model: Name binding is defined in terms of programmatic encoding of name resolution Rules encoded in many language operations Abstractions try to reduce overhead of the programmatic encoding
  • 19. Name Binding in Xtext grammar DomainModel with Terminals Domainmodel :   elements += Type* ; Type:   DataType | Entity ; DataType:   'datatype' name = ID ; Entity:   'entity' name = ID ('extends' superType = [Entity])? '{'      features += Feature*   '}' ; Feature:   many?='many'? name = ID ':' type = [Type] ;
  • 21. Definitions and Reference class C { Class(_, c, _, _): } defines Class c class D { ClassType(c) : C x; refers to Class c } Base(c) : class E : D { refers to Class c C x; }
  • 22. Design Choice: Grammar Annotations "class" Type@=ID "{" ClassBodyDecl* "}" -> Definition {"Class"} Type@ID -> Type {"ClassType"} Class(_, c, _) : defines Class c ClassType(c) : refers to Class c
  • 23. Unique and Non-Unique Definitions class D { C1 x; Class(NonPartial(), c, _, _) : } defines unique Class c class C1 { D d; Class(Partial(), c, _, _) : } defines non-unique Class c class C2: C1 { Int i; Base(c) : } refers to Class c partial class C3: C2 { Int j; ClassType(c) : } refers to Class c partial class C3 { Int k; }
  • 24. Namespaces namespaces Class Method Field Variable Class(NonPartial(), c, _, _): defines unique Class c Field(_, f) : class x { defines unique Field f int x; void x() { Method(_, m, _, _): int x; x = x + 1; defines unique Method m } Call(m, _) : } refers to Method m VarDef(_, v): defines unique Variable v VarRef(x): refers to Variable x otherwise refers to Field x
  • 25. Scope class C { Class(NonPartial(), c, _, _): int y; defines unique Class c void m() { scopes Field, Method int x; x = y + 1; Class(Partial(), c, _, _): } defines non-unique Class c } scopes Field, Method class D { Method(_, m, _, _): void m() { defines unique Method m int x; scopes Variable int y; { int x; x = y + 1; } Block(_): x = y + 1; scopes Variable } }
  • 26. C# Namespaces are Scopes namespace N { class N {} Namespace(n, _): namespace N { defines Namespace n class N {} scopes Namespace, Class } }
  • 27. Imports using N; Using(qn): imports Class from Namespace ns namespace M { where qn refers to Namespace ns class C { int f; } } Base(c): imports Field namespace O { from Class c {transitive} class E: M.C { imports Method void m() {} from Class c {transitive} } class F:E { } }
  • 28. Definition Context class C { Foreach(t, v, exp, body): void m(int[] x) { defines Variable v foreach (int x in x) of type t WriteLine(x); in body } }
  • 29. Interaction with Type System (1) class C { int i; } FieldAccess(e, f): class D { refers to Field f in c C c; where e has type ClassType(c) int i; void init() { i = c.i; } }
  • 30. Interaction with Type System (2) class C { int i; int get() { return i; } Method(t, m, ps, _) : } defines Method m of type t class D { MethodCall(e, m, _): C c; refers to Method m int i; of type t in c void init() { where e has type ClassType(c) i = c.get(); } }
  • 31. Interaction with Type System (3) Method(t, m, ps, _) : class C { defines unique Method m void m() {} of type (ts, t) void m(int x) {} where ps has type ts void m(bool x) {} void m(int x, int y) {} Param(t, p): void m(bool x, bool y) {} defines unique Variable p void x() { of type t m(); m(42); MethodCall(e, m, es): m(true); refers to Method m m(21, 21); of type (ts, t) in c m(true, false); where e has type ClassType(c) } where es has type ts }
  • 32. Name Binding class defines partial class type refers inheritance namespaces namespace using scopes method field imports variable parameter block
  • 33. Name Binding Language in Spoofax (*)
  • 34. Name Binding Language in Spoofax (*) derivation of editor services checking code completion reference resolution multi-file analysis parallel evaluation incremental evaluation (*) alpha; NBL is compiled to Stratego implementation of name resolution algorithm (ask me for pointers)
  • 35. Outlook Language definition = CFG + NBD + TS + DS Single source for Language reference manual Efficient (incremental) compiler Execution engine (interpreter) Integrated development environment