2.3 Context-Dependent Analysis

Transcription

2.3 Context-Dependent Analysis
Content of Lecture
1. Introduction
2. Syntax and Type Analysis
Compilers and Language Processing Tools
2.1 Lexical Analysis
2.2 Context-Free Syntax Analysis
2.3 Context-Dependent Analysis
Summer Term 2011
3. Translation to Target Language
Prof. Dr. Arnd Poetzsch-Heffter
3.1 Translation of Imperative Language Constructs
3.2 Translation of Object-Oriented Language Constructs
Software Technology Group
TU Kaiserslautern
4. Selected Aspects of Compilers
4.1
4.2
4.3
4.4
4.5
Intermediate Languages
Optimization
Data Flow Analysis
Register Allocation
Code Generation
5. Garbage Collection
6. XML Processing (DOM, SAX, XSLT)
c Prof. Dr. Arnd Poetzsch-Heffter
1
Context-Dependent Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
2
Context-Dependent Analysis
Section outline
1. Problem and specification techniques
2. Name analysis
2.3 Context-Dependent Analysis
2.1 Specification of name analysis
2.2 Realizations based on symboltables
2.3 Implementation of attributions
3. Type analysis
3.1 Specification of type analysis
3.2 Type safety
3.3 Implementation aspects
4. Interplay of name and type analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
3
Context-Dependent Analysis
Syntax and Type Analysis
4
Context-Dependent Analysis
Terminology
Learning objectives
• Most authors talk about semantic analysis or analysis of the static
• Tasks of context-dependent analysis
semantics.
• Typical context conditions of programing languages
• We use the term context-dependent analysis, because
I
the phase (as the other phases) aims at checking whether the
program is a program at all. If it is not a program, it has no
semantics.
I
the term semantic analysis applies better to the analysis of the
program semantics (e.g. for optimizing, avoiding runtime errors ...)
c Prof. Dr. Arnd Poetzsch-Heffter
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
• Name analysis and relation to binding
• Type analysis and relation to verification
• Techniques for specification and implementation of
context-dependent analysis
5
Context-Dependent Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Syntax and Type Analysis
6
Problem and Specification Techniques
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
2.3.1 Problem and Specification Techniques
Syntax Tree
Context Checking/
Attribute Evaluation
Decorated Syntax Tree
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
7
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
8
Context-Dependent Analysis
Problem and Specification Techniques
Context-Dependent Analysis
Problem of context-dependent analysis
Problem and Specification Techniques
Examples
Connection to declaration/inheritance:
1
2
3
4
5
6
The context-dependent analysis
• creates connection between declaration and applied occurrence
of a program element
• checks the correct usage of program elements
class A {
void m() { x = B.x; }
static B x;
}
class B extends A { }
Questions:
• checks further context conditions, e.g. initialization of variables
• 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?
• Is the static field x inherited,
i.e. is the program context correct?
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
9
Problem and Specification Techniques
Syntax and Type Analysis
Context-Dependent Analysis
10
Problem and Specification Techniques
Examples (2)
Examples (3)
Connection between declaration and access control rules:
Usage of declaration and type information: Information computed
by the context-dependent analysis is a prerequisite for the translation.
class MyClassIsMyCastle {
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();
}
}
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");
}
}
class Overloading {
public static void main(String[] argv){
Object xact = new A();
new B(). m( xact );
}
}
Questions:
• Which qualified names are
permitted?
• May the inner class
Bathroom access a field of
the inner class
DiningRoom?
Type of xact required to select correct method.
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
11
Problem and Specification Techniques
Syntax and Type Analysis
Context-Dependent Analysis
Specification and implementation techniques
12
Problem and Specification Techniques
Attribute grammars
• Informal specification techniques for name and type rules are
diverse.
• Declare for each non-terminal and each terminal attributes (name
• Formal specification techniques are based on attribute grammars
and type).
and inference rules.
• Attributes are a concept to incrementally decorate tree structures
(not only for compiler construction). We consider
I
I
attribute grammars
attributatio of abstract syntax
• Declare for each production of the CFG, (semantic) rules how the
values of the attributes are computed.
Beispiel:
(Einführung
Attributierung)
Beispiel:
Beispiel:
(Einführung
(Einführung
Attributierung)
Attributierung)
Betrachte
die Grammatik
Betrachte
Betrachte
die Grammatik
die
Grammatik
Prog
ProgProg! AnwSeq
! AnwSeq
, ! ,AnwSeq ,
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
Problem and Specification Techniques
Attribute grammars (2)
Example: Consider the grammar
AnwSeq
! eAnwSeq
| Anw
AnwSeq
AnwSeq
! e!
| eAnw
| Anw
AnwSeq
, ,AnwSeq ,
AnwIdent
Anw Anw ! !
Ident
|!
AnwSeq
| Ident
AnwSeq| AnwSeq
c Prof. Dr. Arnd Poetzsch-Heffter
13
Syntax and Type Analysis
Att
ibtiktBerechnung
B Anzahl
dder
A
hl der
d
Attributgrammatik
Att
ibAttributgrammatik
Attt ib t Attributgrammatik
tik zur
Bzur Berechnung
Bh tikhzur
der
d Berechnung
Ader
d h
Anzahl
Ahl der
d hlder
d Anzahl
Vorkommen
eines
geg.
Bezeichners
im Programm:
Context-Dependent
Analysis
Problem
and Specification Techniques
Vorkommen
Vorkommen
eineseines
geg.geg.
Bezeichners
Bezeichners
im Programm:
im Programm:
- die Nichtterminale
u.die
Anw
- die -Nichtterminale
die Attribute
Nichtterminale
AnwSeq
AnwSeq
u. Anw
u.AnwSeq
Anw
erhalten
erhalten
dieerhalten die
grammars
(3)
vomund
Typvom
Ident
undTyp
Attribute
Attribute
id vom
idAttribute
vom
Typ Ident
TypidIdent
und
anz
anz
vom
Typ
intanzintvom Typ int
- die Attributierungsregeln
wie folgt aus,
- die -Attributierungsregeln
die Attributierungsregeln
sehen
sehen
wie folgt
wie sehen
folgt
aus,aus,
wobei
id
anz bedeutet.
wobei
wobei id und
id undanz bedeutet.
anzund
bedeutet.
• Prog → AnwSeq,
ProgProg
• AnwSeq → | Anw AnwSeq,
• Anw → Ident | AnwSeq
I
AnwSeq
AnwSeq
AnwSeq
AnwSeq
id of type Ident
anz of type int
• The semantic rules are as follows:
Prog
BezeichnerBezeichnerBezeichnereingabe
eingabe
eingabe drucken
drucken
Attribute grammar to compute number of occurrences of an identifier in
a program:
• non-terminals AnwSeq and Anw get attributes
I
0
0
Syntax and Type Analysis
15
A S
AnwSeq
A AnwSeq
AnwSeq
A
S S
drucken
AnwSeq
AnwSeq
AnwAnw
AnwAnw
Anw
+
+
+
Anw
AnwSeq
AnwSeq
AnwSeq
Anw
AnwAnw
if = then 1 else 0
= then
1 else
1 else
0
0
0 if = if then
IdentIdent
c Prof. Dr. Arnd Poetzsch-Heffter
14
Ident
AnwSeq
AnwSeq
AnwSeq
TU Type
Kaiserslautern
c Prof.10.05.2007
Dr. Arnd Poetzsch-Heffter © A. Poetzsch-Heffter,
Syntax and
Analysis
10.05.2007
10.05.2007
© A. Poetzsch-Heffter,
© A. Poetzsch-Heffter,
TU Kaiserslautern
TU Kaiserslautern
128
128
128
16
Context-Dependent Analysis
Problem and Specification Techniques
Context-Dependent Analysis
Attribute grammars (4)
Attribute grammars (5)
Definition
Remarks:
Let Γ be a CFG. An attribute grammar over Γ consists of
• Synthesized attributes are, for example, the type of an expression
• two disjoint set of attributes, Inh(X) the set of inherited attributes
or the target code.
and Syn(X) the set of synthesized attributes
• Inherited attributes are, for example, declaration information or
• an association of attributes to symbols X ∈ T ∪ N of Γ.
symbol tables.
• the types/domains of the attributes
• Different classes of attribute grammars are distinguished by the
• semantic rules for each inherited attribute a ∈ Inh(Xi ) on the right
permitted dependencies between attributes.
side of a production and for each synthesized attribute
a ∈ Syn(X0 ) on the left side of a production
• Attribute grammars are generalizations of recursion schemes.
• We use attribute grammars to describe processes.
Xi .a = f (Yj , . . .)
• Attribute grammars are the specification technique for many
compiler generators.
• potentially further semantic actions, e.g. for output
c Prof. Dr. Arnd Poetzsch-Heffter•
Attributgrammatiken
stellen
Verallgemeinerung
Syntax and
Type eine
Analysis
von Rekursionsschemata dar.
17
Problem and g
Specification
Techniques
von Abläufen.
• Wir benutzen AGen zur Beschreibung
Context-Dependent Analysis
Problem and Specification Techniques
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
• AGen bilden die Spezifikationstechnik für viele
Example: Combining
productions
Übersetzer-Generatoren.
18
Problem and Specification Techniques
Attribution of abstract syntax
Beispiel: (Zusammensetzen von attributierten
Produktionen)
Prog
Attributierung abstrakter Syntax:
BezeichnerBezeichner
dr cken
drucken
• analog zu Attributgrammatiken
eingabe
• Analogue to attribute grammars
AnwSeq den Typen der abstrakten
• Die Attribute werden
Syntax zugeordnet. +
• Attributes are associated to types of abstract syntax
• Attributes of variant types are used for all occurrences of this type,
• Attribute der Variantentypen
werden an alle
Anw
AnwSeq
Varianten „vererbt“, d.h. wenn
a ein Attribut eines
+
Variantentyps
mit
Variante
V
i
ist,
dann hat auch
AnwSeq
Anw
AnwSeq
Vi ein Attribut a.
i.e. if a is an attribute of the variant type V , then each variant Vi
has an attribute a.
• Tuple types (and list types) are also attributed.
0
Tupeltypdeklarationen
• attributiert werden
0
if = then 1 else 0
(und Listentypproduktionen;
hier nicht behandelt)
Ident
© A. Poetzsch-Heffter, TU Kaiserslautern
10.05.2007
c Prof. Dr. Arnd Poetzsch-Heffter
130
Syntax and Type Analysis
19
Beispiel: (Attributierung abstrakter Syntax)
Context-Dependent Analysis
Problem and Specification Techniques
Syntax and Type Analysis
Context-Dependent Analysis
Prog Attribution
( AnwSeq
)
Example:
of abstract
syntax
20
Problem and Specification Techniques
Inference rules - concept
AnwSeq = Leer( ) | Elem( Anw, AnwSeq )
Ident ) | SeqAnw( AnwSeq )
Anw
= IdAnw(
• Prog(AnwSeq)
• AnwSeq = Empty () | Elem(Anw, AnwSeq)
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.
Wir benutzen die gleiche graphische Notation wie
bei AGen, verwenden allerdings den Tupeltypen als
Some
semantic
linke
Seiterules:
der Produktion:
• Anw = IdAnw(Ident) | SeqAnw(AnwSeq)
IdAnw
c Prof. Dr. Arnd Poetzsch-Heffter
Principle:
SeqAnw
• Considered property is formalized with a fixed number of
parameters. Each parameter is a program part.
if = then 1 else 0
• With inference rules, the property (judgment) is defined inductively
over the program structure.
AnwSeq
Ident
© A. Poetzsch-Heffter, TU Kaiserslautern
10.05.2007
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
131
21
Problem and Specification Techniques
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Inference rules - concept (2)
Syntax and Type Analysis
22
Problem and Specification Techniques
Literature
Example: Language as in the previous examples
Property: Identifier Y occurs N times in program part P (Y ` P : N)
Rules:
Y ` AS : N
Y ` Prog(AS) : N
Y ` Empty() : 0
Y 6= X
Recommended reading:
Y ` AS : N
Y ` Elem(A, AS) : M + N
Wilhelm, Maurer: Section 9.2, pp. 424-429
Y ` AS : N
Y ` SeqAnw(AS) : N
Y =X
Y ` IdAnw(X ) : 0
c Prof. Dr. Arnd Poetzsch-Heffter
Y `A:M
Y ` IdAnw(X ) : 1
Syntax and Type Analysis
23
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
24
Context-Dependent Analysis
Name Analysis
Context-Dependent Analysis
Name Analysis
Name Analysis
• Name analysis determines that each used identifier is also
declared.
2.3.2 Name Analysis
• Checks context conditions
• Has to make declaration information available at applied
occurrence of identifier
I
I
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
25
Name Analysis
by connecting declaration and applied occurrence
by transferring declaration information to applied occurrence
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
26
Name Analysis
Name Analysis (2)
Name analysis is the prerequisite for binding of names to program
elements, thus this phase is often called identification.
2.3.2.1 Specification of name analysis
Program elements are for instance:
• packages, modules, classes, types
• procedures, functions
• variables, fields, records, parameters
• labels
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
27
Name Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Specification of name analysis
Name Analysis
SieheVorlesung
Vorlesung
Siehe
Beispiel:(Zusammenwirken
(Zusammenwirkender
derBegriffe)
Begriffe)
Beispiel:
Abstract Syntax Abstrakte
of
C Sub-Language:
Abstrakte
Syntaxeiner
einerC-Teilsprache:
C-Teilsprache:
Syntax
• 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.
Program
Program
((
GlobDeclList* *
GlobDeclList
GlobDecl
GlobDecl
==
Var
Var
((
Proc
Proc
((
LocVarList * *
LocVarList
LocVar
LocVar
((
GlobDeclList ) )
GlobDeclList
GlobDecl
GlobDecl
Var | | Proc
Proc
Var
Identidid) )
Ident
Identid,
id,LocVarList,
LocVarList,
Block
Ident
Block
) )
LocVar
LocVar
Identidid) )
Ident
Block
Block
Stat
Stat
Assign
Assign
Call
Call
SeqStat
SeqStat
Exp
Exp
ExpList
ExpList
LocVarList,Stat
Stat) )
LocVarList,
Assign
| Block
Assign| |Call
Call| |SeqStat
SeqStat
| Block
Ident
Identid,
id,Exp
Exp) )
Ident
idid,
Identid,
id ExpList
ExpList ) )
Stat
Stat, ,Stat
Stat ) )
Ident
Identidid) )
Exp
Exp
In der Attributierung gehen wir davon aus, dass die
GlobDeclList
GlobDeclList= =
GlobalDeclEmpty
GlobalDeclEmpty
||GlobDeclElem(
) )
GlobDeclElem(GlobDecl,
GlobDecl,GlobDeclList
GlobDeclList
10.05.2007
10.05.2007
Syntax and Type Analysis
((
==
((
((
((
((
**
In der Attributierung gehen wir davon aus, dass die
For attribution, we
considerSequenzen
list types
as sequences
Listentypen
aufgelöst
sind;
z.B.:
ListentypenininSequenzen
aufgelöst
sind;
z.B.:
Remark: Specification techniques for name analysis are not as
advanced as for context-free analysis
(Gap between formal and informal specification techniques).
Context-Dependent Analysis
28
Example: Specification of name analysis
Informal specification technique
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Beispiele:(zur
(zurErläuterung
Erläuterungder
derBegriffe)
Begriffe)
Beispiele:
29
Name Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
© A. Poetzsch-Heffter, TU Kaiserslautern
© A. Poetzsch-Heffter, TU Kaiserslautern
Context-Dependent Analysis
Example: Specification of name analysis (2)
136
136
Syntax and Type Analysis
30
Name Analysis
Example: Specification of name analysis (3)
• The scopes are defined as:
I for a global variable, from declaration to end of program
I for a procedure, from the beginning of the parameter list to the end
of the program
I for a formal parameter, from the end of the parameter list to the end
of the enclosing procedure
I 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
• Program elements: global variables, procedures, formal
parameters, local variables, ...
• Occurrences of Ident in Var, Proc, Loc Var are defining, all other
occurrences 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.
• Visibility is defined as usual.
• For each identifier at most one binding pair may be visible at each
program point.
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
31
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
32
Context-Dependent Analysis
Name Analysis
Context-Dependent Analysis
Name Analysis
Spezifikation mit funktionaler Attributierung:
Name analysis
with functional attribution
Idee:
Berechne für jede Anwendungsstelle eines Bezeichners
Idea: Compute for
each applied occurrence of an identifier, the list of
die Liste der dort sichtbaren Bindungspaare (bzw. eine
Tabelle,
die a
jedem
Bezeichner
seine Deklaration
visible binding pairs
(or
table
that maps
each identifier to its
zuordnet).
d ) Einfachste
Ei f h
Variante:
V i
Benutze
B
funktionale
f ki
l
declaration)
Sprache für die Spezifikation/Realisierung.
Simplest solution: Functional language
D t t
Datentypen
u. Funktionen
F kti
zur Beschreibung
B
h ib
d
des
2.3.2.2 Realizations based on symboltables
Environments bzw. der Symboltabelle:
Data types
and functions
and symbol table:
Attributierung
für for
dieenvironments
Namensanalyse:
Decl =
GlobDecl
|
LocVar
Env * Typen
Decl
Die folgenden
der abstrakten Syntax erhalten
ein ererbtes
Attribut envin
Typappfront(d,e)
Env
FCT enter(Decl
d, Env vom
e) Env:
(dargestellt als
) : GlobDeclList, GlobDecl,
FCT concat( Env e1, Env e2 ) Env:
LocVarList,
ExpList
IF LocVar,
e2=Env() Stat,
THEN Exp,
e1
ELSE
concat( appback(e1,first(e2)),
GlobDecl erhält außerdem
ein abgeleitetes
rest(e2)
)
A ib FCT
Attribut
envout
(dargestellt
(d Ident i,ll Env
als
l e ) Decl:
)
lookup(
IF
e=Env()
ELSF i=id(first(e))
Program
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
33
Env( )
c Prof. Dr. Arnd Poetzsch-Heffter
10.05.2007
THEN nil
THEN first(e)
ELSE GlobDeclElem
lookup(i,rest(e))
Syntax and Type Analysis
© A. Poetzsch-Heffter, TU Kaiserslautern
GlobDeclList
Name Analysis
Context-Dependent Analysis
Attribution for name analysis
34
138
Name Analysis
Attribution for name analysis (2)
Die folgenden Typen der abstrakten Syntax erhalten
• GlobDeclList,
GlobDecl, LocVarList, LocVar, Stat, Exp, ExpList get
ein ererbtes Attribut envin vom Typ Env
inherited(dargestellt
attribute envin
Env
als
)of: type
GlobDeclList,
GlobDecl,
LocVar, Stat,
Exp, ExpList
• GlobDeclLocVarList,
gets synthesized
attribute
envout
GlobDecl erhält außerdem ein abgeleitetes
envin
A ib envout (dargestellt
Attribut
(d
ll als
l
)
Program
GlobDecl
GlobDeclEmpty
Attributierung für die Namensanalyse:
GlobDeclList
Var
lookup( ) = nil ?
lookup(_,_)
enter(_,_)
Ident
Proc
GlobDeclElem
Env( )
lookup(_,_) = nil ?
GlobDeclList
GlobDecl
GlobDeclEmpty
Ident
35
Ident
Name Analysis
lookup(_,_) = nil ?
Ident
36
LocVar
Name Analysis
Ident
concat( )
concat(_,_)
LocVarElem
g
Assign
Block
LocVarList
Block
isVar( lookup(_,_))?
Env( )
139
enter(_,_)
concat(_,_)
LocVarList
LocVar
Stat
Ident
SeqStat
Call
isProc( lookup(_,_))?
lookup(_,_)
p(_ _) = nil ?
Stat
Stat
Ident
ExpList
(Prüfe
checkParameteranzahl)
number of parameters
Ident
10.05.2007
© A. Poetzsch-Heffter, TU Kaiserslautern
140
g
Assign
Block
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
37
isVar(
lookup(_,_))?
Name Analysis
Context-Dependent Analysis
LocVarList
Ident
38
Name Analysis
Exp
ExpElem
SeqStat
isVar(
lookup(_,_))?
• The data type for representing environments and its functional
treatment have advantages for specification
(easy to understand, more appropriate for verification)
Call
isProc( lookup(_,_))?
Ident
Exp
Stat
Syntax and Type Analysis
Attribution for name analysis (6)
Stat
Exp
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Env( )
Attribution
forconcat(_,_)
name analysis (5)
10.05.2007
Exp
LocVarList
LocVar
Stat
139
Syntax and Type Analysis
p(_ _) = nil ?
Attribution for lookup(_,_)
name
analysis (4)
enter(_,_)
© A. Poetzsch-Heffter, TU Kaiserslautern
10.05.2007
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Proc
Attribution for name analysis (3)
LocVarEmpty
Block
© A. Poetzsch-Heffter, TU Kaiserslautern
LocVar
LocVarList
enter(_,_)
Env( )
concat( )
concat(_,_)
enter(_,_)
Syntax and Type Analysis
Context-Dependent Analysis
LocVarElem
)
LocVarList
10.05.2007
lookup( ) = nil ?
lookup(_,_)
c Prof. Dr. Arnd Poetzsch-Heffter
LocVarEmpty
Env(
GlobDeclList
envout
Var
enter(_,_)
• Specification can be directly implemented, e.g. in functional
ExpList
languages.
• For real compilers, a more efficient technique should be used;
Ident
ExpList
ExpEmpty
(Prüfe Parameteranzahl)
© A. Poetzsch-Heffter, TU Kaiserslautern
Remarks: Bemerkungen:
problems:
I
I
140
• Example shows basic technique for name analysis.
• Das Beispiel
p demonstriert eine grundlegende
g
g
Technik für die Namensanalyse.
sketched.
• Transport von Deklarationsinformation zur
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax
and Type
Analysis
Anwendungsstelle
wird
allerdings
nur ansatzweise
verdeutlicht.
verdeutlicht
Linear search for identifiers is too expensive
If hashing is used, values of data type environment cannot be easily
represented by sharing, copying of symbol tables would be
necessary.
• Transport of declaration information to applied occurrence is only
• Der einfache Datentyp zur Repräsentation des
Environments und dessen funktionale Behandlung
bieten aus Spezifikationssicht Vorteile (relativ leicht
39
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
40
Context-Dependent Analysis
Name Analysis
Context-Dependent Analysis
Name analysis with symbol table
Name Analysis
Name analysis with symbol table (2)
A. Schritte zum Erreichen des sequentiellen Flusses:
1. Operationen
p
zum Öffnen und Schließen eines
Gültigkeitsbereichs
G:
Steps for sequential
access to symbol
table
- Das Öffnen markiert quasi die Deklarationen von G.
A. Schritte zum Erreichen des sequentiellen Flusses:
Compilation units can contain a large number of identifiers. Thus,
1. Operationen
pand lookup zum
Öffnenshould
und Schließen
eines
insertion
of identifiers
have constant
complexity.
1. Operations for entering and exiting scope G:
Gültigkeitsbereichs G:
Solution:
- Das Öffnen markiert quasi die Deklarationen von G.
I
I
- Beim Schließen werden alle Deklarationen von G
Entering of scope G marks declarations of G
a s derallTabelle
aus
gelöscht.
gelöscht
Exiting G deletes
declarations
of G
• Hashing of identifiers in compilation unit (e.g. during scanning);
- Beim Schließen werden alle Deklarationen von G
Block
each compilation unit has finite number of identifiers:
a
der Tabelle
gelöscht.
gelöscht
→ aus
uses array
with constant
access
implemented as globalBlock
data structure with destructive updates:
→ avoids copying
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
L V Li t
LocVarList
Syntax and Type Analysis
41
St t
Stat
Name Analysis
Name analysis with symbol table (3)
2. Schreibende und lesende Zugriff auf die Symboltabelle werden sequentialisiert;
q
; um das in der
Attributierung zu erzwingen, betrachtet man die
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Symboltabelle
formal auch als Ergebnis einer
lesenden Operation wie
lookup.
Context-Dependent Analysis
Name Analysis
isVar( lookup(_,_))?
Steps for development of data structure
1. Since scopes in the considered language (and in most other
Ident
Exp
languages) are nested, entering and exiting scopes can be
organized10.05.2007
with a stack. © A. Poetzsch-Heffter, TU Kaiserslautern
143
a read operation, such as a lookup.
I
Assign
I
I
isVar( lookup(_,_))?
I
I
10.05.2007
c Prof. Dr. Arnd Poetzsch-Heffter
Exp
© A. Poetzsch-Heffter, TU Kaiserslautern
Context-Dependent Analysis
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)
Symbol table is stack of finite mappings
stackof (Ident → DeclInf)
143
Syntax and Type Analysis
42
Assign
Name analysis with symbol table
(4)
2. Schreibende und lesende Zugriff auf die Symboltabelle werden sequentialisiert;
q
; um das in der
Attributierung zu erzwingen, betrachtet man die
2. Write
and read accesses
symbol
are sequentialized.
Symboltabelle
formaltoauch
alstable
Ergebnis
einer
For attribution, symbol table is formally considered as the result of
lesenden Operation wie lookup.
Ident
St t
Stat
L V Li t
LocVarList
close_scp(_)
open_scp(_)
close_scp(_)
open_scp(_)
• Enforce sequential access to symbol table such that it can be
43
Name Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Name analysis with symbol table (5)
Syntax and Type Analysis
44
Name Analysis
Symboltabelle für Sprachen mit
geschachtelten
Gültigkeitsbereichen:
Symbol table for
languages
with nested scopes
Momentaufnahme der Datenstruktur bei aktueller
Snapshot at nesting
level 2
Schachtelungstiefe
2:.
nl:
2. Transformation to a finite mapping of identifiers to stacks of
declaration information, i.e.
IdNo:
1
2
2
•
•
max nl
max_nl
•
...
2
1
0
•
•
•
DE
••
...
stackof (Ident → DeclInf)
curr nl
curr_nl
is transformed to
•
m
•
max_id
•
DE
••
DE
••
DE
••
DE
••
...
k
Ident → stackof(DeclInf)
...
This can be efficiently implemented by a matrix-linked data
structure.
Dabei sind die Deklarationseinträge für einen
Declarations for each
identifier
are stored
in rows
a stack that
Bezeichner
zeilenweise
angeordnet.
Sieforming
bilden einen
Keller,
der von contain
rechts nach
links
wächst. Die
Spalten
from right to left. The
columns
the
nesting
levels.
DE is the
enthalten die Einträge zu einer Schachtelungstiefe.
Schachtelungstiefe
grows
declaration entry (includes IdNo of entry).
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
45
Name Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Operations on symbol table
DE bezeichnet den Deklarationseintrag (umfasst IdNo).
10.05.2007
Syntax and Type Analysis
© A. Poetzsch-Heffter, TU Kaiserslautern
Name Analysis
46
145
Advantages of SymTab data type
• create(): initializes empty symbol table
• open_scp(st): increments curr_nl
• close_scp(st): deletes all entries for curr_nl, restores identifier
• Complexity of lookup and enter is constant.
links, decrements curr_nl
• Complexity of exiting scope is linear in the number of declarations
• enter(id,de,st): adds entry for curr_nl, the entry contains
I declaration entry (de)
I pointer for nl links
I pointer to other entries for the identifier id
of this scope.
• Efficient memory management possible.
• lookup(id,st): access to declaration information of visible binding
pair for id
For sequential access in attribution, each operation returns pointer to
symbol table. The specified data type is called SymTab.
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
47
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
48
DeclInf
VInf
PInf
= VInf | PInf
()
( Int parcount )
2. Attributierung:
Context-Dependent Analysis
Name Analysis
Zur Sequentialisierung der Symboltabellenzugriffe
erhalten
h lt ffastt alle
ll Typen
T
der
d abstrakten
b t kt Syntax
S t
ein ererbtes Attribut symin vom
Typ
SymTab
Name
Analysis
(dargestellt als
) und ein abgeleitetes
Attribut symout (dargestellt als ). Darüberhinaus
b k
bekommen
LocVarList
L V Li t und
d ExpList
E Li t ein
i abgeleitetes
b l it t
Attribut length vom Typ Int (dargestellt als ).
Context-Dependent Analysis
Example: Usage of SymTab data type
Example: Semantic rules
Anwendung der Symboltabelle anhand der
Beispielsprache:
Program
1. Bestimmung der Deklarationsinformation:
1. Declaration information:
DeclInf
VInf
PInf
open_scp(_)
= VInf | PInf
()
( Int parcount )
GlobDeclList
Attribut length vom Typ Int (dargestellt als
Context-Dependent Analysis
close_scp(_)
lookup( ) = nil ?
lookup(_,_)
49
Name Analysis
open_scp(_)
LocVar
GlobDeclList
Ident
LocVar
open_scp(_)
GlobDeclEmpty
Ident
close_scp(_)
LocVarList
lookup( ) = nil ?
lookup(_,_)
close_scp(_)
147
Block
LocVar
LocVar
close_scp(_)
ExpEmpty
length
148
Context-Dependent Analysis
0
Stat
51
Name Analysis
Ident
Stat
Exp
Exp
SeqStat
ExpList
E
Exp
Stat
© A. Poetzsch-Heffter, TU Kaiserslautern
Ident
Stat
ExpElem
Stat
149
+1
isProc( lookup(_,_))?
Ident
ExpList
Syntax and Type Analysis
Context-Dependent Analysis
149
Call
© A. Poetzsch-Heffter, TU Kaiserslautern
Exp
53
Name Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
E
Exp
10.05.2007
parcount(lookup(_,_)) = _?
ExpList
Syntax and Type Analysis
Context-Dependent Analysis
Declaration information
Ident
Declaration information has to be sufficient
• to check the correct application of an identifier
54
Name Analysis
© A. Poetzsch-Heffter, TU Kaiserslautern
Declaration information (2)
isVar( lookup(_,_))?
I
Name Analysis
Assign Exp
SeqStat
ExpEmpty
I
52
isVar( lookup(_,_))?
10.05.2007
I
Syntax and Type Analysis
isVar( lookup(_,_))?
Ident
I
+1
c Prof. Dr. Arnd Poetzsch-Heffter
isVar( lookup(_,_))?
c Prof. Dr. Arnd Poetzsch-Heffter
Stat
Example: Semantic rules (5)
Assign
0
ExpElem
LocVarList
Context-Dependent Analysis
Example: Semantic rules (4)
148
close_scp(_)
open_scp(_)
LocVarList
LocVarListSyntax and Type Analysis
© A. Poetzsch-Heffter, TU Kaiserslautern
10.05.2007
©
10.05.2007
LocVarList
Block
+1
open_scp(_)
10.05.2007
+1
Ident
Block
LocVarElem
0
c Prof. Dr. Arnd Poetzsch-Heffter
LocVarElem
0
LocVarList
Ident
LocVarEmpty
Block
enter( Vinf() )
enter(_,Vinf(),_)
LocVarEmpty
open_scp(_)
Ident
close_scp(_)
enter(_,Pinf(_),_)
enter( Vinf() )
enter(_,Vinf(),_)
© A. Poetzsch-Heffter, TU Kaiserslautern
enter(_,Pinf(_),_)
50
Name Analysis
= nil ?
Example: Semantic rules (3)
GlobDeclList
Proc
lookup(
lookup(_,_)
) = nil ?
lookup(_,_) = nil ?
10.05.2007
ProcAnalysis
Syntax and Type
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis lookup(_,_)
GlobDecl
enter( Vinf() )
enter(_,Vinf(),_)
Ident
enter( Vinf() )
enter(_,Vinf(),_)
Example: Semantic rules (2)
147
Var
).
createSyntax and Type Analysis
lookup( ) = nil ?
lookup(_,_)
GlobDeclList
© A. Poetzsch-Heffter, TU Kaiserslautern
10.05.2007
GlobDeclElem
Var
c Prof. Dr. Arnd Poetzsch-Heffter
GlobDecl
GlobDeclEmpty
2. Attribution: 2. Attributierung:
For sequential access
to the symbolder
table,
almost all types of the
Zur Sequentialisierung
Symboltabellenzugriffe
abstract syntax erhalten
get
symin
h an
lt inherited
ffastt alle
ll Typen
T attribute
der
d abstrakten
b
t kt of Syntax
Stype
t SymTab
ein attribute
ererbtes Attribut
symin
vom Typ SymTab
and a synthesized
symout
.
(dargestelltand
als ExpList
) und ein
abgeleitetes
Additionally, LocVarList
get
an synthesized attribute
Attribut symout (dargestellt als ). Darüberhinaus
length of type Int.
b k
bekommen
LocVarList
L V Li t und
d ExpList
E Li t ein
i abgeleitetes
b l it t
Program
GlobDeclElem
create
150
• How to determine declaration information?
I Which declaration and program elements exist?
I Which information is necessary for each program element?
• Implementation of declaration information
I Reference to declaration position (simple)
I Using special data structures (designed for the particular task,
e.g. table for class elements)
Is a variable allowed at the applied occurrence?
Is there a procedure, type, label ... allowed?
Is the number of parameters
correct?
Call
Are the types correct?
• to isProc(
handle lookup(_,_))?
named scopes
parcount(lookup(_,_)) = _?
I Is a selector admissible?
I Which methods and fields are contained in a class?
ExpList
• to check additionalIdent
context conditions
(e.g. case statement in Pascal)
10.05.2007
c Prof. Dr. Arnd Poetzsch-Heffter
© A. Poetzsch-Heffter,
TU Kaiserslautern
Syntax and Type Analysis
150
55
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
56
Context-Dependent Analysis
Name Analysis
Context-Dependent Analysis
Declaration information (3)
Name Analysis
Literature
Remarks:
• Name analysis and its implementation depend on the rules of the
programing language, e.g. name spaces, application of
declarations, overloading
Recommended Reading:
• Wilhelm, Maurer: Sections 9.1.1 and 9.1.2, pp. 408 – 416
• For separate translation, a symbol table for each translation unit
• Appel: Section 5.1, pp. 108 – 119
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
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Syntax and Type Analysis
57
Name Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
58
Name Analysis
Implementation of attribution
Learning objectives:
2.3.2.3 Implementation of attributions
• Different interpretations of attributions
• Introduction into the semantics of attributions
• Classes of attribute grammars
• Implementation techniques
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Syntax and Type Analysis
59
Name Analysis
I
I
Remarks:
• Use semantic actions with side effects carefully
w/o storage: goal is computation of attribute values at root node
only
with storage: goal is computation of all attribute values for further
processing phases
Syntax and Type Analysis
“copy” actions (if two attributes are equated)
semantic actions
An attribute value v is stored as long as all actions have been
executed that use v as parameter.
2. Functional or computational interpretation
An AG determines how attribute values in a tree are computed.
Context-Dependent Analysis
Name Analysis
3. Control of action executions
An AG describes in which order semantic actions are executed
Question: Has the system a (unique) solution?
c Prof. Dr. Arnd Poetzsch-Heffter
60
Interpretations of attribute grammars (2)
1. Specification and equation interpretation
An attribute grammar (AG) assigns attribute values to nodes of
grammar trees. The specification is given by an equation system
that is generated for each tree.
I
Syntax and Type Analysis
Context-Dependent Analysis
Interpretations of attribute grammars
I
c Prof. Dr. Arnd Poetzsch-Heffter
• For classification of attribute grammars, only attribute
dependencies are considered.
61
Name Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
Classes of attribute grammars
62
Name Analysis
Classes of attribute grammars (2)
Definition (Non-circular attribute grammar)
An attribute grammar AG over Γ is
Let AG be an attribute grammar over CFG Γ. For syntax trees S of Γ,
let Dt(S) denote the directed attribute dependency graph. The AG is
non-circular if Dt(S) is cycle free for all S.
• S-attributed if AG only has synthesized attributes
• L-attributed if the attributes for each syntax tree for Γ can be
evaluated with a left-right depth-first traversal.
Remarks:
Other important classes of attribute grammars:
• For non-circular attribute grammars, the equation system for each
• absolute non-circular (Kennedy & Warren)
syntax tree has a unique solution.
• ordered (Karstens)
• Testing non-circularity is exponential in general. Thus and for
generating efficient attribute evaluators, restricted classes of
attribute grammars are considered.
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
63
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
64
Context-Dependent Analysis
Name Analysis
Context-Dependent Analysis
Implementation of attribute grammars
Name Analysis
Example: L-AG implemnted by recursive functions
Beispiel: (Impl. v. L-AG mit rekursiver Funktion)
Implementierung einer einfachen L-AG (s. Vorlesung)
durch eine rekursive Funktion. Der EnvironmentConsider simple L-AG
(cf. lecture): The environment parameter represents
Parameter entspricht dem ererbten Attribut, das
the inherited attribute,
the dem
result
represents
Ergebnis
abgeleiteten
Attribut:the synthesized attribute:
1. Computational interpretation w/o storage
datatype exp =
Int of int
| Add of exp * exp
| Var of string
| Let of string * exp * exp
;
For L-attributed grammars and other simple classes of attribute
grammars, attribute evaluation can be performed by recursive
functions:
val mp =
• inherited attributes become parameters
• synthesized attributes become results
type env = (string * int) list;
For each production, a function is defined that calls the semantic
actions and the functions for the children (sometimes one function can
handle several productions).
Sollen die Attributwerte in Baum gespeichert
fun lkup s []
= raise notDeclared
werden,
man die= Attribute als Komponenten
| lkupkann
s ((s1,i)::es)
if s=s1 then i else lkup s es
der Baumknoten
vorsehen
und durch Besuche
;
der Bäume in geeigneter Reihenfolge die
fun eval E (Int i)
= i
Attributwerte
berechnen:
| eval E (Add
(le,re))
=
Remark:
For other attribute grammar classes, several functions per production
might be used.
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
Let ("x",Add (Int 1, Int 2),
Let ("y",Int 5,
Let ("z",Int 8, Add (Add
2. Berechnungsinterpretation
mit Speicherung:
(Var "x",Var "y"), Var "z"))));
(eval E le) + (eval E re)
= lkup s E
| eval E (Let (s,e1,e2)) =
eval ((s,eval E e1)::E) e2
| eval E (Var s)
! Attributauswerter
65
Name Analysis
Attributauswerter
können von Hand realisiert oder
;
zu einer
i
AG automatisch
t andtiTypehAnalysis
generiert
i t werden.
d157
Syntax
© A. Poetzsch-Heffter,
TU Kaiserslautern
c Prof. Dr. Arnd Poetzsch-Heffter 10.05.2007
Context-Dependent Analysis
66
Name Analysis
Beispiel: (Attributauswerter)
Implementation of attribute grammars (2)
Example: Attribute evaluator
Wir betrachten die Namensanalyse für folgende
Task: Name analysis
forJava-Teilsprache:
Java subset
einfache
Class
D lLi t
DeclList
DeclEmpty
DeclElem
Decl
FieldDecl
MethDecl
2. Computational interpretation with storage
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
( DeclList )
= DeclEmpty
D lE t | DeclElem
D lEl
()
( Decl DeclList )
= FieldDecl | MethDecl
( Ident )
( Ident Ident Ident )
Erläuterung:
siehe Vorlesung
SignatureSignatur
of symbol
table
der
Symboltabelle:
Attribute evaluators can be implemented manually or generated
automatically.
emptyST : ! SymTab
lookup : Ident x SymTab ! FieldMethUndef
© A. Poetzsch-Heffter, TU Kaiserslautern
10.05.2007
Signatur der Symboltabelle:
enter
158
: Ident x FieldMeth x SymTab ! SymTab
emptyST : ! SymTab
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
67
lookup : Ident x SymTab ! FieldMethUndef
Context-Dependent Analysis
enter
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Attributierung:
Name Analysis
Context-Dependent Analysis
: Ident x FieldMeth x SymTab ! SymTab
Example: Attribute evaluator (2)
Name Analysis
Example: Attribute evaluator (3)
Class
Attribution of abstract
syntax
Attributierung:
lookup(_,_) = UNDEF ?
Class
DeclEmpty
DeclEmpty
FieldDecl
emptyST
emptyST
68
enter(_,FIELD,_)
DeclList
Ident
DeclList
Schnittstelle der Symboltabelle:
class SymTab
y
static int
static int
static int
DeclElem
lookup(_,_) = UNDEF ?
c Prof. Dr. Arnd Poetzsch-Heffter
10.05.2007
Context-Dependent Analysis
DeclList
Syntax and Type Analysis
69
© A. Poetzsch-Heffter,
TU Kaiserslautern
Name Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
// ...
) {
}
Attribute
declaration
and
2-pass evaluation
Attributdeklaration
und
2-Passauswertung:
class JClass {
DeclList dl;
// keine Attribute
static
t ti SymTab
S T b emptyST()
t ST() {
return new SymTab();
void evalAttr() {
dl.stIn = SymTab.emptyST();
dl.evalAttr0();
dl.st
= dl.stOut;
dl.evalAttr1();
}
}
}
SymTab enter( String id, boolean isField ) {
// ... }
}
class JClass {
DeclList dl;
// keine Attribute
70
159
}
}
Attributdeklaration und
2-Passauswertung:
Syntax
and Type Analysis
Syntax and Type Analysis
int lookup(
p( String
10.05.2007g id )© A.{
160
Poetzsch-Heffter, TU Kaiserslautern
SymTab.FIELD
oder
}
© A. Poetzsch-Heffter,
TU Kaiserslautern
Name...
Analysis
SymTab enter(
String
id, boolean
Example:
Attribute
evaluator
(5)isField
{
FIELD = 1;
METH = 2;
UNDEF = 3;
c Prof. Dr. Arnd Poetzsch-Heffter
Ident
DeclList
lookup(_,_) = FIELD ?
//
liefert
10.05.2007
Context-Dependent
Analysis
159
InterfaceSchnittstelle
of symbol table
der Symboltabelle:
int lookup(
p( String
g id ) {
// liefert SymTab.FIELD oder ...
Ident
static
t ti SymTab
S T b emptyST()
t Decl
ST() {
lookup(_,_) = METH ?
return new SymTab();
}
Example: Attribute evaluator (4)
class SymTab
y
static int
static int
static int
enter(_,METH,_)
{
FIELD = 1;
METH = 2;
UNDEF = 3;
Ident
Decl
MethDecll
DeclElem
10.05.2007
71
© A. Poetzsch-Heffter, TU Kaiserslautern
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
161
72
Context-Dependent Analysis
Name Analysis
Context-Dependent Analysis
Example: Attribute evaluator (6)
Name Analysis
Example: Attribute evaluator (7)
abstract class DeclList {
SymTab stIn;
SymTab stOut;
SymTab st;
abstract void evalAttr0();
abstract void evalAttr1();
}
abstract class Decl {
SymTab stIn;
SymTab stOut;
SymTab st;
abstract void evalAttr0();
abstract void evalAttr1();
}
class DeclEmpty extends DeclList {
void evalAttr0() { stOut = stIn; }
void evalAttr1() { }
}
class MethDecl extends Decl {
String dmid;
String umid;
String uaid;
class DeclElem extends DeclList {
Decl dc;
DeclList dl;
void evalAttr0() {
if( this.stIn.lookup(dmid)!=SymTab.UNDEF ){
so println("Identifier
so.println(
Identifier already declared!
declared!");
);
}
this.stOut = this.stIn.enter(dmid,false);
}
void evalAttr0() {
dc.stIn = this.stIn;
dc.evalAttr0();
dl.stIn = dc.stOut;
dl.evalAttr0();
this.stOut = dl.stOut;
}
void evalAttr1() {
if( this.st.lookup(umid)!= SymTab.METH ){
so.println("Method not declared!");
}
if( this
this.st.lookup(uaid)!
st lookup(uaid)!= SymTab.FIELD
SymTab FIELD ){
so.println("Attribute not declared!");
}
}
void evalAttr1() {
dc.st = this.stIn;
dl.st = this.stOut;
dc.evalAttr1();
dl.evalAttr1();
}
}
}
10.05.2007
© A. Poetzsch-Heffter, TU Kaiserslautern
c Prof. Dr. Arnd Poetzsch-Heffter
162
Syntax and Type Analysis
Context-Dependent Analysis
73
Name Analysis
c 10.05.2007
Prof. Dr. Arnd Poetzsch-Heffter
© A. Poetzsch-Heffter, TU Kaiserslautern
Context-Dependent Analysis
Example: Attribute evaluator (8)
Syntax and
Type Analysis
163
74
Name Analysis
Remark and literature
class FieldDecl extends Decl {
String id;
Remark:
Attribution and attribute evaluation are techniques to decorate tree
structures and are not limited to context-dependent analyses of
languages.
void evalAttr0() {
if( this.stIn.lookup(id)!= SymTab.UNDEF ){
so.println("Identifier already declared!");
}
thi
this.stOut
tO t = thi
this.stIn.enter(id,true);
tI
t (id t
)
}
Recommended Reading
Wilhelm, Maurer: Sections 9.2 and 9.3, pp. 424–436
void evalAttr1() { }
}
The evaluator methods evalAttr, evalAttr0, evalAttr1 can be
generators.
Die im obigen Beispiel demonstrierten Auswertungsgenerated
automatically
by attribute
methoden
evalAttr,
evalAttr0, evalAttr1
lassenevaluator
sich
von Attributauswertergeneratoren automatisch
erzeugen.
Bemerkung:
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
75
Attributierung und Attributauswertung sind allg.
Context-Dependent
Analysis
Type Analysis
Verfahren zur
Anreicherung von Baumstrukturen.
Ihre Anwendung ist nicht auf die kontextabhängige
Analyse von Sprachen beschränkt.
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Syntax and Type Analysis
76
Type Analysis
Type analysis
Lesen Sie zu Abschnitten 2.3.2.3:
Wilhelm, Maurer:
• die Abschnitte 9.2 und 9.3 (S. 424-436)
10.05.2007
© A. Poetzsch-Heffter, TU Kaiserslautern
Relevance of Typing
164
2.3.3 Type Analysis
• improves readability of programs
• improves possibilities for static checks
• allows more efficient implementations, because runtime checks
can be avoided and memory can be allocated more specifically
• prerequisite for high-level programming techniques
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
77
Type Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Syntax and Type Analysis
78
Type Analysis
Tasks of Type Analysis
• Computation and inference of explicit type information
I for declared program elements often from declaration information
I for expressions from sub-expressions
I potentially complex algorithm (because of overloading, generic
types, missing type declarations)
2.3.3.1 Specification of type analysis
• Type correctness: check of typing rules
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
79
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
80
• wie die Typen/Typinstanzen in Beziehung stehen;
• welche Regeln ein typisiertes Programm erfüllen
muss, um typkorrekt zu sein.
Context-Dependent Analysis
Bei einer formalen Spezifikation
p
eines Typsystems
yp y
Type Analysis
werden Typen mit einer abstrakten Syntax,
die Typregeln mittels Inferenzregeln beschrieben.
Type Analysis
Context-Dependent Analysis
Specification of type analysis
Example: Type System
Beispiel: (Typsystem)
Java kennt die folgenden Typen:
Java (w/o generics) has the following types:
JavaType
= PrimitiveType | ReferenceType
PrimitiveType = byte | short | int | long
| char | boolean | float | double
ReferenceType = ClassType( Ident )
| InterfaceType( Ident )
| ArrayType( JavaType )
The type system of a programing language determines
• which types/type schemes exists in the language
• how types and type instances relate
• which rules a typed program has to satisfy to be type correct
For a formal specification of a type system, types are described by an
abstract syntax and type rules by inference rules.
The
types are
pre-defined.
and interface types are user
Die primitive
Basisdatentypen
(primitive
types) Class
sind vordefiniert.
defined.
types are defined
implicitly.
Klassen-Field
undSubtypbeziehung
Schnittstellentypen
sind benutzerdefiniert.
Definition
der
in Java:
Feldtypen
F
ldt
sind
i d implizit
i li it d
definiert.
fi i t
S ist Subtyp von T, in Zeichen S ! T, gdw.
S = T oder
S und T Referenztypen
sind und
10.05.2007
© A. Poetzsch-Heffter, TU Kaiserslautern
S den Typ T erweitert oder implementiert
oder T = Object
c Prof. Dr. Arnd Poetzsch-Heffter
Type Analysis
oder S = ArrayType(SE) Syntax
undandT=ArrayType(TE)
und SEType
! TE
Context-Dependent Analysis
Analysis
oder es gibt einen Typ U mit S ! U und U ! T.
Note:
A program
has infinitely
manyviele
types.
Beachte:
Ein Programm
hat unendl.
Typen.
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
81
Type Analysis
Subtyping
166
82
Type rules
Betrachte folgende Teilsprache von Java:
Consider the following subset of Java:
Stat
IfStat
Invoc
A i
Assign
Exp
= IfStat | Invoc | Assign
( Exp cond, Stat then, Stat else )
( Exp target, Ident name, Exp par )
( Id
Ident
t lh
lhs, E
Exp rhs
h )
= Assign | Invoc | Arithm | Relation
| Cast | Const | Var | Null
Arithm
( Exp le, ArithmOp op, Exp re )
Relation ( Exp
p le,
, RelOp
p op,
p, Exp
p re )
Cast
( JavaType t, Exp e )
Const
= IntConstTypregeln
| LongConst
Ausgewälte
von Java:
| FloatConst |...
Bezeichne
!
Var
( Ident id )"#$e : S die Eigenschaft, dass in der
N ll
Null
()
Umgebung
! der Ausdruck e vom Typ S ist.
Definition of subtyping relation in Java:
S is a subtype of T, S ≤ T, iff
• S = T or
• S and T are reference types and
I S extends or implements the type of T or
I T = Object or
I S = ArrayType(SE) and T = ArrayType(TE) and SE ≤ TE or
I or there exists type U with S ≤ U and U ≤ T
10.05.2007
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
83
Type Analysis
Type rules (2)
Type rules (similar to Java):
Bezeichne ! "#$s . die Eigenschaft, dass in der
Umgebung ! die Anweisung s typkorrekt ist.
© A. Poetzsch-Heffter, TU Kaiserslautern
S ReferenceType in !
• Let Σ |= e : S denote the property that in the environment Σ the
! "#$i : int
environment Σ
where Σ captures bindings of variables to types and the subtype
relation.
! "#$f : float
! "#$e : S , S,T % { int, long, float }
! "#$e1 : T, ! "#$e2 : T
! "# Cast(T,e) : T
• a,b,m: Ident
! "#$$Arithm(e1,aop,e2) : T
• i: IntConst, l:LongConst, f:FloatConst
! "#$e : S , S & '$in !
! "#$e : S , T & S in !
[ a!T]
!T]
!$$ ! "#$e
" !$ "# :Cast(T,e)
T
! "# Cast(T,e)
: T %!$$,
:T
• aop: ArithOp, rop: RelOp
• e,e1,e2: Exp, s,s1,s2:Stat
Syntax and Type Analysis
85
10.05.2007
! "#$$Assign(a,e) : T
c Prof. Dr. Arnd Poetzsch-Heffter
© A. Poetzsch-Heffter, TU Kaiserslautern
Syntax and Type Analysis
168
86
! "#$e1 : S , [ (S,m)!(TP,TR)
] %!$ , ! "#$e2 : TP
Type Analysis
Type Analysis
Context-Dependent Analysis
Type rules (5) ! "#$$Invoc(e1,m,e2) : TR
Type rules (4)
! "#$$Assign(a,e) : T
! "#$e1 : T, ! "#$e2 : T
! "#$$Assign(a,e) .
! "#$$Relation(e1,rop,e2) : boolean
! "#$e1 : T, ! "#$e2 : T
! "#$$Invoc(e1,m,e2) : TR
! ""#$$Invoc(e1,m,e2)
( , , ).
! "#$$Arithm(e1,aop,e2) : T
[ a!T]
!T] %!$$,
!$$ ! "#$e
" $ :T
! "#$$e: boolean , ! "#$$s1. , ! "#$$s2.
! "#$$Assign(a,e) : T
! "#$$IfStat(e,s1,s2) .
! "#$e1 : S , [ (S,m)!(TP,TR) ] %!$ , ! "#$e2 : TP
Zeige: !$$"#$$$Assign( a, Assign( b, Null() ) ) :
InterfaceType(EinTyp) mit
! "#$$Invoc(e1,m,e2) : TR
! "#$$Assign(a,e) : T
! "#$$Assign(a,e) .
c Prof. Dr. Arnd Poetzsch-Heffter
! "#$l : long
! "#$$Relation(e1,rop,e2) : boolean
• S,T, ... : JavaType
Context-Dependent Analysis
[ a!T] %!
! "#$$e : int
! "#$$e : long
! "#$e1
: T, ! "#$e2!:"#$e
T : S, S & T in !
! "#$ e : long
! "#$ e : float
! "#$e : T
In the following, we use the following variables
c Prof. Dr. Arnd Poetzsch-Heffter
84
! ""#$$Var(a)
( ):T
! ""#$null
$ u
:S
expression e is of type S
• Let Σ |= s. the property that the statement s is type correct in the
167
Syntax and
Type
Analysis
Die logischen Variablen
sind
wie
folgt sortiert:
S,
T,
...
:
JavaType,
a,b,m:
Ident,
i: IntConst,
Context-Dependent Analysis
Type Analysis
l: LongConst, f: FloatConst, aop: ArithOp,
rop:
p(3)
RelOp,
p, e,, e1,, e2: Exp,
p, s,, s1,s2
, : Stat
Type rules
c Prof. Dr. Arnd Poetzsch-Heffter
! "#$$Invoc(e1,m,e2) : TR
! ""#$$Invoc(e1,m,e2)
( Analysis
, , ).
Syntax and Type
! "#$$e: boolean , ! "#$$s1. , ! "#$$s2.
! "#$$IfStat(e,s1,s2) .
Zeige: !$$"#$$$Assign( a, Assign( b, Null() ) ) :
87
!$= { [ a! InterfaceType(EinTyp) ],
[ b! ClassType(MeinTyp)
Cl
T
(M i T ) ],
]
ClassType(MeinTyp) & InterfaceType(EinTyp),
c Prof. Dr. Arnd Poetzsch-Heffter
...
} Syntax and Type Analysis
10.05.2007
© A. Poetzsch-Heffter, TU Kaiserslautern
88
169
! "#$$Assign(a,e) : T
! "#$$Assign(a,e) .
Type Analysis
! "#$$Invoc(e1,m,e2)
: TR
! ""#$$Invoc(e1,m,e2)
( , , ).
Type rules (6)
Context-Dependent Analysis
Context-Dependent Analysis
Type Analysis
Example: Featherweight Java
! "#$$e: boolean , ! "#$$s1. , ! "#$$s2.
! "#$$IfStat(e,s1,s2) .
Task:
Zeige: !$$"#$$$Assign( a, Assign( b, Null() ) ) :
InterfaceType(EinTyp) mit
Specification of name and type analysis in Featherweight Java
A. Igarashi, B.C. Pierce, P. Wadler: Featherweight Java: A Minimal
Core Calculus for Java and GJ, ACM Transactions on Programming
Languages and Systems, 23(3), May 2001
!$= { [ a! InterfaceType(EinTyp) ],
[ b! ClassType(MeinTyp)
Cl
T
(M i T ) ],
]
ClassType(MeinTyp) & InterfaceType(EinTyp),
...
}
© A. Poetzsch-Heffter, TU Kaiserslautern
10.05.2007
c Prof. Dr. Arnd Poetzsch-Heffter
169
Syntax and Type Analysis
Context-Dependent Analysis
89
Type Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Syntax and Type Analysis
90
Type Analysis
Type Safety
A type characterizes specific properties of program elements and
constructs, e.g.
2.3.3.2 Type safety
• a variable of type T is only for storing values of type T
• an expression of type T only provides values of type T
Types can also carry semantic information.
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
91
Type Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Terminology
Syntax and Type Analysis
92
Type Analysis
Example: Guaranteed Program Properties
• A variable of type A contains only values of type A.
• A program is type correct if it satisfies the type rules.
• A type error occurs if
I a value of an incorrect type is assigned to a variable
I an operation is called with an inadmissible parameter
• Evaluation of an expression of type A only yields values of type A.
• If a method m is called, its existence in the target object is
ensured.
• A program is type safe if its execution does not lead to type errors.
• A programing language is strongly typed if type correctness
Bemerkungen:
implies type safety. (This is a proof of a semantic program
• Typsicherheit kann auch durch Laufzeitprüfungen
property. → semantic analysis)
und entsprechende Fehlerbehandlung erreicht
Remarks:
• Type safety can also be obtained by runtime checks and error
handling.
• Type safety ensures the safe application of operations and is the
prerequisite of further safety checks.
werden.
• Typsicherheit gewährleistet sinnvolle Anwendung
Bemerkungen:
von Operationen
und
ist Laufzeitprüfungen
Voraussetzung für weiter• Typsicherheit
kann auch
durch
gehende
Sicherheitsaspekte.
und
entsprechende
Fehlerbehandlung erreicht
werden.
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
93
• Typsicherheit gewährleistet sinnvolle Anwendung
Type Analysis
und ist Voraussetzung
für weiterBeispiel: (Typisierte
Sprachen)
gehende Sicherheitsaspekte.
Context-Dependent
Analysis
von Operationen
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Syntax and Type Analysis
94
Type Analysis
• C ist eine
typisierte Sprache,
erlaubt aber
Typed
Languages
- Example
typunsichere Konvertierungen; demzufolge ist
Beispiel: (Typisierte Sprachen)
C nicht stark typisiert.
• C ist eine typisierte Sprache, erlaubt aber
tKonvertierungen;
s1
1language,
= 1
1; demzufolge
• C short
ish a typed
but permits
typunsichere
ist
C nicht
stark
typisiert.
short
s2 strongly
= 2; typed.
thus
C is
not
non-type safe conversions,
short
hshort
t s1
1*sp
= 1
1;= &s2;
short
s2 = 2;sp)
*((long*)
= 89;
short
*sp
&s2;
assert(
t(= s1
1 == 1 );
)
*((long*) sp) = 89;
assert(
t( s1
1 == 1 );
)
2.3.3.3 Implementation aspects
• Java ist eine stark typisierte Sprache, die die
Java
is a
strongly
language
that needs
• •
Java
ist eine
stark
typisiertetyped
die die
Schwächen
des
Typsystems
yp ySprache,zur
Laufzeit auffängt.
g
runtime.
Schwächen
des Typsystems
yp y
zur Laufzeit auffängt.
g
some type checks at
String[]
strfeld
= {“1“,
“0“,
“2“ };
String[]
strfeld
= { “0“,
“2““1“,
};
Object[]
objfeld
= strfeld;
Object[]
objfeld
= strfeld;
objfeld[0]
= new
objfeld[0]
= Object();
new Object();
// ArrayStoreException
// ArrayStoreException
strfeld[0].length();
strfeld[0].length();
10.05.2007
10.05.2007
© A. Poetzsch-Heffter, TU Kaiserslautern
172
© A. Poetzsch-Heffter, TU Kaiserslautern
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
172
95
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
96
Context-Dependent Analysis
Type Analysis
Context-Dependent Analysis
Implementation Aspects
Type Analysis
Implementation Aspects (2)
Procedure:
Techniques and algorithms for type analysis strongly depend on the
concrete language:
1. For each program construct to be typed, determine exactly one
type (type inference)
2. Check context conditions (type checking)
• For many languages, type inference can be implemented by
S-attribution.
Type of conditions in a conditional statement or loop has to be
boolean.
I Type of current parameter expressions has to be compatible with
formal parameter
types.
Beispiel:
(Typanalyse)
I Implicit type conversions have to be possible.
I
• For languages with polymorphism, type analysis can be arbitrarily
complex or even undecidable.
Demonstration anhand ausgewählter Produktionen
der obigen Java
Java-Teilsprache
Teilsprache.
1. Datentypen für die Attributierung:
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
Context-Dependent Analysis
Zur Repräsentation der
Typinformation:
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
97
Context-Dependent Analysis
Type Analysis - Example
Type Analysis - Example (2)
NullType dient der Typisierung der Konstanten null
und ist Subtyp von allen Referenztypen
Referenztypen.
Zur Repräsentation der Umgebungsinformation
Consider previously defined Java subset.
For representing environment information, we use symbol tables
betrachten wir funktionale Symboltabellen vom Typ
of type SymTab with the following operations:
SymTab mit den Operationen:
1. Data Types for Attribution
For representing type information:
JType = JavaType
NullType()
|
lookupVar: Ident x SymTab ! JType
lookupMth: Ident x Ident x SymTab ! (Signatur | undef)
Signature
g
( JType
yp p
partyp,
yp JType
yp restyp
yp )
isCompatible: JType x JType ! boolean
max: JType x JType x SymTab ! ( Jtype | undef )
NullType
where NullType() is used to type the constant null and is a
subtype of all reference types.
T, wenn S ! T
S wenn T ! S
S,
T, wenn S ist zu T konvertierbar
S, wenn T ist zu S konvertierbar
undef,
sonst
Syntax and Type Analysis
wobei max (S,T) =
c Prof. Dr. Arnd Poetzsch-Heffter
98
JType
= JavaType Type
| Analysis
NullType
NullType ( )
Type Analysis
Syntax and Type Analysis
Context-Dependent Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
99
und isCompatible(S,T) gdw. TypeSAnalysis
!T
Type Analysis
Context-Dependent Analysis
Type Analysis - Example oder
(4) S ist zu T konvertierbar
Type Analysis - Example (3)
© A. Poetzsch-Heffter, TU Kaiserslautern
10.05.2007
174
2. Attribution:
Statements and expressions get the inherited attribute SymTab.
where


T





S
max(S, T ) = T



S




undef
100
if S ≤ T
Expressions get the synthesized attribute JType which are
if T ≤ S
denoted by:
2. Attributierung:
if S is convertible to T
Anweisungen Ausdrücke: SymTab stab
Anweisungen,
if T is convertible to
S
Ausdrücke:
JType typ
else
In der folgenden Attributierung
gehen
davon aus,
For thewir
attribution,
we assume that name analysis has already
and isCompatible(S, T ) iff S ≤ T or S is convertible
to T
. NamensanalyseNote:
dass
die
bereits
abgeschlossen ist
ist.
been
completed.
IfStat
2. Attributierung:
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Syntax and Type Analysis
101
Anweisungen Ausdrücke: SymTab stab
Anweisungen,
Ausdrücke:
JType
Type
Analysis typ
Syntax and Type Analysis
Context-Dependent Analysis
In der-folgenden
Attributierung
Type Analysis
Example
(5) gehen wir davon aus,
IfStat
- Example (6)
Assign
Invoc
_ = boolean ?
Stat
_ ! undef ?
Invoc
_ ! undef ?
restyp(_)
lookupMth(_,_,_)
Stat
partyp(_)
Ident
Exp
102
Type Analysis
Type Analysis
Stat
Stat
Exp
dass die Namensanalyse bereits abgeschlossen ist
ist.
Exp
_=
boolean ?
c Prof. Dr. Arnd
Poetzsch-Heffter
lookupVar(_,_)
isCompatible(_,_) ?
isCompatible(_,_)
?
Ident
Exp
Exp
Arithm
restyp(_)
!"# undef ?
lookupMth(_,_,_)
Exp
c Prof. Dr. Arnd Poetzsch-Heffter
10.05.2007
partyp(_)
Ident
isCompatible(_,_) ?
10.05.2007
max(_,_)
© A. Poetzsch-Heffter, TU Kaiserslautern
Exp
Syntax and Type Analysis
© A. Poetzsch-Heffter, TU Kaiserslautern
175
Exp
175
103
c Prof. Dr. Arnd Poetzsch-Heffter
...
Exp
Syntax and Type Analysis
Relation
!"# undef ?
boolean
max(_,_)
104
Arithm
!"# undef ?
Context-Dependent Analysis
Type Analysis
Type Analysis - Example (7)
...
Exp
max(_,_)
Context-Dependent Analysis
Type Analysis
Type Analysis - Example (8)
Exp
!"# undef ?
Cast
max( )
max(_,_)
Relation
!"# undef ?
JavaType
boolean
Exp
max(_,_)
Var
E
Exp
...
E
Exp
Null
NullType()
lookupVar(_,_)
Ident
10.05.2007
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
© A. Poetzsch-Heffter, TU Kaiserslautern
Syntax and Type Analysis
176
105
Type Analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Bemerkungen:
Context-Dependent Analysis
Syntax and Type Analysis
106
Type Analysis
• Spezifikation und Implementierung der Typregeln
Type Analysis - Example (9)
Literature
sind so unterschiedlich, dass man die Korrektheit
der Implementierung beweisen sollte.
• Für die nachfolgenden Phasen ist der bestimmte
Typ eines Ausdrucks wichtig. Er entscheidet z.B.
über die auszuführende arithmetische Operation.
Remarks:
• For the subsequent compiler phases, the type of an expression is
needed; for instance, it determines the arithmetic operations to be
executed.
• The attribution is deterministic in contrast to the specification.
• Man beachte,
beachte dass die Attributierung im Unter
Unter-
Recommended
schiedReading:
zur Spezifikation deterministisch ist.
• Appel, Sections 5.2, 5.3, pp. 116 – 127
Lesen Sie zu Abschnitt 2.3.3:
Appel:
• Section 5.2, 5.3, S. 116-127
© A. Poetzsch-Heffter, TU Kaiserslautern
10.05.2007
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Syntax and Type Analysis
107
Interplay of name and type analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
177
Syntax and Type Analysis
108
Interplay of name and type analysis
Connections between Context-Dependent Analyses
Name and type analysis are in general mutually dependent.
For instance:
2.3.4 Interplay of name and type analysis
• For expressions of the form e.a, the type of e is required or name
analysis of a (analog for method calls).
• For overload resolution of procedure or method names, the types
of the current parameters are required.
In particular, rules and algorithms for overload resolution are strongly
language dependent.
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Syntax and Type Analysis
109
Interplay of name and type analysis
c Prof. Dr. Arnd Poetzsch-Heffter
Context-Dependent Analysis
Syntax and Type Analysis
110
Interplay of name and type analysis
Beispiel: (Überladungsauflösung)
Overload Resolution
in Java (2)
Overload Resolution in Java
Auszug aus dem Java-Sprachbericht:
In vorangehenden Abschnitten wurde definiert, was
Part of Java Language
Report: Before, it was defined what it means that a
es heißt, dass „a method declaration is accessible and
method declaration
is accessible
and
applicable to a method invocation.
applicable
to a method
invocation“.
• Example of language report, i.e. basis of language implementation
15.12.2.2 Choose the Most Specific Method
If more than one method declaration is both accessible
and applicable to a method invocation, it is necessary to
choose one to provide the descriptor for the run-time
method dispatch. The Java programming language uses
the rule that the most specific method is chosen.
• Learn overloading rules of a commonly used language
The informal intuition is that one method declaration is
more specific than another if any invocation handled by
the first method could be passed on to the other one
without a compile-time type error.
Goals:
• Connections between context-dependent analysis
The precise definition is as follows. ...
A method is said to be maximally specific for a method
invocation if it is applicable and accessible and there is no
other applicable and accessible method that is more
specific. If there is exactly one maximally specific method,
then it is in fact the most specific method; ...
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
111
c Prof. Dr. Arnd Poetzsch-Heffter
10.05.2007
Syntax and Type Analysis
© A. Poetzsch-Heffter, TU Kaiserslautern
112
179
Context-Dependent Analysis
Interplay of name and type analysis
Context-Dependent Analysis
Overload Resolution in Java (3)
Interplay of name and type analysis
Literature
class Point { int x, y; }
class ColoredPoint extends Point { int color; }
class Test {
static void test(ColoredPoint p, Point q)
{
out.println("(ColoredPoint, Point)");
}
static void test(Point p, ColoredPoint q)
{
out.println("(Point, ColoredPoint)");
}
static void test(ColoredPoint p,ColoredPoint q)
{
out.println("(ColoredPoint, ColoredPoint)");
}
public static void main(String[] args)
{
ColoredPoint cp = new ColoredPoint();
test(cp, cp); // ***** Aufrufstelle
}
}
Recommended Reading:
• Wilhelm, Maurer: Section 9.1.3, pp. 416 –419
Die dritte Deklaration ist most specific an der markierten
Aufrufstelle. Wenn sie fehlen würde, wäre der Aufruf
The third declaration
is most specific at the marked applied
occurrence. If it
fehlerhaft und ein Übersetzungsfehler wäre die Folge.
Folge
was missing, the call would be erroneous and cause a translation error.
Lesen
c Prof. Dr. Arnd Poetzsch-Heffter
Sie zu Abschnitt
2.3.4:
Syntax and Type
Analysis
113
Wilhelm,
Wilh
l M
Maurer:
• Unterabschnitt 9.1.3 (S. 416-419)
10.05.2007
© A. Poetzsch-Heffter, TU Kaiserslautern
180
c Prof. Dr. Arnd Poetzsch-Heffter
Syntax and Type Analysis
114

Similar documents