CSwithZain
WhatsApp Join a class

AS Level 9618 · Paper 1 · 5.2

Language Translators

How programs are translated, and the tools in an IDE. Read the notes, try the past-paper questions, then check the mark scheme.

Past papers from June 2021 to June 202619 questions · 100 marks

    In this subtopic

    • Assemblers, compilers and interpreters, and when each is used
    • Benefits and drawbacks of compilers and interpreters
    • Partial compiling and interpreting, such as Java
    • Features of a typical Integrated Development Environment (IDE)

    5.2 · 1 of 12Book page Contents

    Why translators are needed

    A processor can only execute machine code. Programs are written as source code, in a language people can read, so all source code programs must be translated into machine code before they can be executed. Assemblers, compilers and interpreters are all types of program translator, but they translate programs in different ways.

    High-level languages, such as Visual Basic, C++ and Python, are used to write programs that do not depend on the computer architecture they will run on. To go from high-level source code to executable machine code, the source code must be translated. There are two main ways to do this: compilation or interpretation.

    Whether source code is compiled or interpreted depends on the particular implementation of a programming language. Some implementations provide compilers, some provide interpreters, and some provide both.

    Low-level language code, that is, assembly language programs, also needs to be translated. The translators used for assembly language programs are called assemblers.

    TranslatorTranslatesHow
    AssemblerAssembly languageEach assembly instruction becomes one machine code instruction.
    CompilerHigh-level languageTranslates the whole program at once into an executable file.
    InterpreterHigh-level languageTranslates and executes the program one line at a time.

    5.2 · 2 of 12Book pages Contents

    Compilers

    A compiler translates source code into a form that can be executed, but the compiler does not execute the code. It produces executable code that cannot be read by a person or easily reverse-engineered (turned back into source code). Most commercial software is sold as compiled code so that the source code is protected. This helps commercial developers who make money from their code.

    The compiler takes the source code and puts it through a series of processes to produce executable code:

    Note These stages of compilation are taught at A Level, in 16.2 Translation software.

    Any compilation errors are reported at the end of the compilation process. This is a disadvantage for large programs, because errors may be linked to each other. If errors are found, the program has to be corrected and compiled again, and this is repeated until the program is free of errors.

    Once the program has compiled successfully, the executable file does not need to be translated again (unless the source code is changed). This makes it run faster, because no more translation is needed, which is a particular advantage for programs where performance is critical, such as fast-moving games.

    Another advantage of a compiler is that the user does not need a translator (an interpreter) installed on their computer, because they never need to translate the program themselves. However, a Visual Basic program compiled to run on an Intel processor, for example, would need to be recompiled to run on an Apple processor.

    For a compiler, these steps apply:

    1. The compiler program and the source code file are made available, but no data is needed.
    2. The compiler program begins execution.
    3. The first line of the source code is read.
    4. The line is analysed.
    5. If an error is found, this is recorded.
    6. If no error is found, the line of source code is converted to an intermediate code.
    7. The next line of source code is read and steps 4 to 7 are repeated.
    8. When the whole of the source code has been dealt with, one of two things happens. If no error has been found in the whole source code, the complete intermediate code is converted into object code. If any errors have been found, a list of them is output and no object code is produced.

    The program can only be executed once compilation has shown no errors. This can happen straight away under the control of the compiler, if data for the program is available. Otherwise the object code is stored, and the program is executed later with no involvement of the compiler.

    5.2 · 3 of 12Book pages Contents

    Interpreters

    Builds on

    An interpreter translates and executes each line of source code, one line at a time. Each program line, when it is run, is first syntax-checked. If a syntax error is found, such as a missing bracket, the program stops and the error is reported. After the syntax check, the interpreter converts the line into its machine code equivalent and executes it. If a run-time error happens, the program crashes.

    This line-by-line approach lets the developer test the program and quickly find and fix each error as it happens, without translating the whole program every time. So interpreters are very useful while a program is being developed.

    Interpreted source code runs more slowly than compiled code, because it must be translated all over again every time the program is run. Also, the user must have the interpreter installed to run the program. However, interpreted code helps if the programmer does not know which platform will run the code. For example, JavaScript code is interpreted: the translation is done by the browser on the user’s computer.

    For an interpreter, these steps apply:

    1. The interpreter program, the source code file and the data to be used by the source code program are all made available.
    2. The interpreter program begins execution.
    3. The first line of the source code is read.
    4. The line is analysed.
    5. If an error is found, it is reported and the interpreter program stops execution.
    6. If no error is found, the line of source code is converted to an intermediate code.
    7. The interpreter program uses this intermediate code to execute the required action.
    8. The next line of source code is read and steps 4 to 8 are repeated.

    Compiler and interpreter in one line each

    • A compiler translates the whole program, reports all errors at the end, and produces an executable file that runs without the compiler.
    • An interpreter translates and runs one line at a time, stops at the first error, and is needed every time the program runs.

    5.2 · 4 of 12Book pages Contents

    Benefits and drawbacks of compilers and interpreters

    Whether a compiler or an interpreter is used depends on the programming language. For example, PHP programs (used to create dynamic web pages) are interpreted. When choosing between a language that can be compiled and one that can be interpreted, the programmer should weigh up the benefits and drawbacks of each.

    Compilers: benefits

    • The code can be well optimised by the compiler.
    • Compiled programs can be given out as machine code, so the source code is hidden and can be kept secret for commercial reasons.
    • No translation is needed when the program runs, because it was done when the source code was compiled.

    Compilers: drawbacks

    • You have to wait until the whole program has been compiled before you see any errors, which can take some time for a large program.
    • If you want to change anything in the program, you have to recompile the whole program.
    • The executable code produced only runs on one type of processor.

    Interpreters: benefits

    • Errors are reported as soon as they are found, so you do not have to wait for the whole program to be translated.
    • Errors are shown line by line as the interpreter reaches them.
    • Debugging is quicker and easier, because errors are reported in the console, which shows what the error is and where it is in the code.

    Interpreters: drawbacks

    • The program needs an interpreter to run.
    • Translating the code line by line while it runs is slower than running compiled machine code.
    • The interpreter takes up space in memory.

    5.2 · 5 of 12Book pages Contents

    Choosing a compiler or an interpreter

    These tables give the reasons to choose each translator, comparing the two side by side.

    Why a compiler should be used rather than an interpreter:

    CompilerInterpreter
    Translator the user needsThe end user only needs the executable code, so they do not need to buy a compiler to translate the program before using it.The end user needs a translator (an interpreter or a compiler) to translate the source code before it can be used.
    Control of the source codeThe developer keeps the source code, so the end user cannot change or extend it, and the developer can charge for upgrades and alterations.The developer gives up control of the source code, so it is harder to charge for upgrades and alterations. Because end users can see the source code, they could use the developer’s intellectual property.
    Speed when runningCompiled programs take less time to execute, because translation has already been done and the machine code may have been optimised by the compiler.An interpreted program can take longer to execute than the same program compiled, because each line of the source code is translated before it is executed, every time the program is run.
    Errors left in the programCompiled programs have no syntax or semantic errors.Interpreted programs may still contain syntax or semantic errors if any part of the program has not been fully tested; these errors will need to be debugged.
    Running on another computerThe source program can be translated on one type of computer and then executed on another type, as long as the compiler produced the object code for that computer’s processor.Interpreted programs cannot be interpreted on one type of computer and run on another type of computer.

    Why an interpreter should be used rather than a compiler:

    CompilerInterpreter
    Finding errorsA compiler finds all the errors in a program. One error can make the compiler report other, dependent errors later in the program that will not be errors once the first one is corrected. So the number of errors reported may be more than the real number.It is easier to develop and debug a program with an interpreter, because errors can be corrected on each line and the program restarted from that place, so the programmer can easily learn from each error.
    Crashes while testingUntested programs with errors may cause the computer to crash.Untested programs should not be able to cause the computer to crash.
    Seeing partial resultsThe developer has to write special routines to see partial results during development, which makes it harder to judge the quality of particular sections of code.Partial results can be seen during development, so the developer can make informed decisions about a section of code, for example whether to keep it, change it, or scrap it and start again.
    Changes by the end userEnd users do not have the source code or the run-time libraries, so they cannot make changes and rely on the developer for updates and alterations.If an interpreted program is bought, end users have all the source code and the run-time libraries, so they can change the program as needed without buying anything more.

    Interpreter while writing, compiler when finished

    • While the program is being written, use an interpreter: incomplete code can be tested, it stops at the first error, and errors can be corrected in real time with the effect seen immediately.
    • When the program is complete, use a compiler: it produces an executable file that can be tested and run many times without translating again, users do not need a translator, and the source code is not given away.

    5.2 · 6 of 12Book pages Contents

    Partly compiled, partly interpreted: Java

    Some high-level languages are partially compiled and partially interpreted. Most interpreted languages now use bytecode as an intermediate stage to speed up translation. The source code is compiled to produce bytecode (a low-level code), which is then interpreted (translated and executed) by a virtual machine.

    Java works like this in console mode. A Java program is first compiled into Java bytecode. The bytecode is not the machine code of any real processor. When the program is run, the Java Virtual Machine on the user’s computer interprets the bytecode for that computer’s processor.

    Why some languages are partially compiled and partially interpreted

    • The same compiled bytecode can be used on different platforms, because it is interpreted for each processor when it runs.
    • It is more efficient to run: the source code does not need recompiling for each platform, and running the bytecode is faster than interpreting the source code line by line.
    • The machine code can be optimised for the processor, because it is generated when the program runs.

    5.2 · 7 of 12Book page Contents

    Assemblers

    An assembler translates assembly code programs into executable code. Because assembly code is specific to a type of processor, assemblers are specific too.

    There is a one-to-one relationship between assembly code and machine code: each assembly instruction becomes one machine code instruction. So, compared with compiling high-level source code, assembling is a fairly simple translation. Any comments in an assembly code program are removed by the assembler, and symbolic references such as labels are replaced with actual addresses.

    5.2 · 8 of 12Book page Contents

    Integrated development environment (IDE)

    An integrated development environment (IDE) is used by programmers to help them write and develop programs. There are many different IDEs: some support just one programming language, others can be used for several. NetBeans, PyCharm, Visual Studio and SharpDevelop are all IDEs in use today.

    IDEs usually have:

    PurposeIDE features
    CodingContext-sensitive prompts, text completion
    Initial error detectionDynamic syntax checks
    PresentationPrettyprint, expand and collapse code blocks
    DebuggingSingle stepping, breakpoints, report window showing variables and expressions

    5.2 · 9 of 12Book pages Contents

    Source code editor

    Builds on

    ASProgram testing and maintenance12.3: logic error, syntax error

    A source code editor lets a program be written and edited without using a separate text editor. An integrated source code editor speeds up development, because editing can be done without switching to a different piece of software every time the program needs correcting or adding to.

    • Prettyprint: most source code editors colour code the words in the program and lay the program out in a meaningful way, such as indenting code blocks. This is called prettyprinting.
    • Context-sensitive prompts: as the code is typed, the editor suggests ways to complete it, with text completion for variable names and reserved words.
    • Dynamic syntax checks: the editor checks the syntax as the code is typed.

    The figures show these features in the PyCharm source code editor.

    PyCharm editor with a Python program that prints a pyramid. On line 13 the programmer has typed the letter p, and a pop-up list suggests print, pySize, pass, pip, pow, property and other words starting with p.
    Context-sensitive prompts in PyCharm: suggestions to complete what is being typed

    Dynamic syntax checking finds possible syntax errors while the program code is being typed into the source code editor, and alerts the programmer straight away, before the program is translated and run. So many errors can be found and corrected while the program is being written and edited, before it is run. Logic errors can only be found when the program is run.

    Line 6 of the program, for i in range(1, pySize+1), is missing its colon at the end. The editor underlines the end of the line and shows a message saying a colon is expected.
    Dynamic syntax checking: the missing colon is flagged as it is typed

    Expand and collapse code blocks: in larger programs with more than one code block, some code blocks can be collapsed to a single line in the editor, so the programmer sees only the blocks being worked on. A collapsed block can be expanded again to show all its code.

    Practise this: past-paper questions

    Show 5 more

    5.2 · 10 of 12Book page Contents

    Compilers and interpreters in an IDE

    Most IDEs provide a compiler, an interpreter or both, to run the program. The interpreter is often used while developing the program, and the compiler to produce the final version of the object code.

    With PyCharm there can be more than one interpreter, for different versions of the Python language. The program results are shown in the run-time environment provided.

    PyCharm with the pyramid program in the editor and the Run panel below it. The program asks for the size of the pyramid, the user enters 3, and the output is a pyramid of 1, 3 and 5 hash symbols, followed by Process finished with exit code 0.
    The program run in PyCharm, with its output in the run-time environment

    5.2 · 11 of 12Book page Contents

    Run-time environment with a debugger

    A debugger is a program that runs the program being developed and helps with debugging (finding and correcting errors). It lets the programmer:

    • single step through the program, running it one line at a time (single stepping), so the effect of each line can be seen
    • set a breakpoint, which stops the program running at a chosen line of the source code.

    A report window then shows the contents of the variables, and the values of expressions, at that point in the program. This lets the programmer look for logic errors and check that the program works as intended.

    PyCharm debugger. Red breakpoint dots are set on lines 9 and 12, and the program has stopped at line 9. The variables pane below shows i = 1, pySize = 3, spaceNum = 2 and spaces = 0, each with its type int.
    The program stopped at a breakpoint, with the report window showing each variable

    Each variable used is shown in the report window together with its data type and its contents at that point in the program. Answers to calculations and other expressions can also be shown.

    Debugging features of an IDE

    • Single stepping: runs the program one line at a time, so the effect of each line can be checked.
    • Breakpoint: stops the program at a set line, so the values at that point can be checked.
    • Report window (variable watch window): shows the values of variables and expressions and how they change as the program runs.

    Practise this: past-paper questions

    Show 6 more

    5.2 · 12 of 12Book page Contents

    Auto-documenter

    Most IDEs provide an auto-documenter, which explains the function and purpose of programming code.

    The cursor is on the word for in line 6. A pop-up box titled The for statement explains what a for loop does, gives its syntax, and shows a short example.
    The auto-documenter explaining the for statement

    5.2

    Concepts in 5.2

    Every idea explained in 5.2, and where else in the notes it is used.

    ConceptIn one lineAlso used in
    assemblerA translator that turns assembly language into machine code, one instruction for one.only here
    auto-documenterAn IDE tool that explains the function and purpose of code.5.2 Integrated development environment (IDE)
    breakpointA marked line where the program stops running so values can be checked.5.2 Integrated development environment (IDE)
    bytecodeAn intermediate low-level code, not for any real processor, interpreted by a virtual machine.only here
    compilerA translator that turns the whole program into an executable file, reporting all errors at the end.5.2 Why translators are needed, 5.2 Interpreters, 5.2 Benefits and drawbacks of compilers and interpreters, 5.2 Choosing a compiler or an interpreter, 5.2 Integrated development environment (IDE), 5.2 Compilers and interpreters in an IDE
    context-sensitive promptsSuggestions to complete code, shown as it is typed.5.2 Integrated development environment (IDE)
    debuggerA program that runs the code under test with tools such as breakpoints and single stepping.5.2 Integrated development environment (IDE)
    dynamic syntax checkingHighlighting syntax errors as the code is typed, before the program is run.5.2 Integrated development environment (IDE)
    executable file (object code)The machine code a compiler produces, which runs without the compiler.5.1 Dynamic link libraries (DLLs), 5.2 Why translators are needed, 5.2 Interpreters, 5.2 Benefits and drawbacks of compilers and interpreters, 5.2 Choosing a compiler or an interpreter, 5.2 Assemblers
    expand and collapse code blocksHiding a block of code as one line, or showing it again, to focus on one part.5.2 Integrated development environment (IDE)
    high-level languageA programming language close to English that does not depend on the processor, such as Python.4.2 Characteristics of low-level languages, 4.2 Advantages and disadvantages of low-level languages, 5.1 Program libraries, 5.2 Partly compiled, partly interpreted: Java
    integrated development environment (IDE)Software with an editor, translators, a debugger and other tools to help write programs.5.2 Compilers and interpreters in an IDE, 5.2 Run-time environment with a debugger, 5.2 Auto-documenter
    interpreterA translator that translates and executes a program one line at a time.5.2 Why translators are needed, 5.2 Compilers, 5.2 Benefits and drawbacks of compilers and interpreters, 5.2 Choosing a compiler or an interpreter, 5.2 Integrated development environment (IDE), 5.2 Compilers and interpreters in an IDE
    low-level languageA language close to machine code, such as assembly language.only here
    partially compiled and partially interpretedCompiled to bytecode first, then the bytecode is interpreted when run, as in Java.only here
    prettyprintColour coding and laying out code, such as indenting blocks, so it is easy to read.5.2 Integrated development environment (IDE)
    program translatorA program that turns source code into machine code: an assembler, compiler or interpreter.only here
    report windowA window showing the values of variables and expressions as the program runs.5.2 Integrated development environment (IDE)
    run-time environmentThe part of an IDE that runs the program and shows its results.5.2 Integrated development environment (IDE)
    single steppingRunning a program one line at a time so the effect of each line can be seen.5.2 Integrated development environment (IDE)
    source codeA program as the programmer wrote it, in a high-level or assembly language.4.2 Assembly language and machine code, 4.2 Assemblers: single pass and two pass, 5.2 Compilers, 5.2 Interpreters, 5.2 Benefits and drawbacks of compilers and interpreters, 5.2 Choosing a compiler or an interpreter, 5.2 Partly compiled, partly interpreted: Java, 5.2 Assemblers, 5.2 Run-time environment with a debugger
    source code editorThe part of an IDE where program code is typed and edited.5.2 Integrated development environment (IDE)

    When you want more than reading

    The notes are the front door. The class is where it clicks.

    Join a class →Ask on WhatsApp