deklarierte namen in · pdf file2 objektknoten class obj {enum kind {con, var, type, meth,...

22
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

Upload: hoangtu

Post on 06-Mar-2018

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

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

Page 2: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

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

Page 3: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

3

Strukturknoten und Scope-Knotenclass 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}

Page 4: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

4

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

Page 5: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

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

Page 6: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

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:

kindnametypenext

val / adrlevelnparslocals

Obj

kindelemType

nfields

Struct

outerlocalsnVars

Scope

Page 7: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

7

Beispiel: Bei Punkt ➀

Struktur der 3 Knotenarten:

kindnametypenext

val / adrlevelnparslocals

Obj

kindelemType

nfields

Struct

outerlocalsnVars

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

01--

Var"ch"

charType

01--

Var"arr"

01--

ArrnoType

--

Type"int"

intType

----

Type"char"

charType

----

topScope

Page 8: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

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

01--

Var"ch"

charType

01--

Var"arr"

01--

ArrnoType

--

Type"int"

intType

----

Type"char"

charType

----

scope "C"

Page 9: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

9

Beispiel: Bei Punkt ➂

topScope

0

0

0 1 2 3

Var"c"

01--

ArrcharType

--

Var"max"

intType

11--

Var"npp"

charType

21--

Con"null"

nullType

----

Prog"ABC"

noType

----

Meth"chr"

charType

--1

Meth"ord"

intType

--1

Meth"len"

intType

--1

scope "Universe"

Var"i"

intType

01--

Var"ch"

charType

01--

Var"arr"

01--

ArrnoType

--

Type"int"

intType

----

Type"char"

charType

----

Type"C"

----

Class-0

scope "C"

Page 10: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

10

Beispiel: Bei Punkt ➃

topScope

0

0

0 1 2 3

Var"c"

01--

ArrcharType

--

Var"max"

intType

11--

Var"npp"

charType

21--

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

01--

Var"ch"

charType

01--

Var"arr"

01--

ArrnoType

--

Type"int"

intType

----

Type"char"

charType

----

Type"C"

----

Class-3

scope "C"

Page 11: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

11

Beispiel: Bei Punkt ➄

0 1 2 3

0

0

topScope

0 1 2

Var"c"

01--

ArrcharType

--

Var"max"

intType

11--

Var"npp"

charType

21--

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

01--

Var"ch"

charType

01--

Var"arr"

01--

ArrnoType

--

Type"int"

intType

----

Type"char"

charType

----

Type"C"

----

Class-3

Var"this"

02--

scope "put"

scope "C"

Var"x"

intType

12--

Page 12: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

12

Beispiel: Bei Punkt ➅

0 1 2 3

0

0

topScope

Var"c"

01--

ArrcharType

--

Var"max"

intType

11--

Var"npp"

charType

21--

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

01--

Var"ch"

charType

01--

Var"arr"

01--

ArrnoType

--

Type"int"

intType

----

Type"char"

charType

----

Type"C"

----

Class-3

Var"this"

02--

scope "put"

scope "C"

Var"x"

intType

12--

Page 13: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

13

Beispiel: Bei Punkt ➆

0

Var"c"

01--

ArrcharType

--

Var"max"

intType

11--

Var"npp"

charType

21--

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

01--

Var"ch"

charType

01--

Var"arr"

01--

ArrnoType

--

Type"int"

intType

----

Type"char"

charType

----

topScope

0

Type"C"

----

Class-3

Var"this"

02--

scope "put"

scope "C"

Var"x"

intType

12--

Page 14: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

14

Beispiel: Bei Punkt ➇

0

Var"c"

01--

ArrcharType

--

Var"max"

intType

11--

Var"npp"

charType

21--

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

01--

Var"ch"

charType

01--

Var"arr"

01--

ArrnoType

--

Type"int"

intType

----

Type"char"

charType

----

Type"C"

----

Class-3

Var"this"

02--

scope "put"

scope "C"

Var"x"

intType

12--

Page 15: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

15

Füllen der Symbolliste

/** VarDecl = Type ident { "," ident } ";" . */private void VarDecl() { Type(); check(ident);

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

} check(semicolon);}

Page 16: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

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

Page 17: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

17

Symbolliste verwenden

/** Type = ident [ "[" "]" ] . */private void Type() { check(ident);

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

}

}

Page 18: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

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

Page 19: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

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

Page 20: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

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(); check(rbrace);}

Page 21: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

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(); } ...

Page 22: Deklarierte Namen in · PDF file2 Objektknoten class Obj {enum Kind {Con, Var, Type, Meth, Prog} Kind kind; String name; Struct type; int val; // Con: value int adr; // Var, Meth:

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(); } ...