0% found this document useful (0 votes)
14 views52 pages

Chapter 1

The document provides an overview of computer programming, defining key concepts such as algorithms, programming languages, and the evolution of programming languages over time. It discusses the reasons for the existence of various programming languages, including evolution, special purposes, and personal preferences, as well as factors contributing to a language's success. Additionally, it covers compilation and interpretation processes, implementation strategies, and the importance of programming environments in supporting programmers.

Uploaded by

halisadam391
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views52 pages

Chapter 1

The document provides an overview of computer programming, defining key concepts such as algorithms, programming languages, and the evolution of programming languages over time. It discusses the reasons for the existence of various programming languages, including evolution, special purposes, and personal preferences, as well as factors contributing to a language's success. Additionally, it covers compilation and interpretation processes, implementation strategies, and the importance of programming environments in supporting programmers.

Uploaded by

halisadam391
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Programming Languages

(CSEg4306)

Chapter 1
Introduction
What is Computer program ?
ØA collection of instructions that perform a specific task when executed by a
computer.

What is computer programming ?


ØProgramming is the process of designing and building an executable computer
Program to accomplish a specific computing result.
ØProgramming is coding, modeling, simulating or presenting the solution to a
problem
• Problem: can be defined as a real-world problem need to develop a set of instructions.
• Algorithm: is a set of instructions /a step by step procedure/ how to solve a problem.
• Program: is the implementation of an algorithm in code.
Cont’d
Cont’d
Cont’d
What is programming language ?
ØA formal language, which comprises a set of instructions that produce various kinds of
output.
ØProgramming languages are used in computer programming to implement algorithms.

ØToday there are thousands of


high-level programming
languages, and new ones
continue to emerge.
Why are there so many Programming Languages ?
1. Evolution
ØComputer science is a young discipline; we’re constantly finding better ways to do
things.
ØLate 1960s and early 1970s = “structured programming “ ( goto-based control flow)
Ex : Fortran, Cobol, and Basic3, gave way to while loops, case (switch)statements,
and similar higher level constructs.
ØIn the late 1980s the nested block structure of languages like Algol, Pascal, and Ada
began to give way to the object-oriented structure of Smalltalk, C++, Eiffel, and so on.
Cont’d
Why are there so many Programming Languages ?
1. Evolution:
(GCD) of two integers, using Euclid’s algorithm
x86 (Pentium) Machine Language

Machine language is the sequence of bits that directly controls a processor


Cont’d
Why are there so many Programming Languages ?
1. Evolution:

(GCD) of two integers,


using Euclid’s algorithm
x86 assembly language

Assembly languages: were invented to allow operations to be expressed with mnemonic


Assembler :Translating from mnemonics to machine language became the job of a systems
program
Cont’d
Why are there so many Programming Languages ?
1. Evolution

(GCD) of two integers,


using Euclid’s algorithm
High-level language

Compiler: Translating from a


high-level language to assembly
or machine language is the job of
a systems program
Cont’d
Why are there so many Programming Languages ?
1. Evolution
Frameworks
ØA software framework is built on top of a programming language.
ØUsing a software framework to develop applications lets you focus on the high-level
functionality of the application.
ØThis is because any low-level functionality is taken care of by the framework itself.
ØDjango and Flask - web frameworks built on top of the Python programming language.
ØReact and Angular - front-end web frameworks built on top of the JavaScript programming
language.
Cont’d
Why are there so many Programming Languages ?
2. Special Purpose:-
ØMany languages were designed for a specific problem domain.
ØLisp dialects are good for manipulating symbolic data and complex data
structures.
ØIcon and Awk are good for manipulating character strings.
ØC is good for low-level systems programming.
ØProlog is good for reasoning about logical relationships among data.
ØEach of these languages can be used successfully for a wider range of tasks, but
the emphasis is clearly on the specialty
Cont’d
Why are there so many Programming Languages
3. Personal Preference:-
ØDifferent people like different things.
ØSome people love the terseness /shortness of C; some hate it.
ØSome people find it natural to think recursively; others prefer iteration.
ØSome people like to work with pointers; others prefer the implicit dereferencing
of Lisp, Clu, Java, and ML.
ØThe strength and variety of personal preference make it unlikely that anyone
will ever develop a universally acceptable programming language.
Cont’d
What makes a language successful?
1. Expressive Power
Ø Language features clearly have a huge impact on the programmer’s ability to
write clear, concise, & maintainable code, especially for very large systems.

2. Ease of Use for the Novice


ØSuccess due to its very low “learning curve.”
ØLogo is popular among elementary level educators
ØPascal was taught for many years in introductory programming language courses
ØIn recent years phyton , c++, Java has come to play a similar role.
Cont’d
What makes a language successful?
3. Ease of Implementation.
ØEasy implementation on tiny machines, with limited resources.

4. Standardization
ØStandardization of both the language and a broad set of libraries is the only truly
effective way to ensure the portability of code across platforms.
Cont’d
What makes a language successful?
5. Open Source.
ØMost programming languages today have at least one open-source
compiler or interpreter.
ØLinux, the leading open-source operating system, is written in C.

6. Excellent Compilers
ØFortran owns much of its success to extremely good compiler.
ØIt is a matter of language design
Cont’d
What makes a language successful?
7. Economics, Patronage and Inertia
ØThere are factors other than technical merit that greatly influence success.
ØThe backing of a powerful sponsor.
ØPL/I, at least to first approximation, owes its life to IBM.
ØCobol and, more recently, Ada owe their life to the U.S. Department of Defense.
ØC#, despite its technical merits, would probably not have received the attention it
has without the backing of Microsoft.
Why Study Programming Languages?
1. To have good understanding of language design and implementation.
Øhelp one choose the most appropriate language for any given task.
ØMost languages are better for some things than for others.
Øshould one choose C, C++, or C# for systems programming?
ØFortran or C for scientific computations?
ØPHP or Ruby for a web-based application?
ØAda or C for embedded systems?
ØVisual Basic or Java for a graphical user interface?
Cont’d
Why Study Programming Languages?
2. Make it easier to learn new languages.
ØMany languages are closely related.
ØJava and C# are easier to learn if you already know C++;
ØCommon Lisp if you already know Scheme;
ØHaskell if you already know ML.
3. To learn basic concepts that underlie all programming languages.
ØTypes, control (iteration, selection, recursion, non determinacy, concurrency),
abstraction, and naming.
ØThinking in terms of these concepts makes it easier to assimilate the syntax (form) and
semantics (meaning) of new languages, compared to picking them up in a vacuum.
Programming Language Categories

• The many existing languages can be classified into families based on their model of
computation
Two common language groups:-
Imperative (focus is on how the computer should do it)
Øvon Neumann (Fortran, Pascal, Basic, C)
Øobject-oriented (Smalltalk, Eiffel, C++, Java)
Øscripting languages (Perl, Python, JavaScript, PHP)
Declarative (focus is on what the computer is to do )- The top-level division.
Øfunctional (Scheme, ML, pure Lisp, FP)
ØLogic constraint-based (Prolog, VisiCalc, RPG/ Report Program Generator)
Compilation and Interpretation
Compilation
ØThe compiler translates the high-level source program into an equivalent target
program (typically in machine language), and then goes away.
ØThe compiler is the locus of control during compilation; the target program is the
locus of control during its own execution.
ØThe compiler is itself a machine language program, presumably created by
compiling some other high-level program.
ØWhen written to a file in a format understood by the operating system, machine
language is commonly known as object code.
ØSemantic error checking can be performed statically
Cont’d
Compilation and Interpretation

At the highest level of abstraction, the compilation and execution of a program in a


high-level language look something like this:
Cont’d
Interpretation
ØAn alternative style of implementation for high-level languages is known as
interpretation.
ØUnlike a compiler, an interpreter stays around for the execution of the application.
ØIn fact, the interpreter is the locus of control during that execution.
ØThe interpreter reads statements in that language more or less one at a time,
executing them as it goes along.
ØSemantic error checking performed at run time
ØIn general, interpretation leads to greater flexibility and better diagnostics (error
messages) than does compilation.
vBecause the source code is being executed directly, the interpreter can include an excellent
source-level debugger.
Cont’d
Interpretation
Cont’d
Compilation and Interpretation
While the conceptual difference between compilation and interpretation is clear, most
language implementations include a mixture of both.
Implementation strategies -1 :- Preprocessing
ØMost interpreted languages employ an initial translator (a preprocessor) that removes
comments and white space, and groups characters together into tokens such as keywords,
identifiers, numbers, and symbols.
ØThe translator may also expand abbreviations in the style of a macro assembler. Finally, it may
identify higher-level syntactic structures, such as loops and subroutines.
ØThe goal is to produce an intermediate form that mirrors the structure of the source, but can be
interpreted more efficiently.
ØIn some very early implementations of BASIC, the manual actually suggested removing
comments from a program in order to improve its performance. These implementations were
pure interpreters;
Ø Re-read (and then ignore) the comments every time they executed a given part of the program.
Cont’d
Implementation strategies -2:- Library routines and linking
ØThe typical Fortran implementation comes close to pure compilation.
ØThe Library routines and linking compiler translates Fortran source into machine
language.
ØUsually, however, it counts on the existence of a library of subroutines that are not part
of the original program.
ØExamples include mathematical functions (sin, cos, log, etc.) and I/O.
ØThe compiler relies on a separate program, known as a linker, to merge the appropriate
library routines into the final program:
Cont’d
Implementation strategies -2
Cont’d
Implementation strategies -3:- Post-compilation assembly

ØMany compilers generate assembly language instead of


machine language.
ØThis convention facilitates debugging, since assembly
language is easier for people to read.
Cont’d
Implementation strategies -4:- The C preprocessor
ØCompilers for C (and for many other languages running
under Unix) begin with a preprocessor that removes
comments and expands macros /E.g #define PI 3.14/

ØThe preprocessor can also be instructed to delete


portions of the code itself, providing a conditional
compilation facility that allows several versions of a
program to be built from the same source.
Cont’d
Implementation strategies -5:- Source-to-source translation (C++)
ØA surprising number of compilers generate output in some high-level language
commonly C or some simplified version of the input language.
ØSuch source-to-source translation is particularly common in research languages and
during the early stages of language development.
ØOne famous example was AT&T’s original compiler for C++. This was indeed a true
compiler, though it generated C instead of assembler: it performed a complete analysis
of the syntax and semantics of the C++ source program, and with very few exceptions
generated all of the error messages that a programmer would see prior to running the
program. (cfront)
Cont’d
Implementation strategies -5
Cont’d
Implementation strategies -6:- Bootstrapping
ØBootstrapping widely used compilation development, a type of compiler used to
compile its own source code.
ØMany compilers are self-hosting: they are written in the language they compile Ada
compilers in Ada, C compilers in C.
ØThis raises an obvious question: how does one compile the compiler in the first place?
o The answer is to use a technique known as bootstrapping .

ØJust-in-time compiler that translates byte code into machine language immediately
before each execution of the program
Cont’d
Implementation strategies -6
ØA Pascal compiler, written in Pascal, that would generate output in P-code, a stack-
based language similar to the byte code of modern Java compilers
ØThe same compiler, already translated into P-code
ØA P-code interpreter, written in Pascal

Machine code/ p-code is a low-level code


can be directly understood by computers
Byte code is produced as intermediate code
produced after the source code is compiled
Programming Environments
ØCompilers and interpreters do not exist in isolation.
ØProgrammers are assisted in their work by a host of other tools.
ØAssemblers, debuggers, preprocessors, linkers , Editors were mentioned earlier.
ØEditors allow the programmer to find the point at which an object is defined.
ØStyle checkers enforce syntactic or semantic conventions that may be tighter than those
enforced by the compiler .
ØConfiguration management tools help keep track of dependencies among the (many
versions of) separately compiled modules in a large software system.
ØProfilers and other performance analysis tools often work in conjunction with
debuggers to help identify the pieces of a program that consume the bulk of its
computation time.
Cont’d
Programming Environments

Prettyprint is the process of converting


source code or other objects in a legible
and attractive way
An Overview of Compilation
Phases of Compilation

source program/
front end of the
compiler

target program/
back end of the
compiler
Cont’d
An Overview of Compilation
ØThe first few phases (up through semantic analysis) serve to figure out the meaning of
the source program. They are sometimes called the front end of the compiler.
ØThe last few phases serve to construct an equivalent target program. They are sometimes
called the back end of the compiler.
Øcompilation is a series of passes/Phases. If desired, a pass may be written as a separate
program, reading its input from a file and writing its output to a file.
ØCompilers are commonly divided into passes so that the front end may be shared by
compilers for more than one machine (target language) .
ØScanning and parsing serve to recognize the structure of the program, without regard to
its meaning.
Cont’d
Scanner (lexical analysis)
ØDone by the lexer, also known as the scanner.
ØIdentifies lexemes /alphanumeric characters/within the source code
ØWhite space and comments are discarded
ØGenerates a stream of tokens (such as :constants, identifiers, operators, reserved words, and separators)
ØEach token passed to the parser on request
ØCreates a symbol table of 'names' used in the source code
ØMay also create a strings table
ØVery limited error checking at this stage
Ø Tokens: smallest meaningful units of the program.
Cont’d
Scanner (lexical analysis)
Cont’d
Parser (syntax analysis)
• Parsing organizes tokens into a parse tree that represents higher-level constructs (statements,
expressions, subroutines, and so on) in terms of their constituents.
• Checks that the rules of the language have been followed correctly.
• Syntax can be defined using a context free grammar.
• Backus-Naur Form is a well known notation for communicating grammar. Grammar is applied
programmatically to build a parse tree.
• Parse tree is refined to become an abstract syntax tree. Abstract syntax tree traversed several times
during semantic analysis
• Symbol table frequently accessed and updated Symbol table often implemented as a hash table
• Polish (+ab) & reverse Polish (ab+) represent expressions without parentheses. Expressions can be
represented using trees or directed acyclic graphs
Parser (syntax analysis)
Cont’d
Cont’d
Semantic analysis
ØSemantic analysis is the discovery of meaning in a program.
ØUsing the symbol table, the semantic analyzer enforces a large variety of rules that are not
captured by the hierarchical structure of the context-free grammar and the parse tree.
ü Undeclared variables
ü Multiple declarations within the same scope Misuse of reserved identifiers
ü Attempting to access a variable that is out of scope
ü Type mismatches
ü Parameter type mismatches when calling functions and procedures
ü Function return value must match the specified return type
ü Arithmetic operators must operate on numeric types
ü The condition in an If statement must evaluate to true or false
ü The exit condition of a loop must evaluate to true or false
Cont’d
Semantic analysis
Cont’d
Semantic analysis
ØNot all semantic rules can be checked at compile time.
qThose that can are referred to as the static semantics of the language that can be
checked at compile time.
q Those that must be checked at run time are referred to as the dynamic semantics
of the language.
ØStatic objects are constructs (identifiers, statements, expressions etc.) & Dynamic
objects are (instances of) values, locations and the like.
ØC has very little in the way of dynamic checks.
Cont’d
Semantic analysis
ØExamples of rules that other languages enforce at run time include the following.
✔ Variables are never used in an expression unless they have been given a value.
✔ Pointers are never dereferenced unless they refer to a valid object.
✔ Array subscript expressions lie within the bounds of the array.
✔ Arithmetic operations do not overflow.

ØIn the process of checking static semantic rules, the semantic analyzer typically
transforms the parse tree into an abstract syntax tree (otherwise known as an AST, or
simply a syntax tree) by removing most of the “artificial” nodes in the tree’s interior.
Cont’d
Lexical and Syntax Analysis
Intermediate form
ØIntermediate form (IF) done after semantic analysis (if the program passes all checks)
ØIFs are often chosen for machine independence, ease of optimization, or compactness.
ØThey often resemble machine code for some imaginary idealized machine.
ØMany compilers actually move the code through more than one IF
qWrite three address code for the following
Optimization / Code generation phase
ØOptimization takes an intermediate-code program and produces another one that does
the same thing faster, or in less space.
qThe optimization phase is optional
qRemoves unnecessary code line and arranges the sequence of statements
ØCode generation phase produces assembly language or (sometime) relocatable
machine language
qThus code improvement often appears as two additional phases of compilation, one
immediately after semantic analysis and intermediate code generation, the other
immediately after target code generation.
Section wide Group Project
Objective
Design and Implementation of a New Programming Language.
Define Purpose and Feature
What will this language be used for?
Interpreted or Compiled?
What programming paradigm will it follow?
What are the basic data types, control structures, and unique features?
Name for the Language.

You might also like