Download presentation
Presentation is loading. Please wait.
1
בדיקת טיפוסים Aho, Sethi, Ullman – Chapter 6
2
בדיקת טיפוסים (type checking) המטרות העיקריות של בדיקת הטיפוסים בדיקת תקינות פתרון של בעיות overloadingו- polymorphism בדיקת שקילות של טיפוסים
3
ההקשר הכללי – בדיקות סטטיות דוגמאות בדיקת טיפוסים בדיקת תקינות של זרימת הבקרה goto ליעד לא מוגדר? בדיקת יחידוּת שני משתנים עם אותו השם? בדיקות הקשורות במופעים של שמות בתכנית begin A … end B? בדיקות המתבצעות בזמן ריצה נקראות דינאמיות בדיקת טיפוסים (type checking)
4
front end – שלב הניתוח - תזכורת תוכנית מקור תוכנית מטרה lexical analysis syntax analysis semantic analysis token stream syntax tree decorated syntax tree scanner parser היכן מוסיפים את בדיקות הטיפוסים?
5
front end – שלב הניתוח - תזכורת תוכנית מקור תוכנית מטרה lexical analysis syntax analysis semantic analysis token stream syntax tree decorated syntax tree scanner parser Type Checking
6
front end – שלב הניתוח - תזכורת תוכנית מקור תוכנית מטרה lexical analysis syntax analysis semantic analysis token stream syntax tree decorated syntax tree scanner parser Type Checking
7
בדיקת טיפוסים – למה זה טוב? (דיון בכתה)
8
type systems יחוס של טיפוס לתוצאה של פעולה “If both operands of the operators of additions, subtraction and multiplication are of type integer, then the result is of type integer” [The Pascal Report] “The result of the unary & operator is a pointer to the object referred to by the operand. If the type of the operand is ‘…’, the type of the result is ‘pointer to …’.” [The C Reference Manual]
9
type systems ביטויי טיפוסים (type expressions) טיפוס בסיסי boolean, character, integer, real,… enumerated types…, void, type error שמות של ביטויי טיפוסים תוצאת ההפעלה של type constructor ביטויי טיפוס יכולים להכיל משתנים מטיפוס "ביטוי טיפוס".
10
type constructor מערכים מכפלה קרטזית ( ) רשומות (record או struct או class) כמו מכפלה קרטזית, אבל עם שמות) מחוונים (pointers) פונקציות
11
type systems array(3..6, real)array [3..6] of real pointer (row)var p: ↑row record ((address integer) (name array(1..5, char))) type row = record address: integer; name: array [1..5] of char; end; array (1..100, row)var table: array [1..100] of row integer integer → integerfunction mod (integer, integer) : integer
12
ייצוג ביטויי טיפוס כגרף עבור char x char → pointer (integer) עלים – טיפוסים בסיסיים שאר הצמתים – תוצאה של הפעלה של type constructors integerchar pointer → ייצוג כעץ integerchar pointer → ייצוג כגרף חסר מעגלים
13
type systems מערכת טיפוסים היא אוסף חוקים להצמדת ביטויי טיפוס לחלקים שונים של התוכנית מימוש מערכת הטיפוסים של שפת תכנות – באמצעות type checkers במערכת טיפוסים שהיא sound ניתן לוודא תקינות בזמן קומפילציה שפה עבורה מערכת הטיפוסים היא sound נקראת – strongly typed באופן מעשי, בדיקות מסוימות ניתן לבצע רק בזמן ריצה דוגמא: חריגה מגבולות מערך.
14
הסקת טיפוסים וטיפול בשגיאות דוגמא – מהביטוי 5 + x ניתן להסיק ש- x הוא integer הערה – גם בשפות בהן הטיפוסים דינאמיים, ידיעה טובה של הטיפוסים מסייעת ליצירת קוד יעיל יותר / לאינטרפרטציה יעילה יותר הערה – שפות מסוימות מחייבות מערכת משוכללת של הסקת טיפוסים טיפול בשגיאות באמצעות הסקת טיפוסים וזיהוי שגיאות על ידי הגדרת טיפוס שגיאה, על מנת למנוע התפשטות של שגיאות
15
דוגמא ל- type checker תוכנית היא הכרזות + ביטוי P → D ; E משפטי הכרזה D → D ; D D → id : T T → char T → integer T → ↑T T → array [ num ] of T הכרזה על פונקציות T → T → T
16
דוגמא ל- type checker ביטויים E → literal E → num E → id E → E mod E E → E [ E ] E → ↑E הפעלה של פונקציות E → E ( E )
17
דוגמא ל- type checker תוכנית היא הכרזות + ביטוי P → D ; E משפטי הכרזה D → D ; D D → id : T { addtype (id.entry, T.type) } T → char { T.type := char } T → integer { T.type := int } T → ↑T 1 { T.type := pointer (T 1.type) } T → array [ num ] of T 1 { T.type := array (1..num.val, T 1.type) } הכרזה על פונקציות T → T 1 → T 2 { T.type := T 1.type → T 2.type }
18
דוגמא ל- type checker ביטויים E → literal { E.type := char } E → num { E.type := int } E → id { E.type := lookup (id.entry) } E → E 1 mod E 2 { E.type := if E 1.type = int and E 2.type = int then int else type_error } E → E 1 [ E 2 ] {E.type := if E2.type = int and E 1.type = array (s, t) then t else type_error } E → ↑E 1 {E.type := if E 1.type=pointer(t) then t else type_error } הפעלה של פונקציות E → E 1 ( E 2 ) { E.type := if E 2.type = s and E 1.type = s → t then t else type_error }
19
הרחבת השפה נוסיף לשפה משפטים. תוכנית היא כעת: P → S ; E הכללים שהגדרנו עבור ביטויים עדיין שימושיים, משום שמשפטים כוללים בתוכם ביטויים.
20
משפטים S → id := E S → if E then S S → while E do S S → S ; S
21
משפטים S → id := E { S.type := if id.type = E.type then void else type_error } S → if E then S 1 { S.type := if E.type = boolean then S 1.type else type_error } S → while E do S 1 { S.type := if E.type = boolean then S 1.type else type_error } S → S 1 ; S 2 { S.type := if S 1.type = void and S 2.type = void then void else type_error } איך נראים משפטי השמה בשפת C? הרעיון: error propagation
22
equivalence of type expressions מושג השקילות הכרחי להגדרת נכונות של תכנית תמונת השקילות של ביטויים לא תמיד מובנת מאליה בשפות בהן יש user defined types הערה: בדיקת השקילות היא לפעמים חלקית, למשל האם מערכים בעלי תחומי הגדרה שונים שקולים?
23
structural equivalence function sequiv (s, t): boolean; begin if s and t are the same basic type then return true else if s = array(s 1, s 2 ) and t = array(t 1, t 2 ) then return sequiv (s 1, t 1 ) and sequiv (s 2, t 2 ) else if s = s 1 x s 2 and t = t 1 t 2 then return sequiv (s 1, t 1 ) and sequiv (s 2, t 2 ) else if s = pointer (s 1 ) and t = pointer (t 1 ) then return sequiv (s 1, t 1 ) else if s = s 1 → s 2 and t = t 1 → t 2 then return sequiv (s 1, t 1 ) and sequiv (s 2, t 2 ) else return false end;
24
הערות אפשר להרחיב את האלגוריתם על מנת שיטפל בסוגים נוספים של טיפוסים האלגוריתם מסוגל לטפל בעצים או בגרפים חסרי מעגלים הערה: נתבונן בכלל העוסק בגבולות של מערכים else if s = array(s 1, s 2 ) and t = array(t 1, t 2 ) then return sequiv (s 1, t 1 ) and sequiv (s 2, t 2 ) על ידי ויתור על המרכיב הזה נוותר על זהות גדלי המערך
25
equivalence of type expressions user defined types typelink =↑cell; varnext: link; last: link; p: ↑ cell; q, r: ↑ cell; בפסקל לא הוגדר המושג identical type ולכן לא ברור האם p ו– next שווים ובאופן כללי name equivalence ≠ structural equivalence
26
equivalence of type expressions הגדרות מעגליות -- בעיה אופיינית לטיפול ב- records typelink =↑cell; cell = record; info: integer; next: link; end; pointernextintegerinfo xx x cell = record על ידי הצבה + הצבעה cell pointernextintegerinfo xx x cell = record על ידי הצבה
27
equivalence of type expressions הגדרות מעגליות -- בעיה אופיינית לטיפול ב- C struct cell { int info; struct cell *next; }; הגישה – structural equivalence עבור כל הטיפוסים חוץ מ- struct כל ה- type names צריכים להיות מוגדרים לפני השימוש, חוץ ממצביעים ל- struct שעדיין לא הוכרז cell pointernextintinfo xx x cell = struct
28
המרת טיפוסים המרת טיפוסים יכולה להיות מפורשת. למשל, בפסקל משתמשים בפונקציות ord ו-chr כדי להמיר מ-char ל-integer ולהיפך. המרת טיפוסים יכולה להיות גם משתמעת (implicit, נקרא לעיתים coercion). המרה משתמעת מתבטאת לעיתים קרובות בקוד כקריאה לפונקציה פנימית. מומלץ לבצע המרות משתמעות של קבועים בזמן הידור ולא בזמן ריצה...
29
המרת טיפוסים Semantic Ruleproduction integer real lookup(id.entry) 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.type := E → num E → num. num E → id E → E 1 op E 2 כללים להמרה משתמעת מ-integer ל-real
30
overloading יתכן overloading הן של פונקציות והן של אופרטורים overloaded symbol – מקבל משמעות שונה בתלות בהקשר resolution – קביעת משמעות יחידה דוגמא – פעולות אריתמטיות הן overloaded
31
קבוצת אפשרויות לטיפוסים עבור תת ביטויים דוגמא: הרחבה של פעולת ה- * ב- Ada function “*” (i, j : integer) return complex; function “*” (x, y : complex) return complex; האפשרויות: integer integer → integer integer integer → complex complex complex → complex הטיפוס של 3 *5 מוגדר על פי ההקשר אם 2 * (5 * 3) הוא כל הביטוי אזי 5 * 3 מוגדר כ- integer. אם הביטוי הוא * (5 * 3) z באשר z מטיפוס complex, אזי 5 * 3 הוא מטיפוס complex
32
המרת טיפוסים – overloading type checking rules for coercion from integer to real אם E.types ריק – type_error ב- Ada צריך בסופו של דבר להקבע טיפוס יחיד; התהליך נקרא narrowing semantic ruleproduction lookup ( id.entry ) { t | there exists an s in E 2.types such that s → t is in E 1.types } E.types := E → id E → E 1 ( E 2 )
33
דברים שמסבכים type systems ירושה generics הסקת טיפוסים
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.