Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

ADVANCED PROGRAMMING LANGUAGES

CHAPTER 1. INTRODUCTION

What is a programming language? They are notations used for specifying, organizing, and reasoning about computations. Reasons for Studying Concepts of Programming Languages 1. Increased capacity to express ideas 2. Improved background for choosing appropriate languages 3. Increased ability to learn new languages 4. Better understanding of the significance of implementation 5. Increased ability to design new languages Programming Domains 1. Scientific Applications 2. Business Applications 3. Artificial Intelligence 4. Scripting Languages 5. Systems Programming Languages 6. Special-purpose Languages Language Evaluation Criteria Readability ease with which programs can be read and understood Writability - measure of how easily a language can be used to create programs for a chosen problem domain Reliability - it performs to its specification under all conditions Cost - cost of training programmers, cost of writing programs in the language (time, space). Cost of maintaining programs. Portability - ease with which a program can be moved from one implementation to another Generality - applicability of a language to a wide-range of applications Views of What Programs are for Original View: The purpose of software is to control computers Modern View: The purpose of software is to specify computations (and the purpose of computers is to execute the software). A programs most important audience is not machine but people. Why are there so many programming languages? Many different machines (old view) Many different applications Need for better tools (yielding software thats more trustworthy, faster and less expensive) Early programming Languages 1. Machine Languages (1940s) Machine-specific (non-portable) Verbose (wordy) Modularity: zero 2. Assembly Language (1950s) A variant of a machine language in which names and symbols take the place of the actual codes for machine operations, values and storage locations, making individual instructions more readable. High-level Languages (1960s) Readable familiar notations which were designed to have the following benefits: (a) portability, (b) availability of program libraries and (c) consistency checks during implementation that can detect errors, (d) readability, (e) modularity subroutines/procedures. First high-level languages: FORTRAN, COBOL, ALGOL

3.

Transition from Machine to High-Level Languages brought about Small increase in cost of use as software became slightly bigger and slower Large reduction in the cost of development as software became much cheaper, more portable, and more reliable. Language Designers balance Making computing convenient for people with Making efficient use of computing machines * Convenience comes first. Without it, efficiency is irrelevant. Programming Languages are designed to be both Higher-level - if it is independent of the underlying machine General-purpose - if it can be applied to a wide-range of problems A programming language helps if It is small and simple as possible (but no simpler) It suppresses unnecessary detail It consists of a few parts that fit together in simple ways It supports modularization (the process of reorganizing a program so that related program parts are collected together and considered as a single module.) Programs are readily built-up from separate subprograms (modules) Modules interfaces are clear and simple It supports sharing and reuse of program parts. Programming Paradigms (new ways of thinking about programming) 1. Imperative Programming based on the way of thinking that the computer is dumb. Imperative Languages o are action-oriented; that is, a computation is viewed as a sequence of actions o Examples: Fortran - for scientific programming Algol - the description of numerical processes in publications Pascal - teaching language C - implementation language for software; general-purpose Language Object-oriented Programming Languages o Designed as both a description language and a programming language o Based on the way of thinking that everything can be treated as classes of objects which have both properties and behavior. o Examples: C++, Java, Smalltalk

2.

Declarative Programming based on the way of thinking that the computer is intelligent. Functional Programming Languages o Are recursive or growing; that is; a function can be used to create a bigger function which, in turn, can be used to create another bigger one and so on. o Examples: LISP (List Processor) - designed for applications in AI. Logic Programming o Languages that have the brevity and expressiveness of logic o Examples: Prolog - developed for natural language processing SQL - developed for database applications

Language Implementation

Virtual Fortran Computer Fortran Compiler LISP Interpreter LISP interpreter C++ Compiler

Virtual C++ Computer

Operating System Macroinstruction interpreter Bare machine

Virtual LISP Computer

Assembler Virtual Assembly Language Computer

C Compiler

Virtual C Computer

Layered interface of virtual computers, provided by a typical computer system

Operating system A large collection of programs which supplies higher-level primitives than those of a machine language Primitives provide system resource management, input and output operations, file management system, text and/or program editors, and a variety of other commonly needed functions. Macroinstruction Machine language of the computer Compiler Translates a program into a code that runs The target code is run at runtime
Source Program

Lexical Analyzer Lexical units Symbol Table Syntax Analyzer Parse Trees Intermediate Code Generator Intermediate Code Code Generator Input data Machine Language Computer Optimization (optional)

Results

Interpreter Takes a program and its input at the same time. It scans the program, implementing operations as it encounters them, and doing I/O as needed. Execution of each statement of a program is done one at a time.

Source Program

Input data Interpreter

Results

Hybrid Implementation Systems A compromise between compilers and pure interpreters They translate high-level language programs to an intermediate language designed to allow easy interpretation. Faster than pure interpretation because the source language statements are decoded only once.
Source Program

Lexical Analyzer Lexical units Syntax Analyzer Parse Trees Intermediate Code Generator Input data Intermediate Code

Interpreter

Results

Compilation vs. Interpretation Compilation can be more efficient than interpretation o Compilation can be an order of magnitude faster than interpretation for the same programming language. o Interpreter examines the program repeatedly while it is executing which could be the source of inefficiency. However, there are 2 forms of performance penalties due to compilation: o Machine time is needed to compile a source program into target code o The target code created by a compiler typically takes longer to run and occupies more space than carefully written machine code.

Interpretation can be more flexible than compilation o The interpreter directly runs the source program, so it can allow programs to be changed on the fly to add features or correct errors. o An interpreter works with the source text, so it can pinpoint an error in the source text and report it accurately

However, with a compiler, o All translations are completed before the target program is run, which prevents the target program from being readily adapted as it runs. Phases of a Compiler Lexical Analyzer o scanning and grouping of characters of the source program statements into tokens such as identifier, operator, constant, etc. Syntax Analyzer o parsing which involves grouping of the tokens of the source program into grammatical phrases that are used by the compiler to synthesize output. Usually, the grammatical phrases of the source program are represented by a parse tree. Semantic Analyzer o Checks the source program for semantic errors and gathers type information for the subsequent codegeneration phase. It uses the hierarchical structure determined by the syntax analysis phases to identify the operators and operands of expressions and statements. Intermediate Code Generator o An explicit intermediate representation of the source program which can be thought of as a program for an abstract machine. This intermediate representation should be easy to produce and easy to translate into the target program. Code Optimizer o Attempts to improve the intermediate code, so that faster-running machine code will result Code Generator o Final phase of the compiler in the generation of the target code, consisting normally of relocatable machine code or assembly code Symbol Table o A data structure containing a record for each identifier, with fields for the attributes of the identifier Error Handler o A phase that deals with errors that are encountered so that compilation can proceed, allowing further errors in the source program to be detected.

Example:
position = initial + rate * 60

Lexical analyzer

id1 = id2 + id3 * 60

Syntax analyzer

= id1 id2 id3 Semantic analyzer + * 60

= id1 id2 id3 + * int to real 60 Intermediate code generator

temp1 = inttoreal (60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3

Code optimizer

temp1 = id3 * 60.0 id1 = id2 + temp1

Code generator

MOVF id3 , R2 MULF #60.0, R2 MOVF id2 , R1 ADDF R2, R1 MOVF R1, id1

You might also like