Compiled by Benjamin Muganzi 3.2 Functions and Purposes of Translators Computing 9691 Paper 3 1.

Slides:



Advertisements
Similar presentations
Chapter 11 Introduction to Programming in C
Advertisements

COMPILER CONSTRUCTION
GCSE Computing Lesson 5.
compilers and interpreters
Compilers and Language Translation
FIT1001- Computer Systems Lecture 12 Introduction to System Software.
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
Programming Types of Testing.
Lab6 – Debug Assembly Language Lab
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
CPSC Compiler Tutorial 9 Review of Compiler.
COSC 120 Computer Programming
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 1, Lab.
CS 104 Introduction to Computer Science and Graphics Problems Software and Programming Language (2) Programming Languages 09/26/2008 Yang Song (Prepared.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
C++ Crash Course Class 1 What is programming?. What’s this course about? Goal: Be able to design, write and run simple programs in C++ on a UNIX machine.
Chapter 17 Programming Tools The Architecture of Computer Hardware and Systems Software: An Information Technology Approach 3rd Edition, Irv Englander.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
A Simple Two-Pass Assembler
HERY H AZWIR Computer Software. Computer Software Outline Software and Programming Languages  Software  Programming  Programming language development.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Lecture 1 Introduction Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Basic of Programming Language Skill Area Computer System Computer Program Programming Language Programmer Translators.
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
The Software Development Process
The Functions and Purposes of Translators Syntax (& Semantic) Analysis.
Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
The Functions and Purposes of Translators Translators, Interpreters and Compilers - High Level Languages.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
The Functions and Purposes of Translators Translators, Interpreters and Compilers - High Level Languages.
Hello world !!! ASCII representation of hello.c.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Introduction to Computer Programming Concepts M. Uyguroğlu R. Uyguroğlu.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Software Engineering Algorithms, Compilers, & Lifecycle.
F453 Module 8: Low Level Languages 8.1: Use of Computer Architecture.
Advanced Computer Systems
Component 1.6.
Compiler Design (40-414) Main Text Book:
Chapter 1 Introduction.
Introduction to Compiler Construction
Component 1.6.
CSCI-235 Micro-Computer Applications
F453 Computing Questions and Answers
Chapter 1 Introduction.
-by Nisarg Vasavada (Compiled*)
A451 Theory – 7 Programming 7A, B - Algorithms.
Translators & Facilities of Languages
CSCE Fall 2013 Prof. Jennifer L. Welch.
Lesson Objectives Aims Key Words Compiler, interpreter, assembler
CMP 131 Introduction to Computer Programming
CSCE Fall 2012 Prof. Jennifer L. Welch.
PROGRAMMING FUNDAMENTALS Lecture # 03. Programming Language A Programming language used to write computer programs. Its mean of communication between.
Chapter 1 Introduction.
Chapter 10: Compilers and Language Translation
1.3.7 High- and low-level languages and their translators
WJEC GCSE Computer Science
Presentation transcript:

Compiled by Benjamin Muganzi 3.2 Functions and Purposes of Translators Computing 9691 Paper 3 1

Compiled by Benjamin Muganzi Assembly language and Machine code An assembly language is a low-level programming language for a computer, microcontroller, or other programmable device, in which each statement corresponds to a single machine code instruction Assembly language uses mnemonics to represent each low-level machine operation (instruction) and opcode (Register or other resources). Machine code or machine language is the binary commands that can be directly executed by a processor. They are 1s and 0s. Their order tells the computer what to do. This code is the lowest level of software. All other kinds of software need to be translated into machine code before they can be used. An Assembler is the utility software used to convert Assembly language code to machine code. 2

Compiled by Benjamin Muganzi How an assembler converts assembly language code to machine code An assembler creates object code by translating assembly instruction mnemonics into opcodes, and by resolving symbolic names for memory locations and other entities like registers etc. An opcode is a single instruction that can be executed by the CPU. In machine language it is a binary or hexadecimal value such as 'B6' loaded into the instruction register. (Refer to topic 3.3) In assembly language mnemonic form, an opcode is a command such as MOV or ADD or JMP. – For example MOV, AL, 34h The opcode is the MOV instruction. The other parts are called the 'operands'. Operands are manipulated by the opcode. In this example, the operands are the register named AL and the value 34 hex. 3

Compiled by Benjamin Muganzi Interpretation and Compilation Object code is the product obtained when source code is translated by a compiler. Object code may almost be machine code or close to machine code. 4 Source Code Machine Code Object Code Compiler Assembly Code Interpreter Assembler

Compiled by Benjamin Muganzi Interpreters and Compilers Levels of Programming Languages Low level (Machine code) Mid level (Assembly) High Level (C/C++, VB, Java, Fortran, etc) Code written in Mid level and high level languages (i.e. source code) must be translated to machine code, which is what the processor understands. Translators are used. The types of translators are: Compilers Interpreters 5

Compiled by Benjamin Muganzi Interpreters An interpreter takes each instruction in turn and translates it into machine code. The translated instruction is executed before the next instruction is translated. This is particularly useful when there is not enough memory to hold the compiled Program especially on old computers. 6

Compiled by Benjamin Muganzi Compilers A compiler translates source code into machine or object code A compiler translates the whole program as one complete unit to create an executable file 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

Compiled by Benjamin Muganzi Compilers –Adv. And Disadv. 8 Advantages. – the translation is done once only and as a separate process. The program that is run is already translated into machine code so is much faster in execution. – compiled programs can run on any computer Disadvantages – A Compiler uses a lot of computer resources. It 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. – When an error in a program occurs it is difficult to pin- point its source in the original program

Compiled by Benjamin Muganzi Interpreters – Adv. And Disadv. Advantages – Need less memory than compilers (useful in early computers which had limited power and memory). – 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. Disadvantages – every line has to be translated each time it is executed, thus interpreters tend to be slow. – As interpreter doesn't create an object file the source code must be distributed along with the interpreter in order for the user to run the software. 9

Compiled by Benjamin Muganzi Interpreters vs. Compilers 10 COMPILERINTERPRETER Fast, creates executable file that runs directly on the CPU Slower, interprets code one line at a time Debugging is more difficult. One error can produce many spurious errors Debugging is easier. Each line of code is analysed and checked before being executed More likely to crash the computer. The machine code is running directly on the CPU Less likely to crash as the instructions are being carried out either on the interpreters' command line or within a virtual machine environment which is protecting the computer from being directly accessed by the code. Easier to protect Intellectual Property as the machine code is difficult to understand Weaker Intellectual property as the source code (or bytecode) has to be available at run time. For example if you write a Flash Actionscript application, you can easily get de-compilers that convert the p-code back into actionscript source code (unless you use encryption, but that is another story). Uses more memory - all the execution code needs to be loaded into memory, although tricks like dynamic Link Libraries lessen this problem Uses less memory, source code only has to be present one line at a time in memory Unauthorised modification to the code more difficult. The executable is in the form of machine code. So it is difficult to understand program flow. Easier to modify as the instructions are at a high level and so the program flow is easier to understand and modify

Compiled by Benjamin Muganzi The Translation process The basic translation process is identical for both interpreters and compilers. Source code goes through various stages and is converted into Intermediate Language. From this, final object code is generated, which can be optimized. 11

Compiled by Benjamin Muganzi Lexical Analysis stage White spaces, blank lines and comments are removed from the code. Using the grammar of the language being used, the lexical analyzer assigns tokens to a meaningful string of characters. Single characters are converted into their ASCII codes. A token could be anything from 16-bit unsigned integers (starting from 256) to simple labels. Variable names require extra information. A symbol table is used to keep record of variables. This table is used throughout the translation process. During Lexical Analysis, only variable names are noted into the symbol table. The symbol table is stored as a Linked List and searching is performed using hashing. Some basic error reporting is done in this stage. E.g. Illegal Identifier… 12

Compiled by Benjamin Muganzi Syntax analysis stage All computer languages have their specific grammar (syntax) of writing valid programming statements. This grammar is defined using BNF (Backus-Naur Form). The Syntax Analyzer (or Parser) will analyze the tokenized code against the grammar of the language. The parsing transforms the code into a data structure, usually a Binary Tree, which is suitable for further processing. Invalid command names, such as INPT instead INPUT will be identified at this point. Some languages require variables to be declared before they can be used. The syntax analyzer will catch variables without declarations (using the symbol table). 13

Compiled by Benjamin Muganzi Syntax analysis – Example of BNF Taking a very elementary language, an assignment statement may be defined to be of the form and expression is The parser must take the output from the lexical analyser and check that it is of this form. If the statement is sum := sum + number, the parser will receive which becomes and then, which is valid. If the original statement is sum := sum + + number, this will be input as and this does not represent a valid statement hence an error message will be returned. 14

Compiled by Benjamin Muganzi Syntax analysis – semantic analysis During syntax analysis, certain semantic checks are carried out:  Label checks – make sure the line a GOTO statement passes control to exists  Flow of control checks – make sure statements are used in the correct place and order e.g. CONTINUE can only be placed inside of a loop, IF statement matched with correct END IF  Declaration checks – make sure all variables have been properly declared Note: although semantic checks check the logic to a certain extent, it is not the same as checking for a logic error. Remember, a logic error will not cause the program to crash, it will simply cause unexpected results. A compiler cannot find such errors. E.g. if the programmer has written a = b + c instead of a = b - c 15 Semantic means relating to meaning in language or logic. Something may be syntactically correct but semantically meaningless. ‘Jake ate a banana’ has meaning and obeys the rules of English but ‘A banana ate Jake’ obeys the rules but not the semantics.

Compiled by Benjamin Muganzi Code Generation At this stage, 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, is turned into executable code / machine code – All errors due to incorrect use of the language have been removed by this stage 16

Compiled by Benjamin Muganzi Code optimisation Once the code generator has created machine code, it tries to optimise the code to make it more efficient. Consider the following lines of code x = y + 3 b = x c = b The code optimiser will remove redundant code so the above example could become: c = y + 3 A compiler’s code optimiser can favour speed or memory optimisation because in the real world you often cannot optimise both. 17

Compiled by Benjamin Muganzi Library routines Library routines are pre-compiled modules available for use by other programs.  Programs are made up of modules  Commonly used modules can be compiled and stored ready for repeated use  These modules are stored in an a library  They are called library routines Windows uses libraries in the form of DLL files (dynamic link library) Because the variable names and memory addresses will be different from one use of the library to the next, two programs are needed at runtime or when an executable file is created: These programs are Loaders and Linkers. A loader has the job of loading all the modules into memory A linker resolves references in the main program, known as links or symbols, to library routines. If a function in a library routine is called, the linker will match the call in the program with the function in the library routine.  Libraries reduce the amount of code that needs to be written  If a library routine is updated, programs using it may stop working if the interfaces between the modules change 18

Compiled by Benjamin Muganzi Sample Questions 1.Explain why the size of the memory available is particularly relevant to the process of compilation. (4) 2.a) Explain the difference between the two translation techniques of interpretation and compilation. (2) b) Give one advantage of the use of each of the two translation techniques. (2) 3.State any three stages of compilation and describe, briefly, the purpose of each. (6) 4.Explain, in detail, the stage of compilation known as lexical analysis. (6) 19