📑 Contents

Chapter 5.2: Language Translators

9618 AS Computer Science

📚 Learning Objectives
🌟 Did You Know?

Language Translators are programs that translate source code written in Assembly Language or High-Level Language into object code (machine language). Without translators, programmers would have to write programs directly in binary (0s and 1s), which would be extremely time-consuming and error-prone!

Language Translators Overview Source Code TRANSLATOR • Assembler • Compiler • Interpreter • Mixed Mode Object Code

1. What is a Language Translator?

📖 Definition

A language translator is a program that translates program source code into machine code so that it can be executed directly by a processor.

There are three main types of language translators:

Translator Type Source Language Purpose
Assembler Assembly Language (Low-level) Translates mnemonics into machine code
Compiler High-Level Language Translates entire program before execution
Interpreter High-Level Language Translates and executes line by line

1.1 Why Do We Need Translators?

Writing a program directly in machine code (binary) would:

💡 Exam Tip

Remember: Low-level languages (like assembly) are translated by assemblers, while high-level languages (like Python, Java) are translated by compilers or interpreters.

Translation Process Overview Programmer High-Level Code (Python, Java) Input TRANSLATOR Converts to machine code Output Machine Code (0s and 1s) CPU

2. Assembler Software

📖 What is an Assembler?

An assembler is a software that converts assembly language code into machine code. It translates mnemonics (symbolic representations) into binary instructions that the processor can execute.

2.1 What is Assembly Language?

Assembly language is a low-level language that gives instructions to processors for different tasks. It is specific to any processor and uses:

Example: ADD A, B
Here, ADD is the mnemonic (tells processor to add), A and B are the operands. Other mnemonics include SUB, MUL, DIV, MOV, etc.
⚠️ Important

Assembly language programs are machine dependent - they are not portable from one type of computer/processor to another. Each processor architecture has its own assembly language.

2.2 Types of Assemblers

📝 One-Pass (Single-Pass) Assembler
📝 Two-Pass (Multi-Pass) Assembler

Pass 1:

Pass 2:

2. Assembler (Continued)

2.3 Important Tables in Assembly

📖 Opcode Table

Stores the value of mnemonics and their corresponding numeric values. Example:

Mnemonic Binary Opcode
ADD 00000001
SUB 00000010
MOV 00000011
📖 Symbol Table

Stores symbolic names (labels) used by the programmer and their corresponding memory addresses. Example:

Symbol Address
START 1000
LOOP 1015
END 1050
📖 Location Counter

Stores the address of the location where the current instruction will be stored. It increments as the assembler processes each instruction.

2.4 Forward Reference Problem

⚠️ Forward Reference Problem

Rules for assembly programs state that a symbol should be defined somewhere in the program. However, in some cases a symbol may be used prior to its definition. Such a reference is called a forward reference.

Due to this, the assembler cannot translate instructions immediately, creating the forward reference problem. This is why two-pass assemblers are needed - the first pass builds the symbol table, and the second pass resolves all references.

Two-Pass Assembler Process Source Code START: MOV A, 5 LOOP: ADD A, B JMP LOOP HLT Pass 1 PASS 1 • Build Symbol Table • Record locations • Process directives Symbol Table START→1000 LOOP→1005 Pass 2 PASS 2 • Use Symbol Table • Resolve references • Generate code Object Code 01110000 00000101 00000001 01000100 ...

3. Compiler

📖 What is a Compiler?

A compiler is a language translator that translates the entire source program written in a high-level language into object code in machine language before execution. The object code is saved as an executable file.

3.1 How a Compiler Works

📝 Steps in Compilation
  1. Compiler program and source code file are made available (no data needed yet)
  2. Compiler begins execution and reads the first line of source code
  3. Line is analyzed. If an error is found, it is recorded
  4. If no error is found, the line is converted to intermediate code
  5. Next line of source code is read
  6. When the whole source code has been processed:
    • If no errors: complete intermediate code is converted into object code
    • If errors found: list of errors is output, no object code produced
  7. Object code is stored and can be executed later without the compiler

3.2 Advantages of Compilers

Advantage Explanation
Speed of execution Compiled programs run faster as translation is done in advance
Code optimization Compiler can optimize the code for better performance
Source code protection Users do not receive the source code, only executable
Distribution Executable file can be distributed without compiler
No runtime translation Compiler not needed at runtime

3.3 Disadvantages of Compilers

Disadvantage Explanation
Memory intensive Can require significant memory during compilation
Difficult debugging Harder to test sections; errors shown after full compilation
Recompilation required Any changes mean the program must be recompiled
Platform specific Compiled code is designed for one specific processor

4. Interpreter

📖 What is an Interpreter?

An interpreter is a language translator that translates source code written in high-level language into object code during step-by-step execution of the program. No executable file of machine code is produced.

4.1 How an Interpreter Works

📝 Steps in Interpretation
  1. Interpreter program, source code file, and data are all made available
  2. Interpreter starts execution and reads the first line of source code
  3. Line is analyzed for syntax errors
  4. If an error is found, it is reported and interpreter halts
  5. If no error, line is converted to intermediate code
  6. Interpreter uses this intermediate code to execute the required action
  7. Next line of source code is read and the process repeats
⚠️ Key Difference

Interpreters do NOT generate machine code directly. Instead, they call appropriate machine code subroutines to execute each statement.

4.2 Advantages of Interpreters

Advantage Explanation
Easier debugging Stops when it finds a specific syntax error, showing exact location
Immediate feedback Errors can be corrected and processing continue from where it stopped
Less memory Requires less RAM to process the code
Development friendly Ideal for program development stage
Platform independent Same source code can run on any machine with interpreter

4.3 Disadvantages of Interpreters

Disadvantage Explanation
Slower execution Each line is translated every time the program runs
Source code required Source code must be distributed to users
No optimization Code is executed as-is, no optimization performed
Interpreter needed Interpreter must be available each time program runs

5. Compiler vs Interpreter Comparison

Compiler vs Interpreter Comparison COMPILER Source Translate ALL at once Executable File INTERPRETER Source Line 1 → Execute Line 2... Output
Aspect Compiler Interpreter
Translation Entire program at once One line at a time
Speed of execution Fast (pre-translated) Slow (translate each run)
Error detection After full compilation One at a time, immediate
Source code Not distributed to users Must be distributed
Executable file Yes, created No executable produced
Debugging More difficult Easier, immediate feedback
Memory needed More memory intensive Less memory required
Distribution Only executable needed Interpreter + source needed
Optimization Code can be optimized No optimization
Best use Finished, distributed programs Program development stage
🧠 Memory Trick

Compiler = Complete (translates entire program at once, Creates executable)

Interpreter = Immediate (translates line by line, shows errors Immediately)

6. Mixed Mode Translation

📖 What is Mixed Mode Translation?

Mixed mode translation combines features of both a compiler and an interpreter. The program is partially compiled into an intermediate form (bytecode), which is then interpreted at runtime.

6.1 How Mixed Mode Translation Works

📝 Steps in Mixed Mode Translation
  1. Source code is compiled into intermediate code (not full machine code)
  2. This intermediate code (e.g., bytecode) is interpreted by a virtual machine
  3. Optionally, some parts may later be Just-In-Time (JIT) compiled for better performance

6.2 Java Example

Java uses mixed mode translation:
Java Mixed Mode Translation Java Source .java Compile javac Compiler Output Bytecode .class (Portable) Interpret JVM • Windows • Mac OS • Linux Platform Independent Execution!

6.3 Comparison Table

Feature Compiler Interpreter Mixed Mode
Translation Entire program Line at a time To intermediate code
Speed Fastest Slowest Medium (JIT helps)
Portability Not portable Not portable Highly portable
Examples C, C++ Python, JavaScript Java, C#
💡 Exam Tip

When asked about Java's portability, mention: Java source code is compiled to bytecode, which is platform-independent. Any machine with a JVM can run the same bytecode file - this is "Write Once, Run Anywhere".

7. Integrated Development Environment (IDE)

📖 What is an IDE?

An Integrated Development Environment (IDE) is a software application that provides comprehensive facilities to programmers for software development. It normally consists of at least a source code editor, build automation tools, and a debugger.

7.1 Features for Coding

📝 Context-Sensitive Prompts
📝 Prettyprinting (Syntax Highlighting)
Example of Syntax Highlighting:
def calculate_sum(a, b):
# This is a comment
return a + b

7.2 Features for Error Detection

📝 Dynamic Syntax Checks

7. IDE Features (Continued)

7.3 Features for Presentation

📝 Expanding and Collapsing Code Blocks

7.4 Features for Debugging

📖 What is Debugging?

Debugging is the process of finding and correcting errors (bugs) in a program. A debugger is a program that runs the program under development and aids the debugging process.

📝 Single Stepping
📝 Breakpoints
📝 Variable Watch Window
📝 Report Window
IDE Debugging Process CODE WINDOW 1: START 2: x = 10 3: y = 20 → 4: z = x + y 5: PRINT z ↑ Breakpoint at line 3 WATCH WINDOW x = 10 y = undefined z = undefined CONTROLS Step Continue Stop OUTPUT Waiting at breakpoint...

7. Additional IDE Features

7.5 Other Important IDE Features

📝 Source Code Editor
📝 Auto Indent
📝 Error Message List
📝 Stack Contents
📝 Auto-documenter
📝 Crash Dump/Post-Mortem Report
📌 IDE Features Summary
Category Features
Coding Context-sensitive prompts, Autocomplete, Auto indent
Error Detection Dynamic syntax checks, Error highlighting
Presentation Prettyprinting, Expand/collapse code blocks
Debugging Single stepping, Breakpoints, Watch window, Report window

8. Glossary

📖 Key Terms

Assembler → Translates assembly language (low-level) into machine code

Assembly Language → Low-level language using mnemonics to represent machine instructions

Bytecode → Intermediate code produced by partial compilation (e.g., Java .class files)

Compiler → Translates entire high-level language program into machine code before execution

Debugging → Process of finding and correcting errors in a program

Breakpoint → Marker in code where program execution pauses during debugging

IDE → Integrated Development Environment - software for writing, editing, and debugging code

Interpreter → Translates and executes high-level language line by line

JIT Compilation → Just-In-Time compilation - compiling code during execution for better performance

Machine Code → Binary instructions (0s and 1s) directly executable by the processor

Mnemonic → Symbolic representation of a machine instruction (e.g., ADD, MOV, SUB)

Object Code → Output from a translator - machine code ready for execution

Opcode → Operation code - the part of machine instruction specifying the operation

Prettyprinting → Automatic formatting and syntax highlighting of code

Single Stepping → Executing program one line at a time for debugging

Source Code → Program written in high-level or assembly language

Symbol Table → Table storing symbolic names and their corresponding addresses/values

Translator → Program that converts source code into machine code

Two-Pass Assembler → Assembler that scans source code twice to build symbol table and generate code

Virtual Machine → Software that simulates a computer, allowing bytecode execution (e.g., JVM)

9. Exam-Style Questions

1. Describe the difference between a compiler and an interpreter. [4 marks]

Answer:

  • A compiler translates the entire source code into machine code before execution, creating an executable file
  • An interpreter translates and executes source code line by line, without producing an executable file
  • Compiled programs run faster but require recompilation after changes
  • Interpreted programs run slower but are easier to debug and develop
  • Compilers show all errors after compilation; interpreters stop at first error
  • Additional point: Compilers are used for finished programs; interpreters are used during development
2. Explain the purpose of a symbol table in a two-pass assembler. [4 marks]

Answer:

  • A symbol table stores symbolic names (labels) used in the assembly program
  • It records the corresponding memory address for each symbol/label
  • Built during the first pass of the assembler
  • Used in the second pass to resolve forward references
  • Allows symbolic addresses to be replaced with absolute addresses
  • Additional point: Helps handle labels like LOOP, START, END by mapping them to addresses
3. Jennifer uses an IDE to write her computer program. The IDE allows her to use both an interpreter and a compiler. Describe how Jennifer can use both a compiler and an interpreter while developing the program. [4 marks]

Answer:

  • Using Interpreter: Use an interpreter while writing the program to test/debug the partially completed program
  • Errors can be corrected and processing continue from where execution stopped
  • Errors are identified one at a time, making debugging easier
  • Using Compiler: Use the compiler after the program is complete
  • To create an executable file for distribution
  • To test the completed section repeatedly without re-interpreting every time
  • Additional point: Compiler not needed at runtime, only the executable
4. Explain what is meant by mixed mode translation, using Java as an example. [5 marks]

Answer:

  • Mixed mode translation combines compilation and interpretation
  • Source code is partially compiled into intermediate code (bytecode)
  • This bytecode is not specific to any processor/platform
  • Bytecode is then interpreted by a Virtual Machine (JVM for Java)
  • JVM can be installed on any platform (Windows, Mac, Linux)
  • Same bytecode file can run on any machine with JVM installed
  • Additional point: JIT compilation can further improve performance by compiling frequently-used bytecode to machine code
5. Describe two features of an IDE that help with debugging. [4 marks]

Answer:

  • Breakpoints: Markers set at specific lines where execution pauses, allowing inspection of variable values at that point
  • Single Stepping: Execute program one line at a time to identify exact location of errors
  • Watch Window: Monitor values of specific variables during execution to see how they change
  • Report Window: Shows contents of variables and expressions at breakpoints
  • Additional point: Stack contents shows order of function calls and local variables
  • Additional point: Error message list displays detailed error descriptions with code locations

9. Exam-Style Questions (Continued)

6. Explain why assembly language programs are described as "machine dependent". [3 marks]

Answer:

  • Assembly language uses mnemonics that are specific to a particular processor
  • Each processor architecture has its own set of instructions and mnemonics
  • Programs written for one processor cannot run on a different processor architecture
  • They are not portable from one type of computer/chip to another
  • Additional point: For example, x86 assembly differs from ARM assembly
7. Describe the difference between a one-pass and a two-pass assembler. [4 marks]

Answer:

  • One-pass assembler performs the entire conversion in a single scan
  • Puts machine code instructions straight into computer memory
  • Cannot easily handle forward references (symbols used before definition)
  • Two-pass assembler scans the source code twice
  • First pass builds symbol table with labels and addresses
  • Second pass resolves all references and generates final machine code
  • Additional point: Two-pass is more flexible and handles forward references properly
8. Discuss the advantages and disadvantages of using a compiler compared to an interpreter for distributing a program to users. [6 marks]

Answer:

Compiler Advantages:

  • Users only need the executable file, not the compiler or source code
  • Source code is not distributed, protecting intellectual property
  • Executables run faster as translation is already done
  • No extra setup or cost for users (no need for translation software)

Compiler Disadvantages:

  • Compiled code is platform-specific, may need different versions for different systems
  • Could potentially contain viruses (users can't verify source)

Interpreter Advantages:

  • Users have access to source code and can modify or extend the program
  • Same source code can run on any machine with an interpreter

Interpreter Disadvantages:

  • Users need the interpreter to run the program
  • Slower execution as code is translated each time
  • Developers lose control over code (can't charge for upgrades easily)
9. Describe two features of an editor in an IDE that can help a programmer to write program code. [4 marks]

Answer:

  • Feature 1: Context-sensitive prompts/autocomplete
  • Displays hints or choice of keywords appropriate at current insertion point
  • Shows available identifiers and functions that might be appropriate
  • Feature 2: Prettyprinting/Syntax highlighting
  • Automatically colour-codes keywords, strings, comments differently
  • Automatic indentation for code blocks
  • Feature 3: Dynamic syntax checks
  • Highlights syntax errors as code is being typed
  • Additional point: Code blocks can be expanded/collapsed to manage large programs
10. Explain what is meant by the "forward reference problem" in assembly language and how it is resolved. [4 marks]

Answer:

  • The forward reference problem occurs when a symbol is used before it is defined in the program
  • For example, jumping to a label that appears later in the code
  • The assembler cannot immediately translate this instruction because the address is unknown
  • Resolution: A two-pass assembler is used
  • First pass scans the code and builds a symbol table with all labels and their addresses
  • Second pass uses this table to resolve all forward references and generate machine code
  • Additional point: Single-pass assemblers cannot easily handle forward references

10. Exam Success Tips (Part 1)

💡 Translator Types - Quick Reference
💡 Compiler vs Interpreter - Key Differences
🧠 Memory Tricks
💡 IDE Features by Category
❌ Common Mistakes to Avoid

10. Exam Success Tips (Part 2)

⚠️ Key Points for Exam Questions

When asked about Java's portability:

⚠️ When asked about Two-Pass Assembler
💡 Answer Structure Tips
🌟 Quick Reference Table
Topic Key Point
Assembler Assembly language → Machine code (low-level)
Compiler Entire program at once → Executable file
Interpreter Line by line → No executable, immediate errors
Mixed Mode Bytecode + Virtual Machine (Java)
Symbol Table Labels → Addresses (Pass 1 of assembler)
Forward Reference Using a symbol before it's defined
IDE Debugging Breakpoints, Single stepping, Watch window
📌 Final Exam Reminders