DOC PREVIEW
Berkeley COMPSCI 164 - Lecture 26 Some Object Design

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture #26: Some Object DesignSpecificsExampleObservationsGeneral Sketch of an AlgorithmSymbol TablesFrom Algorithm to Methods Sample Details of `gatherVarDecls' Part ISample Details of `gatherVarDecls', Part IIWhere the Work is Done: `defineIdsAsVars'Lecture #26: Some Object DesignOur Example: Handling scope rules for simple variables in the project.Problem Summary. Attach “VarDecls” to each IdNode that repre-sents a simple variable (including parameter s). Call thisresolvingtheseidentifiers.=xx1+Var xAST Semantic Delegates Decl Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 1Specifics• One VarDecl object for eachdistinctvariable.• IdNodes that represent references to the same variable shouldhave same VarDecl.• Thus, new VarDecl for each parameter declaration (in def or lambda),plus• New VarDecl for a variable in any region that contains at least oneassignment to it ( unless previously declared as parameter), . . .• Unless variable is defined global.• Declarative regions are global region, each def, and each lambda.• Variables assigned to in class bodies (n ot i nside a function) arenotsimple variables.Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 2ExampleFor the programa = 3y = 2z = 3def f (a, b):y = a+b+zreturn y‘unpyth –decls’ will show...a#1 = 3y#2 = 2z#3 = 3def f#4 (a#5, b#6):y#7 = a#5 + b#6 + z#3return y#7#1: VarDecl#2: VarDecl#3: VarDecl#4: FuncDecl#5: VarDecl in #4#6: VarDecl in #4#7: VarDecl in #4Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 3Observations• Must collect declarations first, because as signments may occur af-ter uses in text, even in correct programs:while x < N:if x > 0:f (y) # y used hereelse:y = 0 # y assigned herex += 1...• Must also have all outer declarations collected before tr ying to re-solve inner ones.Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 4General Ske tch of an AlgorithmTo resolve identifiers in declarative region R:Find and define all parameters immediately inside R (& check duplicates)Find global declarations immediately inside R (& check duplicates)Find {\bf de f}s immediately inside R (& ch eck duplicates)Find and define all assigned non-global variables immediate ly inside RLook up all identifiers immediately inside R and attach theresults of these lookups to the identifier nodes, andFor each declarative region, R’, immediately inside R,resolve identifiers in declarative region R’ (recursively).Forget all the definitions created above (the identifiers are nowall connected to these definitions, so we don’t need to remember them.•Immediately inside Rmeans “inside R, but not inside any other declar-ative region inside R.”• Handling classes similar, but we create attributes in th a t case.• In the outermost declarative region , ignore global definition s.Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 5Symbol Tables• Our sketch requires a block-structured symbol table as in previouslectures.• From the algorithm, need to be able to:– Start a new block (for a declarative region)– Look up identifers in the latest block– Look up identifiers in any region (innermost to outermost)– Look up identifiers in outermost region (for globals)– Define identifier to refer to p a rticular Decl in current region.– Leave a bl ock (and remove all declarations from it).• Each of these points indicates the need for a method in new type ofobject (I call it class Sy mtab).Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 6From Algorithm to MethodsWe’ll add methods to StaticSemantics for each thin g we need to dofrom algorithm sketch:• Resolve identifi ers in declarative region R: resolve Ids• Define parameters: part of resolveIds• Find global declarations: gatherGlobalDecls• Find assigned non-global variables: gatherVarDecls,gatherVarDeclsFromTargets, and defineIdsA sVars.Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 7Sample Details of ‘gatherVarDecls’ Part IIn StaticSemantics, the default definition:/** Add variable decla r a tions in the outermost de c l a rative region of THIS* to the innermost block of SYMS . D o es not add any declaration for* names previous l y declared in the inne r m o s t block of SYMS. REGION is* the declaratio n of the enclosing declarative region (a FuncDecl,* MethodDecl, or null fo r the glo b al region). */void gatherVar D e cls (Symtab syms, RegionDecl re g i on) {for (int i = 0; i < arity (); i += 1) {getChild (i).gatherV arDecls (syms, re g i o n );}}Don’t want to look inside inner declarative regions or classes, so inFuncDeclSem, Lamb daSem, and ClassDeclSem define:/** Do not go into THIS looking for variab l e targets. */void gatherVar D e cls (Symtab syms, RegionDecl re g i on) { }Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 8Sample Details of ‘gatherVarDecls’, Part IINow we handle assignments, by defin i ng in AssignSe m:void gatherVar D e cls (Symtab syms, RegionDecl re g ion) {getChild (0).gatherV arDeclsFromTargets (syms, reg ion);getChild (1).gatherV arDecls (syms, re g i o n);}where gatherVarDeclsFromTargets is defined in StaticSemantics:/** Add variable decla r a tions for al l assigned-to identifiers in* outermost targ e t s in THIS to the current region of SYMS. */void gatherVar D e clsFromTargets (Symtab syms, RegionDecl region) {for (int i = 0; i < a r i ty (); i += 1)getChild (i).g a t herVarDeclsFromTargets (syms, regi o n ) ;}and by default in TargetSem (supertype of semantics for all targets):/** By default, assign m e nt to a target does not declare anything. */void gatherVar D e clsFromTargets (Symtab syms, RegionDecl region) { }And finally in the only target that matters, Var RefSem:void gatherVar D e clsFromTargets (Symtab syms, RegionDecl region) {getChild (0).defineI dsAsVars (syms, region) ;}Last modified: Fri Apr 1 12:48:57 2005 CS164: Lecture #26 9Where the Work is Done: ‘defineIdsAsVars’Default definition in StaticSema ntics:/** Enter a variable declaration in SYMS for each identifier in THIS that* is not previously decl a r ed in the inner block of SYMS. REGION is* the enclosing declarative region’s declaration. */void defineIds A s Vars (Symtab syms , RegionDecl re g ion) {for (int i = 0; i < a r i ty (); i += 1)getChild (i).d e f ineIdsAsVars (syms, reg i o n);}And the wor k is really done in IdSem:void defineIds A s Vars (Symtab syms , RegionDecl re g ion) {String name = getAST ().getText ();this.decl =


View Full Document

Berkeley COMPSCI 164 - Lecture 26 Some Object Design

Documents in this Course
Lecture 8

Lecture 8

40 pages

Load more
Download Lecture 26 Some Object Design
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 26 Some Object Design and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 26 Some Object Design 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?