







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Allan; Class: COMPILER DESIGN; Subject: Computer Science; University: Utah State University; Term: Unknown 1989;
Typology: Study notes
1 / 13
This page cannot be seen from the preview
Don't miss anything!








CS 5300 - SJAllan 2
CS 5300 - SJAllan 3
CS 5300 - SJAllan 4
CS 5300 - SJAllan 7
ย Applying "type constructors" to type expressions forms new type expressions
ย A function maps elements from one set (domain) to another set (range)
CS 5300 - SJAllan 8
CS 5300 - SJAllan 9
ย In principle, any check can be done dynamically if the target code carries type information
ย A sound type system eliminates the need for dynamic checking
ย A language is strongly typed of its compiler only accepts programs that have no type errors
ย In practice, some checks can only be done dynamically (e.g., checking array indices)
CS 5300 - SJAllan 10
ย The simple language above allows a sequence of declarations followed by a single expression ย A simple syntax-directed definition can specify a type checker for this language
Pร D ; E D ร D ; D | id : T T ร char | integer | array [num] of T | โ T E ร literal | num | id | E mod E | E[E] | E โ
CS 5300 - SJAllan 13
S.type := if S 1 .type = void and S 2 .type = void then void else type_error
S ร S 1 ; S 2
S.type := if E.type = boolean S ร while E do S (^1) then S 1 .type else type_error
S.type := if E.type = boolean S ร if E then S (^1) then S 1 .type else type_error
S.type := if id.type = E.type S ร id := E then void else type_error
Production Statements
CS 5300 - SJAllan 14
E.type := if E 2 .type = s and E 1 .type = s ร t then t else type_error
E ร E 1 (E 2 )
id.type := arguments.type ร standard_type.type
subprogam_head ร function id arguments : standard_type ';'
Production Statements
CS 5300 - SJAllan 15
CS 5300 - SJAllan 16
ย Static
CS 5300 - SJAllan 19
T โ array [ num ] of T 1 T.type = array( num .val, T 1 .type)
T โ โT 1 T.type = pointer(T 1 .type)
T โ integer T.type = integer
T โ char T.type = char
D โ id : T addtype( id .entry, T.type)
D โ D ; D
P โ D ; E
Production Semantic Rules
CS 5300 - SJAllan 20
E.type = if E 1 .type = pointer(t) then t else type_error
E โ E 1 โ
E.type = if E 2 .type = integer and E 1 .type = array(s,t) then t else type_error
E โ E 1 [E 2 ]
E.type = if E 1 .type = integer and E 2 .type = integer then integer else type_error
E โ E 1 mod E 2
E โ id E.type = lookup(id.entry)
E โ num E.type = integer
E โ literal E.type = char
Production Semantic Rules
CS 5300 - SJAllan 21
S.type = if S 1 .type = void and S 2 .type = void then void else type_error
S โ S 1 ; S (^2)
S.type = if E.type = boolean then void else type_error
S โ while E do S (^1)
S.type = if E.type = boolean then void else type_error
S โ if E then S (^1)
S.type = if id.type = E.type then void else type_error
S โ id = E
Production Semantic Rules
CS 5300 - SJAllan 22
E.type = if E 2 .type = s and E 1 .type = s โ t then t else type_error
T โ T 1 โโ โ T 2 T.type = T 1 .type โ T 2 .type
Production Semantic Rules
CS 5300 - SJAllan 25
E.type = if E 1 .type = integer and E 2 .type = integer then integer else if E 1 .type = integer and E 2 .type = real then real else if E 1 .type = real and E 2. type = integer then real else if E 1 .type = real and E 2 .type = real then real else type_error
E โ E 1 op E 2
E โ id E.type = lookup( id .Entry)
E โ num. num E.type = real
E โ num E.type = integer
Production Semantic Rules