Compilation and interpretation
แนะนำใหม่
13:59
ทรัพยากร คอมไพเลอร์ และ RPN
เปิดเบราว์เซอร์Playersเพลง และเกม คุณมีโปรเซสเซอร์เพียงตัวเดียว — หรือบางทีก็หลายคอร์ — แต่ทั้งหมดดูเหมือนจะทำงานพร้อมกัน และทั้งหมดต้องการมากกว่านั้น…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| 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. |
บั๊กที่ทำให้ยานอวกาศพังพินาศ ซึ่งพบได้จากลูกน้ำ
- NASA's Mariner 1 ถูกทำลาย 293 วินาทีหลังจากรawan launch ใน 1962. ความล้มเหลวมักถูก blame จากตัวอักษรผิดเพียงตัวเดียวในซอฟต์แวร์นำทาง
- เรื่องราวนี้ถูกเล่าซ้ำแล้วซ้ำเล่าเพราะมันเป็นความกลัวที่นักเขียนโปรแกรมทุกคนรู้จัก: คอมไพเลอร์แปลโปรแกรมที่บอกสิ่งที่คุณไม่ได้ตั้งใจอย่างสบายใจ
- คอมไพเลอร์ไม่ใช่กระบวนการเดียวแต่เป็นห้า และแต่ละขั้นตอนจับข้อผิดพลาดประเภทต่าง ๆ ได้ การรู้ว่าขั้นตอนไหนจับอะไร บอกให้คุณได้ว่าข้อผิดพลาดนั้นมาจากจุดใด
- บทเรียนนี้คือวิธีที่ อินเทอร์โพรเตอร์ รันโปรแกรมโดยไม่สร้างอะไรออกมา และห้าขั้นตอนที่ คอมไพเลอร์ ผ่านไป

วิธีที่อินเทอร์โพรเตอร์รันโปรแกรม
- อินเทอร์โพรเตอร์แปลและรันซอร์สโค้ด พร้อมกัน โดยทำทีละบรรทัด
- สำหรับแต่ละบรรทัด มันอ่านบรรทัด วิเคราะห์ ตรวจสอบชนิดข้อมูล แล้ว ดำเนินการทันที และข้ามไปยังบรรทัดถัดไป
- มันไม่สร้าง ไฟล์ที่ runnable ได้: การแปลมีอยู่เพียงในหน่วยความจำและถูกทิ้งไป นั่นคือจุดสำคัญตามหลักสูตรพอดี คือมันสามารถรันโปรแกรมได้โดยไม่ต้องสร้างเวอร์ชันที่แปลแล้ว
- มันรายงานข้อผิดพลาดทันทีเมื่อเจอบรรทัดที่ผิดและมักจะหยุด ซึ่งให้ feedback ที่รวดเร็วขณะพัฒนา ทุกครั้งที่รันจะทำการแปลใหม่ ดังนั้นจึงช้ากว่า และทั้งซอร์สโค้ดและอินเทอร์โพรเตอร์ต้องมีอยู่ด้วย

แปลครั้งเดียวแล้วเก็บไว้ หรือแปลแล้วรันแล้วลืม
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: การวิเคราะห์.lexical analysis
- การวิเคราะห์ lexical รวมตัวอักษรแต่ละตัวจากซอร์สโค้ดเข้าด้วยกันเป็น token: คำสำคัญ (keywords), ชื่อตัวแปร (identifiers), ตัวดำเนินการ (operators) และค่าคงที่ (literals)
- ช่องว่างและ_heading are discarded, since they have no meaning to the compiler, and identifiers are entered into a symbol table.
- ดังนั้น
total ← count * 2จึงกลายเป็นลำดับ token เป็น identifier, assignment, identifier, operator, literal 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: การวิเคราะห์ syntax
- การวิเคราะห์ syntax หรือการ parse ตรวจสอบว่าลำดับ token เข้ากับ ไวยากรณ์ ของภาษาหรือไม่ และสร้าง abstract syntax tree เพื่อแสดงโครงสร้าง
- การขาดวงเล็บ, การขาด
ENDIFหรือการใช้คำสำคัญผิดตำแหน่งจะถูกจับที่นี่ นี่คือสิ่งที่เรียกว่า syntax error และเป็นเหตุผลที่คอมไพเลอร์สามารถรายงานได้โดยไม่ต้องรันโปรแกรมเลย

ไวยากรณ์ที่ parser ตรวจสอบ
ขั้นตอนที่ 3: การวิเคราะห์ semantic analysis
- การวิเคราะห์ semantic ตรวจสอบว่าโปรแกรมที่ถูกต้องตามหลักไวยากรณ์นั้น มีความหมายจริงหรือไม่: ตัวแปรทุกตัวต้องประกาศก่อนใช้ ชนิดข้อมูลทั้งสองฝั่งของการ assign ต้องเข้ากันได้ ฟังก์ชันถูกเรียกด้วยจำนวน arguments ที่ถูกต้อง
total ← "seven" * 2เป็น syntax ที่ดีเยี่ยมแต่เป็นความไร้สาระเพียงอย่างเดียว มีเพียงขั้นตอนนี้เท่านั้นที่จับมันได้- นี่คือความแตกต่างที่ข้อสอบทดสอบ: syntax คือ รูปแบบ, semantics คือ ความหมาย

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: การสร้างโค้ดและการปรับปรุงประสิทธิภาพ
- การสร้างโค้ด เดินผ่าน tree และ emit target machine code โดยเลือก registers และวาง layout ข้อมูล
- Code optimisation ปรับปรุงโค้ดนั้นโดยไม่เปลี่ยนสิ่งที่มันทำ: การงานซ้ำซ้อน, การคำนวณนิพจน์คงที่ during compile time, และการเรียงลำดับคำสั่งเพื่อให้เหมาะกับ pipeline
- ผลลัพธ์คือ ไฟล์ runnable แบบ standalone ที่รันได้โดยไม่ต้องมีคอมไพเลอร์

ห้าขั้นตอน และแต่ละขั้นจับสิ่งที่ขั้นก่อนหน้าจับไม่ได้
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 +: tokens ไม่เข้ากับ grammar มี operator แต่ไม่มี right operand ดังนั้น syntax analysisx ← y + 1ที่yไม่เคยถูก declare: รูปแบบดีแต่ความหมายไม่ดี ดังนั้น semantic analysisIF a > b THEN OUTPUT aที่ไม่มีENDIF: syntax analysis อีกครั้ง- โปรแกรมที่รันและ print ค่าเฉลี่ยผิด: ไม่มีขั้นตอนไหนจับได้ นั่นคือ logic error และมีการ test เท่านั้นที่หาได้ ชนขั้นตอนและอธิบายว่าทำไมขั้นตอนก่อนหน้าถึงปล่อยให้มันผ่าน

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]
- คอมไพเลอร์แปล ทั้ง programa ก่อนที่จะรันและสร้าง executable ซึ่ง luego runs without the compiler; an interpreter translates and executes one statement at a time and produces no executable, so the interpreter must be present every run.
- คอมไพเลอร์รายงาน ทั้งหมด ข้อผิดพลาดรวมกันหลังการแปล; อินเทอร์โพรเตอร์รายงาน ข้อผิดพลาดแรก เมื่อเจอบรรทัดนั้นแล้วหยุด
- คะแนนแต่ละข้อคือการจับคู่: บอกว่าอันหนึ่งทำอะไรและอีกอันทำอะไรแทน
คะแนนที่หลุดหายไป
- อินเทอร์โพรเตอร์สร้าง เวอร์ชันที่แปลแล้วไม่มี นั่นคือวลีที่หลักสูตรใช้และเป็นคะแนนที่มอบให้
- Lexer สร้าง tokens และ discard whitespace และ comments; มันไม่ตรวจสอบว่าโปรแกรมถูกต้องหรือไม่
- Syntax คือรูปแบบ, semantics คือความหมาย. ตัวแปรที่ไม่ได้ประกาศเป็น semantic error, ไม่ใช่ syntax error
- Optimization ต้องไม่เปลี่ยน สิ่งที่โปรแกรมทำ เปลี่ยนแค่ความเร็วหรือความกระชับในการกระทำเท่านั้น
คุณเข้าใจแล้ว
- an interpreter แปลงและดำเนินการตาม statement หนึ่งครั้งต่อหนึ่งครั้ง, ผลิต no executable, รายงาน first error at its line, และ re-translate ใหม่ทุกครั้งที่รัน
- การวิเคราะห์.lexical สร้าง โทเคน และตัดช่องว่างและ_heading; การวิเคราะห์_syntax ตรวจสอบไวยากรณ์และสร้าง ต้นไม้ไวยากรณ์นามธรรม เพื่อตรวจจับข้อผิดพลาดทางไวยากรณ์
- การวิเคราะห์ความหมาย (semantic analysis) ตรวจสอบความหมาย: การประกาศตัวแปร, ประเภทข้อมูล, จำนวนอาร์กิวเมนต์
- การสร้างโค้ด (code generation) ปล่อยโค้ดเครื่องและ การปรับปรุงประสิทธิภาพ (optimisation) ปรับปรุงให้ดีขึ้นโดยไม่เปลี่ยนพฤติกรรม เพื่อให้ได้ไฟล์ที่รันได้ด้วยตัวเอง
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้