GSS-5.1 5. Binding Names to Entities Names in the source code represent entities to describe the meaning of the text. Occurrences of names are bound to entities. Scope rules of the language specify how names are to be bound. E.g.: * Every name a, used as a structure name or as a type name is bound to the same entity. * A type name a is an applied occurrence of a name. There must be a defining occurrences of a somewhere in the text. * Field names are bound separately for every structure. some occurrences of names: some bindings: some entities: Customer ( addr: Address; * a structure (named Address) account:int; ) * a field (named name) Address ( name: String; . . zip: int; * a Structur (named Article) . ) * a different field (named name) Article ( name: String; price: int; * ... ) (c) 2012 bei Prof. Dr. Uwe Kastens -------------------------------------------------------------------------------- Lecture Generating Software from Specifications SS 2012 / Slide 501 Objectives: Understand binding of names to entities In the lecture: Explain the notions using the example: * entities the text refers to, * names of entities, * occurrence of a name bound to an entity, * scope of bindings. Suggested reading: GdP-3.1 ff -------------------------------------------------------------------------------- GSS-5.2 Keys and Properties attributed tree Eli tools implement properties of entities and of envivronments Entities are represented by keys. environments Properties are associated to them. Structures have a property called Environment Definition module Entities and their keys their properties Structures fields (c) 2007 bei Prof. Dr. Uwe Kastens -------------------------------------------------------------------------------- Lecture Generating Software from Specifications SS 2012 / Slide 502 Objectives: Overview over properties of entities In the lecture: The topics of the slide are explained. -------------------------------------------------------------------------------- GSS-5.3 Bindings and Environments Environment: nested sets of bindings global environment Binding: associates a name with a key Address The global environment binds all structure and type names. Customer The environment of a structure binds its field names. Eli tools implement properties of addr entities and of envivronments account Definition module Entities and their keys nested environments sets of bindings their properties structures fields (c) 2007 bei Prof. Dr. Uwe Kastens Customer environment -------------------------------------------------------------------------------- Lecture Generating Software from Specifications SS 2012 / Slide 503 Objectives: Overview over bindings In the lecture: The topics of the slide are explained. -------------------------------------------------------------------------------- GSS-5.4 Attributed Tree for Name Analysis attributed tree Program Attributes of the tree nodes Env describe properties of the program construct Structure Structure . . . StructureName Fields Sym Bind Key Env Program has the global Env environment Field Field . . . StructureName and Fields have the environment of the structure FieldName TypeName Sym Bind Key Sym Bind Key Every node for a name occurrences has attributes for * the code of the identifier, * the binding of its name, and * its key (c) 2007 bei Prof. Dr. Uwe Kastens -------------------------------------------------------------------------------- Lecture Generating Software from Specifications SS 2012 / Slide 504 Objectives: Names and bindings in the tree In the lecture: The topics of the slide are explained. -------------------------------------------------------------------------------- GSS-5.5 Attributes, Environments, and Keys attributed tree Program global Env environment Address Structure Structure . . . Customer StructureName Fields Sym Bind Key Env Env Field Field . . . addr FieldName TypeName Sym Bind Key Sym Bind Key account Entities and their keys nested environments sets of bindings their properties structures fields (c) 2007 bei Prof. Dr. Uwe Kastens Customer environment -------------------------------------------------------------------------------- Lecture Generating Software from Specifications SS 2012 / Slide 505 Objectives: Roles of tree, bindings, and properties In the lecture: The topics of the slide are explained. -------------------------------------------------------------------------------- GSS-5.6 Environment Module Implements the abstract data type Environment: hierarchally nested sets (tree) of bindings (name, environment, key) Functions: NewEnv () creates a new environment e, that is the root of a new tree; used in root context NewScope (e1) creates a new environment e2 that is nested in e1. Every binding of e1 is a binding of e2, too, if it is not hidden by a binding established for the same name in e2; used in range context BindIdn (e, id) creates a new binding (id, e, k), if e does not yet have a binding for id; k is then a new key for a new entity; the result is in both cases the binding (id, e, k); used for defining occurrences. BindingInEnv (e, id) yields a binding (id, e1, k) of e oder of a surrounding environment of e; if there is no such binding it yields NoBinding; used for applied occurrences BindingInScope (e, id) yields a binding (id, e, k) of e, if e directly contains such a binding; NoBinding otherwise; e.g. used for qualified names (c) 2007 bei Prof. Dr. Uwe Kastens -------------------------------------------------------------------------------- Lecture Generating Software from Specifications SS 2012 / Slide 506 Objectives: Know the interface of the module In the lecture: The roles of the functions are explained -------------------------------------------------------------------------------- GSS-5.8 Example: Names and Entities for the Structure Generator Abstract syntax RULE: Descriptions LISTOF Import | Structure END; RULE: Import ::= quotesingle importquotesingle ImportNames quotesingle fromquotesingle FileName END; RULE: ImportNames LISTOF ImportName END; RULE: Structure ::= StructureName quotesingle (quotesingle Fields quotesingle )quotesingle END; RULE: Fields LISTOF Field END; RULE: Field ::= FieldName quotesingle :quotesingle TypeName quotesingle ;quotesingle END; RULE: StructureName ::= Ident END; RULE: ImportName ::= Ident END; RULE: FieldName ::= Ident END; RULE: TypeName ::= Ident END; Different nonterminals for identifiers in different roles, because different computations are expected, e.g. for defining and applied occurrences. (c) 2007 bei Prof. Dr. Uwe Kastens -------------------------------------------------------------------------------- Lecture Generating Software from Specifications SS 2012 / Slide 508 Objectives: Continue the running example In the lecture: * refer to GSS-1.11and GSS-5.1, * present the abstract syntax, * explain the identifier roles. -------------------------------------------------------------------------------- GSS-5.9 Computation of Environment Attributes Root of the SYMBOL Descriptions INHERITS RootScope END; environment hierarchy SYMBOL Fields INHERITS RangeScope END; Fields play the role of a Range. RULE: Structure ::= StructureName quotesingle (quotesingle Fields quotesingle )quotesingle COMPUTE The inherited Fields.Env = StructureName.Env; computation of Env is END; overridden. Each structure entity SYMBOL StructureName COMPUTE has an environment SYNT.GotEnvir = as its property. IF (EQ (GetEnvir (THIS.Key, NoEnv), NoEnv), ResetEnvir It is created only once (THIS.Key, for every occurrence of NewScope (INCLUDING Range.Env))); a structure entity. SYNT.Env = That environment is GetEnvir (THIS.Key, NoEnv) <- SYNT.GotEnvir; embedded in the END; global environment. In that environment the field names are bound. (c) 2012 bei Prof. Dr. Uwe Kastens -------------------------------------------------------------------------------- Lecture Generating Software from Specifications SS 2012 / Slide 509 Objectives: Systematic computation of Env attributes In the lecture: The topics of the slide are explained * the Range role, * root of nested environments created by NewEnv(), (computation can be omitted for the Grammar root). * in the example language fields may be associated to one structure s in several structure descriptions for s. * The property Envir stores one envirenment for each structure entity in the definition module. -------------------------------------------------------------------------------- GSS-5.10 Defining and Applied Occurrences of Identifiers Computations CLASS SYMBOL IdentOcc: Sym: int, IdentOcc for all CLASS SYMBOL IdentOcc COMPUTE identifier occurrences. SYNT.Sym = TERM; END; All defining occurrences SYMBOL StructureName bind their names in the INHERITS IdentOcc, IdDefScope END; next enclosing Range SYMBOL ImportName INHERITS IdentOcc, IdDefScope END; SYMBOL FieldName INHERITS IdentOcc, IdDefScope END; Bind an applied occurrence of an SYMBOL TypeName identifier in the enclosing INHERITS IdentOcc, IdUseEnv, ChkIdScope END; environment; report an error if there is no valid binding. (c) 2012 bei Prof. Dr. Uwe Kastens -------------------------------------------------------------------------------- Lecture Generating Software from Specifications SS 2012 / Slide 510 Objectives: Classify computations for identifier contexts In the lecture: The following topics are explained: * CLASS symbols represent computational roles. * Establish a binding in an environment. * Using the Range role. --------------------------------------------------------------------------------