5.2 Language Translators
5.2 Language Translators
2 Language Translators
Syllabus
Programming Languages
Program
• A program is a list of instruction that enables a computer to
perform a specific task.
• Program are way of telling computers what to do, how to do it
and when to do it.
• Example
High level Language
• A language that is close to human language.
• A high-level language is a programming
language designed to simplify
computer programming.
• High level language are portable as it can be
used on different types of computer or OS
• Example: Visual basic, Python, C++
High level language
• High level language enable a programmer to
focus on the problem to be solved
• And it requires no knowledge of the hardware
and instruction set of the computer that will
use the program
Benefits of using High level language
• Easier to understand for programmers to read
as it is close to English language.
• Easier to debug at the development stage
• Development is faster
• It is easier for a programmer to understand
another programmer’s code written in HLL.
Low Level Language
• Low level language relate to the specific
architecture and hardware of a particular type
of computer.
• Low level language can refer to machine
code(binary instruction), or an assembly
language that needs to be translated in
machine code.
Benefits of Low level language
• Low level language can be used to program
directly for the special hardware
• Low level language are used for writing
processor dependant programs
• Low level language takes less space in
computer memory
• Low level program are really fast
Machine code
• It is the binary language that a computer uses.
• It is the program statement that directly
understand by microprocessor.
Assembly Language
• It is the low level programming language that
requires to be translated in the machine code.
• It is hardware dependant.
• Few programmers write programs in an assembly
language.
• Example:
LDM first
ADD second
STO sum
Why programmers write code in Assembly
language
• To make use of special hardware
• To make use of special machine dependant
instructions
• To write code that doesn’t take up much space
in primary memory
• To write code that performs a task very quickly
Assembler
• An assembler translates assembly language
into machine code.
• Assembly language consists of mnemonics for
machine op-codes so assemblers perform a
1:1 ratio translation from mnemonic to a
direct instruction.
• Example: LDA #4 converts 0001001000100100
Advantages of using an Assembler
• Very fast in translating assembly language to
machine code as 1 to 1 relationship
• Assembly code is often very efficient (and
therefore fast) because it is a low level
language
• Assembly code is fairly easy to understand due
to the use of English-like mnemonics
Disadvantages of using Assembler
• Assembly language is written for a certain
instruction set and/or processor
• Assembly tends to be optimized for the
hardware it's designed for, meaning it is
incompatible with different hardware
• Lots of assembly code is needed to do
relatively simple tasks, and complex programs
require lots of programming time
Compiler
• Complier is the translator that translates the entire
program & displays all the error with the error message &
the location of the error on the screen.
• It translates the program before executing it.
• Compiler is a computer program that translates code
written in a high level language to a lower level language,
object/machine code .
• Compiler scans the entire program and translates the
whole of it into machine code at once.
• E.g.: Translator of C, C++, C#, Java
• Produce .Exe file
Steps in compilation of a Program
• In a compiler, the source code is translated to
object code successfully if it is free of errors.
• The compiler specifies the errors at the end of
compilation with line numbers when there are
any errors in the source code.
• The errors must be removed before the
compiler can successfully recompile the
source code again.
Advantages of using a compiler
• Source code is not included, therefore
compiled code is more secure than
interpreted code
• Compiler is faster than interpreter
• Produces an executable file, and therefore the
program can be run without need of the
source code
• No copy right issues
Disadvantages of using a compiler
• Difficult to debug
• Object code needs to be produced before a
final executable file, this can be a slow
process.
• The source code must be 100% correct for the
executable file to be produced
Interpreter
• The translation of single statement of source program into machine
code is done by language processor and executes it immediately before
moving on to the next line is called an interpreter.
• If there is an error in the statement, the interpreter terminates its
translating process at that statement and displays an error message.
• The interpreter moves on to the next line for execution only after
removal of the error.
• An Interpreter directly executes instructions written in a programming
or scripting language without previously converting them to an object
code or machine code.
• It translates the program while executing it.
• E.g.: Python and Matlab
• Works in emulator
Advantages of using an Interpreter
• Easier to debug(check errors) than a compiler.
• Easier to create multi-platform code, as each different
platform would have an interpreter to run the same code
• Useful for prototyping software and testing basic program
logic.
• Uses less memory, source code only has to be present
one line at a time in memory .
• Easier to modify as the instructions are at a high level and
so the program flow is easier to understand and modify.
• Does not crash a computer
Advantages of using an Interpreter
Disadvantages of using an Interpreter
• Source code is required for the program to be
executed, and this source code can be read,
making it insecure.
• Interpreters are generally slower than
compiled programs due to the per-line
translation method.
• Copy right issues
Compiler Interpreter Assembler
Translate a high level Execute a high level Translate a low level
language program into language program into a language program into
machine code statement at a time machine code
An executable file of No executable file of An executable file of
machine code is produced machine code is produced machine code is produced
One high level language One high level language One low level language
statement can be program statement may statement is usually
translated into several require several machine translated into one
machine code instruction code instructions to be machine code instruction
executed
Compiled programs are Interpreted programs can Assembled programs can
used without compiler not be used without the be used without the
Interpreter assembler
A compiled programs is An interpreter is often used An assembler program is
usually distributed for when a program is being usually distributed for
general use developed general use
Source Code
• Source codes are the actual programming codes
that are produced before the translation of the
program into machine codes.
OR
• Human-readable program statements written by a
programmer or developer in a high-level or
assembly language that are not directly readable by
a computer. Source code needs to be compiled into
object code before it can be executed by a
computer.
Object Code
• Object codes are the codes that are produced
after the translation of the program into
machine codes.
OR
• The code, generated by a compiler or an
assembler that was translated from the source
code of a program.
Translator
• It is a system software(utility program)
• This software allows new programs to be
written and run on computers, by converting
source code (human readable) into machine
code that are under stood by the computer.
• Translation has to be take place before a
program can be used by a computer as
computer works in machine code.
Similarities and differences between
assemblers, compilers and interpreters
Pros (blue cells) and cons (white cells) of
compilers and interpreters
Pros (blue cells) and cons (white cells) of
compilers and interpreters
Errors
OR
• an error in the grammar of a source program
Logical Error
• Logical error is where the program does not
do what the programmers wanted to do.
OR
Python IDE
Context-sensitive prompts
• A context-sensitive user interface offers the user options
based on the state of the active program. Context sensitivity is
ubiquitous in current graphical user interfaces, often
in context menus.
• A user-interface may also provide context sensitive feedback,
such as changing the appearance of the mouse pointer or
cursor, changing the menu color, or with auditory or tactile
feedback.
Dynamic syntax check
Prettyprinting
Pretty-printing is the application of any of various stylistic formatting conventions
to text files, such as source code, markup, and similar kinds of content.
Prettyprinting
• the practice of displaying or printing well set
out and formatted source code, making it
easier to read and understand.
Expand and collapse code block
• You can choose to hide some code from view by
collapsing a region of code so that it appears under a
plus sign (+).
• You expand a collapsed region by clicking the plus
sign.
Expand and collapse code block
Breakpoint
• a deliberate pause in the execution of a
program during testing so that the contents of
variables, registers, and so on can be
inspected to aid debugging
Breakpoints (i.e variables, expressions,
report window)
• Breakpoints are one of the most important
debugging techniques in your developer's
toolbox.
• You set breakpoints wherever you want to
pause debugger execution.
• For example, you may want to see the state of
code variables.
Set breakpoints in source code
• You can set a breakpoint on any line of executable code.
• For example, in the following C# code, you could set a
breakpoint on the line of code with the variable assignment
(int testInt = 1), the for loop, or any code inside the for loop.
• You can't set a breakpoint on method signatures, declarations
for a namespace or class, or variable declarations if there's no
assignment and no getter/setter.
• To set a breakpoint in source code, click in the far left margin
next to a line of code. You can also select the line and press F9,
select Debug > Toggle Breakpoint, or right-click and
select Breakpoint > Insert breakpoint. The breakpoint appears
as a red dot in the left margin.
Breakpoint
When you debug, execution pauses at the breakpoint, before the code on that line
is executed. The breakpoint symbol shows a yellow arrow.
At the breakpoint in the following example, the value of testInt is still 1. So, the value hasn't changed since the variable was initialized (set to a value of 1) because the statement in yellow hasn't yet executed.
When the debugger stops at the breakpoint, you can look at the current state of
the app, including variable values and the call stack.
Manage breakpoints in the Breakpoints
window
• You can use the Breakpoints window to see and manage all the
breakpoints in your solution.
• This centralized location is especially helpful in a large solution,
or for complex debugging scenarios where breakpoints are
critical.
• In the Breakpoints window, you can search, sort, filter,
enable/disable, or delete breakpoints. You can also set conditions
and actions, or add a new function or data breakpoint.
• To open the Breakpoints window,
select Debug > Windows > Breakpoints, or
press Alt+F9 or Ctrl+Alt+B.
Breakpoint window
Breakpoint labels
• You can use labels to sort and filter the list of
breakpoints in the Breakpoints window.
• To add a label to a breakpoint, right-click the
breakpoint in the source code or
the Breakpoints window, and then select Edit
labels. Add a new label or choose an existing
one, and then select OK.
For debugging :Single stepping
• When your program is in break mode—that is,
the program has paused at a breakpoint—you
can control how execution continues.
• The following table shows the commands
available for this purpose on the Debug menu,
along with their keyboard shortcuts.
Single stepping
• For example, if you had the following code:
Sub GetData()
Dim gName As String
gName = InputBox("Enter your name")
SetData(gName)
End Sub
Sub SetData(gName As String)
Text1 = "Name: " & gName
End Sub
• Both Step Into and Step Over would behave the same, until
they reached SetData(gName) .
• Using Step Over, Visual Basic would run the SetData
procedure, and the cursor would be moved to the next
statement in that procedure (in this case, End Sub). Using
Step Into, the cursor would be moved to the beginning of the
SetData procedure
Single stepping
• the practice of running a program one
line/instruction at a time.
Report window
• a separate window in the run-time
environment of the IDE that shows the
contents of variables during the execution of a
program