The Functions and Purposes of Translators - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

The Functions and Purposes of Translators

Description:

Translator program maintains a dictionary of reserved words. ... that a reserved word has been misspelled and will display a message which ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 44
Provided by: MrL61
Category:

less

Transcript and Presenter's Notes

Title: The Functions and Purposes of Translators


1
The Functions and Purposes of Translators
  • Interpreters, Compilers, Lexical Analysis, Syntax
    Analysis, Code Generation (Intermediate Code,
    Optimisation, Final Code), Linkers Loaders.

2
Learning Objectives
  • Describe the difference between interpretation
    and compilation.
  • Describe what happens during lexical analysis.
  • Describe what happens during syntax analysis,
    explaining how errors are handled.
  • Explain the code generation phase.
  • Explain the purpose of linkers and loaders.

3
Machine Code
  • Computers work and understand only machine code.
  • Simple instructions represented by a binary
    pattern in the computer.
  • Programming in machine code takes a long time and
    is prone to errors.

4
Assembly Languages
  • Developed to improve program writing.
  • Use of mnemonics and names for locations in
    memory.
  • Each assembly instruction represents a single
    machine instruction which means that it is fairly
    easy to translate a program written in assembly
    language to machine code.
  • Assemblers which are loaded into the computer
    translate the assembly language to machine code.
  • Writing programs in assembly language, although
    easier than using machine code, is still tedious
    and takes a long time.

5
High - Level Languages (HLL)
  • Similar to human languages and developed for
    specific applications.
  • FORTRAN (FORmula TRANslation) developed for
    science and engineering programs and it used
    formulae in the same way as would scientists and
    engineers.
  • COBOL (Common Business Oriented Language) was
    developed for business applications.
  • Much easier for humans to program in HLL but as
    computers only understand machine code programs
    written in HLLs need to be translated into
    machine code before they can be executed.

6
Compiler
  • Translate high-level languages into machine code.
  • The machine code version can be loaded into the
    machine and run without any further help as it is
    complete in itself.
  • The high-level language version of the program is
    called the source code and the resulting machine
    code program is called the object code.

7
Disadvantages Of Compilers
  • Use a lot of computer resources.
  • Has to be loaded in the computer's memory at the
    same time as the source code and there has to be
    sufficient memory to hold the object code.
  • Has to be sufficient memory for working storage
    while the translation is taking place.
  • Errors in the original program are difficult to
    pin-point.

8
Interpreters
  • Take each instruction in turn and translates it
    into machine code.
  • Executes the translated instruction before the
    next instruction is translated.

9
Interpreters
  • Advantages
  • Need less memory than compilers (useful in early
    computers which had limited power and memory).
  • Continual compilation of whole code is wasteful /
    time consuming during testing particularly if
    very minor changes have been made.
  • During testing translator diagnostics will be
    more complete as error messages will be produced
    in relation to the HLL being used and not the
    machine code.
  • As the error messages when the error is produced
    on the line it is encountered it is easier to
    identify / isolate the instruction causing the
    problem.
  • Individual segments can be run without needing
    compile the whole program.

10
Interpreters
  • Disadvantages
  • Slow execution compared to that of a compiled
    program because
  • The original program has to be translated every
    time it is executed.
  • Instructions inside a loop have to be translated
    each time the loop is entered.

11
Interpreters Compilers
  • Many high-level languages use both.
  • Programmers use the interpreter during program
    development and, when the program is fully
    working, use a compiler to translate it into
    machine code.
  • This machine code version can then be distributed
    to users who do not have access to the original
    code.

12
Translation Process
13
Lexical Analysis
  • Simple error-checking
  • E.g. Illegal identifiers
  • Variable names must follow the rules of the
    language, the translator tries the rules against
    the names used and reports any errors.
  • Removes white space
  • i.e. comments, spaces, tabs, mew-line characters
  • Converts all reserved words / keywords,
    operators, constants, identifiers / variable
    names in the source code into a series of tokens
    (unique symbols) to be passed on to the syntax
    analyser.
  • Variable names are noted in a symbol table.

14
Lexical Analysis
  • If the high-level language requires variables to
    be declared before being used, the lexical
    analyser will pick up invalid reserved words /
    keywords.
  • E.g. PRIT instead of PRINT
  • Translator program maintains a dictionary of
    reserved words.
  • If the reserved word used is not in this
    dictionary then it will be assumed to be a
    variable but it will not be in the symbol table
    as it wont have been declared.
  • So an error has been made.
  • It will then assume that a reserved word has been
    misspelled and will display a message which
    suggests one close to spelling provided.

15
Symbol Table
  • Organised as a hash table.
  • i.e. the position of entries in the table is
    calculated by applying a hashing algorithm to the
    identifier
  • Linearly searching the table would not be fast
    enough.
  • When two names are hashed to the same address, a
    linked list can be used (see hashing algorithms
    AS).

16
Example Converting source code to tokens
  • sum sum number
  • converted to tokens by lexical analysis becomes
  • ltvariablegt ltassignment_operatorgt ltvariablegt
    ltarithmetic_operatorgt ltvariablegt

17
Syntax ( Semantic) Analysis
  • Syntax Analysis
  • Checking each sequence of tokens to check that
    they are grammatically correct (form valid
    sentences).
  • The grammar of programming languages is defined
    by means of BNF notation or syntax diagrams (see
    later).

18
Example Grammar Rule
  • ltassignment statementgt
  • should be
  • ltvariablegt ltassignment_operatorgt ltexpressiongt
  • ltexpressiongt
  • should be
  • ltvariablegt ltarithmetic_operatorgt ltvariablegt

19
Example Grammar Check -Valid
  • sum sum number
  • converted to tokens by lexical analysis becomes
  • ltvariablegt ltassignment_operatorgt ltvariablegt
    ltarithmetic_operatorgt ltvariablegt
  • which becomes
  • ltvariablegt ltassignment_operatorgt ltexpressiongt
  • and then
  • ltassignment statementgt
  • which is valid.

20
Example Grammar Check - Invalid
  • sum sum number
  • converted to tokens by lexical analysis becomes
  • ltvariablegt ltassignment_operatorgt ltvariablegt
    ltarithmetic_operatorgt ltarithmetic_operatorgt
    ltvariablegt
  • This does not represent a valid statement hence
    an error message will be returned.
  • Another example could be an invalid number of
    brackets.

21
Syntax ( Semantic) Analysis
  • If the high-level language does not require
    variables to be declared then it is at this stage
    that invalid names can be found such as PRIT
    instead of PRINT as PRIT will be read as a
    variable name instead of a reserved word.
  • This will mean that the statement containing PRIT
    will not parse to a valid statement.

22
Syntax ( Semantic) Analysis
  • Semantic analysis
  • Check the meaning of each sequence of tokens
    (each sentence).
  • A Parser is the component of a compiler that
    carries out this task.

23
Syntax ( Semantic) Analysis
  • Label checks
  • Some languages allow GOTO label statements (not
    recommended) which allow control to be passed,
    unconditionally, to another statement which has a
    label.
  • The compiler must check that such a label exists.
  • Flow of control checks
  • Certain control constructs can only be placed in
    certain parts of a program.
  • E.g. C (and C) the CONTINUE statement can only
    be placed inside a loop and the EXIT LOOP
    statement can only be placed inside a loop
    statement.
  • Declaration checks.
  • Check that variables have not been assigned
    illegal values.
  • Contents of variables must be of a specific type
    so an error will be created by the attempted use
    of anything else.
  • E.g. letters to an integer.

24
Syntax ( Semantic) Analysis
  • Also
  • Determines priorities of arithmetic operators in
    an expression
  • Variable types and the block in which it is valid
    (local or global) will also be stored during this
    analysis.

25
Note
  • During both lexical and Syntax ( Semantic)
    analysis if any errors are detected then
    diagnostic error messages will be reported.

26
Code Generation
  • The address of each variable is now calculated
    and stored in the symbol table as each is
    encountered.
  • Intermediate code is produced which, after
    optimisation (see next slide), is turned into
    executable code / machine code
  • All errors due to incorrect use of the language
    have been removed by this stage.

27
Optimisation
  • Reduce the size of the object code by removing
    any duplicate or redundant instructions which
    improve speed of execution.

28
Loaders
  • Decides where in memory to place individual
    modules of a program and library routines
    (regularly used programs) which have been
    previously translated into machine code, copies /
    loads them from storage into memory and adjusts
    memory addresses (see Memory Management earlier).

29
Linkers
  • Calculates the addresses of the separate modules
    and library routines (call and return addresses),
    links them so that they can interact with one
    another, allowing the program to be executed.

30
Linking Loaders
  • Load and link.

31
Symbol Table
Lexical Analysis
  • The variable identifier.
  • The kind of variable.
  • E.g. Simple variable, structured variable such as
    an array, procedure, keyword etc
  • Type of variable.
  • E.g. Integer, decimal, string etc
  • Other Information.
  • E.g. Bounds of array
  • Run-time address of the variable or its value if
    it is a constant.

Syntax ( Semantic Analysis
Code Generation
32
Plenary
  • Describe the difference between interpretation
    and compilation.

33
Plenary
  • Interpreter translates line of code and then runs
    it.
  • Compiler translates entire program before run.
  • Compiler creates an object code.
  • Interpreter retains source code.
  • Compiler must be present for translation.
  • Interpreter must be present for run.

34
Plenary
  • Describe what happens during lexical analysis.

35
Plenary
  • Removes spaces/white space/tabs
  • Removes comments
  • Checked validity of reserved words
  • Checks validity of symbols/variable names
  • Creates a stream of tokens
  • each group of characters is replaced by a token
  • Tokenises reserved words
  • Tokenises operators
  • Symbol table created
  • accessed by hashing algorithm
  • which initially stores just the variable names
  • Redundant characters removed
  • (Some) error diagnostics created
  • e.g. illegal variable names.

36
Plenary
  • Describe what happens during syntax analysis,
    explaining how errors are handled.

37
Plenary
  • (Tokens) are analysed to check for grammatical
    correctness (form valid sentences)
  • (Code)/reserved word checked against rules
  • Invalid number of brackets found
  • Determine priorities of arithmetic operators in
    an expression
  • Produce intermediate code
  • Diagnostic error messages are reported
  • Label checks
  • Flow of control checks
  • Declaration checks

38
Plenary
  • translator program maintains a dictionary of
    reserved words
  • if the reserved word used is not in this
    dictionary then an error has been made
  • message may be given which suggests one close to
    spelling provided
  • variable names must follow the rules of the
    language
  • translator tries the rules against the names used
    and reports any errors
  • contents of variables must be of a specific type
  • error created by the attempted use of anything
    else.

39
Plenary
  • Explain the code generation phase.

40
Plenary
  • The address of each variable is now calculated
    and stored in the symbol table as each is
    encountered.
  • Intermediate code is produced which, after
    optimisation (see next slide), is turned into
    executable code / machine code
  • All errors due to incorrect use of the language
    have been removed by this stage.

41
Plenary
  • Explain the purpose of linkers and loaders.

42
Loaders
  • Decides where in memory to place individual
    modules of a program and library routines
    (regularly used programs) which have been
    previously translated into machine code, copies /
    loads them from storage into memory and adjusts
    memory addresses (see Memory Management earlier).

43
Linkers
  • Calculates the addresses of the separate modules
    and library routines (call and return addresses),
    links them so that they can interact with one
    another, allowing the program to be executed.
Write a Comment
User Comments (0)
About PowerShow.com