Programming language translators

Cards (59)

  • Understand the role of an assembler, compiler and interpreter
  • Compilation
    1. Lexical analysis
    2. Syntax analysis
    3. Code generation
    4. Optimization
  • Assembler
    Translates assembly code into machine code
  • Assembly code
    Low-level language, with each instruction almost equivalent to one machine code instruction
  • Compiler
    Translates high-level language into machine code
  • Different hardware platforms
    Require different compilers
  • Compiler process

    1. Source code input
    2. Compile
    3. Object code (executable machine code) output
  • Object code can be saved and run without the presence of the compiler
  • Interpreter
    Translates high-level language into machine code
  • Intermediate language such as bytecode is produced as the final output by some compilers and is subsequently used
  • Linkers and loaders
    Their function is described
  • Libraries
    Their use is described
  • Interpreter

    A type of programming language translator that analyzes the source code, checks for syntax errors, and executes the code line by line
  • Interpreter operation
    1. Programmer writes program
    2. Interpreter analyzes program for syntax errors
    3. Interpreter translates program to machine code
    4. Interpreter executes program
  • Python program with syntax error
    • print ("abc)
    SeTAT1
    6 print ("a-be)
    7 print ("goodbye")
  • When the program runs, it produces a NameError because the variable 'n' is not defined
  • Python program with syntax error (missing closing bracket)
    • 3 chatb
    4 print("s+b", c)
    5e-a-b
    6 print ("a-b
    7 print ("goodbye")
  • When the program runs, it does not execute any of the code but produces a SyntaxError because of the missing closing bracket
  • The interpreter scans through the whole program checking for certain types of errors before executing any of it
  • Bytecode
    Many languages are not only compiled or only interpreted, there are various possibilities in between such as bytecode
  • Bytecode
    1. Compiling the source code into bytecode
    2. Interpreting the resulting bytecode
  • Bytecode
    • The bytecode may be compiled once and for all, with errors detected before execution as in Python
    • A change in the source code requires recompiling the bytecode
  • Bytecode
    • Achieves platform independence by abstracting away differences between computer architectures and operating systems
    • Provides an extra security layer by allowing the bytecode interpreter to execute the code rather than the program itself
  • Compiling Python to Java bytecode
    1. Using the Jython compiler
    2. Executing the Java bytecode with the Java interpreter
  • Compilers
    • The object code can be saved and run without needing to recompile
    • The object code executes faster than interpreted code
    • The object code is more secure as it cannot be easily read
  • Interpreters
    • Provide platform independence by running the source code on any machine with the appropriate interpreter
    • Useful for program development as there is no need for lengthy recompilation
  • Stages of compilation
    1. Lexical analysis
    2. Syntes analysis
    3. Code generation and optimisation
  • Lexical analysis performs the following functions:
  • Lexical analysis
    1. Removes superfluous spaces
    2. Converts all comments to spaces
    3. Performs simple error checking
  • The lexical analyser will not detect misspelt keywords or undeclared variables, this is the job of the syntax analyser
  • Tokens
    Unique symbols that replace keywords, constants and identifiers in the source code
  • Symbol table

    Contains an entry for every keyword (reserved word) and identifier in the program, including information about the item such as its kind, type, run-time address/value, and pointers to accessing information
  • Typical entries in a symbol table

    • input (keyword)
    • pi (constant, real, 3.14159)
    • radius (variable, real, (7))
    • operator
    • a20a (variable, real, (?))
    • numSides (array, integer, (?))
  • The lexical analyser puts the anther and to run-in the symbol table so th can replace them in the source code by tokens
  • The lexical analyser will not fill in the end of item and Type of le done later by the syntax analyser
  • The symbol table must be organised in such a way that entries can be found as quickly as posable
  • Hash table
    A common way of organising the symbol table where the keyword or identifier is hashed to produce an any sub
  • Synonyms (colision) are inevitable in a hash table, and a common way of handing the is to store the synonym in the neat avalable too space in the table
  • Syntax analysis
    The process of chamining whether the sequence of input characters, symbols, or tokens form a valid sentence in the language
  • Parsing
    The task of systematically applying the set of rules to aach statement to determine whether it is valid