English
The CPU can only run machine code. A high-level program must first be changed into machine code. The software that does this is a translator 翻译程序. There are three types: a compiler and an interpreter (for high-level code), and an assembler (for low-level code).
Compiler
A compiler translates the whole program into machine code before it is run. After translating, the machine code can be run many times without translating again.
- translation happens once, in full;
- it reports all the errors together, at the end of translating;
- the finished program runs fast and does not need the compiler to run.
Interpreter
An interpreter 解释器 translates and runs the program one line at a time.
- it stops at the first error it finds, which helps when testing;
- the program runs more slowly, because it is translated each time it runs;
- the interpreter must be present every time the program runs.
|
Compiler |
Interpreter |
| Translates |
the whole program at once |
one line at a time |
| Errors |
all reported at the end |
stops at the first error |
| Speed of finished program |
faster |
slower |
| Needed to run later? |
no |
yes |
Use a compiler when you want to share a fast, finished program. Use an interpreter when you are writing and testing a program and want to find errors easily.
Assembler
An assembler 汇编器 translates a low-level assembly-language program (written in mnemonics such as LDA, ADD) into machine code. Each mnemonic becomes one machine instruction – a direct one-to-one translation, unlike a compiler or interpreter, which work on high-level code.
Worked example. A team is writing a program. While developing it they want to find and fix errors quickly; when they ship it they want customers to run it fast without seeing the source code. Which translator suits each stage? While developing, use an interpreter: it translates and runs one line at a time and stops at the first error, so a mistake is easy to locate. To ship, use a compiler: it translates the whole program once into machine code, which then runs quickly with no translator present, and the customer receives only the executable rather than the source. The two are not rivals - name the stage and give the reason, because "a compiler is faster" on its own earns nothing.
العربية
يمكن لوحدة المعالجة المركزية تشغيل كود الآلة فقط. يجب تحويل برنامج عالي المستوى أولاً إلى كود آلة. البرمجيات التي تقوم بذلك هي المُترجم. توجد ثلاثة أنواع: المُترجم الكامل والمُفسِّر (للأكواد عالية المستوى)، والمُجمِّع (للأكواد منخفضة المستوى).
المترجم (Compiler)
المُترجم الكامل يُترجم الكامل البرنامج إلى كود آلة قبل تشغيله. بعد الترجمة، يمكن تشغيل كود الآلة مرات عديدة دون الحاجة لإعادة الترجمة.
- تحدث الترجمة مرة واحدة وبشكل كامل;
- يُبلغ عن جميع الأخطاء معًا، في نهاية عملية الترجمة;
- البرنامج النهائي يعمل بسرعة ولا يحتاج إلى وجود المُترجم الكامل أثناء التشغيل.
المُفسِّر (Interpreter)
المُفسِّر يُترجم ويشغل البرنامج سطراً تلو الآخر.
- يتوقف عند أول خطأ يجده، وهو ما يساعد في مرحلة الاختبار;
- يعمل البرنامج بشكل أبطأ، لأنه يتم ترجمته كل مرة يتم فيها تشغيله;
- يجب أن يكون المُفسِّر موجودًا في كل مرة يتم فيها تشغيل البرنامج.
|
المترجم |
المفسر |
| الترجمة |
البرنامج بأكمله دفعة واحدة |
سطرًا تلو الآخر |
| الأخطاء |
تُبلغ جميعها في النهاية |
يتوقف عند أول خطأ |
| سرعة البرنامج النهائي |
أسرع |
أبطأ |
| هل هو مطلوب للتشغيل لاحقاً؟ |
لا |
نعم |
استخدم المُترجم الكامل عندما ترغب في مشاركة برنامج نهائي سريع. استخدم المُفسِّر عندما تكون في مرحلة كتابة واختبار برنامج وتريد اكتشاف الأخطاء بسهولة.
المترجم
المُجمِّع يُترجم برنامج لغة تجميع منخفضة المستوى (مكتوب برموز مجزية مثل LDA، ADD) إلى كود آلة. كل رمز مجزي يصبح تعليمية واحدة من تعليمات الآلة — ترجمة مباشرة من واحد لواحد، على عكس المُترجم الكامل أو المُفسِّر الذي يعمل على الأكواد عالية المستوى.
*المُترجم الكامل يترجم البرنامج كاملاً مرة واحدة إلى كود آلة؛ بينما المُفسِّر يترجم ويشغل سطراً تلو الآخر
مثال محلول. فريق يكتب برنامجًا. أثناء تطويره، يرغبون في العثور على الأخطاء وإصلاحها بسرعة؛ وعند إطلاقه للعملاء، يرغبون في أن يعمل Program بسرعة دون رؤية الكود المصدر. أي مُترجم يناسب كل مرحلة؟ أثناء التطوير، استخدم المُفسِّر: فهو يترجم ويشغل سطراً تلو الآخر ويتوقف عند أول خطأ، مما يجعل定位 الخطأ سهلاً. للإطلاق، استخدم المُترجم الكامل: فهو يترجم البرنامج كاملاً مرة واحدة إلى كود آلة، والذي بدوره يعمل بسرعة بدون وجود المُترجم، ويستلم العميل الملف التنفيذي بدلاً من الكود المصدر. الاثنان ليسا منافسين - حدد المرحلة وقدم السبب، لأن عبارة "المُترجم الكامل أسرع" وحدها لا تمنح نقاطاً.