Upcoming SlideShare
×

# Declarative Name Binding and Scope Rules

1,096
-1

Published on

Slid

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
1,096
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
9
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Declarative Name Binding and Scope Rules

1. 1. Language-Parametric Name Resolution Based on Declarative Name Binding and Scope RulesGabriël Konat, Lennart Kats, Guido Wachsmuth, Eelco Visser
2. 2. Language-Parametric Name Resolution Based on Declarative Name Binding and Scope RulesGabriël Konat, Lennart Kats, Guido Wachsmuth, Eelco Visser
3. 3. Name Binding and Scopefunction power(x: Int, n: Int): Int { if(n == 0) { return 1; } else { return x * power(x, n - 1); }}
4. 4. Fundamentalist Domain-Speciﬁc Programming
5. 5. Populist Domain-Speciﬁc ProgrammingInternal DSLProgrammatic encodingLimited abstraction from implementation strategyBound to speciﬁc host languageCaptures implementation, not understanding
6. 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 deﬁnition: paradise lost and regained. Lennart C. L. Kats, Eelco Visser, Guido Wachsmuth. Onward 2010: 918-932
7. 7. Fundamentalist Domain-Speciﬁc Programming External DSL Abstraction from implementation strategy Not bound to speciﬁc host language Captures understanding (and room for many implementations)
8. 8. Name Binding and Scope
9. 9. Name Binding and Scopestatic checkingeditor servicestransformation refactoringcode generation
11. 11. Code Completion
12. 12. Classical Approaches
13. 13. Type SystemsSource: Luca Cardelli. Type Systems. In Handbook of Computer Science and Engineering,Chapter 103. CRC Press, 1997.
14. 14. Modular Structural Operational SemanticsSource: Peter Mosses. Modular SOS: Differences from SOS.First APPSEM-II Workshop, Nottingham, March 2003
15. 15. Reference Attribute GrammarsSource: 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. 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. 17. What is the Problem?
18. 18. What is the Problem?Mental model:Name binding is deﬁned in terms ofprogrammatic encoding of name resolutionRules encoded in many language operationsAbstractions try to reduce overheadof the programmatic encoding
19. 19. Name Binding in Xtextgrammar DomainModel with TerminalsDomainmodel :  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];
20. 20. Name Binding Language
21. 21. Deﬁnitions 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. 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. 23. Unique and Non-Unique Deﬁnitions 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. 24. Namespacesnamespaces Class Method Field VariableClass(NonPartial(), c, _, _): defines unique Class cField(_, 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 mVarDef(_, v): defines unique Variable vVarRef(x): refers to Variable x otherwise refers to Field x
25. 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. 26. C# Namespaces are Scopes namespace N { class N {}Namespace(n, _): namespace N { defines Namespace n class N {} scopes Namespace, Class } }
27. 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. 28. Deﬁnition 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. 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. 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. 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. 32. Name Binding class defines partial class type refers inheritancenamespaces namespace using scopes method ﬁeld imports variable parameter block
33. 33. Name Binding Language in Spoofax (*)
34. 34. Name Binding Language in Spoofax (*) derivation of editor services checking code completion reference resolution multi-ﬁle analysis parallel evaluation incremental evaluation (*) alpha; NBL is compiled to Stratego implementation of name resolution algorithm (ask me for pointers)
35. 35. OutlookLanguage deﬁnition = CFG + NBD + TS + DS Single source for Language reference manual Efﬁcient (incremental) compiler Execution engine (interpreter) Integrated development environment
1. #### A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.