1 / 8

Lecture # 21

Lecture # 21. Chapter 6 Uptill 6.4. Type System. A type system is a collection of rules for assigning type expressions to the various parts of the program. A Type Checker implements a Type System. Type checking of Expressions. E  char { E.type =char} Eint { E.type = int }

svea
Download Presentation

Lecture # 21

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture # 21 Chapter 6 Uptill 6.4

  2. Type System • A type system is a collection of rules for assigning type expressions to the various parts of the program. • A Type Checker implements a Type System

  3. Type checking of Expressions • E char {E.type=char} • Eint {E.type=int} • Eid {E.type= lookup (id.entry)} • E E1 mod E2 { E.type= if E1.type= integer and E2.type=integer then integer else error } • E E1[E2] {E.type= if E2.type= integer and E1.type= lookup(E1.type) then E1.type else error }

  4. Type Checking of Statements • S id :=E { S.type := ifid.type = E.typethenvoidelsetype_error } • S ifEthenS { S.type := ifE.type = booleanthenS1.typeelsetype_error } • S whileEdoS1{ S.type := ifE.type = booleanthenS1.type elsetype_error } • S S1;S2 { S.type := ifS1.type = void and S2.type = void then void elsetype_error }

  5. Testing the Type Equivalence of Expressions • Public boolsequiv (s,t) { if s and t are the same basic type return true else if s=array(s1,s2) and t= array(t1,t2) then return sequiv(s1,t1) and sequiv(s2,t2) else if s=s1*s2 and t=t1* t2 then return sequiv(s1,t1) and sequiv(s2,t2) else return false }

  6. Encoding of Type Expressions • The expressions can be encoded in bits to check their type equivalence • Type Constructor Encoding pointer 01 array 10 freturns 11

  7. Basic Types Encoding boolean 0000 char 0001 integer 0010 real 0011

  8. Example Type Encoding • Type Expression • Char 000000 0001 • Freturns(char) 000011 0001 • Pointer(Freturns(char)) 000111 0001 • Array(Pointer(Freturns(char))) 100111 0001

More Related