310 likes | 419 Vues
Languages and Environments. Higher Computing Unit 2 – Software Development. What I need to know. Description and comparison of procedural, declarative and event-driven languages Comparison of the functions, uses and efficiency of compilers and interpreters
E N D
Languages and Environments Higher Computing Unit 2 – Software Development
What I need to know • Description and comparison of procedural, declarative and event-driven languages • Comparison of the functions, uses and efficiency of compilers and interpreters • Description of the features and uses of scripting language (including creating and editing a macro) • Explanation of the need for and benefits of scripting languages • Description of the use of module libraries
Types of Languages • Programming languages may be classified according to their type. • Types of programming languages include: • Procedural; • Declarative; • Event-driven; & • Scripting
Procedural Languages • Definition • A language in which the user sets out a list of instructions in the correct sequence in order to solve a problem. • Features: • Data storage using variables of different data types; • Arithmetic and logical operations; • Program control using sequence, repetition and selection; • Subprograms or procedures; & • Data flow using parameters
Procedural Languages • A program written in a procedural language has: • a definite start and finish point; & • all the steps are in a sequential order. • Here is a typical algorithm which could be easily implemented in a HLL: • Get numbers • Calculate average • Display average
Procedural Languages • When a procedural language is run, the sequence of instructions is followed from the beginning of the program to the end in the order of the code. • For this reason, procedural languages are also known as imperative languages, since the word imperative in the context of the computer languages means giving or expressing commands. • VB, COMAL and Pascal are examples of procedural languages
Declarative Languages • The use of a declarative language is very different from the use of a procedural language. • Instead of giving a concise list of instructions set out in the correct order, a declarative language states ‘what has to be done’ rather than ‘how to do it’. • The code is written using a list of facts and rules rather than telling the program what to do.
Languages- declarative • Declarative languages like Prolog, contain sets of statements or clauses, like this: cold(snow). means that snow is cold cold(ice). means that ice is cold hot(molten_lava). means that molten lava is hot • which can be used to describe rules like: melts(X,Y):- means that X melts Y if hot(X), cold(Y). X is hot and Y is cold
Languages- declarative • The user enters queries into the system: ?hot(molten_lava). Which asks the question , is molten lava hot? This query would return yes. ?melts(molten_lave, X). This query would return X=Snow, X=Ice. • Rules of declarative languages: • Only variables have capitals. • Must have a full stop at the end.
Declarative Languages • The facts and rules in a declarative language program are known as the knowledge base. • When a query is entered, the declarative language looks for the solution in the knowledge base. • Features: • Recursion in a declarative language is equivalent to looping in a procedural language. • Recursion involves a rule referring to or calling itself. • Recursion must be used with care, because it is easy to enter an infinite loop, and if this happens, the program may cause the computer to hang or perhaps crash.
Event-Driven Languages • An event-driven language is a language which helps you to design the user interface by placing graphics on screen. The code can then be placed within these graphics/icons. • Because of this it is much quicker to design a program with a graphical user interface when using an event-driven language.
Event-Driven Languages • Event-driven languages have no definite start or end. • When the program is running, the user interacts with the event-driven language by selecting from a choice of screen objects like buttons, menus or windows. Clicking on one of these objects activates the program code associated with that object. • The action of clicking a screen object is called an event and the event is detected by the program.
Event-Driven Languages • The code below is written in the Visual Basic language: Private Sub GetName_Click() dim v_name as string v_name = inputbox(“Please enter your name :”) End Sub • By clicking the button “GetName” we run this code. • Runrev is also an event-driven language On MouseUp local v_name ask (“Please enter your name :”) put it into v_name End MouseUp
Scripting Languages • Features of scripting language • Scripting languages may be divided into two types: • those which are embedded within an application package and which make use of only those commands which are available within the package. • those which work with the operating system and across and between suitable applications.
Scripting Languages • A user of an application package would use a scripting language to automate a complex task that has to be carried out many times. • The user would then active the script by clicking a button or a keystroke rather than going through the task manually. • Programs written using a scripting language are often called macros.
Scripting Languages • A macro is a set of keystrokes and instructions that are recorded, saved and assigned to a single or combination of keys. • When the key code is typed, the recorded keystrokes and instructions are carried out.
Scripting Languages • Creating a macro • A macro may be created either by recording a sequence of keystrokes and mouse actions or by entering a suitable script in an appropriate language into an editor. • This macro, in Excel, creates a simple spreadsheet which adds 2 numbers. Sub Example() ‘Keyboard shortcut: option+cmd+shift+A Range(“B2”).Select ActiveCell.FormulaR1C1 = ‘number one’ Range(“B3”).Select ActiveCell.FormulaR1C1 = ‘number two’ Range(“C5”).Select ActiveCell.FormulaR1C1 = ‘=SUM(R[-3]C:R[-2]C)’ Range(“B5”).Select ActiveCell.FormulaR1C1 = ‘total’ End Sub
Scripting Languages • Editing a macro • A macro may be edited by making changes to its code and saving the new version. • The need for scripting languages arises from the requirements to be able to make changes to the commands or menus, or to automate tasks within an application package or an operating system. • Benefits: • They allow application packages to be enhanced with additional commands; • They automate tasks for an expert user; & • They allow a beginner to perform tasks that they would not otherwise be able to undertake.
Translator • Definition • A translator is a computer program used to convert high level program code to machine code. • Translators are used because it is very difficult to write programs directly into machine code (all 0’s and 1’s). It is easier for the programmer to write the program in an HLL and use a translator to change it into machine code. • There are two type of translator programs that we need to know about: • Compilers; & • Interpreters.
Compilers • A compiler translates a high level language program, the source code, into machine code which is the object code. • The complier changes each single high level language instruction into several machine code instructions. • Compilation involves analysing the source code, checking to see if it is correct and producing the object code. • These separate compilation stages are known as lexical analysis, syntax analysis and code generation.
Compilers • Once a program has been compiled and is working properly the user will run the program from the object code which can be saved and run separately from the source code. • This means that when the program runs it does not need to be re-translated which means that it is more efficient on memory and processor. • Machine code is specific to a processor. This means that machine code that runs on an Intel will not work on an AMD. Machine code programs are not portable.
Interpreters • An interpreter translates and runs a high level language program on instruction (or statement) at a time. • No object code is produced. • Interpreted programs run slower than compiled programs because each line must be translated every time the program is run. If the program has a fixed loop which runs 10 times the code will be interpreted 10 times. • An interpreter will report any syntax errors in the code as it is being developed.
Interpreter • Interpreted programs use more memory than compiled programs because the interpreter and source code must be present in the computer’s memory when the program is run. • Whereas with a compiled program only the object code is loaded into memory and run.
Comparison of Translators Compiler Interpreter Source Code (HLL program) Source Code (single line of HLL) Reports Errors Reports Errors Compiler Interpreter Get Next Instructions Object Code (A Machine code Program) Machine Code Instructions Object code is not executed immediately and can be saved Each instruction is executed immediately and cannot be saved.
Use of compilers and interpreters • When a program that uses an interpreter is being created, both the source code and interpreter are required to write, edit, test and use the completed program. • When a compiled program is being created, the source code and the compiler are needed at the writing and editing stages; the source code, object code and compiler are required at the testing stage and only the object code is required to use the completed program.
Module Libraries • A module library is part of a software library. • A software library is a collection of software held permanently accessible on backing storage. • A software library will typically include complete programs, a module library and a set of machine code routines, which may be loaded into user programs when required.
Module Libraries • Using a module library speeds up the whole software development process because time is saved in design, implementation, testing and documentation. • Saving time in design. • Using a module library saves time in program design because the design has already been created when the module was created in the first place.
Module Libraries • Saving time in implementation. • A programmer would not write a procedure each time it was needed, the procedure would simply be loaded from a module library. • Saving time in testing. • All of the modules in a module library are already tested and known to be free from errors. • Saving time in documentation. • Each module in a module library is already fully documented.