• Keine Ergebnisse gefunden

Deklarierte Namen in MicroJava

N/A
N/A
Protected

Academic year: 2022

Aktie "Deklarierte Namen in MicroJava"

Copied!
22
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Deklarierte Namen in MicroJava

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

Programm Program()

Konstanten ConstDecl()

Globale Variablen VarDecl() level = 0

Klassen ClassDecl()

Felder VarDecl() level = 1

Methoden MethodDecl(clazz)

Formale Parameter FormPars()

Lokale Variablen VarDecl() level = 2

Methoden MethodDecl(null)

Formale Parameter FormPars()

Lokale Variablen 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

Map<String, Obj> locals;

}

(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 and methods void addMethod(Obj m) {…} // Class: Inserts a method

int nrFields() { ... } // Class: number of fields (without methods) }

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);

}

class TabImpl extends Tab { 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)

program ABC ➀ class C { ➁

char[] c;

int max;

char npp; ➂

public int put ➃ (int x) { ➄ x++;

print(x, 5);

npp = 'C';

return x;

} ➅ } ➆ {} ➇

Beispiel: Symbollistenaufbau

(6)

program ABC ➀ class C { ➁

char[] c;

int max;

char npp; ➂

public int put ➃ (int x) { ➄ x++;

print(x, 5);

npp = 'C';

return x;

} ➅ } ➆ {} ➇

Beispiel: Symbollistenaufbau

Struktur der 3 Knotenarten:

kind name

type next val / adr

level npars locals Obj

kind elemType

n fields Struct

outer locals nVars Scope

(7)

Beispiel: Bei Punkt ➀

Struktur der 3 Knotenarten:

kind name

type next val / adr

level npars locals Obj

kind elemType

n fields Struct

outer locals nVars Scope

Vordefinierte Typen und Objekte:

Int - - - intType

Char - - - charType

Class - 0

nullType

None - - - noType

Var

"noObj"

noType - - - - noObj 0

0

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 - - - -

topScope

(8)

Beispiel: Bei Punkt ➁

topScope 0 0

0

Type

"C"

- - - -

Class -0

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 - - - -

scope "C"

(9)

Beispiel: Bei Punkt ➂

topScope 0

0

0 1 2 3

Var

"c"

0 1 - -

Arr charType

- -

Var

"max"

intType 1 1 - -

Var

"npp"

charType 2 1 - - 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 - - - -

Type

"C"

- - - -

Class -0

scope "C"

(10)

Beispiel: Bei Punkt ➃

topScope 0

0

0 1 2 3

Var

"c"

0 1 - -

Arr charType

- -

Var

"max"

intType 1 1 - -

Var

"npp"

charType 2 1 - -

Meth

"put"

intType

? - - 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 - - - -

Type

"C"

- - - -

Class -3

scope "C"

(11)

11

Beispiel: Bei Punkt ➄

0 1 2 3 0

0

0 1 2

Var

"c"

0 1 - -

Arr charType

- -

Var

"max"

intType 1 1 - -

Var

"npp"

charType 2 1 - -

Meth

"put"

intType

? - 2 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 - - - -

Type

"C"

- - - -

Class -3

Var

"this"

0 2 - -

scope "put"

scope "C"

Var

"x"

intType 1 2 - -

(12)

Beispiel: Bei Punkt ➅

0 1 2 3 0

0

topScope

Var

"c"

0 1 - -

Arr charType

- -

Var

"max"

intType 1 1 - -

Var

"npp"

charType 2 1 - -

Meth

"put"

intType

? - 2 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 - - - -

Type

"C"

- - - -

Class -3

Var

"this"

0 2

scope "put"

scope "C"

Var

"x"

intType 1 2

(13)

Beispiel: Bei Punkt ➆

0

Var

"c"

0 1 - -

Arr charType

- -

Var

"max"

intType 1 1 - -

Var

"npp"

charType 2 1 - -

Meth

"put"

intType

? - 2 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 - - - -

topScope 0

Type

"C"

- - - -

Class -3

Var

"this"

0 2 - -

scope "put"

scope "C"

Var

"x"

intType 1 2 - -

(14)

Beispiel: Bei Punkt ➇

0

Var

"c"

0 1 - -

Arr charType

- -

Var

"max"

intType 1 1 - -

Var

"npp"

charType 2 1 - -

Meth

"put"

intType

? - 2 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 - - - -

Type

"C"

- - - -

Class -3

Var

"this"

0 2

scope "put"

scope "C"

Var

"x"

intType 1 2

(15)

Füllen der Symbolliste

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

private void VarDecl() { Type();

check(ident);

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

check(ident);

}

check(semicolon);

}

(16)

/** 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

(17)

Symbolliste verwenden

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

private void Type() { check(ident);

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

check(rbrack);

}

}

(18)

/** 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

(19)

19

Klassen

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

private void ClassDecl() { check(class_);

check(ident);

check(lbrace);

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

}

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

MethodDecl();

}

check(rbrace);

(20)

20

Klassen

/** 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();

}

if (tab.curScope.nVars() > MAX_FIELDS) { error(TOO_MANY_FIELDS);

}

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

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

Obj m = MethodDecl(clazz);

clazz.type.addMethod(m);

}

tab.closeScope();

(21)

21

MethodDecl

// MethodDecl = ( Type | "void" ) ident "(" [ FormPars ] ")" ...

private void MethodDecl() { if (sym == ident) {

Type();

} else if (sym == void_) { scan();

} else {

error(METH_DECL);

}

check(ident);

check(lpar);

if (sym == ident) { FormPars();

}

(22)

22

MethodDecl

// MethodDecl = ( Type | "void" ) ident "(" [ FormPars ] ")" ...

private Obj MethodDecl(Obj clazz) { StructImpl type = Tab.noType;

if (sym == ident) { type = Type();

} else if (sym == void_) { scan();

} else {

error(METH_DECL);

}

check(ident);

String methodName = t.str;

meth = tab.insert(Obj.Kind.Meth, methodName, type);

meth.adr = code.pc;

check(lpar);

tab.openScope();

if (clazz != null) { // insert this reference tab.insert(Obj.Kind.Var, "this", clazz.type);

}

if (sym == ident) {

FormPars();

Referenzen

ÄHNLICHE DOKUMENTE

Hinweis: Die Methode equals(Struct other) der Klasse Struct wird nur von den JUnit-Testfällen verwendet und prüft nicht die korrekte

static int level; // nesting level of current scope static void init();. static

private int level; // nesting level of current scope public Tab(Parser parser);. public

public Parser parser; // target for errors public Scope curScope; // current scope. private int level; // nesting level of

public Parser parser; // target for errors public Scope curScope; // current scope. private int curLevel; // nesting level of

private int curLevel; // nesting level of current scope public Tab(Parser parser);. public

public void openScope(); // shortcut for openScope(0) public void openScope(int nVars); // local variables in outer scope public void closeScope();. public

1 ALLGEMEINE ANGABEN ZUM BAUWERK (NEU- UND ERWEITERUNGSBAU) UND ZU DEN WOHNUNGEN NOTIZIE GENERALI SULL’OPERA (NUOVO FABBRICATO ED AMPLIAMENTO) E SULLE ABITAZIONI..