• Keine Ergebnisse gefunden

Deklarierte Namen in MicroJava

N/A
N/A
Protected

Academic year: 2022

Aktie "Deklarierte Namen in MicroJava"

Copied!
16
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

1

Deklarierte Namen in MicroJava

Programm

Konstanten

Globale Variablen Klassen

Felder Methoden

Formale Parameter Lokale Variablen

Wird ein Name deklariert, wird er in die Symbolliste eingefügt

Program() ConstDecl()

VarDecl() level = 0 ClassDecl()

VarDecl() level = 1 MethodDecl()

FormPars()

VarDecl() level = 1

(2)

Objektknoten

class Obj { enum Kind {

Con, Var, Type, Meth, Prog }

Kind kind;

String name;

Struct type;

int val; // Con: value

int adr; // Var, Meth: address

int level; // Var: 0 = global, 1 = local int nPars; // Meth: number of parameters // Meth: parameters and local objects

// Prog: global variables, constants, // classes and methods

(3)

3

Strukturknoten und Scope-Knoten

class Struct { enum Kind {

None, Int, Char, Arr, Class }

Kind kind;

Struct elemType; // Arr: element type

Map<String, Obj> fields; // Class: list of fields

int nrFields() { return fields.size(); } // Class }

class Scope {

Scope outer; // next outer scope

Map<String, Obj> locals; // list of objects in this scope int nVars; // number of variables in this scope

}

(4)

Symboltabelle

class Tab {

public static final Struct

noType, intType, charType, nullType;

public Obj noObj, chrObj, ordObj, lenObj;

public Parser parser; // target for errors public Scope curScope; // current scope

private int curLevel; // nesting level of current scope

public Tab(Parser parser);

public void openScope();

public void closeScope();

public Obj insert(Obj.Kind kind, String name, Struct type);

public Obj find(String name);

public Obj findField(String name, Struct type);

(5)

5

Füllen der Symbolliste

/** VarDecl = Type ident { "," ident } ";" . */

private void VarDecl() { Type();

check(ident);

while (sym == comma) { scan();

check(ident);

}

check(semicolon);

}

(6)

/** VarDecl = Type ident { "," ident } ";" . */

private void VarDecl() { Struct type = Type();

check(ident);

tab.insert(Obj.Kind.Var, t.str, type);

while (sym == comma) { scan();

check(ident);

tab.insert(Obj.Kind.Var, t.str, type);

}

check(semicolon);

}

Füllen der Symbolliste

(7)

7

Symbolliste verwenden

/** Type = ident [ "[" "]" ] . */

private void Type() { check(ident);

if (sym == lbrack) { scan();

check(rbrack);

}

}

(8)

/** Type = ident [ "[" "]" ] . */

private Struct Type() { check(ident);

Obj o = tab.find(t.str);

if (o.kind != Obj.Kind.Type) { error(NO_TYPE);

}

Struct type = o.type;

if (sym == lbrack) { scan();

check(rbrack);

type = new Struct(type);

}

return type;

}

Symbolliste verwenden

(9)

9

Klassen

/** ClassDecl = "class" ident "{" {VarDecl} "}" . */

private void ClassDecl() { check(class_);

check(ident);

check(lbrace);

while (sym == ident) { VarDecl();

}

check(rbrace);

}

(10)

/** ClassDecl = "class" ident "{" {VarDecl} "}" . */

private void ClassDecl() { check(class_);

check(ident);

Obj clazz = tab.insert(Obj.Kind.Type,

t.str, new Struct(Struct.Kind.Class));

check(lbrace);

tab.openScope();

while (sym == ident) { VarDecl();

}

check(rbrace);

clazz.type.fields = tab.curScope.locals;

tab.closeScope();

}

Klassen

(11)

11

Beispiel: Symbollistenaufbau

program ABC  char[] c;

int max;

char npp;

{

int put  (int x) {  x++;

print(x, 5);

npp = 'C';

return x;

} 

} 

(12)

Beispiel: Bei Punkt 

Vordefinierte Typen und Objekte: Struktur der 3 Knotenarten:

Int - - -

Char - - -

kind name

type Obj

kind elemType

n Struct

outer locals nVars Scope Class

- 0

None - - -

Var

"noObj"

noType Con

"null"

nullType

- - - -

Prog

"ABC"

noType

- - - - 0

Meth

"chr"

charType

- - 1

Meth

"ord"

intType

- - 1

Meth

"len"

intType

- - 1

scope "Universe"

Var

"i"

intType

0 1 - -

Var

"ch"

charType

0 1 - -

Var

"arr"

0 1 - -

Arr noType

- - 0

topScope

Type

"int"

intType

- - - -

Type

"char"

charType

- - - -

(13)

13

Beispiel: Bei Punkt 

0

0

topScope 0 1 2 3

Var

"c"

0 0 - -

Arr charType

- -

Var

"max"

intType

1 0 - -

Var

"npp"

charType

2 0 - -

Meth

"put"

intType

- - -

scope "ABC"

Con

"null"

nullType

- - - -

Prog

"ABC"

noType

- - - - Meth

"chr"

charType

- - 1

Meth

"ord"

intType

- - 1

Meth

"len"

intType

- - 1

scope "Universe"

Var

"i"

intType

0 1 - -

Var

"ch"

charType

0 1 - -

Var

"arr"

0 1 - -

Arr noType

- - Type

"int"

intType

- - - -

Type

"char"

charType

- - - -

(14)

Beispiel: Bei Punkt 

Var

"x"

intType

scope "put"

0

3

Var

"c"

0 0 - -

Arr charType

- -

Var

"max"

intType

1 0 - -

Var

"npp"

charType

2 0 - -

Meth

"put"

intType

- - 1

scope "ABC"

Con

"null"

nullType

- - - -

Prog

"ABC"

noType

- - - - Meth

"chr"

charType

- - 1

Meth

"ord"

intType

- - 1

Meth

"len"

intType

- - 1

scope "Universe"

Var

"i"

intType

0 1 - -

Var

"ch"

charType

0 1 - -

Var

"arr"

0 1 - -

Arr noType

- - Type

"int"

intType

- - - -

Type

"char"

charType

- - - -

(15)

15

Beispiel: Bei Punkt 

Var

"x"

intType

0 1 - - 0

3

topScope

Var

"c"

0 0 - -

Arr charType

- -

Var

"max"

intType

1 0 - -

Var

"npp"

charType

2 0 - -

Meth

"put"

intType

- - 1

scope "ABC"

Con

"null"

nullType

- - - -

Prog

"ABC"

noType

- - - - Meth

"chr"

charType

- - 1

Meth

"ord"

intType

- - 1

Meth

"len"

intType

- - 1

scope "Universe"

Var

"i"

intType

0 1 - -

Var

"ch"

charType

0 1 - -

Var

"arr"

0 1 - -

Arr noType

- - Type

"int"

intType

- - - -

Type

"char"

charType

- - - -

(16)

Beispiel: Bei Punkt 

0

topScope

Var

"x"

intType 0 Var

"c"

0 0 - -

Arr charType

- -

Var

"max"

intType

1 0 - -

Var

"npp"

charType

2 0 - -

Meth

"put"

intType

- - 1

scope "ABC"

Con

"null"

nullType

- - - -

Prog

"ABC"

noType

- - - Meth

"chr"

charType

- - 1

Meth

"ord"

intType

- - 1

Meth

"len"

intType

- - 1

scope "Universe"

Var

"i"

intType

0 1 - -

Var

"ch"

charType

0 1 - -

Var

"arr"

0 1 - -

Arr noType

- - Type

"int"

intType

- - - -

Type

"char"

charType

- - - -

Referenzen

ÄHNLICHE DOKUMENTE

Important: the error symbol should always be followed by a terminal synchronization symbol, SEMI in this

Element Node: price NodeList Text Node: 11.95 NodeList. Element

Wer hat Kontrolle über das Parsen: die Anwendung oder der

Wer hat Kontrolle über das Parsen: die Anwendung oder der

Den kanonischen LR ( k ) -Automaten LR ( G, k ) erhält man aus c ( G, k ) , indem man nach jedem Übergang beliebig viele ǫ liest und dann den Automaten deterministisch macht .....

Building on existing reviews of the PA network literature and based on a citation network analysis, we identified three distinct clusters of research focused on policy

Die Definition deutet an, daß die Projektentwicklung als eine komplexe Perspektive zu verstehen ist, die über einzelne Vorstellungen bezüglich Nutzungskonzept, Architektur,

Women, on the other hand, have been seen to take care of the household (domus) and were therefore more likely to be in the private sphere 6. According to this understanding,