0% found this document useful (0 votes)
15 views30 pages

Systems Software T4 Programming Language Translators

The document outlines the roles and differences between assemblers, compilers, and interpreters in programming language translation. It details the stages of compilation, including lexical analysis, syntax analysis, code generation, and optimization, as well as the functions of linkers and loaders. Additionally, it discusses the use of libraries and the concept of bytecode as an intermediate step in the compilation process.

Uploaded by

bsawyer873
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views30 pages

Systems Software T4 Programming Language Translators

The document outlines the roles and differences between assemblers, compilers, and interpreters in programming language translation. It details the stages of compilation, including lexical analysis, syntax analysis, code generation, and optimization, as well as the functions of linkers and loaders. Additionally, it discusses the use of libraries and the concept of bytecode as an intermediate step in the compilation process.

Uploaded by

bsawyer873
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Programming

OCR language
A Level translators
Unit 2
Computer
Systems software
Science and applications
H446 – Paper 1 generation

4
Objectives
• Understand the role of an assembler, compiler and interpreter
• Explain the difference between compilation and interpretation,
and describe situations when each would be appropriate
• Explain why an intermediate language such as bytecode is
produced as the final output by some compilers and how it is
subsequently used
• Describe the stages of compilation: lexical analysis, syntax
analysis, code generation and optimisation
• Describe the function of linkers and loaders
• Describe the use of libraries
Programming language translators
Unit 2 Systems software and applications generation

Assembly code
• Computers execute machine code
• It is difficult for humans to read, write and debug
machine code
• A machine code instruction might look like this: 01000101

• Assembly code instructions are equivalent to


machine code but easier for humans to work with
• An assembly code instruction might look like this: LDA 5
Programming language translators
Unit 2 Systems software and applications generation

Assembler
• Assembly code is a low level language
• Translating assembly code instructions into machine
code is done by an assembler
• Each processor has its own instruction set and so
the object code produced will be hardware specific
Programming language translators
Unit 2 Systems software and applications generation

Compiler
• A compiler translates a whole program written in a
high level language into executable machine code,
going through several stages
• Compiled high level languages include Visual Basic and C++

• The resulting machine code is called object code


• The object code produced is also hardware specific
Programming language translators
Unit 2 Systems software and applications generation

Interpreter
• An interpreter also translates code written in a high
level language into machine code
• Interpreted high level languages include JavaScript and PHP

• However, the interpreter does this line by line rather


than translating the whole program before any of it
can be executed
Programming language translators
Unit 2 Systems software and applications generation

Compiler vs Interpreter
• What do you think the advantages might be?

Compiler Interpreter
Programming language translators
Unit 2 Systems software and applications generation

Compiler vs Interpreter
• What do you think the advantages might be?

Compiler Interpreter
Program can be run many times Source code can be run on any
without the need to recompile machine with the interpreter

If a small error is found, no need


Faster to execute
to recompile the entire program

Executable code does not require


the interpreter to run

Compiled code cannot be easily


read and copied by others
Programming language translators
Unit 2 Systems software and applications generation

Bytecode
• Most languages are not solely compiled or
interpreted – they use a combination of both
• For example, Java is compiled into bytecode which
is an intermediate step between source code and
machine code
• The bytecode is interpreted by a bytecode
interpreter, for example the Java virtual machine
Programming language translators
Unit 2 Systems software and applications generation

Worksheet 4
• Do the questions in Task 1 on the worksheet
Programming language translators
Unit 2 Systems software and applications generation

• Lexical Analysis
• This is the first stage of compiling code. The
compiler scans your program and breaks it into
smaller parts called tokens (like keywords, variable
names, and symbols). It also removes any
unnecessary spaces and comments.
Programming language translators
Unit 2 Systems software and applications generation

• Syntax Analysis
• In this stage, the compiler checks if the code follows
the correct syntax rules (like grammar in a
language). If there’s a mistake, such as a missing
bracket, it generates an error for the programmer to
fix.
Programming language translators
Unit 2 Systems software and applications generation

• Code Generation
• Once the syntax is correct, the compiler translates
the high-level code into machine code (binary) so
the computer can understand and execute it. This is
the final step in creating a working program.
Programming language translators
Unit 2 Systems software and applications generation

Stages of compilation
• A compiler goes through several stages to convert
source code to object code
• Lexical analysis
• Symbol table
• Syntax analysis
• Semantic analysis
• Code generation
Programming language translators
Unit 2 Systems software and applications generation

Lexical analysis
• All unnecessary spaces and all comments
are removed
• Keywords (e.g. print), constants and identifiers are
replaced with tokens representing their function in
the program
Programming language translators
Unit 2 Systems software and applications generation

Lexical analysis
• For example look at the following code:
age = 17
print ( age )
• This might produce the following tokens:
<identifier> <operator> <number>
<keyword> <open_bracket> <identifier>
<close_bracket>
Programming language translators
Unit 2 Systems software and applications generation

Symbol table
• The lexer will build up a symbol table for every
keyword and identifier in the program
• The symbol table helps to keep track of the run-time
memory address for each identifier

Item name Kind of item Type of item Value Pointer

age (memory address)


=
Programming language translators
Unit 2 Systems software and applications generation

Syntax analysis
• The stream of tokens from the lexing stage is split up
into phrases
• Each phrase is parsed which means it is checked
against the rules of the language
• If the phrase is not valid, an error will be recorded
• For example, this sequence of tokens may not be
valid and this would be picked up by syntax analysis
<number> <operator> <identifier>
(e.g. the source code might be 5 = a)
Programming language translators
Unit 2 Systems software and applications generation

Syntax rules
• The rules of the language need to be defined
• Syntax rules can be drawn as a syntax diagram
• Can you give an example of a word in this language?

Letter Word
Programming language translators
Unit 2 Systems software and applications generation

Worksheet 4
• Complete Task 2 on Worksheet 4
Programming language translators
Unit 2 Systems software and applications generation

Semantic analysis
• It is possible to create a sequence of tokens which is
valid syntax but is not a valid program
• Semantic analysis checks for this kind of error
• For example this phrase may be valid syntax:
<if> <identifier> <operator>
<number>
(e.g. the source code might be: if a > 5 )
• …however if the identifier has not previously been
declared then semantically it is not a valid program
Programming language translators
Unit 2 Systems software and applications generation

Code generation
• Once the program has been checked, the compiler
generates the machine code
• It may do this in several ‘passes’ over the code
because code optimisation will also take place
Programming language translators
Unit 2 Systems software and applications generation

Code optimisation
• Sometimes source code is written inefficiently
• Code optimisation aims to
• Remove redundant instructions
• Replace inefficient code with code that achieves the same
result but in a more efficient way
• Can you think of any disadvantages that might result
from code optimisation?
Programming language translators
Unit 2 Systems software and applications generation

Worksheet 4
• Complete Task 3 on Worksheet 4
Programming language translators
Unit 2 Systems software and applications generation

Libraries
• Most languages have sets of pre-written (and pre-
compiled) functions called libraries
• Examples could include functions for generating
random numbers or for mathematical operations
• Can you think of any other libraries you may have used?

• A programmer can also write their own libraries


• Library functions can be called within a program
• What are the advantages of using libraries?
Programming language translators
Unit 2 Systems software and applications generation

Linker
• The linker needs to put the appropriate memory
addresses in place so that the program can call and
return from a library function

Source code random Library

random.choice(1,2,3) Linker def choice(a,b,c):


# code here
...

def otherstuff(a):
# code…
Programming language translators
Unit 2 Systems software and applications generation

Loader
• The job of the loader is to copy the program and any
linked subroutines into main memory to run
• When the executable code was created it may
assume the program will load in memory address 0
• However, memory addresses in the program will
need to be relocated by the loader because some
memory will already be in use
Programming language translators
Unit 2 Systems software and applications generation

Plenary
• An assembler converts from low level code to
machine code
• Compilers and interpreters convert from high level
code to machine code
• Compilers convert the whole program at once
• Interpreters translate the program line by line

• Many languages convert source code to an


intermediate stage called bytecode
Programming language translators
Unit 2 Systems software and applications generation

Plenary
• When a program is compiled it is lexed, syntactically
and semantically analysed, and optimised before
executable code is generated
• Code from other files called libraries can be used
• Libraries are linked to the executable code by the
linker
• The loader copies the executable code into main
memory and adjusts memory addresses
Programming language translators
Unit 2 Systems software and applications generation

Copyright

© 2016 PG Online Limited

The contents of this unit are protected by copyright.

This unit and all the worksheets, PowerPoint presentations, teaching guides and other associated files
distributed with it are supplied to you by PG Online Limited under licence and may be used and copied by you
only in accordance with the terms of the licence. Except as expressly permitted by the licence, no part of the
materials distributed with this unit may be used, reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic or otherwise, without the prior written permission of PG Online Limited.

Licence agreement

This is a legal agreement between you, the end user, and PG Online Limited. This unit and all the worksheets,
PowerPoint presentations, teaching guides and other associated files distributed with it is licensed, not sold, to
you by PG Online Limited for use under the terms of the licence.

The materials distributed with this unit may be freely copied and used by members of a single institution on a
single site only. You are not permitted to share in any way any of the materials or part of the materials with any
third party, including users on another site or individuals who are members of a separate institution. You
acknowledge that the materials must remain with you, the licencing institution, and no part of the materials may
be transferred to another institution. You also agree not to procure, authorise, encourage, facilitate or enable any
third party to reproduce these materials in whole or in part without the prior permission of PG Online Limited.

You might also like