Nothing Special   »   [go: up one dir, main page]

CSC 432 With PQ

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 32

Texts in red are contents that are not in the materials👌

Programming Language
- It is a computer language that is used by programmers to communicate with computers.
- It is a set of instructions written in any specific language.

It is used to develop website, mobile and desktop applications.

Benefits of programming language as a course


1. Studying concepts of programming languages can help you express your ideas and
algorithms more effectively.

2. Understanding different programming languages can also help you choose the most
appropriate language for a given task. Without this knowledge, it's easy to fall back on
the language you're most familiar with, even if it's not the best fit for the job.

3. Knowing the concepts behind different programming languages can also make it easier
to learn new languages. For example, if you know the concepts behind object-oriented
programming, it will be easier to learn a language like Java that uses these concepts.

4. Studying programming languages can also give you a better understanding of the
significance of implementation. How you implement an algorithm can have a big impact
on how well it performs and how easy it is to maintain.

5. Knowing the concepts behind different programming languages can also help you use
the languages you already know more effectively.

6. Studying the concepts of programming languages can contribute to the advancement of


computing by giving you the tools to create better, more efficient software.

APPLICATION DOMAINS
1. Scientific Applications
2. Data processing Applications
3. Text processing Applications
4. Artificial intelligence Applications
5. Systems Programming Applications
6. Web software

1. Scientific Applications: The use of programming languages and software


development techniques to create programs and simulations that aid in the
advancement of scientific research. These programs and simulations can help
scientists process and understand large amounts of data, make predictions, and
test hypotheses. Some examples of programming languages and tools
commonly used in scientific computing include Python, C/C++, Fortran, MATLAB,
and Pascal.
2. Data processing Applications: Those programming languages used in the
creation, maintenance, extraction and summarization of data in records and files.
Python, R, and SQL, Cobol

3. TEXT PROCESSING APPLICATIONS: are characterized as those whose


principal activity involves the manipulation of natural language text, rather than
numbers as their data. SNOBOL and C language have strong text processing
capabilities.
4. ARTIFICIAL INTELLIGENCE APPLICATIONS: are characterized as those
programs which are designed principally to emulate intelligent behavior. They
include game playing algorithms such as chess, natural language understanding
programs, computer vision, robotics and expert systems. LISP has been the
predominant AI programming language, and also PROLOG using the principle of
‘’Logic programming’’ Lately AI applications are written in Java, c++ and python.
5. SYSTEMS PROGRAMMING APPLICATIONS: involve developing those
programs that interface the computer system (the hardware) with the
programmer and the operator. These programs include compilers, assembles,
interpreters, input-output routines, program management facilities and schedules
for utilizing and serving the various resources that comprise the system. Ada and
Modula – 2 are examples of programming languages used here. Also C.
6. WEB SOFTWARE: This is a collection of languages, they include
- Markup (e.g. XHTML)
- Scripting for dynamic content under which we have the Client side, using scripts
embedded in the XHTML documents e.g. Javascript, PHP, Server side using the
common Gateway interface e.g. JSP, ASP, PHP
- General- purpose, executed on the web server through cGI e.g. Java, C++

CRITERIA FOR LANGUAGE EVALUATION AND COMPARISON


1. Expressivity: This means the ability of a language to clearly reflect the
meaning intended by the programmer. (it encourages the use of control
structures like if-then-else and while loop.
2. Well-Definedness: We mean that the language’s syntax and semantics
are free of ambiguity, are internally consistent and complete.
3. Data types and structures: we mean the ability of a language to support
a variety of data values (integers, real, strings, pointers etc.)
4. Modularity
5. Input-Output facilities
6. Portability: one which is implemented on a variety of computers
7. Efficiency: a language which permits fast compilation and execution on
the machines where it is implemented
8. Pedagogy: Easy to teach and to learn
9. Generality: Means that a language is useful in a wide range of
programming applications.

INFLUENCES ON LANGUAGE DESIGN

1. Computer Architecture: Languages are developed around the prevalent computer


architecture, known as the Von Neumann architecture (the most prevalent computer
architecture).

2. Programming Methodologies: New software development methodologies (e.g. object


Oriented Software Development) led to new paradigms in programming and by extension, to
new programming languages.

LANGUAGE PARADIGMS (Developments in Programming Methodology)

1. Imperative: This is designed around the Von Neumann architecture. Computation is


performed through statements that change a program’s state. Central features are variables,
assignment statements and iteration, sequence of commands, explicit state update via
assignment. Examples of such languages are Fortran, Algol, Pascal, c, c++, Java, Perl,
Javascript, Visual BASIC.NET, python.

2. Functional: Here, the main means of making computations is by applying functions to


parameters. Examples are LISP, Scheme, ML, Haskell, F#. It may also include OO (Object
Oriented) concepts. They can be applied to languages like Java, python, c#.

3. Logic: This is Rule-based (rules are specified in no particular order). Computations here are
made through a logical inference process. Examples are PROLOG, Mercury, oz, and CLIPS.
This may also include OO concepts. It uses logical statements to express desired behavior of a
program.

TRADE-OFFS IN LANGUAGE DESIGN

1. Reliability Vs. Cost of Execution: For example, Java demands that all references to array
elements be checked for proper indexing, which leads to increased execution costs.
2. Readability vs. Writability: - APL provides many powerful operators (and a large number of
new symbols), allowing complex computations to be written in a compact program but at the
cost of poor readability.
3. Writability (Flexibility) vs. reliability: The pointers in c++ for instance are powerful and very
flexible but are unreliable.

IMPLEMENTATION METHODS
1. Compilation – Programs are translated into machine Language & System calls
2. Interpretation – Programs are interpreted by another program (an interpreter)
3. Hybrid – Programs translated into an intermediate language for easy interpretation
4. Just –in-time – Hybrid implementation, then compile sub programs code the first time they are
called.
5. Pure Interpretation

COMPILATION
- Translates high level program (source language) into machine code (machine language)
- Slow translation, fast execution
- Compilation process has several phases
*Lexical analysis converts characters in the source program into lexical units (e.g.
identifiers, operators, keywords).
*Syntactic analysis: transforms lexical units into parse trees which represent the
syntactic structure of the program.
*Semantics analysis checks for errors hard to detect during syntactic analysis; generate
intermediate code.
*Code generation – Machine code is generated

INTERPRETATION
- Easier implementation of programs (run-time errors can easily and immediately be displayed).
- Slower execution (10 to 100 times slower than compiled programs)
- Often requires more memory space.
- Interpreters are usually implemented as a read-eval-print loop:
*Reads the expression in the input language (usually translating it in some internal form)
*Evaluates the internal forms of the expression
*Print the result of the evaluation
*Loops and reads the next input expression until exit
- Interpreters act as a virtual machine for the source language:
*Fetch execute cycle replaced by the read-eval-print loop
*Usually has a core component, called the interpreter “run-time” that is a compiled
program running on the native machine.

HYBRID IMPLEMENTATION
- This involves a compromise between compilers and pure interpreters. A high level program is
translated to an intermediate language that allows easy interpretation.
- Hybrid implementation is faster than pure interpretation. Examples of the implementation occur
in Perl and Java.
*Perl programs are partially compiled to detect errors before interpretation.
*Initial implementations of Java were hybrid.

JUST-IN-TIME IMPLEMENTATION
Just-in-time (JIT) compilation is a method of improving the performance of a computer program
by compiling the program into machine code at the time it is executed, rather than ahead of
time. This implementation initially translates programs to an intermediate language then
compiles the intermediate language of the subprograms into machine code when they are
called. JIT compilation is often used in virtual machines, such as the Java Virtual Machine
(JVM) and the Common Language Runtime (CLR), to improve the performance of interpreted
languages such as Java and C#.

Why is it useful for a programmer to have some background in language design, even
though he or she may never actually design a programming language?
1. Understanding how languages work.
2. Problem-solving skills
3. Debugging and performance optimization
4. Innovation: Understanding the history and design of programming languages can inspire
a programmer to come up with new ideas for programming languages and tools that can
improve the development experience.

What does it mean for a program to be reliable?


Correctness: The program must produce the correct results for a given input.
Robustness: The program must be able to handle unexpected or invalid inputs without
crashing or producing incorrect results.
Maintainability: The program should be easy to understand, change, and test, so that
bugs can be identified and fixed easily.
Scalability: The program should be able to function well and continue to produce correct
results even when the amount of data or the number of users increases.

What is: Aliasing?; What is exceptional handling?


Aliasing: refers to the phenomenon where two different variables refer to the same
memory location. This can occur when a program creates multiple references to the same
object, or when a function or method passes a reference to an object as an argument and then
modifies the object through that reference. Aliasing can cause unexpected behavior in a
program, because changes made to the object through one reference will be visible through all
other references to that object.

Exception handling: Exception handling is a mechanism in programming languages that allows


a program to handle and recover from exceptional conditions, such as run-time errors or
unexpected inputs. Exceptions are typically represented by objects that are thrown and caught
by the program. When an exception is thrown, the program's normal execution flow is
interrupted and control is transferred to a special block of code called an exception handler. The
exception handler can then take appropriate action, such as displaying an error message,
logging the exception, or trying to recover from the error.

Why is readability important in writability?


Readability is important in writing because it helps the audience understand and engage with
the content. Clear and easy-to-read writing is more likely to hold the reader's attention and
convey the intended message effectively. Additionally, using simple language, proper grammar,
and logical organization can also make a text more accessible to a wider range of readers,
including those with limited reading skills or for whom English is not their first language.

What are the three fundamental features of an object-oriented language?


1. Encapsulation: objects are used to encapsulate data and behavior, providing a way to
hide the implementation details of an object from the rest of the system.
2. Inheritance: objects can inherit characteristics and behavior from parent objects,
allowing for code reuse and a hierarchy of objects.
3. Polymorphism: objects can be treated as instances of a more general class, allowing for
more flexibility in the code and the ability to use objects of different types
interchangeably.

What role does the symbol table play in the compiler?


A symbol table is used by a compiler to store information about the program being compiled,
such as the names of variables, functions, and types. The symbol table is typically used during
the parsing and semantic analysis phases of the compilation process.

What does a linker do?


A linker is a program that takes one or more object files generated by a compiler and combines
them into a single executable program or a shared library.

What are the advantages in implementing a language with a pure interpreter?


1. Simplicity: Interpreters are generally simpler to implement than compilers because they
do not need to perform tasks such as code optimization and code generation.
2. Portability: Interpreters can be easily ported to different platforms because they do not
need to be recompiled for each platform.
3. Flexibility: Interpreters can be easily modified to add new features or change the
behavior of the language.
4. Debugging: Interpreters can provide more detailed error messages and better support
for debugging because they can execute the source code line by line.
5. Interactivity: Interpreters can provide an interactive environment for running and
experimenting with code, which can be useful for prototyping and testing.
6. Accessibility: Interpreters can be used to run code on devices where installing a compiler
would be impractical or impossible.

What are the three general methods of implementing a programming language?


Compilation: The source code of a program is translated into machine code by a compiler,
which can then be executed directly by the computer's hardware.

Interpretation: The source code of a program is read and executed by an interpreter, which
converts the code into machine instructions on-the-fly.

Transpilation: The source code of a program is translated into the source code of another
language. That source code is then compiled or interpreted.

Which produces faster program execution, a compiler or a pure interpreter and how?

Generally, a compiled program will execute faster than a program interpreted by a pure
interpreter. This is because a compiler generates machine code, which the computer's
hardware can execute directly, whereas an interpreter must interpret the source code line by
line and convert it into machine instructions on-the-fly.

A BRIEF HISTORY OF PROGRAMMING LANGUAGES.


Assembly languages
IBM 704 and Fortran–FORmula TRANslation
LISP –LISt Processing
ALGOL 60 – International Algorithmic language
Simul 67 – First object oriented language
Ada – history’s largest design effort
C++ - Combining Imperative and Object – Oriented Features
Java – An Imperative – Based Object – Oriented language
Prolog– Logic Programming

Types of programming language


1. Low-level language: Low-level language is a machine-dependent programming
language. It is divided into two

a. Machine Language: It is also called machine code or object code. Machine


language is easier to read because it is normally displayed in binary or
hexadecimal form (base 16). It does not require a translator to convert the
programs because computers directly understand the machine language
programs. The advantage of machine language is that it helps the programmer to
execute the programs faster than the high-level programming language.

b. Assembly Language- ASM is a type of low-level programming language that is


designed for specific processors. It represents the set of instructions in a
symbolic and human-understandable form. It uses an assembler to convert the
assembly language to machine language. The advantage of assembly language
is that it requires less memory and less execution time to execute a program.

2. High-level language (HLL) is designed for developing user-friendly software programs


and websites. This programming language requires a compiler or interpreter to translate
the program into machine language (execute the program). The main advantage of a
high-level language is that it is easy to read, write, and maintain. High-level
programming language includes Python, Java, JavaScript, PHP, C#, C++, Objective C,
Cobol, Perl, Pascal, LISP, FORTRAN, and Swift programming language.

High-level language is further divided into three parts


i. Procedural Oriented programming (POP) language is derived from
structured programming and based upon the procedure call concept. It divides a
program into small procedures called routines or functions. The advantage of POP
language is that it helps programmers to easily track the program flow and code can be
reused in different parts of the program. Example: C, FORTRAN, Basic, Pascal,
COBOL, etc

ii. Object-Oriented Programming (OOP) language is based upon the objects.


In this programming language, programs are divided into small parts called objects. It is
used to implement real-world entities like inheritance, polymorphism, abstraction,etc in
the program to make the program reusable, efficient, and easy-to use. The main
advantage of object-oriented programming is that OOP is faster and easier to execute,
maintain, modify, as well as debug. Example: C++, Java, Python, C#, Ruby, etc.

iii. Natural language is a part of human languages such as English, Russian,


German, and Japanese. It is used by machines to understand, manipulate, and interpret
human's language. The main advantage of natural language is that it helps users to ask
questions in any subject and directly respond within seconds.

3. Middle-level programming language lies between the low-level and high-level


programming language. It is also known as the intermediate programming language and
pseudo-language. A middle-level programming language's advantages are that it
supports the features of high-level programming, it is a user-friendly language, and
closely related to machine language and human language. Example: C, C++, language

Categories of all programming languages. It is divided into

1. Interpreted Programming Languages: its implementations execute instructions


directly, without previously compiling a program into machine-language
instructions. The interpreter executes the program directly. Ex. apl, basic, eiffel,
lisp, pascal, perl, python, ruby.
2. Functional Programming Languages: They focus on the application of functions.
Many of the functional programming languages are bound to mathematical
calculations. Ex. F#, joy, Kite, Q.
3. Compiled Programming Languages: A compiled language is a programming
language whose implementations are typically compilers (translators that
generate machine code from source code). Ex. C, C++, C#, Cobol, Cobra,
Fortran, Java, Visual Basic.

4. Procedural Programming Languages: Procedural (imperative) programming


implies specifying the steps that the programs should take to reach to an
intended state. A procedure is a group of statements that can be referenced
through a procedure call. Procedures help in the reuse of code. Ex. MATLAB,
PL/I, Component Pascal, Go, Curl.

5. Scripting Programming Languages: Scripting languages are programming


languages that control an application. Scripts can execute independent of any
other application. They are mostly embedded in the application and they
control tasks. Ex. F-Script, VBScript, php, Windows PowerShell, AppleScript, R

6. Markup Programming Languages: A markup language is an artificial language


that uses annotations to text that define how the text is to be displayed. Ex.
Curl, HTML, XML, XHTML,

7. Logic-Based Programming Languages: Logic programming is a type of


programming paradigm which is largely based on formal logic. Any program
written in a logic programming language is a set of sentences in logical form,
expressing facts and rules about some problem domain. Ex prolog, roop, oz

8. Concurrent Programming Languages: Concurrent programming languages are a


type of programming language that are designed to handle multiple tasks or
processes at the same time. either within a single computer, or across a number
of systems. Ex. E, limbo, SR, ABCL,

9. Object-Oriented Programming Languages: Object-oriented programming (OOP)


is a programming paradigm based on the concept of “objects”, which may
contain data, in the form of fields, often known as attributes; and code, in the
form of procedures, often known as methods. Ex. Agora, BETA, Lava, Cecil,
Oxygene

Most commonly used Programming Language


Python, Java, C, C++, C#, JavaScript, R, PHP, Go, Ruby

Expression Notations
B * C: This type of notation is referred to as infix since the operator is in between the two
operands that it is working on. Consider another infix example, A + B * C. The operators + and *
still appear between the operands, but there is a problem. Which operands do they work on?
Does the + work on A and B or does the * take B and C? The expression seems ambiguous.

Each operator has a precedence level. Operators of higher precedence are used before
operators of lower precedence. The only thing that can change that order is the presence of
parentheses. The precedence order for arithmetic operators places multiplication and division
above addition and subtraction. If two operators of equal precedence appear, then a left-to-right
ordering or associativity is used.

One way to write an expression that guarantees there will be no confusion is to create a fully
parenthesized expression. (one pair of parentheses for each operator). There is no need to
remember any precedence rules. The expression A + B * C + D can be rewritten as ((A + (B *
C)) + D).

Prefix expression notation requires that all operators precede the two operands that they work
on.
Postfix, on the other hand, requires that its operators come after the corresponding operands.

A + B * C would be written as + A * B C in prefix.

In postfix, the expression would be A B C * +.

(A + B) * C (infix) * + A B C (prefix) A B + C * (postfix)


(A + B) * (C + D) (infix) * + A B + C D (prefix) A B + C D + * (postfix)
A + B + C + D (infix) + + + A B C D (prefix) A B + C + D + (postfix)

They talked about how postfix and prefix works using stacks. How they can be
rearranged / Converted
Page (5-12) CSC 432 Lecture 3.pdf

Infix notation: operators are written in-between their operands. X+Y. The language uses
operator precedence and associativity, and brackets ( ) allow users to override these rules.

Postfix notation: operators are written after their operands. XY+ . It is also called reverse polish
notation. The order of evaluation is left to right.

Prefix notation: operators are written before their operands. +XY. It is also called polish notation.
Although Prefix operators are evaluated left-to-right, they use values to their right, and if these
values themselves involve computations then this changes the order that the operators have to
be evaluated in.
Conversion using parse tree.
Prefix is often used for operators that take a single operand (e.g. negation) and function calls.

Although Postfix and Prefix notations have similar complexity, Postfix is slightly easier to
evaluate in simple circumstances, as the operators really are evaluated strictly left-to-right.

Abstract syntax tree


In computer science, an abstract syntax tree (AST), or just syntax tree, is a tree representation
of the abstract syntactic structure of source code written in a programming language. Each node
of the tree denotes a construct occurring in the source code.

(Read about it somewhere else if you want to, it is poorly explained here)

Language Semantics
Semantics is the study of the relationship between words and how we draw meaning from those
words. Semantics is the study of meaning in language.
Semantics involves the deconstruction of words, signals, and sentence structure. It influences
our reading comprehension as well as our comprehension of other people’s words in everyday
conversation.

Since meaning in language is so complex, there are actually different theories used within
semantics, such as formal semantics, lexical semantics, and conceptual semantics etc

Formal Semantics is the study of grammatical meaning in natural languages using formal tools
from logic and theoretical computer science.
Formal semantics uses techniques from math, philosophy, and logic to analyze the broader
relationship between language and reality, truth and possibility. This is a branch of mathematical
logic that studies the meaning of formal languages, such as those used in programming and
mathematics.

Lexical Semantics - Lexical semantics deconstruct words and phrases within a line of text to
understand the meaning in terms of context. This is the branch of semantics that deals with the
meaning of individual words, and the relationships between words.
This can include a study of individual nouns, verbs, adjectives, prefixes, root words, suffixes, or
longer phrases or idioms

Conceptual Semantics - Conceptual semantics deals with the most basic concept and form of
a word before our thoughts and feelings add context to it. For example, at its most basic we
know a cougar to be a large wild cat. But, the word cougar has also come to indicate an older
woman who’s dating a younger man. This is where context is important.

Denotation Semantics - A denotational definition of a language consists of three parts: the


abstract syntax of the language, a semantic algebra defining a computational model, and
valuation functions. The valuation functions map the syntactic constructs of the language to the
semantic algebra. Recursion and iteration are defined using the notion of a limit. The
programming language constructs are in the syntactic domain while the mathematical entity is in
the semantic domain and the mapping between the various domains is provided by valuation
functions. Denotational semantics relies on defining an object in terms of its constituent parts.
It focuses on the literal or "core" meaning of words and phrases, as opposed to their figurative
or idiomatic meanings.

Connotation Semantics - on the other hand, refers to the associations that are connected to a
certain word or the emotional suggestions related to that word. It focuses on the emotional or
cultural associations that words and phrases may have, in addition to their literal meanings.

Axiomatic semantics: a method of defining the meaning of programming languages by


specifying a set of axioms, or rules, that describe the behavior of the language's constructs. The
axiomatic semantics of a programming language are the assertions about relationships that
remain the same each time the program executes.

Operational Semantics - also known as transition semantics, is a method of defining the


meaning of programming languages by specifying the steps, or transitions, that a computer
takes when executing a program written in the language.
consists of two parts: an abstract syntax and an interpreter. An interpreter defines how to
perform a computation. When the interpreter evaluates a program, it generates a sequence of
machine configurations that define the program's operational semantics. The interpreter is an
evaluation relation that is defined by rewriting rules. The interpreter may be an abstract machine
or recursive functions.
Expressions, Statements, and Blocks
Variables and operators are basic building blocks of programs. You combine literals, variables,
and operators to form expressions.
Expressions — segments of code that perform computations and return values.
By grouping statements together with braces { and } you create blocks of code.
Expressions
Expressions perform the work of a program. Among other things, expressions are used to
compute and to assign values to variables and to help control the execution flow of a program.
Expressions are statements or fragments of code that can be evaluated to produce a value.
Operators are special symbols in programming languages that perform specific operations on
one, two, or three operands, and then return a result. Ex. +, -, *, /, % etc.
Functions are reusable blocks of code that perform a specific task and can take zero or more
input parameters and return zero or one output value.

An expression is a series of variables, operators, and method calls (constructed according to


the syntax of the language) that evaluates to a single value.

Statements
Statements are roughly equivalent to sentences in natural languages.

The following types of expressions can be made into a statement by terminating the expression
with a semicolon (;):
Assignment expressions
Any use of ++ or --
Method calls
Object creation expressions

These kinds of statements are called expression statements.

Statements in programming are the basic building blocks of a program, and are used to specify
the actions that the program is to perform.
Some types of statements are input/output statements, function calls, looping statements,
conditional, assignment, return statements. Etc.

Blocks
A block is a group of zero or more statements between balanced braces and can be used
anywhere a single statement is allowed.

********Chomsky Classification of Grammars


Regular Expressions

A regular expression is a sequence of characters that defines a search pattern. They are often
used in text processing and searching applications, such as text editors and search engines.
Regular expressions can be used to match a single character, a set of characters, or a range of
characters.

Declarations
A declaration is a statement describing an identifier, such as the name of a variable or a
function. A declaration is a statement that defines a variable, constant, function, class, or other
programming construct. Function declaration in JavaScript: function add(x, y) { return x + y; }

Two types of declaration: implicit declaration, explicit declaration

Implicit declaration or default declaration: They are those declarations which are done by
the compiler when no explicit declaration or user defined declaration is mentioned.

Example $abc='astring'; $abc=7; In 'perl' the compiler implicitly understands that $abc ='astring'
is a string variable and $abc=7 is an integer variable.

Explicit declaration of data object: In explicit we or user explicitly defined the variable type.

Float A, B;
In this example it specifies that it is of float type of variable which has name A & B.

Purpose of Declarations

1. Choice of storage representation: AS Translator determines the best storage


representation of data types that is why it needs to know primarily the information of data
type and attribute of a data object.
2. Storage Management: It allows us to use the best storage management for a data
object by providing its information and this information tells the lifetime of a data object.
3. Type checking:- Declaration is basically for static type checking rather than dynamic.
STACKS
Stacks are linear data structures. A stack is a list of elements in which an element may be
inserted or deleted only at one end, called the top of the stack. Stack principle is LIFO
(last in, first out). Which element inserted last onto the stack that element deleted first from the
stack.

Application of stack
1. Stack is used to evaluate a postfix expression.
2. Stack is used to convert an infix expression into postfix/prefix form.
3. Tracking function calls in programming languages.
4. Stack is used by compilers to check for balancing of parentheses, brackets and braces.
5. A browser's forward and backward navigation
6. Undo/Redo functionality in text editors and word processors.

Converting and evaluating Algebraic expressions

An algebraic expression is a legal combination of operators and operands.


Operand is the quantity on which a mathematical operation is performed. Operand may be a
variable like x, y, z or a constant like 5, 4, 6.
Operator is a symbol which signifies a mathematical or logical operation between the operands.
Ex. include +, -, *, /, ^ etc.

An algebraic expression can be represented using three different notations. They are infix,
postfix and prefix notations.

Elements of a Program
If you are going to construct a building, you need two things: the bricks and a blueprint that tells
you how to put them together. In computer programming, you need two things: data (variables)
and instructions (code or functions). Variables are the basic building blocks of a program.
Instructions tell the computer what to do with the variables.

Comments are used to describe the variables and instructions. They are notes by the author
documenting the program so that the program is clear and easy to read. Comments are ignored
by the computer.

Basic structure of a C++ program

Structure (in C and C++) is a collection of variables of different data types under a single name.
It is similar to a class in that both hold a collection of data of different data types.

C++ Basic Syntax

‘//’ (double forward slash) is used to specify a single line comment


// It prints Hello, World!
cout<<"Hello World!";
/* */ If you want to comment multiple lines
/* It prints
Hello World! */
cout<<"Hello World!"
C++ Tokens
Tokens are smallest individual units that can either be a keyword, an identifier, a constant, a
string literal, or a symbol. We have five types of tokens

Keywords: These are reserved words in a programming language that have a specific
meaning. ex "if", "else", "for", and "while".

Identifiers: These are names given to variables, functions, and other program elements.
They typically follow specific naming conventions and rules set by the programming
language.

Constants: These are fixed values, such as numbers and strings, that are used in a
program. They don't change during the execution of a program.

Operators: These are special symbols that perform operations on variables and values,
such as mathematical and logical operations. Examples include "+", ">", and "&&".

Punctuators/Separators: These are characters that separate different elements of a


program, such as commas, semicolons, and parentheses ( and ), and brackets [ and ],
asterisk (*), colon (:), #(hash) .

C++ Semicolon
The semicolon is a statement terminator. Each statement must be ended with a semicolon.
Otherwise, the compiler will raise a syntax error.

Curly Braces In C++


In C++, an opening and closing curly braces is used to group all of the statements in a block.
Block is a set of statements grouped together inside opening and closing braces.

{//start of block

//block of statement(s)

}//end of block

In C++, a program is divided into the following three sections:


1. Standard Libraries Section
2. Main Function Section
3. Function Body Section

Standard Libraries Section


#include <iostream>
Using namespace std;
#include is a specific preprocessor command that effectively copies and pastes the entire text of
the file, specified between the angle brackets, into the source code. It is used to include code
from other files, known as header files, into the current source file. It includes standard libraries,
such as the C standard library, or to include user-defined header files that contain declarations
and definitions for functions, variables, and other program elements.

The <iostream> which is a standard file that should come with the C++ compiler, is short for
input-output streams. This command contains code for displaying and getting an input from the
user.

"using namespace std;" is a C++ directive that allows the use of names from the standard C++
library without the need to qualify them with the std:: prefix. The standard C++ library is defined
in the "std" namespace, and it contains many useful functions, classes, and objects, such as
cout, cin, string, vector, and many more.

Main function section


Int main() {}
The starting point of all C++ programs is the main function
This function is called by the operating system when your program is executed by the computer.
{ signifies the start of a block of code, and } signifies the end.

Function body section


The name cout is short for character output
The return keyword tells the program to return a value to the function int main

cout << “Hello World” << endl;


return 0;

How to declare a structure in C++ programming?


struct Person
{
char name[50];
int age;
float salary;
};

—- Arden’s Theorem (Might be important, read about it )

Compiled and Interpreted Languages


A compiled language is a programming language in which the source code is converted into
machine code (binary) before the program is executed. The machine code is then run directly
on the computer's hardware. Examples of compiled languages include C, C++, and Fortran.
An interpreted language is one where the instructions are not directly executed by the target
machine, but instead read and executed by some other program (which normally is written in
the language of the native machine).

Bytecode Languages
Bytecode languages are a type of programming language that fall under the categories of both
compiled and interpreted languages because they employ both compilation and interpretation to
execute code. Java and the .Net framework are easily the most common examples of bytecode
languages (dubbed Common Intermediate Language in .Net). In fact, the Java Virtual Machine
(JVM) is such a common virtual machine to interpret bytecode that several languages have
implementations built to run on the JVM.
In a bytecode language, the first step is to compile the current program from its human-readable
language into bytecode. Bytecode is a form of instruction set that is designed to be efficiently
executed by an interpreter and is composed of compact numeric codes, constants, and memory
references. From this point, the bytecode is passed to a virtual machine which acts as the
interpreter, which then proceeds to interpret the code as a standard interpreter would.

A type, also known as a data type, is a classification identifying one of various types of data.

Type checking is the process of verifying and enforcing the constraints on data types of
variables, function arguments, and return values in a program. It is a way to ensure that the
values used in a program are of the correct type and format, and to prevent type errors that can
lead to unexpected behavior or crashes.

Type systems is a collection of rules that assigns a data type to various constructs in a program.

Categories of types or data types

1 . primitive data types - integers, booleans, floats, and characters


2. Abstract types - hash, set, queue, and stack.
3. Composite types - these are composed of more than one primitive type, e.g. an array or
record
4. Other types - such as pointers and functions.

pointers - a type which holds as its value a reference to a different memory location

Types of type checking: static and dynamic.

1. Static type checking is performed at compile-time, before the program is executed. In


languages with static type checking, the data types of variables and expressions are
checked before the program runs, and errors are reported if there are any type
mismatches. Examples of languages with static type checking include C, C++, Java, and
Haskell, Fortran, Pascal.

2. Dynamic type checking is performed at runtime, while the program is executing. In


languages with dynamic type checking, the data types of variables and expressions are
checked at the time they are used, and errors are reported if there are any type
mismatches. Examples of languages with dynamic type checking include Python, Ruby,
php, prolog, lisp, and JavaScript.

Compiler Interpreter

Compile-time vs Run-time Compiled languages are Interpreted languages are


translated into machine code executed directly by an
before the program is interpreter.
executed

Execution speed Compiled languages tend to Interpreted languages require


be faster than interpreted the interpreter to read and
languages because they are execute the source code on
executed as machine code the fly.

Error handling Compiled languages tend to Interpreted languages catch


catch more errors at compile- errors at runtime.
time

Flexibility Interpreted languages are


generally considered more
flexible than compiled
languages, as they allow for
more dynamic and interactive
development, and they can
also be run on different
platforms without the need for
recompilation.

Type checking Compiled languages Interpreted languages often


generally have strict static have more flexible dynamic
type checking type checking

languages Fortran, C, C++, C#, Go Python, Javascript, Ruby,


php
Static type checking Dynamic type checking

Timing Static type checking is dynamic type checking is


performed at compile-time, performed at runtime, while
before the program is the program is executing
executed

Error detection Static type checking tends to dynamic type checking


catch more errors at compile- catches errors at runtime
time

Flexibility static type checking is Dynamic type checking


generally more strict and can allows for more flexibility in
catch more errors at compile- how data types are used, as
time. it can handle more complex
data types and operations

Memory usage Static type checking can dynamic type checking can
optimize memory usage, as it detect errors and report them
can detect and prevent type at runtime, it may not
errors before the program optimize memory as well.
runs

Performance Static type checking can dynamic type checking may


improve performance, as it have a performance
can detect and prevent type overhead as it performs the
errors before the program type checking at runtime.
runs

C# and Java use a combination of static and dynamic type checking

Common Misconceptions
A common misconception is to assume that all statically-typed languages are also strongly-
typed languages, and that dynamically-typed languages are also weakly-typed languages.
This isn’t true, and here’s why:
A strongly-typed language is one in which variables are bound to specific data types, and will
result in type errors if types do not match up as expected in the expression – regardless of when
type checking occurs. A simple way to think of strongly-typed languages is to consider them to
have high degrees of type safety. Ex. x = 1 + “2”. Weak will not give an error, ex php.

Garbage Collection
Garbage collection is a mechanism used in programming languages to automatically manage
the memory used by a program. It works by periodically scanning the memory used by a
program and identifying any memory that is no longer being used, also known as "garbage."
The garbage collector then frees up this memory, making it available for future use.
Why Do We Need GC
GC helps save the developer from several memory-related issues – the foremost being memory
leaks. As you allocate more and more memory to the heap, if the program doesn’t consistently
release this memory as it becomes unneeded, memory size will begin to add up – resulting in a
heap overflow. Even if heap memory is diligently managed by the developer – all it takes is one
variable to be consistently left undeleted to result in a memory leak, which is bad.

Heap memory is a region of memory that is used to store dynamically allocated memory during
runtime.
Heap overflow, also known as a buffer overflow, occurs when a program writes more data to a
heap buffer than the buffer can hold. This can cause the program to crash or, in the case of
maliciously crafted input, can be used to execute arbitrary code, leading to a potential security
vulnerability.

Even if there are no memory leaks, what happens if you are attempting to reference a memory
location which has already been deleted or reallocated? This is called a dangling pointer.

How and When does the Garbage Collector Run?

This depends entirely on the algorithm used for GC. Sometimes the garbage collector will run at
predetermined time intervals, and sometimes it waits for certain conditions to arise before it will
run. The garbage collector will just about always run on a separate thread in tandem with your
program. And depending on the language’s implementation of GC, it can either stall your
program to sweep out all the garbage at once, run incrementally to remove small batches, or
run concurrently with your program.

Garbage Collection Algorithms

1. Reference Counting: it is the most basic form of GC, and the easiest to implement on
your own. Anytime you reference a memory location on the heap, a counter for that
particular memory location increments by 1. Every time a reference to that location is
deleted, the counter decrements by 1. When that counter gets to 0, then that particular
memory location is garbage collected.
This method is used in languages like Python, and some implementations of C++.

Disadvantages
a. Circular references can’t be garbage collected – meaning that if object A has a
reference to object B, and object B has a reference back to object A, then neither
of these objects can ever be garbage collected according to reference counting.
b. Reference counting is very inefficient because of the constant writes to the
counters for each memory location

2. Mark-Sweep: – is a form of a tracing GC algorithm, which involves tracing which objects


are reachable from one or multiple “roots” in order to find unreachable (and thus unused)
memory locations. This form of GC is not constantly checking and it can theoretically run
at any point in time.
It does not keep counters and can get rid of circular references.

Disadvantage
A. The entire program has to be suspended while it is running. (Stop-the-world GC). And
because tracing GC can happen at any time, you don’t ever have a good idea of when
one of these stalls will happen.

B. Heap memory is also iterated over twice – which slows down your program even
more

C. in marksweep there’s no handling of fragmented memory

3. Mark-Compact: The mark-sweep algorithm works by first "marking" all of the objects in
the heap that are still in use by the program, and then "sweeping" through the heap to
identify and free all of the objects that were not marked. This can help prevent memory
leaks caused by objects that are no longer needed by the program but are still taking up
space in the heap.

4. Copying: (also known as Cheney’s Algorithm) is slightly similar to mark-compact, but


instead of iterating potentially multiple times over a single memory region, you instead
just copy the “surviving” memory blocks of the region into an entirely new empty region
after the mark phase – which thus compacts them by default. After the copying is
completed, the old memory region is deallocated, and all existing references to surviving
memory will point to the new memory region. This relieves the GC of a lot of processing,
and 10 brings down the specs to something even quicker than a mark-sweep process
since the sweep phase is eliminated.

5. Generational GC takes concepts from copying algorithms, but instead of copying all
surviving members to a new memory region, it instead splits up memory into
generational regions based on how old the memory is

Past Question
1a. The programming language semantics can be described by various techniques. Outline the
most commonly used techniques in programming language semantics (3Mks)

- Formal Semantics
- Lexical Semantics
- Conceptual Semantics
- Denotation Semantics
- Connotation Semantics
- Axiomatic Semantics
- Operational Semantics

Discuss the three concepts of mathematical theories

- Syntactic rules: These determine the structure of formulas which are the statements of interest
- Axioms: These describe the basic properties of the system.
- Inference rules: These are the mechanisms for deducing new theorems from axioms and other
theorems.

Declarations provide information about the name and type of data objects needed during
program execution. What are the two types of declaration?

- Implicit declaration or default declaration: They are those declarations which are done by the
compiler when no explicit declaration or user defined declaration is mentioned. Ex.
$abc='astring';
- Explicit declaration: In explicit we or users explicitly defined the variable type. Ex. Float A,B;

Outline the purpose of Declarations in programming

- Choice of storage representation: AS Translator determines the best storage representation of data
types that is why it needs to know primarily the information of data type and attribute of a data
object.
- Storage Management: It allows us to use best storage management for data object by
providing its information and this information tells the lifetime of a data object.
- Polymorphic operations: In most languages, some special symbol like + to designate any
one of the several different operations which depends on type of data or argument is
provided. This operation has some name like as we discussed + in this case the operation
symbol is said to be overloaded because it does not designate one specific operation.
- Type Checking: - Declaration is basically for static type checking rather than dynamic.
What is the difference between a break statement and a continue statement as used in object
oriented programming?

- The break statement is used to exit a loop prematurely. When the break statement is encountered
in a loop, it immediately terminates the current iteration of the loop and transfers control to the
next statement after the loop.
- The continue statement is used to skip the current iteration of a loop and move on to the next
iteration. When the continue statement is encountered in a loop, it causes the current iteration to
be skipped and the next iteration to start.

Outline the three main types of grammar used in programming languages

- Context-Free Grammars: a type of grammar that defines a set of rules for generating strings in a
language. It specifies the structure of the language, but does not specify the meaning of the
strings.
- Regular Grammars: a type of grammar that defines a set of rules for generating strings in a
regular language. Regular grammars are a subset of context-free grammars and can be used to
describe simple, repetitive patterns.
- Context-Sensitive Grammars: a type of grammar that defines a set of rules for generating strings
in a language, taking into account the context in which the strings are used. Context-sensitive
grammars are more powerful than context-free grammars, as they can capture more complex
patterns and relationships in the language.

Note: There is a fourth type of grammar but it is not used for programming languages because it is called
unrestricted grammar and it is unrestricted.

Using good examples, differentiate between Static Type Checking and Dynamic Type

Static Type Checking: Type checking is performed at compile-time, before the program is executed and
cannot be changed at runtime. For example, in a statically typed language like Java, C++, C, typescript.

Dynamic Type Checking: Type checking is performed at runtime, during the execution of the program. In
dynamic type checking, the type of a variable is determined at runtime, based on the value assigned to it,
and can change during the course of the program. For example, in Python, Ruby, and JavaScript.
What is the difference between Linker and Editor?

● Linker: A linker is a program in a computer system that takes one or more object
files generated by a compiler and combines them into a single executable file. It
resolves any external references, such as libraries, and merges all the object files
into a single executable that can be run on a computer.

● Editor: An editor is a software application used for editing and creating text-based
files. It provides features for adding, deleting, and modifying text and can also be
used to write, debug and run computer programs.

Write a simple C++ program to show the use of block variable declarations and definitions

#include <iostream>
int main()
{
// Block variable declaration
int x;
// Block variable definition and initialization
int y = 10;

// Block variable definition, declaration, and


initialization
int z = 20;
// Using block variables
x = y + z;
std::cout << "The value of x is: " << x << std::endl;

return 0;
}

How is Compiler different from Interpreter in program execution?

Compiler: A compiler is a program that translates source code written in a high-level


programming language into machine code, which can be executed directly by the computer's
hardware. The compiler generates a single executable file that can be run multiple times without
recompilation.
Interpreter: An interpreter is a program that executes the source code line by line, interpreting
each line and executing its instructions immediately. Unlike a compiler, an interpreter does not
generate an executable file, but instead executes the code directly.

State the kind of variable a class can consist of using java program

A Java class can consist of the following types of variables:

● Instance variables: Also known as object variables, they are defined within a class
but outside any method. They are unique to each instance of the class and store
data that is specific to an individual object.

● Static variables: Also known as class variables, they are shared by all objects of a
class. They are declared using the static keyword and are created when the class is
loaded into memory.

● Local variables: They are defined within a method and are only accessible within
that method. They are created when a method is called and destroyed when the
method returns.

What are the features of ideal programming language

● Readability: The language should be easy to read and understand, with a syntax
that is clear and concise.

● Expressiveness: The language should be powerful enough to express complex


algorithms and data structures, but not so complex that it becomes difficult to use.

● Portability: The language should be platform-independent, so that programs


written in the language can run on any system without modification.

● Scalability: The language should be able to handle large and complex programs,
with efficient support for large data structures and algorithms.

● Interoperability: The language should be able to interact with other languages and
systems, allowing for seamless integration of existing software.
● Performance: The language should have efficient run-time performance, with low
overhead and quick execution times.

● Security: The language should provide strong security features to prevent


unauthorized access to sensitive data and resources.

● Error handling: The language should provide adequate error handling and
debugging support, making it easy to identify and fix problems.

● Community: The language should have a large and active user community, with
readily available resources such as tutorials, libraries, and tools.

● Support: The language should have strong commercial and academic support,
with ongoing development and maintenance to ensure its continued evolution and
improvement.

Write a Java program segment to create an array of 100 with integers from 1-100

int[] arr = new int[100];


for (int i = 0; i < 100; i++) {
arr[i] = i + 1;
}

What is JVM and how is it considered in context of Java's platform independent feature?

JVM stands for Java Virtual Machine. It is a software component that provides a runtime
environment for executing Java code. JVM is responsible for managing the execution of
Java applications, including memory allocation, security, and garbage collection.

The JVM is one of the key factors in Java's platform independence. When a Java program
is compiled, it produces bytecode, which is a machine-independent format. This bytecode
can be executed on any platform that has a JVM installed, without modification. This
means that a Java program can run on any device, such as a laptop, desktop, or
smartphone, that has a JVM installed
Write a short note on the following programming methodologies

1. Procedural Programming: Procedural programming is a programming paradigm that is


based on the concept of procedures, or subroutines, that perform specific tasks. The
procedural programming approach involves breaking down a program into smaller,
reusable, and well-defined blocks of code, called procedures or functions, that perform
specific tasks. C, Pascal, and FORTRAN.

2. Object Oriented Programming: It is a programming paradigm that uses objects to


model real-world entities and their interactions.

3. Logic (Declarative) Programming: it is a programming paradigm that focuses on


describing the desired results of a program rather than specifying how to achieve those
results.

4. Imperative Programming: it is a programming paradigm that focuses on describing


how a computation should be performed, rather than what the result should be.

5. Concurrent Programming: Concurrent programming is a programming paradigm that


focuses on writing programs that can run simultaneously on multiple processors or cores.

Write a C++ structure student defined with three members: name, matric no and department fees

#include <iostream>
#include <string>
struct Student {
std::string name;
int matric_no;
float department_fees;
};
int main() {
Student student1;
student1.name = "John Doe";
student1.matric_no = 123456;
student1.department_fees = 1000.00;
return 0;
}
There are 3 types of comments that Java supports in programming. Show how these comments
can be implemented.

- Single Line Comment: It starts with // and continues until the end of the line.
For example: // This is a single line comment.
- Multi-Line Comment: It starts with /* and ends with */.
For example: /* This is a multi-line comment */
- Document Comment: It starts with /** and ends with */. This type of comment is used for
generating API documentation using the javadoc tool.
For example:
/**
* This is a document comment.
* It can be used to generate API documentation.
*/
Each of these comments serves a different purpose and can be used in different contexts
depending on what the programmer wants to communicate.

Show that the union of two regular sets is regular. Given two regular expressions: RE 1 = a(aa)*
and RE2 = (aa)*

A regular set is a set of strings that can be recognized by a finite automaton, which is also known as a
regular expression. The union of two regular sets is also a regular set, as it can be recognized by a finite
automaton that consists of two separate automata, one for each of the original regular sets.

In this case, we have two regular expressions: RE1 = a(aa)* and RE2 = (aa)*. To show that the union of
the two regular sets is also a regular set, we can construct a finite automaton that recognizes both RE1 and
RE2. The automaton for RE1 would have a start state, an accepting state, and intermediate states that
correspond to the possible states of the string being matched. The automaton for RE2 would be similar,
with its own start state, accepting state, and intermediate states.

To create the automaton for the union of the two sets, we simply create a new start state and connect it to
the start states of both RE1 and RE2. The accepting states of both RE1 and RE2 are also connected to a
new accepting state.

This creates a new automaton that recognizes the union of the two original sets, which is also a regular
set.Thus, the union of two regular sets is a regular set and can be recognized by a finite automaton
Write a simple C++ program to show the use of block variable declarations and definitions.

Already done

Explain the following line used under Java Program: public static void main (String args[])

"public static void main (String args[])" is the starting point of a Java program. The "public"
keyword makes the "main" method accessible to any class. The "static" keyword allows the main
method to be called without creating an instance of the class. The "void" keyword indicates that
the main method does not return any value. The "main" method is a standard name for the
starting point of a Java program. The "String args[]" is an array of strings that represents
arguments passed to the main method. This line of code specifies that the program will receive
an array of strings as input, which can be accessed within the main method.

List the three steps for creating an object for a class?

Declare a class type variable.


Create an instance using "new".
Initialize the object with constructor.

class Dog {

- Declare a class type variable.

String breed;
int age;
String color;

- Create an instance using "new".

public Dog(String breed, int age, String color) {


this.breed = breed;
this.age = age;
this.color = color;
}
}
- Initialize the object with constructor.

Dog dog = new Dog("Labrador", 3, "Black");

Briefly describe the terms “Type checking” as used in programming languages

Type checking is the process of verifying and enforcing the constraints of data types, and it can
occur either at compile time (i.e. statically) or at runtime (i.e. dynamically). (Like making sure that
if you declare variable age to be an integer, then it must only contain integer value)

State the two ways of type checking

- Static type checking: A language is statically-typed if the type of a variable is known at


compile time instead of at runtime. Ada, C, C++, C#, JADE, Java, Fortran, Haskell, ML,
Pascal, and Scala.
- Dynamic type checking: Dynamic type checking is the process of verifying the type
safety of a program at runtime. Groovy, JavaScript, Lisp, Lua, Objective-C, PHP, Prolog,
Python, Ruby, Smalltalk and Tcl

Convert the following Infix expression to Prefix and Postfix notations

1) A + B 2) A + B * C 3) A + B * C + D 4) A + B + C + D

Itemize the general methods of implementing a programming language

- Compilation
- Interpretation
- Hybrid
- Just in time compiler: compiles source code into machine code as the code is executed,
rather than ahead of time.
- Pure interpretation

What are the application domains of programming languages?


- Scientific application
- Data processing application
- Text processing application
- Web software
- Artificial intelligence application
- System program application

Briefly describe the two main types of garbage collection activity that usually happens in Java

- Mark Sweep: This is a basic type of garbage collection that involves marking all the
objects that are still in use and then sweeping up the rest, freeing up the memory
occupied by unreferenced objects.
- Generational Garbage Collection: It divides the memory into different sections, called
generations, based on the age of the objects stored there. The youngest objects are stored
in the youngest generation, and objects that have lived longer are moved to older
generations.

What is the output of the following python statements

i) print str + “TEST” if str = “Hello World”? – Hello WorldTEST

ii) print list[0] if list = [‘abcd’, 786. 2.23, ‘john’, 70.2]? – abcd

iii) print list[1:3] if list = [‘abcd’, 786. 2.23, ‘john’, 70.2]? – [786.2, 2.23]

You might also like