Docsity

Mutual Recursion - Compiler Design - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Mutual Recursion - Compiler Design - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Lecture #16, March 7, 2007

  • Mutual Recursion
  • Symbol Tables
  • Class Hierarchies
  • Type checkers that rebuild code

Mutual Recursion

  • Mutual recursion is found in almost all real languages
  • Mutual recursion requires different techniques
    • Multiple passes.
    • One pass to build a temporary table with just the

information about the mutually recursive entities

  • Second pass to type the recursive entities where the

current scope is extended with the temporary table.

  • Inside this pass, the table is often extended even more to capture information about entities declared in a single one of the mutually recursive entities.
  • Finally, the temporary table is exported to the scope in

which the mutually recursive entities exist.

Concrete Example

and Dec = Valdec of (stringMLtype)Exp | Fundec of (stringMLtypeMLtype)stringExp | Mutdec of Dec list

  • Assume we represent declarations as above.
  • Every val and fun declaration contains explicit

information about their types (as is true in Mini-Java)

  • Then the concrete syntax may look like:

val (x:int) = 56;

fun (f:int->int) (x:int) = x + 5;

Type Checking Declarations

  • Type checking will depend upon two attribute

computations.

  • Checking declarations will map an

environment to a delta environment.

  • New environment will be the old one plus the

delta.

TCExp:Exp ->(string * MLtype)list ->MLtype TCDec:Dec ->(string * MLtype)list ->(string * MLtype) list

x: int f: int -> int

even: int -> bool odd: int -> bool

fun (even:int ->bool) (n:int) = if n= then true else odd (n-1) and (odd:int -> bool) (n:int) = if n= then false else if n= then true else even (n-1);

Mutually recursive case

| TCDec (Mutdec ds) cntxt =

let fun pass1 [] cntxt = cntxt | pass1 (Valdec(p,b)::ds) cntxt = pass1 ds (p::cntxt) | pass1 (Fundec((f,d,r),x,b)::ds) cntxt = pass1 ds ((f,Arrow(d,r))::cntxt) val temp = pass1 ds cntxt val pass2 = map (fn d => TCDec d temp) ds in List.concat pass2 end;

x: int f: int -> int

odd: int->bool even: int->bool x: int f: int -> int

even: int->bool odd: int->bool

even: int->bool odd: int->bool

cntxt temp^ pass2^ result

Symbol Tables

  • Symbol Tables map names to information
  • Many kinds of Names
    • Variables
    • Procedure and function names
    • Labels
    • Data Structure names (Constructors in ML or named Struct in C)
    • Types
  • Many kinds of information
    • Type
    • Physical location in source (line number)
    • Size
    • Storage class
    • Lexical location (Inside what class)
    • Location in memory in the translation

Nested Scope

  • Many tables record information with block structure or

nested scope.

  • Stack allocation is possible
  • Easy to implement in a functional language like ML
    • Use inherited attribute computation
    • Table becomes a parameter to the function
    • Entering new block means a new call to function with the

parameter augmented.

  • Exiting scope means restoring the old parameter
    • Natural in a functional style since the old parameter is always still around.

Operations on nested scope

  • Initialize Scope
  • Insert name value Scope
  • Lookup name Scope
  • Finalize Scope

type ('name,'value)table = ('name * 'value) list; type ('name,'value)Scope = (('name,'value)table) list ;

fun initialize scope = [ ] :: scope fun insert name value (table::scope) = ((name,value)::table)::scope fun Lookup name (table::scope) = case List.find (fn (x,y) => x=name) table of NONE => error | SOME (x,y) => y fun finalize (table::scope) = scope

More sophisticated kinds of tables can be used here rather than a linked list of pairs. Hash Tables, Balanced trees, etc.

Judgments from the type rules

Th, C, TE |- exp : type

Th, R, C, TE |= stmt

C |~ t 1 ≤ t 2

t 1 = t 2

C defines x

In C p has method f

Data Structure

datatype CTab

= Node of Id * (* Class Name ) (Id * Type)list * ( Class Variables ) (Id * Type list * Type)list * ( Methods ) CTab ref * ( Parent Class ) CTab list ( Sub Classes *) | NullClass;

val root = Node("object",[],[],ref NullClass,[]);

CTab for class table is an n-way branching tree.

object

point numeric

int double

boolean

colorpoint

void

val t1 = newClass "point" [] [] "object" root;

Node(“object”, [ ], [ ], ref NullClass,[ ])

Node(“point”, [ ], [ ], ref _ ,[ ])

root

new

Node(“object”, [ ], [ ], ref NullClass, [Node(“point”, [ ], [ ], ref _ ,[ ])])

newP

  • The final step is to overwrite the pointer to root with newP, and then return newP
  • newClass rebuilds the part of the tree that are unchanged
  • The references make cyclic structures

val t1 = newClass "point" [] [] "object" root;

val t2 = newClass "colorpoint" [] [] "point" t1;

Node(“point”, [ ], [ ], ref _ ,[ _ ])

Node(“object”, [ ], [ ], ref NullClass ,[ _ ])

Node(“colorpoint”, [ ], [ ], ref _ ,[ ])

t

C defines x

fun defines name NullClass = false

| defines name

(Node(n,vs,ms,p,ss)) =

if name=n

then true

else List.exists (defines

name) ss;

t 1 = t 2

fun basiceq (x,y) =

case (x,y) of

(Real,Real) => true

| (Int,Int) => true

| (Bool,Bool) => true

| (,) => false

fun typeeq (x,y) =

case (x,y) of

(BasicType x,BasicType y) => basiceq(x,y)

| (ArrayType x,ArrayType y) => basiceq(x,y)

| (ObjType x,ObjType y) => x=y

| (VoidType,VoidType) => true

| (,) => false Docsity.com