Compilation and interpretation
הוצג
13:59
משאבים, קומפילרים ו-RPN
פתח דפדפן, נגן מוזיקה ומשחק. יש לך מעבד אחד — אולי כמה ליבות — אך כולם נראים כאילו רצים בו-זמנית. יחד הם שואפים ליותר…
קריינות באנגלית · כתוביות באנגלית ובסינית 中文 שאפשר להחליף ביניהן
בשיעור הזה
| English | משמעות |
|---|---|
| guidance software | Programs that control the direction and movement of a vehicle. |
| programmer | A person who writes and develops computer programs. |
| compiler/kəmˈpaɪlə/ | מתרגם |
| interpreter/ɪnˈtɜːprɪtə/ | מפרש |
| executable file | A file containing a program that a computer can run. |
| source/sɔːs/ | The original program instructions written by a programmer in a programming language. |
| statement/ˈsteɪtmənt/ | A line of code that tells a program what to do. |
| types/taɪps/ | Categories that describe what kind of data values can be stored or used. |
| memory/ˈmeməri/ | The part of a computer that holds data and programs currently in use. |
| Lexical analysis/ˈleksɪkl əˈnæləsɪs/ | The process of grouping source code characters into tokens and removing unnecessary whitespace and comments. |
| characters/ˈkærɪktəz/ | A text unit such as a letter, digit, punctuation mark or other symbol. |
| tokens/ˈtəʊkənz/ | טוקנים |
| keywords/ˈkiːwɜːdz/ | Words with special meanings that are part of a programming language. |
| identifiers | Names chosen by programmers for parts of a program. |
| operators/ˈɒpəreɪtəz/ | Symbols or keywords specifying operations to perform on values or expressions. |
| literals | Fixed values written directly in source code. |
| Whitespace/ˈwaɪtspeɪs/ | Spaces and other characters used to separate parts of source code. |
| comments/ˈkɒments/ | Explanatory text in source code or markup that does not execute as a program instruction. |
| symbol table/ˈsɪmbl ˈteɪbl/ | A compiler data structure recording identifiers and information such as their types, scopes or locations. |
| assignment/əˈsaɪnmənt/ | Storing a value in a variable or other named location. |
| lexer/ˈleksə/ | The part of a compiler that groups source code characters into tokens. |
| Syntax analysis/ˈsɪntæks əˈnæləsɪs/ | The process of checking tokens against language grammar and building a tree of their structure. |
| syntax/ˈsɪntæks/ | The rules that determine the correct structure of statements in a programming language. |
| grammar/ˈɡræmə/ | Rules defining valid sequences of symbols in a programming language. |
| abstract syntax tree/ˈæbstrækt ˈsɪntæks triː/ | עץ סינטקס קולט |
| syntax error/ˈsɪntæks ˈerə/ | שגיאת סינטקס |
| parser/ˈpɑːsə/ | The part of a compiler that checks syntax and builds a tree showing program structure. |
| Semantic analysis/səˈmæntɪk əˈnæləsɪs/ | Checking that a program follows meaning rules, such as using compatible types. |
| variable/ˈveərɪəbl/ | A quantity or named value that can change or take different values. |
| function/ˈfʌŋkʃn/ | A mapping that assigns exactly one output to each allowed input. |
| arguments/ˈɑːɡjuːmənts/ | Values or references supplied to a function or procedure when it is called. |
| semantics/səˈmæntɪks/ | The meaning of program statements according to the rules of a programming language. |
| Code generation/kəʊd ˌdʒenəˈreɪʃn/ | The process of producing machine code from the program's analysed structure. |
| target machine code | Instructions produced for the computer or processor that will run the program. |
| registers/ˈredʒɪstəz/ | A small, fast storage location within a processor used to hold data, addresses or instructions temporarily. |
| Code optimisation/kəʊd ˌɒptɪmaɪˈzeɪʃn/ | Improving code so it runs faster or uses less space without changing what it does. |
| constant expressions | Calculations made entirely from fixed values that can be worked out before a program runs. |
| compile time | The period when source code is translated into a form a computer can run. |
| instructions/ɪnˈstrʌkʃnz/ | Commands that tell a computer what actions to perform. |
| pipeline/ˈpaɪplaɪn/ | An arrangement that overlaps stages of different instructions or tasks to improve overall throughput. |
| stand-alone executable | A program file that can run without the compiler being present. |
| operand/ˈɒpərænd/ | A value, register or memory location that an instruction operates on. |
| logic error/ˈlɒdʒɪk ˈerə/ | A mistake in an algorithm or program that produces an incorrect result despite valid syntax. |
| testing/ˈtestɪŋ/ | Running a program with chosen inputs to check that it gives the expected outputs. |
| semantic error | An error where code follows grammar rules but breaks meaning rules, such as using incompatible types. |
התיקון שגרם לאבן חלל להיפסד, נמצא על ידי פסיק
- המרינר 1 של נאסה הושמד 293 שניות לאחר השקה ב-1962. כשל זה מיוחס לעיתים קרובות לאות שגויה אחת בתוכנת ההנחיה.
- הסיפור מסופר פעמים רבות משום שהוא מבטא את הפחד שמכיר כל מתכנת: קומפיילר שמעביר בחיבה תוכנית אומרת דבר שלא התכוונת אליו.
- קומפיילר אינו תהליך אחד אלא חמישה, וכל אחד מהם תופס מחלקה שונה של טעות. ידועה המידע על כל שלב ומה הוא תופס מאפשר לך לדעת מאין מגיעה כל טעות נתונה.
- שיעור זה מסביר כיצד מפרש (interpreter) מפעיל תוכנית ללא יצירת כל תוצר, ואת חמישת השלבים שאותם עובר קומפיילר.

כיצד מפרש מפעיל תוכנית
- מפרש מעביר ומפעיל את המקור באותו זמן, סעיף אחד בכל פעם.
- עבור כל סעיף הוא קורא את השורה, מנתח אותה, בודק את הסוגים, ואז מבצע את הפעולה מיד, ועובר לסעיף הבא.
- הוא לא מייצר קובץ ביצוע: העיבור קיים רק בזיכרון ומיטול. זוהי בדיוק הנקודה במסלול הלימודים, שהיא יכולה לבצע תוכנית ללא יצירת גרסה מעוברת.
- הוא מדווח על שגיאה ברגע שהוא מגיע לשורה הפוגעת ובדרך כלל עצור, מה שמספק משוב מהיר בעת פיתוח. כל הפעלה מעבירה מחדש, ולכן היא איטית יותר, ויש צורך בנוכחות גם המקור וגם המפרש.

העבר פעם אחת והחזק, או העבר ופעל ושכח

An interpreter:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
It works line by line, reporting errors as it reaches them; nothing is saved as an executable, and it is generally slower.
An interpreter can execute a program without producing a translated version of it.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
The translation exists only in memory, statement by statement, and is discarded. That is why the interpreter must be present every time the program runs.
שלב 1: ניתוח לקסיקלי
- ניתוח לקסיקלי מקבץ את האותיות הבודדות של המקור ל-טוקנים: מילות מפתח, מזהה, אופרטורים וליטרלים.
- רווחים ותגובות מוחלקים, מכיוון שהם אין להם משמעות עבור הקומפיילר, והמזהים נכנסים לטבלת סימנים.
- כך
total ← count * 2הופך לרצף הטוקנים מזהה, הצבה, מזהה, אופרטור, ליטרל. הלেকסר (lexer) לא יודע ולא מתעניין אם הדבר הזה הגיוני.

Lexical analysis (the lexer) turns:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
The lexer groups characters into tokens and discards whitespace/comments; parsing then builds the tree.
Lexical analysis groups the source characters into ____ and discards whitespace and comments.
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Keywords, identifiers, operators and literals. The lexer makes no judgement about whether the sequence is a valid program.
שלב 2: ניתוח סינטקטי
- ניתוח סינטקטי, או פרסוף, בודק שרצף הטוקנים מתאים ל-דקדוק השפה, ובונה עץ סינטקטי抽象 המייצג את המבנה.
- סוגרי חסר, סימן
ENDIFחסר או מילת מפתח במקום שגוי נתפסים כאן: זהו שגיאת סינטקס, וזוהי הסיבה שהמייצר יכול לדווח עליה ללא ריצת התוכנית לעולם.

הדקדוק נגדו בוחן המפרש

שלב 3: ניתוח סמנטי
- ניתוח סמנטי בודק שמתקף תוכנה תקני מורפולוגית באמת עולה על הקרקע: כל משתנה מוגדר לפני השימוש, הסוגים בשני צדי ההטלה תואמים, ופונקציה נקראת עם מספר הארגומנטים הנכון.
total ← "seven" * 2הוא קוד מורפולוגי תקין לחלוטין אך חסר כל משמעות. רק השלב הזה חושף זאת.- זהו ההבדל שהמבחן בודק: מורפולוגיה היא צורה, סמנטיקה היא משמעות.

Which of these is checked by semantic analysis rather than syntax analysis?
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Brackets are grammar, so syntax; whitespace is the lexer; registers are code generation. Declarations and types are meaning.
שלבים 4 ו-5: ייצור קוד ואופטימיזציה
- ייצור קוד עובר על העץ ומפיק את קוד המכונה היעד, בוחר רשומות ומסדר את הנתונים.
- אופטימיזציה של קוד משפרת את הקוד ללא שינוי בהתנהגותו: הסרת עבודה מיותרת, חישוב ביטויים קבועים בזמן הרצאה, והסדר מחדש של פקודות להתאמה לשבך.
- הפלט הוא קובץ ביצוע עצמאי שמריץ ללא נוכחות המתרגם.

חמישה שלבים, וכאשר אחד חושף שגיאה שהשלב הקודם לא זיהה

The phases of compilation
Step through what a compiler does to your source. Each phase hands its output to the next — characters become tokens, tokens become a tree, the tree becomes optimised machine code.
Match each compiler phase to what it does.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Each phase transforms the program a step further: tokens, then a tree, then checked, then optimised code.
Put the compiler stages in order.
רמת קושי 3/5 · 200 מטבעות + בונוס קטן בהשלמה הראשונה היום
Lexical → syntax → semantic → code generation → optimisation.
Code optimisation aims to:
רמת קושי 1/5 · 100 מטבעות + בונוס קטן בהשלמה הראשונה היום
Optimisation improves the generated code (constant folding, removing redundancy, reordering for the pipeline).
דוגמא פתורה: איזה שלב חושף איזה שגיאה
x ← 5 +: הטוקנים אינם עומדים בדקדוק, אופרטור ללא ארגומנט ימני, ולכן ניתוח מורפולוגי.x ← y + 1שבוyמעולם לא הוגדר: הצורה תקינה אך המשמעות אינה כזו, ולכן ניתוח סמנטי.IF a > b THEN OUTPUT aללאENDIF: ניתוח מורפולוגי שוב.- תוכנה שמריצה ומוציאה את הממוצע הלא נכון: שום שלב לא חושף זאת. זוהי שגיאת לוגיקה, ורק בדיקות מאפשרות גילויה. ציין את השלב וגם מדוע השלבים הקודמים אפשרו את מעברו.

Match each faulty line to the compiler stage that catches it.
רמת קושי 2/5 · 140 מטבעות + בונוס קטן בהשלמה הראשונה היום
Syntax is form, semantics is meaning, and a logic error is valid in both but wrong in intent.
דוגמא פתורה: השוו בין שני המתרגמים
- תן שתי הבדלים בין אופן התמודדותם של מתרגם ומפרש עם תוכנה. [4]
- מתרגם מתרגם את כל התוכנה לפני הרצה ומפיק קובץ ביצוע, שמריץ לאחר מכן ללא נוכחות המתרגם; מפרש מתרגם ומבצע פקודה אחת בכל פעם ולא מפיק קובץ ביצוע, ולכן המפרש חיוני בכל הרצה.
- מתרגם מדווח על כל השגיאות יחד לאחר ההרצה; מפרש מדווח על השגיאה הראשונה כשהוא מגיע לשורה הזו ועוצר.
- כל ציון מייצג זוג פעולות: לציין מה אחת עושה ומה השנייה עושה במקום זאת.
סימונים נופלים מהיד
- המתרגם אינו מייצר גרסה מתורגמת. זוהי הניסוח המופיע בסילבוס והציון שמוענק עליו.
- המנתק (lexer) מייצר טוקנים ומשליך רווחים ותגיות; הוא לא בודק האם התוכנית תקפה.
- סינטקס הוא הצורה, סמנטיקה היא המשמעות. משתנה שלא הוכרז הוא שגיאת סמנטיקה, ולא שגיאת סינטקס.
- אופטימיזציה לא יכולה לשנות מה שהתוכנית עושה, אלא רק את מהירות או הדחיסות של ביצועה.
הבנתך מושלמת
- מתרגם מתרגם ומבצע פקודה אחת בכל פעם, ללא יצירת קובץ נמצא, מדווח על השגיאה הראשונה בשורה שלה, ומתרגם מחדש בכל ריצה
- ניתוח לקסיקלי יוצר טוקנים ומשליך רווחים ותגיות; ניתוח סינטקטי בודק את הדקדוק ובונה עץ סינטקס מופשט, תופש שגיאות סינטקס
- ניתוח סמנטי בודק משמעות: הכרזות, סוגים, מספר פרמטרים
- יצירת קוד מפיצה קוד מכונה ואופטימיזציה משפרת אותו ללא שינוי התנהגות, תוך מתן קובץ נמצא עצמאי
מה השתבש? רק צוות האתר רואה זאת.