בדיקת טיפוסים Aho, Sethi, Ullman – Chapter 6. בדיקת טיפוסים (type checking) המטרות העיקריות של בדיקת הטיפוסים בדיקת תקינות פתרון של בעיות overloadingו-

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
ממיבחניםC שאלות ++.
תוכנה 1 סמסטר א ' תשע " ב תרגול מס ' 7 * מנשקים, דיאגרמות וביטים * לא בהכרח בסדר הזה.
מבוא למדעי המחשב לתעשייה וניהול
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
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.
Lecture # 20 Type Systems. 2 A type system defines a set of types and rules to assign types to programming language constructs Informal type system rules,
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Chapter 6 Type Checking Section 0 Overview 1.Static Checking Check that the source program follows both the syntactic and semantic conventions of the source.
CH4.1 CSE244 Type Checking Aggelos Kiayias Computer Science & Engineering Department The University of Connecticut 371 Fairfield Road, Unit 1155 Storrs,
Compiler Construction
Lesson 12 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
טבלאות סמלים נכתב ע"י אלכס קוגן סמסטר חורף, תשס"ח.
Fall 2003CS416 Compiler Design1 Type Checking A compiler has to do semantic checks in addition to syntactic checks. Semantic Checks –Static – done during.
מתמטיקה בדידה תרגול 3.
1 Pertemuan 18 & 19 Semantic Analyzer (type checking) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
רקורסיות נושאי השיעור פתרון משוואות רקורסיביות שיטת ההצבה
תרגול 5 רקורסיות. רקורסיה קריאה של פונקציה לעצמה –באופן ישיר או באופן עקיף היתרון : תכנות של דברים מסובכים נעשה ברור ונוח יותר, מכיוון שזו למעשה צורת.
חורף - תשס " ג DBMS, צורות נורמליות 1 צורה נורמלית שלישית - 3NF הגדרה : תהי R סכמה רלציונית ותהי F קבוצת תלויות פונקציונליות מעל R. R היא ב -3NF.
שפות ביניים – Intermediate Languages/Representations Aho, Sethi and Ullman – Chapter 8 Cooper and Torczon – Chapter 5.
תרגול חזרה. מבנה האובייקט תאר את מבנה האובייקט כולל מבנה טבלאות הפונקציות הוירטואליות עבור התכנית הבאה struct A { int x; virtual void a() {}; }; struct.
מסדי נתונים תשס " ג 1 תכנון סכמות – אלגוריתם פירוק לתבניות בצורת BCNF מסדי נתונים.
1 מבוא למדעי המחשב ביטויים. 2 ביטויים expressions ביטויים (expressions) הינם יצורים תחביריים בעלי טיפוס וערך. הגדרה אינדוקטיבית של ביטויים : קבועים הם.
תכנות תרגול 6 שבוע : תרגיל שורש של מספר מחושב לפי הסדרה הבאה : root 0 = 1 root n = root n-1 + a / root n-1 2 כאשר האיבר ה n של הסדרה הוא קירוב.
Chapter 6 Type Checking Section 0 Overview
11 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול
מערכות הפעלה ( אביב 2009) חגית עטיה ©1 מערכת קבצים log-structured  ה log הוא העותק היחיד של הנתונים  כאשר משנים בלוק (data, header) פשוט כותבים את הבלוק.
אלכסנדר ברנגולץ דואר אלקטרוני: אלכסנדר ברנגולץ דואר אלקטרוני: פעולות מורפולוגיות.
מערכים עד היום כדי לייצג 20 סטודנטים נאלצנו להגדיר עד היום כדי לייצג 20 סטודנטים נאלצנו להגדיר int grade1, grade2, …, grade20; int grade1, grade2, …, grade20;
תכנות מונחה עצמים Object Oriented Programming (OOP) אתגר מחזור ב' Templates תבניות.
מבוא למדעי המחשב תרגול 3 שעת קבלה : יום שני 11:00-12:00 דוא " ל :
Last time on Clang משתנה: "פתק" המשמש את המחשב לשמירת מידע. לכל משתנה יש שם וטיפוס כללים לשמות משתנים –חייבים להכיל רק אותיות, מספרים ו '_' –חייבים להתחיל.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
מבוא לשפת C תרגול 4: ביטויים לוגיים ומשפטי תנאי מבוסס על השקפים שחוברו ע"י שי ארצי, גיתית רוקשטיין, איתן אביאור וסאהר אסמיר עבור הקורס "מבוא למדעי המחשב"
1 Pertemuan 11 Semantic Analysis Matakuliah: T0522 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
מתמטיקה בדידה תרגול 2.
1 Formal Specifications for Complex Systems (236368) Tutorial #3 Z introduction and notation (contd.); Birthday book example (Chapter 1 in the book)
11 Introduction to Programming in C - Fall 2010 – Erez Sharvit, Amir Menczel 1 Introduction to Programming in C תרגול
1 מבוא למדעי המחשב רקורסיה. 2 רקורסיה היא שיטה לפתרון בעיות המבוססת על העיקרון העומד ביסוד אינדוקציה מתמטית: אם ידועה הדרך לפתור בעיה עבור המקרים הבסיסיים.
Static checking and symbol table Chapter 6, Chapter 7.6 and Chapter 8.2 Static checking: check whether the program follows both the syntactic and semantic.
הגדרת משתנים יום שישי 18 ספטמבר 2015 יום שישי 18 ספטמבר 2015 יום שישי 18 ספטמבר 2015 יום שישי 18 ספטמבר 2015 יום שישי 18 ספטמבר 2015 יום שישי 18 ספטמבר.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
1 תרגול 2 – מבוא לתכנות JAVA. היום בתרגול : 2  משתנים וטיפוסים  אופרטורים  פונקציות מתמטיות מהמחלקה Math  המרת טיפוסים  תנאים  Debugger.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
1 המרכז להוראת המדעים © האוניברסיטה העברית בירושלים עוברים לג ' אווה.
תכנות אסינכרוני, תקשורת ופיתוח אפליקציות ל- Windows 8.1 ואפליקציות ל- Windows Phone 8 Control (Part II)
מבוא למדעי המחשב לתעשייה וניהול הרצאה 7. סברוטינות subroutines.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
תכנות מכוון עצמים ושפת ++C וויסאם חלילי. TODAY TOPICS: 1. Function Overloading & Default Parameters 2. Arguments By Reference 3. Multiple #include’s 4.
1 Structure of a Compiler Source Language Target Language Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
הגדרת משתנים יום שלישי 14 יוני 2016 יום שלישי 14 יוני 2016 יום שלישי 14 יוני 2016 יום שלישי 14 יוני 2016 יום שלישי 14 יוני 2016 יום שלישי 14 יוני 2016.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
תרגול 7: מנשקים, פולימורפיזם ועוד
Compiler Design – CSE 504 Type Checking
Semantics Analysis.
Semantic Analysis Type Checking
Context-Sensitive Analysis
Operators Overloading
תרגול 7: מנשקים, פולימורפיזם ועוד
Semantic Analysis Chapter 6.
SQL בסיסי – הגדרה אינדוקטיבית
Static Checking and Type Systems
תוכנה 1 תרגול 13 – סיכום.
תכנות מכוון עצמים ו- C++ יחידה 02 העמסת פונקציות, ערכי ברירת מחדל, enum, קימפול מותנה קרן כליף.
Compiler Construction
Compiler Construction
Presentation transcript:

בדיקת טיפוסים Aho, Sethi, Ullman – Chapter 6

בדיקת טיפוסים (type checking) המטרות העיקריות של בדיקת הטיפוסים בדיקת תקינות פתרון של בעיות overloadingו- polymorphism בדיקת שקילות של טיפוסים

ההקשר הכללי – בדיקות סטטיות דוגמאות בדיקת טיפוסים בדיקת תקינות של זרימת הבקרה  goto ליעד לא מוגדר? בדיקת יחידוּת  שני משתנים עם אותו השם? בדיקות הקשורות במופעים של שמות בתכנית  begin A … end B? בדיקות המתבצעות בזמן ריצה נקראות דינאמיות בדיקת טיפוסים (type checking)

front end – שלב הניתוח - תזכורת תוכנית מקור תוכנית מטרה lexical analysis syntax analysis semantic analysis token stream syntax tree decorated syntax tree scanner parser היכן מוסיפים את בדיקות הטיפוסים?

front end – שלב הניתוח - תזכורת תוכנית מקור תוכנית מטרה lexical analysis syntax analysis semantic analysis token stream syntax tree decorated syntax tree scanner parser Type Checking

front end – שלב הניתוח - תזכורת תוכנית מקור תוכנית מטרה lexical analysis syntax analysis semantic analysis token stream syntax tree decorated syntax tree scanner parser Type Checking

בדיקת טיפוסים – למה זה טוב? (דיון בכתה)

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]

type systems ביטויי טיפוסים (type expressions) טיפוס בסיסי boolean, character, integer, real,… enumerated types…, void, type error שמות של ביטויי טיפוסים תוצאת ההפעלה של type constructor ביטויי טיפוס יכולים להכיל משתנים מטיפוס "ביטוי טיפוס".

type constructor מערכים מכפלה קרטזית (  ) רשומות (record או struct או class) כמו מכפלה קרטזית, אבל עם שמות) מחוונים (pointers) פונקציות

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

ייצוג ביטויי טיפוס כגרף עבור char x char → pointer (integer) עלים – טיפוסים בסיסיים שאר הצמתים – תוצאה של הפעלה של type constructors integerchar pointer  → ייצוג כעץ integerchar pointer  → ייצוג כגרף חסר מעגלים

type systems מערכת טיפוסים היא אוסף חוקים להצמדת ביטויי טיפוס לחלקים שונים של התוכנית מימוש מערכת הטיפוסים של שפת תכנות – באמצעות type checkers במערכת טיפוסים שהיא sound ניתן לוודא תקינות בזמן קומפילציה שפה עבורה מערכת הטיפוסים היא sound נקראת – strongly typed באופן מעשי, בדיקות מסוימות ניתן לבצע רק בזמן ריצה דוגמא: חריגה מגבולות מערך.

הסקת טיפוסים וטיפול בשגיאות דוגמא – מהביטוי 5 + x ניתן להסיק ש- x הוא integer הערה – גם בשפות בהן הטיפוסים דינאמיים, ידיעה טובה של הטיפוסים מסייעת ליצירת קוד יעיל יותר / לאינטרפרטציה יעילה יותר הערה – שפות מסוימות מחייבות מערכת משוכללת של הסקת טיפוסים טיפול בשגיאות באמצעות הסקת טיפוסים וזיהוי שגיאות על ידי הגדרת טיפוס שגיאה, על מנת למנוע התפשטות של שגיאות

דוגמא ל- 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

דוגמא ל- type checker ביטויים E → literal E → num E → id E → E mod E E → E [ E ] E → ↑E הפעלה של פונקציות E → E ( E )

דוגמא ל- 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 }

דוגמא ל- 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 }

הרחבת השפה נוסיף לשפה משפטים. תוכנית היא כעת: P → S ; E הכללים שהגדרנו עבור ביטויים עדיין שימושיים, משום שמשפטים כוללים בתוכם ביטויים.

משפטים S → id := E S → if E then S S → while E do S S → S ; S

משפטים 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

equivalence of type expressions מושג השקילות הכרחי להגדרת נכונות של תכנית תמונת השקילות של ביטויים לא תמיד מובנת מאליה בשפות בהן יש user defined types הערה: בדיקת השקילות היא לפעמים חלקית, למשל האם מערכים בעלי תחומי הגדרה שונים שקולים?

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;

הערות אפשר להרחיב את האלגוריתם על מנת שיטפל בסוגים נוספים של טיפוסים האלגוריתם מסוגל לטפל בעצים או בגרפים חסרי מעגלים הערה: נתבונן בכלל העוסק בגבולות של מערכים 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 ) על ידי ויתור על המרכיב הזה נוותר על זהות גדלי המערך

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

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 על ידי הצבה

equivalence of type expressions הגדרות מעגליות -- בעיה אופיינית לטיפול ב- C struct cell { int info; struct cell *next; }; הגישה – structural equivalence עבור כל הטיפוסים חוץ מ- struct כל ה- type names צריכים להיות מוגדרים לפני השימוש, חוץ ממצביעים ל- struct שעדיין לא הוכרז cell pointernextintinfo xx x cell = struct

המרת טיפוסים המרת טיפוסים יכולה להיות מפורשת. למשל, בפסקל משתמשים בפונקציות ord ו-chr כדי להמיר מ-char ל-integer ולהיפך. המרת טיפוסים יכולה להיות גם משתמעת (implicit, נקרא לעיתים coercion). המרה משתמעת מתבטאת לעיתים קרובות בקוד כקריאה לפונקציה פנימית. מומלץ לבצע המרות משתמעות של קבועים בזמן הידור ולא בזמן ריצה...

המרת טיפוסים 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

overloading יתכן overloading הן של פונקציות והן של אופרטורים overloaded symbol – מקבל משמעות שונה בתלות בהקשר resolution – קביעת משמעות יחידה דוגמא – פעולות אריתמטיות הן overloaded

קבוצת אפשרויות לטיפוסים עבור תת ביטויים דוגמא: הרחבה של פעולת ה- * ב- 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

המרת טיפוסים – 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 )

דברים שמסבכים type systems ירושה generics הסקת טיפוסים