Context-Dependent Analysis
Lecture Compilers SS 2009
Dr.-Ing. Ina Schaefer
Software Technology Group TU Kaiserslautern
Content of Lecture
1. Introduction: Overview and Motivation 2. Syntax- and Type Analysis
2.1 Lexical Analysis
2.2 Context-Free Syntax Analysis 2.3 Context-Dependent Syntax Analysis 3. Translation to Target Language
3.1 Translation of Imperative Language Constructs 3.2 Translation of Object-Oriented Language Constructs 4. Selected Aspects of Compilers
4.1 Intermediate Languages 4.2 Optimization
4.3 Command Selection 4.4 Register Allocation 4.5 Code Generation
Outline
1. Difficulties and Specification of Context-Dependent Analysis Difficulties
Specification 2. Name Analysis
Specification of Name Analysis
Name Analysis with Functional Attributes Name Analysis with Symbol Table
Implementation of Attribution 3. Type Analysis
Specification of Type Analysis Type Safety
Implementation Aspects
4. Connections between Context-Dependent Analyses
Terminology
• Most authors talk aboutsemantic analysisoranalysis of the static semantics.
• We use the termcontext-dependent analysis, because
! the phase (as the other phases) aims at checking whether the program is a program at all. If it is no program, it has no semantics.
! the term semantic analysis applies better to the analysis of the program semantics (e.g. for optimizing, avoiding runtime errors ...)
Educational Objectives
• Tasks of context-dependent analysis
• Typical context conditions of programing languages
• Name analysis and relation to binding
• Type analysis and relation to verification
• Techniques for specification and implementation of context-dependent analysis
Task of Context-Dependent Analysis
Check if syntax tree complies to context conditions of language:
• In error case: error handling
• If correct: Provide attributed/decorated syntax tree
Context Checking/
Attribute Evaluation Syntax Tree
Decorated Syntax Tree
Difficulties and Specification of Context-Dependent Analysis Difficulties
Difficulties of Context-Dependent Analysis
The context-dependent analysis
• creates connection between declaration and application position of a program element
• checks the correct usage of program elements
• checks further context conditions, e.g. initialisation of variables
Examples
Connection to Declaration/Inheritance:
1 class A {
2 void m() { x = B.x } 3 static B x;
4 }
6 class B extends A { } Questions:
• Where is declaration position of x in line 2?
• Where is B in line 2 and 3 declared?
• Where is the declaration of A in line 6?
Difficulties and Specification of Context-Dependent Analysis Difficulties
Examples (2)
Connection between Declaration and Access Control Rules:2. Zusammenhang von Deklaration und Zugriffsrechten:
class MyClassIsMyCastle {y y
private static int streetno = 169;
private static class FirstFloor { private static class DiningRoom {
private static int size = 36;
} }
private static class SecondFloor { private static class BathRoom {
private static int size = 16;
private static void mymess(){
System.out.print("Can access the ");
System.out.print("dining room size");
System.out.println(": "+
FirstFloor.DiningRoom.size);
} } }
public static void main( String[] argv ){
SecondFloor.BathRoom.mymess();
} }
- Welche qualifizierten Namen sind erlaubt?
- Ist es erlaubt, aus der inneren Klasse Bathroom auf ein Attribut der inneren Klasse DiningRoom zuzugreifen?
Questions:
• Which qualified names are permitted?
• May the inner class Bathroomaccess a field of the inner class
DiningRoom?
Ina Schaefer Context-Dependent Analysis 9
Examples (3)
Usage of Declaration and Type Information: Information computed by the context-dependent analysis is a prerequisite for the translation.
l A {
3. Anwendung von Deklarations- und Typinformation:
class A {
void m( Object x ) {
System.out.println( x.toString() );
} } }
class B extends A { void m( A x ) {
System out println("Na so was");
System.out.println("Na so was");
} }
class Overloading { class Overloading {
public static void main(String[] argv){
Object xact = new A();
new B(). m( xact );
} } }
Die von der kontextabhängigen Analyse ermittelten Informationen sind Voraussetzung für die Übersetzung Informationen sind Voraussetzung für die Übersetzung
Type ofxactrequired to select correct method.
Difficulties and Specification of Context-Dependent Analysis Specification
Specification and Implementation Techniques
• Informal specification techniques for name and type rules are diverse.
• Formal specification techniques are based on attribute grammars and inference rules.
• Attributes are a concept to incrementally decorate tree structures (not only for compiler construction). We consider
! attribute grammars
! attributation of abstract syntax
Attribute Grammars
• Declare for each non-terminal and each terminalattributes(name and type).
• Declare for each production of the CFG,(semantic) ruleshow the values of the attributes are computed.
Difficulties and Specification of Context-Dependent Analysis Specification
Attribute Grammars (2)
Example: Consider the grammar
• Prog→AnwSeq,
• AnwSeq→!|AnwAnwSeq,
• Anw →Ident|AnwSeq
Attribute grammar to compute number of occurences of an identifier in a program:
• non-terminalsAnwSeqandAnw get attributes
! idof type Ident
! anzof type int
• The semantic rules are as follows:
Difficulties and Specification of Context-Dependent Analysis Specification
Attribute Grammars (3)
Betrachte die Grammatik Prog ! AnwSeq ,
AnwSeq ! e | Anw AnwSeq , Anw ! Ident | AnwSeq
Att ib t tik B h d A hl d
Attributgrammatik zur Berechnung der Anzahl der Vorkommen eines geg. Bezeichners im Programm:
- die Nichtterminale AnwSeq u. Anw erhalten die Attribute id vom Typ Ident und anz vom Typ int Attribute id vom Typ Ident und anz vom Typ int - die Attributierungsregeln sehen wie folgt aus,
wobei id und anz bedeutet.
Prog A S
Bezeichner- eingabe
Prog AnwSeq
drucken +
AnwSeq
AnwSeq
Anw AnwSeq
Anw
AnwSeq Anw Anw
if = then 1 else 0 0
Beispiel: (Einführung Attributierung)
Betrachte die Grammatik Prog ! AnwSeq ,
AnwSeq ! e | Anw AnwSeq , Anw ! Ident | AnwSeq
Att ib t tik B h d A hl d
Attributgrammatik zur Berechnung der Anzahl der Vorkommen eines geg. Bezeichners im Programm:
- die Nichtterminale AnwSeq u. Anw erhalten die Attribute id vom Typ Ident und anz vom Typ int Attribute id vom Typ Ident und anz vom Typ int - die Attributierungsregeln sehen wie folgt aus,
wobei id und anz bedeutet.
Prog A S
Bezeichner- eingabe
Prog AnwSeq
drucken +
AnwSeq AnwSeq
Anw AnwSeq
AnwSeq AnwAnw Anw
if = then 1 else 0
Beispiel: (Einführung Attributierung)
Betrachte die Grammatik Prog ! AnwSeq ,
AnwSeq ! e | Anw AnwSeq , Anw ! Ident | AnwSeq
Att ib t tik B h d A hl d
Attributgrammatik zur Berechnung der Anzahl der Vorkommen eines geg. Bezeichners im Programm:
- die Nichtterminale AnwSeq u. Anw erhalten die Attribute id vom Typ Ident und anz vom Typ int Attribute id vom Typ Ident und anz vom Typ int - die Attributierungsregeln sehen wie folgt aus,
wobei id und anz bedeutet.
Prog A S
Bezeichner- eingabe
Prog AnwSeq
drucken +
AnwSeq AnwSeq
Anw AnwSeq
AnwSeq AnwAnw Anw
if = then 1 else 0
Difficulties and Specification of Context-Dependent Analysis Specification
Attribute Grammars (4)
Definition
LetΓbe a CFG. Anattribute grammaroverΓconsists of
• two disjoint set of attributes, Inh(X) the set of inherited attributes and Syn(X) the set of synthesized attributes
• an association of attributes to symbolsX ∈T ∪N ofΓ.
• the types/domains of the attributes
• semantic rules for each inherited attributea∈Inh(Xi)on the right side of a production and for each synthesized attribute
a∈Syn(X0)on the left side of a production Xi.a=f(Yj, . . .)
• potentially further semantic actions, e.g. for output
Attribute Grammars (5)
Remarks:
• Synthesized attributes are, for example, the type of an expression or the target code.
• Inherited attributes are, for example, declaration information or symbol tables.
• Different classes of attribute grammars are distinguished by the permitted dependencies between attributes.
• Attribute grammars are generalizations of recursion schemes.
• We use attribute grammars to describe processes.
• Attribute grammars are the specification technique for many
Difficulties and Specification of Context-Dependent Analysis Specification
Example: Combining Productions
• Attributgrammatiken stellen eine Verallgemeinerung von Rekursionsschemata dar.
• Wir benutzen AGen zur Beschreibung von Abläufen.g
• AGen bilden die Spezifikationstechnik für viele Übersetzer-Generatoren.
Bezeichner- Prog
dr cken
Beispiel: (Zusammensetzen von attributierten Produktionen)
Bezeichner eingabe
AnwSeq drucken
Anw AnwSeq
+
Anw AnwSeq
AnwSeq
+ AnwSeq
if = then 1 else 0 0
Anw AnwSeq
0
130
© A. Poetzsch-Heffter, TU Kaiserslautern 10.05.2007
Ident
Attribution of Abstract Syntax
• Analogue to attribute grammars
• Attributes are associated to types of abstract synatx
• Attributes of variant types are used for all occurences of this type, i.e. ifais an attribute of the variant typeV, then each variantVi has an attributea.
• Tuple types (and list types) are also attributed.
Difficulties and Specification of Context-Dependent Analysis Specification
Example: Attribution of Abstract Syntax
• Prog(AnwSeq)
• AnwSeq=Empty()|Elemt(Anw,AnwSeq)
• Anw =IdAnw(Ident)I SeqAnw(AnwSeq) Some Semantic Rules:
Attributierung abstrakter Syntax:
• analog zu Attributgrammatiken
• Die Attribute werden den Typen der abstrakten Syntax zugeordnet.
• Attribute der Variantentypen werden an alle Varianten „vererbt“, d.h. wenn a ein Attribut eines Variantentyps mit Variante V
iist, dann hat auch V
iein Attribut a.
• attributiert werden Tupeltypdeklarationen attributiert werden Tupeltypdeklarationen
(und Listentypproduktionen; hier nicht behandelt)
Beispiel: (Attributierung abstrakter Syntax) Beispiel: (Attributierung abstrakter Syntax)
Prog ( AnwSeq )
AnwSeq = Leer( ) | Elem( Anw, AnwSeq )
Anw = IdAnw( Ident ) | SeqAnw( AnwSeq ) Wir benutzen die gleiche graphische Notation wie bei AGen, verwenden allerdings den Tupeltypen als linke Seite der Produktion:
SeqAnw IdAnw
if = then 1 else 0
© A. Poetzsch-Heffter, TU Kaiserslautern 131 10.05.2007
Ident AnwSeq
Inference Rules - Concept
Inference rules are used for specification of static and dynamic
language and program properties, in particular for specification of type rules and operational semantics (e.g. structural operational
semantics), and is also a basis for language implementation tools.
Principle:
• Considered property is formalized with a fixed number of parameters. Each parameter is a program part.
• With inference rules, the property (judgement) is defined inductively over the program structure.
Difficulties and Specification of Context-Dependent Analysis Specification
Inference Rules - Concept (2)
Example: Language as in the previous examples
Property: Identifier Y occurs N times in program part P (Y » P:N) Rules:
Inferenzregeln zur Spezifikation:
Inferenzregeln erfreuen sich zunehmender Beliebtheit zur Spezifikation von statischen und dynamischen zur Spezifikation von statischen und dynamischen Sprach- und Programmeigenschaften. (Hier erläutern wir nur das Prinzip; Genaueres in 2.3.3)
Prinzip: p
• Relevante Eigenschaft wird als eine Aussage mit fester Anzahl von Parametern formalisiert; einer der Parameter der Aussage ist ein Programmteil.
Mi I f l i d di Ei h f (j d )
• Mit Inferenzregeln wird die Eigenschaft (judgement) induktiv über die Programmstruktur definiert.
Beispiel:
Sprache wie in obigen Beispielen zur Attributierung.
Eigenschaft :
Der Bezeichner Y kommt im Programmteil P genau N mal vor formalisiert als Y >> P : N
mal vor, formalisiert als Y >> P : N Regeln:
Y >> Prog( AS ) : N Y >> Elem( A AS ) : M+N Y >> AS : N Y >> A : M Y >> AS : N Y >> Prog( AS ) : N
Y >> Leer() :0
Y >> Elem( A, AS ) : M+N
Y >> SeqAnw( AS ) :N Y >> AS : N
© A. Poetzsch-Heffter, TU Kaiserslautern 132 10.05.2007
Y >> IdAnw( X ) : 0 Y >> IdAnw( X ) : 1
Y = X Y = X
Literature
Recommended Reading:
Wilhelm, Maurer: Section 9.2, pp. 424-429
Name Analysis
Name Analysis
• Name analysis determines that each used identifier is also declared.
• Checks context conditions
• Has to make declaration information available at application position of identifier
! by connecting declaration and application position
! by transferring declaration information to application position
Name Analysis (2)
Name analysis is the prerequisite for binding of names to program elements, thus this phase is often calledidentification.
Program elements are for instance:
• packages, modules, classes, types
• procedures, functions
• variables, fields, records, parameters
• labels
Name Analysis Specification of Name Analysis
Specification of Name Analysis
Informal Specification Technique
• Specify different kinds of named program elements
• Specify defining and applied occurrences of identifiers
• Specify for each declaration which program element is declared and which identifier it binds (binding pair)
• Specify the scope for each programming construct changing scope and which binding pairs are additionally valid and which are hidden
• A program element (PE) with an identifier ID is visible at a program point, if the binding pair (ID, PE) is valid and not hidden.
Remark: Specification techniques for name analysis are not as advanced as for context-free analysis
(Gap between formal and informal specification techniques).
Name Analysis Specification of Name Analysis
Example: Specification of Name Analysis
Abstract Syntax of C Sub-Language:
Siehe Vorlesung Siehe Vorlesung
Beispiel: (Zusammenwirken der Begriffe)
Abstrakte Syntax einer C-Teilsprache:
Program ( GlobDeclList ) GlobDeclList * GlobDecl GlobDecl = Var | Proc GlobDecl = Var | Proc Var ( Ident id )
Proc ( Ident id, LocVarList, Block ) LocVarList * LocVar
LocVar ( Ident id )
Block ( LocVarList, Stat )
Stat = Assign | Call | SeqStat | Block Assign ( Ident id, Exp )
Call ( Ident id ExpList ) Call ( Ident id, ExpList ) SeqStat ( Stat , Stat ) Exp ( Ident id ) ExpList * Exp
In der Attributierung gehen wir davon aus, dass die Listentypen in Sequenzen aufgelöst sind; z.B.:
For attribution, we consider list types as sequences
Siehe Vorlesung Siehe Vorlesung
Beispiel: (Zusammenwirken der Begriffe) Abstrakte Syntax einer C-Teilsprache:
Program ( GlobDeclList ) GlobDeclList * GlobDecl GlobDecl = Var | Proc GlobDecl = Var | Proc Var ( Ident id )
Proc ( Ident id, LocVarList, Block ) LocVarList * LocVar
LocVar ( Ident id )
Block ( LocVarList, Stat )
Stat = Assign | Call | SeqStat | Block Assign ( Ident id, Exp )
Call ( Ident id ExpList ) Call ( Ident id, ExpList ) SeqStat ( Stat , Stat ) Exp ( Ident id ) ExpList * Exp
In der Attributierung gehen wir davon aus, dass die Listentypen in Sequenzen aufgelöst sind; z.B.:
Name Analysis Specification of Name Analysis
Example: Specification of Name Analysis (2)
• Program elements: global variables, procedures, formal parameters, local variables, ...
• Occurrences of Ident in Var, Proc, Loc Var are defining, all other occurences are applied.
• Var defines a global variable, Proc a procedure, LocVar defines either a formal parameter or a local variable, a binding pair consists of a declared identifier and a program element.
Example: Specification of Name Analysis (3)
• The scopes are defined as:
! for a global variable, from declaration to end of program
! for a procedure, from the beginning of the parameter list to the end of the program
! for a formal parameter, from the end of the parameter list to the end of the enclosing procedure
! for a local variable, from the beginning of the block statement to the end of the block The binding pair (ID,PE) of a non-local program element hides all binding pairs in enclosing scopes with the same identifier
• Visibility is defined as usually.
• For each identifier at most one binding pair may be visible at each
Name Analysis Specification of Name Analysis
Name Analysis with Functional Attributes
Idea: Compute for each applied occurrence of an identifier, the list of visible binding pairs (or a table that maps each identifier to its
declaration)
Simplest solution: Functional language
Data Types and Functions for Environments/Symbol Table:
Spezifikation mit funktionaler Attributierung:
Idee:
Idee:
Berechne für jede Anwendungsstelle eines Bezeichners die Liste der dort sichtbaren Bindungspaare (bzw. eine Tabelle, die jedem Bezeichner seine Deklaration
d ) Ei f h V i B f k i l
zuordnet). Einfachste Variante: Benutze funktionale Sprache für die Spezifikation/Realisierung.
D t t F kti B h ib d
Datentypen u. Funktionen zur Beschreibung des Environments bzw. der Symboltabelle:
Decl = GlobDecl | LocVar Env * Decl
FCT enter(Decl d, Env e) Env: appfront(d,e) FCT concat( Env e1, Env e2 ) Env:
IF e2=Env() THEN e1
ELSE concat( appback(e1,first(e2)), rest(e2) ) FCT lookup( Ident i, Env e ) Decl:
IF e=Env() THEN nil ELSF i=id(first(e)) THEN first(e)
ELSE lookup(i,rest(e))
Attribution for Name Analysis
• GlobDeclList, GlobDecl, LocVarList, LocVar, Stat, Exp, ExpList get inherited attributeenvinof type Env
• GlobDecl gets synthesized attributeenvout Attributierung für die Namensanalyse:
Die folgenden Typen der abstrakten Syntax erhalten ein ererbtes Attribut envin vom Typ Env
ein ererbtes Attribut envin vom Typ Env (dargestellt als ) : GlobDeclList, GlobDecl, LocVarList, LocVar, Stat, Exp, ExpList
GlobDecl erhält außerdem ein abgeleitetes
A ib (d ll l )
Program
GlobDeclElem Attribut envout (dargestellt als )
Env( )
GlobDeclList
GlobDeclEmpty GlobDecl GlobDeclList
GlobDeclEmpty GlobDecl GlobDeclList
Var lookup( ) = nil ?
Ident
enter(_,_) lookup(_,_) = nil ?
Proc
enter(_,_) lookup(_,_) = nil ?
envin
envout
Ina Schaefer Context-Dependent Analysis 30
Name Analysis Specification of Name Analysis
Attribution for Name Analysis (2)
Attributierung für die Namensanalyse:
Die folgenden Typen der abstrakten Syntax erhalten ein ererbtes Attribut envin vom Typ Env
ein ererbtes Attribut envin vom Typ Env (dargestellt als ) : GlobDeclList, GlobDecl, LocVarList, LocVar, Stat, Exp, ExpList
GlobDecl erhält außerdem ein abgeleitetes
A ib (d ll l )
Program GlobDeclElem
Attribut envout (dargestellt als )
Env( )
GlobDeclList
GlobDeclEmpty GlobDecl GlobDeclList
GlobDeclEmpty GlobDecl GlobDeclList
Var lookup( ) = nil ?
Ident
enter(_,_) lookup(_,_) = nil ?
Proc
concat( )
enter(_,_) lookup(_,_) = nil ?
Env( )
139
© A. Poetzsch-Heffter, TU Kaiserslautern 10.05.2007
Ident LocVarList Block
concat(_,_) Env( )
Attribution for Name Analysis (3)
LocVarEmpty LocVarElem
LocVar LocVarList
enter(_,_)
LocVar lookup(_,_) = nil ?
Block Assign
Ident p(_ _)
g
Env( ) concat(_,_)
isVar( lookup(_,_))?
LocVarList Stat Ident Exp
Call
SeqStat Call
SeqStat
isProc( lookup(_,_))?
Ina Schaefer Context-Dependent Analysis 32
Name Analysis Specification of Name Analysis
Attribution for Name Analysis (4)
LocVarEmpty LocVarElem
LocVar LocVarList
enter(_,_)
LocVar
lookup(_,_) = nil ?
Block Assign
Ident p(_ _)
g
Env( ) concat(_,_)
isVar( lookup(_,_))?
LocVarList Stat Ident Exp
Call
SeqStat Call
SeqStat
isProc( lookup(_,_))?
140
© A. Poetzsch-Heffter, TU Kaiserslautern 10.05.2007
ExpList
Stat Stat Ident
(Prüfe Parameteranzahl)check number of parameters
Attribution for Name Analysis (5)
Exp ExpElem
Exp ExpList
Ident isVar( lookup(_,_))?
ExpEmpty ExpList
Bemerkungen:
• Das Beispiel demonstriert eine grundlegendep g g Technik für die Namensanalyse.
• Transport von Deklarationsinformation zur
Anwendungsstelle wird allerdings nur ansatzweise verdeutlicht
verdeutlicht.
• Der einfache Datentyp zur Repräsentation des Environments und dessen funktionale Behandlung bieten aus Spezifikationssicht Vorteile (relativ leicht zu verstehen, für Verifikationszwecke besser geeignet).
• Spezifikation lässt sich direkt im MAX-System Remarks:
• Example shows basic technique for name analysis.
• Transport of declaration information to application position is only sketched.
Ina Schaefer Context-Dependent Analysis 34
Name Analysis Specification of Name Analysis
Attribution for Name Analysis (6)
• The data type for representing environments and its functional treatment have advantages for specification
(easy to understand, more appropriate for verification)
• Specification can be directly implemented, e.g. in MAX.
• For real programing languages, however, this technique is to inefficient:
! Linear search for identifiers
! If hashing is used, values of data type environment cannot be easily represented by sharing, copying of symbol tables would be
necessary.
Name Analysis with Symbol Table
Compilation units can contain a large number of identifiers. Thus, insertion and lookup of identifiers should have constant complexity.
Solution:
• Hashing of identifiers in compilation unit (already during
scanning), each compilation unit can only have a finite number of identifiers, thus use of array with constant access
• Implement sequential access to symbol table, such that it can be implemented as global data structure with modification operations (avoids copying)
Name Analysis Specification of Name Analysis
Name Analysis with Symbol Table (2)
Steps for sequential access to symbol table 1. Operations for entering and exiting scope G:
! Entering of scope G marks declarations of G
! Exiting G deletes all declarations of G
1. Operationen zum Öffnen und Schließen eines A. Schritte zum Erreichen des sequentiellen Flusses:
p
Gültigkeitsbereichs G:
- Das Öffnen markiert quasi die Deklarationen von G.
- Beim Schließen werden alle Deklarationen von G a s der Tabelle gelöscht
aus der Tabelle gelöscht.
Block
L V Li t St t
close_scp(_) open_scp(_)
2. Schreibende und lesende Zugriff auf die Symbol- tabelle werden sequentialisiert; um das in der
LocVarList Stat
q ;
Attributierung zu erzwingen, betrachtet man die Symboltabelle formal auch als Ergebnis einer lesenden Operation wie lookup.
Assign isVar( lookup(_,_))?
Ident Exp
Ina Schaefer Context-Dependent Analysis 37
Name Analysis Specification of Name Analysis
Name Analysis with Symbol Table (3)
2. Write and read accesses to symbol table are sequentialized.
For attribution, symbol table is formally considered as the result of a read operation, such as lookup.
1. Operationen zum Öffnen und Schließen eines p Gültigkeitsbereichs G:
- Das Öffnen markiert quasi die Deklarationen von G.
- Beim Schließen werden alle Deklarationen von G a s der Tabelle gelöscht
aus der Tabelle gelöscht.
Block
L V Li t St t
close_scp(_) open_scp(_)
2. Schreibende und lesende Zugriff auf die Symbol- tabelle werden sequentialisiert; um das in der
LocVarList Stat
q ;
Attributierung zu erzwingen, betrachtet man die Symboltabelle formal auch als Ergebnis einer lesenden Operation wie lookup.
Assign isVar( lookup(_,_))?
Ident Exp
Name Analysis Specification of Name Analysis
Name Analysis with Symbol Table (4)
Steps for Development of Data Structure
1. Since scopes in the considered language (and in most other languages) are nested, entering and exiting scopes can be organized with a stack.
! For each nesting level nl, store a mapping of identifiers to declaration information.
! The symbol table is a stack of these mappings.
! When entering a scope, push an empty mapping onto the stack.
! When exiting a scope, pop the top-most mapping from the stack.
! Lookup starts with the top-most mapping, if identifier is not in the domain, consider next mapping on stack (Complexity: max nl)
Name Analysis with Symbol Table (5)
Symbol table is stack of finite mappings stackof (Ident → DeclInf)
2. Transformation to a finite mapping of identifiers to stacks of declaration informations, i.e. Ident → stackof(DeclInf).
This can be efficiently implemented by a matrix-linked data structure.
Name Analysis Specification of Name Analysis
Symbol table for languages with nested scopes
Snapshot at nesting level 2
Symboltabelle für Sprachen mit geschachtelten Gültigkeitsbereichen:
Momentaufnahme der Datenstruktur bei aktueller Schachtelungstiefe 2:.
curr nl max nl
nl: 2 1 0
IdNo:
curr_nl max_nl nl:
1 2
2 1 0
2 . . .
DE
• • • •
• • • •
2
k
. . .
DE DE
DE
•
•
• •
• •
•
•
m
. . .
DE DE
DE DE
• • • • •
max_id
. . .
•
Dabei sind die Deklarationseinträge für einen Bezeichner zeilenweise angeordnet. Sie bilden einen Keller, der von rechts nach links wächst. Die Spalten enthalten die Einträge zu einer Schachtelungstiefe enthalten die Einträge zu einer Schachtelungstiefe.
DE bezeichnet den Deklarationseintrag (umfasst IdNo).
Declarations for each identifier are stored in rows forming a stack from left to right. The columns contain the nesting levels. DE is the declaration entry (with identity number).
Ina Schaefer Context-Dependent Analysis 41
Operations on Symbol Table
• create(): initializes empty symbol table
• open_scp(st): increments curr_nl
• close_scp(st): deletes all entries for curr_nl, restores identifier links, decrements curr_nl
• enter(id,de,st): adds entry for curr_nl, the entry contains
! declaration entry (de)
! pointer for nl links
! pointer to other entries for the identifier id
• lookup(id,st): access to declaration information of visible binding pair for id
For sequential access in attribution, each operation returns pointer to
Name Analysis Specification of Name Analysis
Advantages of SymTab Data Type
• Complexity of lookup and enter is constant.
• Complexity of exiting scope is linear in the number of declarations of this scope.
• Efficient memory management possible.
Example: Usage of SymTab Data Type
1. Declaration Information
Anwendung der Symboltabelle anhand der Beispielsprache:
1. Bestimmung der Deklarationsinformation:
DeclInf = VInf | PInf VInf ()
PInf ( Int parcount ) PInf ( Int parcount )
Zur Sequentialisierung der Symboltabellenzugriffe h lt f t ll T d b t kt S t 2. Attributierung:
erhalten fast alle Typen der abstrakten Syntax ein ererbtes Attribut symin vom Typ SymTab (dargestellt als ) und ein abgeleitetes
Attribut symout (dargestellt als ). Darüberhinaus b k L V Li t d E Li t i b l it t
Program GlobDeclElem
bekommen LocVarList und ExpList ein abgeleitetes Attribut length vom Typ Int (dargestellt als ).
create
open_scp(_)
GlobDeclList GlobDecl GlobDeclList
2. Attribution
For sequential access to the symbol table, almost all types of the abstract syntax get an inherited attributesyminof typeSymTab and an synthesized attributesymout. Additionally,LocVarListand ExpListget an synthesized attributelengthof type Int.
Ina Schaefer Context-Dependent Analysis 44
Name Analysis Specification of Name Analysis
Example: Semantic Rules
Anwendung der Symboltabelle anhand der Beispielsprache:
1. Bestimmung der Deklarationsinformation:
DeclInf = VInf | PInf VInf ()
PInf ( Int parcount ) PInf ( Int parcount )
Zur Sequentialisierung der Symboltabellenzugriffe h lt f t ll T d b t kt S t 2. Attributierung:
erhalten fast alle Typen der abstrakten Syntax ein ererbtes Attribut symin vom Typ SymTab (dargestellt als ) und ein abgeleitetes Attribut symout (dargestellt als ). Darüberhinaus b k L V Li t d E Li t i b l it t
Program GlobDeclElem
bekommen LocVarList und ExpList ein abgeleitetes Attribut length vom Typ Int (dargestellt als ).
create open_scp(_)
GlobDeclList
GlobDeclEmpty GlobDecl GlobDeclList
147
© A. Poetzsch-Heffter, TU Kaiserslautern 10.05.2007
close_scp(_)
symin symout
Var
lookup( ) = nil ? enter( Vinf() ) lookup(_,_) = nil ? enter(_,Vinf(),_)
Proc Ident
lookup(_,_) = nil ?
close_scp(_)
Ident LocVarList Block
enter(_,Pinf(_),_)
open_scp(_)
Ident LocVarList Block
LocVarEmpty LocVarElem
0 +1
LocVar LocVarList
Ina Schaefer Context-Dependent Analysis 45
Name Analysis Specification of Name Analysis
Example: Semantic Rules (2)
lookup( ) = nil ? enter( Vinf() ) lookup(_,_) = nil ? enter(_,Vinf(),_)
Proc Ident
lookup(_,_) = nil ?
close_scp(_)
Ident LocVarList Block
enter(_,Pinf(_),_)
open_scp(_)
Ident LocVarList Block
LocVarEmpty LocVarElem
0 +1
Name Analysis Specification of Name Analysis
Example: Semantic Rules (3)
LocVar
lookup( ) = nil ? enter( Vinf() ) lookup(_,_) = nil ? enter(_,Vinf(),_)
Ident
Block Block
open_scp(_) close_scp(_)
LocVarList Stat
Assign SeqStat
Ident Exp Stat Stat
isVar( lookup(_,_))?
Ina Schaefer Context-Dependent Analysis 47
Name Analysis Specification of Name Analysis
Example: Semantic Rules (4)
lookup( ) = nil ? enter( Vinf() ) lookup(_,_) = nil ? enter(_,Vinf(),_)
Ident
Block Block
open_scp(_) close_scp(_)
LocVarList Stat
Assign SeqStat
Ident Exp Stat Stat
isVar( lookup(_,_))?
© A. Poetzsch-Heffter, TU Kaiserslautern 149 10.05.2007
ExpEmpty ExpElem
0 +1
E
Exp ExpList
Exp
isVar( lookup(_,_))?
Ident
Call
isProc( lookup(_,_))? parcount(lookup(_,_)) = _?
ExpList Ident
Ina Schaefer Context-Dependent Analysis 48
Name Analysis Specification of Name Analysis
Example: Semantic Rules (5)
ExpEmpty ExpElem
0 +1
E
Exp ExpList
Exp
isVar( lookup(_,_))?
Ident
Call
isProc( lookup(_,_))? parcount(lookup(_,_)) = _?
ExpList Ident
Ina Schaefer Context-Dependent Analysis 49
Declaration Information
Declaration information has to be sufficient
• to check the correct application of an identifier
! Is a variable allowed at the application position?
! Is there a procedure, type, label ... allowed?
! Is the number of parameters correct?
! Are the types correct?
• to handle named scopes
! Is a selector admissible?
! Which methods and fields are contained in a class?
• to check additional context conditions (e.g. case statement in Pascal)
Name Analysis Specification of Name Analysis
Declaration Information (2)
• How to determine declaration information?
! Which declaration and program elements exist?
! Which information is necessary for each program element?
• Implementation of Declaration Information
! Reference to declaration position (simple)
! Using special data structures (designed for the particular task, e.g.
table for class elements)
Declaration Information (3)
Remarks:
• Name analysis and its implementation depend on the rules of the programing language, e.g. name spaces, application of
declarations, overloading
• For separate translation, a symbol table for each translation unit should be created that contains the declaration information of the program elements used by other translation units.
• The symbol table is also used for distributing other information in later phases, e.g. address information of variables
Name Analysis Specification of Name Analysis
Literature
Recommended Reading:
• Wilhelm, Maurer: Sections 9.1.1 and 9.1.2, pp. 408 – 416
• Appel: Section 5.1, pp. 108 – 119
Implementation of Attribution
Educational Objectives:
• Different interpretations of attributions
• Introduction into the semantics of attributions
• Classes of attribute grammars
• Implementation techniques
Name Analysis Implementation of Attribution
Interpretations of Attribute Grammars
1. Specification and Equation Interpretation
An attribute grammar assigns attribute values to nodes of
grammar trees. The specification is given by an equation system that is generated for each tree.
Problem: Has the system a (unique) solution?
2. Functional or Computational Interpretation
An attribute grammar determines how attribute values in a tree are computed.
! without memory: goal is computation of attribute value at root node only
! with memory: goal is computation of all attribute values for further processing phases
Interpretations of Attribute Grammars (2)
3. Control of Action Executions
An attribute grammar describes in which order semantic actions are executed
! copy actions (correspond to equation systems)
! semantic actions
An attribute valuev is stored as long as all actions have been executed that usev as parameter
Remarks:
• Mind semantic actions with side effects
• For classification of attribute grammars, only attribute dependencies are considered.
Name Analysis Implementation of Attribution
Classes of Attribute Grammars
Definition (Non-circular attribute grammar)
LetAGbe an attribute grammar over CFGΓ. For syntax treesSofΓ, letDt(S)denote the directed attribute dependency graph. TheAGis non-circular ifDt(S)is cycle free for allS.
Remarks:
• For non-circular attribute grammars, the equation system for each syntax tree has a unique solution.
• Testing non-circularity in general is exponential. Thus, for efficient attribute evaluators, we consider restricted classes of attribute grammars.
Classes of Attribute Grammars (2)
An attribute grammarAGoverΓis
• S-attributedifAGonly has synthesized attributes
• L-attributedif the attributes for each syntax tree forΓcan be evaluated with a left-right depth-first traversal.
Other important classes of attribute grammars:
• absolute non-circular (Kennedy & Warren)
• ordered (Karstens)
Name Analysis Implementation of Attribution
Implementation of Attribute Grammars
1. Computational Interpretation without Memory
For L-attributed grammars and other simples classes of attribute grammars, attribute evaluation can be performed by recursive functions:
! inherited attributes become parameters
! synthesized attributes become results
! For each production, we have one function (or even simpler for L-attribute grammars). For other simple attribute grammars, we can have several functions for each production.
Implementation of Attribute Grammars (2)
2. Computational Interpretation with Memory
If attribute values in the syntax tree should be memorized,
attributes can be stored in components of the tree nodes and can be evaluated by visiting the trees in an appropriate order
→Attribute Evaluators
(can be implemented manually or generated automatically)
Name Analysis Implementation of Attribution
Implementation of L-AG with Recursive Functions
Consider simple L-AG (cf. Lecture): The environment parameter represents the inherited attribute, the result represents the synthesized attribute.
Beispiel: (Impl. v. L-AG mit rekursiver Funktion) Implementierung einer einfachen L-AG (s. Vorlesung)
datatype exp =
durch eine rekursive Funktion. Der Environment- Parameter entspricht dem ererbten Attribut, das Ergebnis dem abgeleiteten Attribut:
datatype exp = Int of int
| Add of exp * exp
| Var of string
| Let of string * exp * exp
;
val mp = Let ("x",Add (Int 1, Int 2), Let ("y",Int 5,
Let ("z",Int 8, Add (Add (Var "x",Var "y"), Var "z"))));
type env = (string * int) list;
fun lkup s [] = raise notDeclared
| lkup s ((s1,i)::es) =
if s=s1 then i else lkup s es
;
fun eval E (Int i) = i fun eval E (Int i) i
| eval E (Add (le,re)) =
(eval E le) + (eval E re)
| eval E (Var s) = lkup s E
| eval E (Let (s,e1,e2)) =
eval ((s,eval E e1)::E) e2
;
Name Analysis Implementation of Attribution
Attribute Evaluator
Example: Name Analysis for Java Subset
Sollen die Attributwerte in Baum gespeichert werden, kann man die Attribute als Komponenten der Baumknoten vorsehen und durch Besuche der Bäume in geeigneter Reihenfolgedie Attributwerte berechnen:
!Attributauswerter
Attributauswerter können von Hand realisiert oder
i AG t ti h i t d
zu einer AG automatisch generiert werden.
Beispiel: (Attributauswerter)
Class ( DeclList )
D lLi t D lE t | D lEl
Wir betrachten die Namensanalyse für folgende einfache Java-Teilsprache:
DeclList = DeclEmpty | DeclElem DeclEmpty ()
DeclElem ( Decl DeclList ) Decl = FieldDecl | MethDecl FieldDecl ( Ident )
MethDecl ( Ident Ident Ident ) Erläuterung: siehe Vorlesung
© A. Poetzsch-Heffter, TU Kaiserslautern 158 10.05.2007
Signature of Symbol TableSignatur der Symboltabelle:
emptyST : !SymTab
lookup : Ident x SymTab ! FieldMethUndef enter : Ident x FieldMeth x SymTab ! SymTab
Attributierung:
Class
DeclEmpty emptyST
DeclList
DeclElem
Decl DeclList
Ina Schaefer Context-Dependent Analysis 62
Name Analysis Implementation of Attribution
Attribute Evaluator (2)
Attributation of Abstract Syntax
Signatur der Symboltabelle:
emptyST : !SymTab
lookup : Ident x SymTab ! FieldMethUndef enter : Ident x FieldMeth x SymTab ! SymTab
Attributierung:
Class
DeclEmpty emptyST
DeclList
DeclElem
Decl DeclList
Ina Schaefer Context-Dependent Analysis 63
Attribute Evaluator (3)
FieldDecl
lookup(_,_) = UNDEF ? enter(_,FIELD,_)
Ident
MethDecll
enter(_,METH,_) lookup(_,_) = UNDEF ?
Ident Ident Ident
Ident Ident Ident
Name Analysis Implementation of Attribution
Attribute Evaluator (4)
Interface of Symbol Table class SymTab {
Schnittstelle der Symboltabelle:
y {
static int FIELD = 1;
static int METH = 2;
static int UNDEF = 3;
t ti S T b t ST() { static SymTab emptyST() {
return new SymTab();
}
int lookup( String id ) {p( g ) {
// liefert SymTab.FIELD oder ... }
SymTab enter( String id, boolean isField ) { // ... }
} }
class JClass {
Attributdeklaration und 2-Passauswertung:
class JClass { DeclList dl;
// keine Attribute void evalAttr() {
dl.stIn = SymTab.emptyST();
dl.evalAttr0();
dl.st = dl.stOut;
dl.evalAttr1();
} } }
Ina Schaefer Context-Dependent Analysis 65
Name Analysis Implementation of Attribution
Attribute Evaluator (5)
Attribute Declaration and 2-Pass-Evaluation
class SymTab {y {
static int FIELD = 1;
static int METH = 2;
static int UNDEF = 3;
t ti S T b t ST() { static SymTab emptyST() {
return new SymTab();
}
int lookup( String id ) {p( g ) {
// liefert SymTab.FIELD oder ... }
SymTab enter( String id, boolean isField ) { // ... }
} }
class JClass {
Attributdeklaration und 2-Passauswertung:
class JClass { DeclList dl;
// keine Attribute void evalAttr() {
dl.stIn = SymTab.emptyST();
dl.evalAttr0();
dl.st = dl.stOut;
dl.evalAttr1();
} } }
Name Analysis Implementation of Attribution
Attribute Evaluator (6)
abstract class DeclList { SymTab stIn;
SymTab stOut;
SymTab st;
abstract void evalAttr0();
abstract void evalAttr1();
}
class DeclEmpty extends DeclList { void evalAttr0() { stOut = stIn; } void evalAttr1() { }
}
class DeclElem extends DeclList { Decl dc;
DeclList dl;
void evalAttr0() { dc.stIn = this.stIn;
dc.evalAttr0();
dl.stIn = dc.stOut;
dl.evalAttr0();
this.stOut = dl.stOut;
}
void evalAttr1() { void evalAttr1() { dc.st = this.stIn;
dl.st = this.stOut;
dc.evalAttr1();
dl.evalAttr1();
162
© A. Poetzsch-Heffter, TU Kaiserslautern 10.05.2007
} }
Attribute Evaluator (7)
abstract class Decl { SymTab stIn;
SymTab stOut;
SymTab st;
abstract void evalAttr0();
abstract void evalAttr1();
} }
class MethDecl extends Decl { String dmid;
String umid;
String uaid;
void evalAttr0() {
if( this.stIn.lookup(dmid)!=SymTab.UNDEF ){
so println("Identifier already declared!");
so.println( Identifier already declared! );
}
this.stOut = this.stIn.enter(dmid,false);
}
void evalAttr1() {
if( this.st.lookup(umid)!= SymTab.METH ){
so.println("Method not declared!");
}
if( this st lookup(uaid)!= SymTab FIELD ){
if( this.st.lookup(uaid)! SymTab.FIELD ){
Name Analysis Implementation of Attribution
Attribute Evaluator (8)
class FieldDecl extends Decl { String id;
void evalAttr0() {
if( this.stIn.lookup(id)!= SymTab.UNDEF ){
so.println("Identifier already declared!");
}
thi tO t thi tI t (id t ) this.stOut = this.stIn.enter(id,true);
}
void evalAttr1() { } }
}
Die im obigen Beispiel demonstrierten Auswertungs- methoden evalAttr, evalAttr0, evalAttr1 lassen sich
Bemerkung:
von Attributauswertergeneratoren automatisch erzeugen.
Attributierung und Attributauswertung sind allg.
Verfahren zur Anreicherung von Baumstrukturen.
Ihre Anwendung ist nicht auf die kontextabhängige Bemerkung:
Analyse von Sprachen beschränkt.
Lesen Sie zu Abschnitten 2.3.2.3:
Wilhelm, Maurer:
• die Abschnitte 9.2 und 9.3 (S. 424-436)
The evaluator methodsevalAttr, evalAttr0, evalAttr1can be generated automatically by attribute evaluator generators.
Remark: Attribution and attribute evaluation are techniques to decorate tree structures and are not limited to context-dependent analysis of languages.
Ina Schaefer Context-Dependent Analysis 69
Literature
Recommended Reading
Wilhelm, Maurer: Sections 9.2 and 9.3, pp. 424–436