14 Compilers, Interpreters and Debuggers

Slides:



Advertisements
Similar presentations
GCSE Computing Lesson 5.
Advertisements

compilers and interpreters
Chapter 1 An Overview of Computers and Programming Languages.
Programming Types of Testing.
CMP 131 Introduction to Computer Programming Violetta Cavalli-Sforza Week 1, Lab.
Compilers and Interpreters. Translation to machine language Every high level language needs to be translated to machine code There are different ways.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Systems Software Operating Systems.
Computer Programming-1 CSC 111 Chapter 1 : Introduction.
1 Chapter-01 Introduction to Computers and C++ Programming.
Introduction to Java Tonga Institute of Higher Education.
P51UST: Unix and Software Tools Unix and Software Tools (P51UST) Compilers, Interpreters and Debuggers Ruibin Bai (Room AB326) Division of Computer Science.
© Janice Regan, CMPT 128, Jan CMPT 128 Introduction to Computing Science for Engineering Students Creating a program.
Systems Software & Operating systems
สาขาวิชาเทคโนโลยี สารสนเทศ คณะเทคโนโลยีสารสนเทศ และการสื่อสาร.
Slide 1 Standard Grade Computing Studies Systems Software.
Computer Programming A program is a set of instructions a computer follows in order to perform a task. solve a problem Collectively, these instructions.
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
Systems Software Operating Systems. What is software? Software is the term that we use for all the programs and data that we use with a computer system.
Getting started with Programming using IDE. JAVA JAVA IS A PROGRAMMING LANGUAGE AND A PLATFORM. IT CAN BE USED TO DELIVER AND RUN HIGHLY INTERACTIVE DYNAMIC.
Debuggers in Python. The Debugger Every programming IDE has a tool called a debugger. This application does NOT locate or fix your bugs for you! It slows.
Intermediate 2 Computing Unit 2 - Software Development Topic 2 - Software Development Languages and Environments.
A compiler is a computer program that translate written code (source code) into another computer language Associated with high level languages A well.
Chapter 4 Software. Chapter 4: Software Generations of Languages Each computer is wired to perform certain operations in response to an instruction. An.
Compilers and Interpreters. HARDWARE Machine LanguageAssembly Language High Level Language C++ Visual Basic JAVA Humans.
Programming and Languages Dept. of Computer and Information Science IUPUI.
Introduction to OOP CPS235: Introduction.
The single most important skill for a computer programmer is problem solving Problem solving means the ability to formulate problems, think creatively.
Lesson 1 1 LESSON 1 l Background information l Introduction to Java Introduction and a Taste of Java.
Compilers and Interpreters
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Programming 2 Intro to Java Machine code Assembly languages Fortran Basic Pascal Scheme CC++ Java LISP Smalltalk Smalltalk-80.
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.
THE SOFTWARE Computers need clear-cut instructions to tell them what to do, how to do, and when to do. A set of instructions to carry out these functions.
Introduction To Software Development Environment.
Evolution and History of Programming Languages
Topic 2: Hardware and Software
Installing Java on a Home machine
Why don’t programmers have to program in machine code?
Lecture 1b- Introduction
Component 1.6.
Component 1.6.
Assembler, Compiler, MIPS simulator
Programming and Debugging with the Dragon and JTAG
Visit for more Learning Resources
Component 1.6.
Introduction to programming
Programming Language Hierarchy, Phases of a Java Program
CSCI-235 Micro-Computer Applications
Lecture 1: Introduction to JAVA
Microprocessor and Assembly Language
A451 Theory – 7 Programming 7A, B - Algorithms.
Teaching Computing to GCSE
TRANSLATORS AND IDEs Key Revision Points.
Teaching Computing to GCSE
Translators & Facilities of Languages
Installing Java on a Home machine
Introduction CSC 111.
Debugging at Scale.
CMP 131 Introduction to Computer Programming
Programming Language Basics
ICT Gaming Lesson 2.
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Computer Programming-1 CSC 111
1.3.7 High- and low-level languages and their translators
WJEC GCSE Computer Science
Programming language translators
Presentation transcript:

14 Compilers, Interpreters and Debuggers Mauro Jaskelioff (Originally by Gail Hopkins)

Introduction The role of language translators Compilers and Interpreters The Java Virtual Machine Debugging (based on material by Kenneth M. Anderson and Robert Steinke) Using print statements to debug a program Basic functions performed by a debugger

The role of language translators Programming languages have evolved to make programming easier for humans Computers still execute machine code Language translators are required to translate between the two A new translator is required for each combination of programming language and machine-code

The role of language translators (2) Translation involves several tasks translating high level program statements into combinations of machine code instructions translating high level data structures into combinations of memory locations linking to existing code (libraries, modules, objects, system calls) to form a final program Two broad approaches to translation compilation and interpretation

Compilers Whole program is translated into a file containing machine code. This file can then be executed when required The programmer writes the source program which is translated into the object program Start at the beginning of the source program REPEAT translate next source program statement UNTIL the end of the source program IF translation errors occurred THEN report errors ELSE produce the object program

Interpreters One statement is translated and then executed at a time Start at the beginning of the program REPEAT translate next program statement if no translation error occurred then execute the statement UNTIL the end of the program or an error occurs

Compilers: Pros and Cons Enable programmers to write human-readable code with a richer set of instructions Program execution is fast Compiled programs can be run more than once without re-compilation Programs can be compiled for different platforms Cons: To run on a different platform, code has to be recompiled

Interpreters: Pros and Cons No need to compile before running the program; program can just be run straight away Program can be run on different platforms without recompiling (provided an interpreter is available for that platform) Cons: Program execution is slower than with compiled code

Java Virtual Machine Until the advent of Java, the trend for programming was moving away from interpreted languages and towards compiled languages Software interpreters are complex (compared to machine code) and so run slowly Java takes a new approach…

Java Virtual Machine (2) Java is compiled into a standard machine language called Bytecode This standard machine language is then interpreted by a software interpreter, the Java Virtual Machine (JVM) JVM takes Bytecode and executes it Bytecode is not the standard machine language for any specific hardware processor Any machine which has a JVM can run the compiled Java program

Advantages of JVM Because Java is compiled before being interpreted it runs more quickly than standard interpreted languages BUT, Java is also portable because the platform it is compiled for is available for lots of different machine architectures Java therefore does not need to be recompiled to run on different types of machine The JVM can be seen as an interpreter for Java bytecodes, so it’s an intermediate solution between compilers and interpreters

Debugging

Debugging A program fails a test case (or a bug is reported). A programmer is given the test case and the associated source code and “debugs” the program until the fault has been corrected. The program, having been fixed, no longer exhibits the failure. The hard part is locating the problem!!

Bridging the Gap One problem with faults is that they are not necessarily located “near” their associated failure. We need to “bring the failure close to the fault”. The simplest way to do this is with print statements.

Using print Statements Your program has compiled but crashes when it runs. What should you do? Sprinkle print statements throughout your program and see how many are executed. Your program runs but is printing the wrong answer. What should you do? Use print statements to print out the values of your internal variables. See which give you the right answer and which give you the wrong answer.

A Buggy awk Program { x = 10 while (x > 5) { x++ } print $x

Program with print Statements { x = 10 print "Got past initialisation\n" while (x > 5) { print "Entered while loop\n" x++ print "incremented x\n" } print "Exited while loop\n" print $x

Output of Buggy Program $ awkInfinite.sh Got past initialisation Entered while Loop incremented x

More Print Statements { x = 10 print "Got past initialisation\n" while (x > 5) { print "Entered while Loop\n" print $x ”\n" x++ print "incremented x\n“ print $x ”\n” } print "Exited while loop\n" print $x

More Output $ awkInfinite.sh Got past initialisation Entered while Loop 10 incremented x 11 12

Debugging Tools A debugger allows a programmer to monitor the internal state of a program while it is executing (saves putting in and taking out print statements). Two types of debugger Interpretive Direct Execution

Types of Debuggers Interpretive Debuggers Direct Execution Debuggers work by reading a program and simulating its execution one line at a time. Direct Execution Debuggers work by running the actual program in a special mode where the debugger can read and write the program’s memory.

Interpretive Vs. Direct Execution Debuggers Interpretive Debuggers Easier to program Safer, a program cannot crash the machine (just the debugger). Direct Execution Faster More accurate, the actual program instructions are being executed

Two Styles of Use Line-at-a-time A programmer loads the program into the debugger and “steps” through the program one line at a time. The debugger stops the program after each line and gives the programmer a chance to check the program’s variables to see if it is operating correctly.

Two Styles of Use (2) Breakpoints A Programmer loads a program into the debugger and specifies “breakpoints” at various locations in the program. The program runs until it hits a breakpoint.

How are Breakpoints Useful? If you think you have function that might have a fault. Set a breakpoint at the beginning of the function. Set another breakpoint at the end of the function. If a program’s data is correct at the beginning of the function but incorrect at the end, then there is a fault in the function. They also allow you to skip over “init” code and debugged code quickly; letting the programmer focus on finding the fault.

Editing Variables Debuggers let the programmer explore “what-if” scenarios: You can execute a program to a certain point, and then alter the values of the program’s variables. So you can explore unusual cases. Plus, if a bug occurs, you can correct an incorrect value and see how far the program goes before it encounters another fault.

Summary Using a debugger is more flexible than print statements. Debuggers can be used to step through code or to run until some breakpoint. They also let you view the values of variables. They take a bit of getting used to but are usually well worth the time invested.