14 compilers interpreters and debuggers
This presentation is the property of its rightful owner.
Sponsored Links
1 / 28

14 Compilers, Interpreters and Debuggers PowerPoint PPT Presentation


  • 57 Views
  • Uploaded on
  • Presentation posted in: General

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)

Download Presentation

14 Compilers, Interpreters and Debuggers

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


14 compilers interpreters and debuggers

14 Compilers, Interpreters and Debuggers

Mauro Jaskelioff

(Originally by Gail Hopkins)


Introduction

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

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

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

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

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

Compilers: Pros and Cons

  • Pros:

    • 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

Interpreters: Pros and Cons

  • Pros:

    • 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

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 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

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


Debugging1

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

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

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

A Buggy awk Program

{

x = 10

while (x > 5) {

x++

}

print $x

}


Program with print statements

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

Output of Buggy Program

$ awkInfinite.sh

Got past initialisation

Entered while Loop

incremented x

Entered while Loop

incremented x

Entered while Loop

incremented x

Entered while Loop

incremented x


More print statements

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

More Output

$ awkInfinite.sh

Got past initialisation

Entered while Loop

10

incremented x

11

Entered while Loop

11

incremented x

12


Debugging tools

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

Types of Debuggers

  • Interpretive 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 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

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

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

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

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

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.


  • Login