PPL Unit-01[1]
PPL Unit-01[1]
PPL Unit-01[1]
Here are some reasons for studying the concepts of programming languages:
Example: Use Python for data science, JavaScript for web development, and C++ for
system programming.
Example: Knowing about recursion and type systems helps you solve complex problems
more efficiently.
Example: If you know loops in C, you’ll easily understand loops in Python, Java, or any
other language.
Ex: Developers created Python to make programming more readable and simple.
Example: Knowing how memory works helps you avoid buffer overflow attacks.
7. Increased ability to express ideas.
Programming Domains and Their Associated Languages
Different programming domains refer to the areas where programming is applied. Each
domain has its specific needs, and different languages are better suited to solve different
types of problems.
Let’s explore the main programming domains and the languages commonly used in each.
Common Tasks:
• Performing calculations
• Analyzing large datasets
• Creating visualizations
Popular Languages:
• Python – Easy to use, rich libraries (NumPy, Pandas)
• R – Built for statistical analysis
• MATLAB – Used for mathematical modeling
• Fortran – Used in scientific computing (older but still used)
• This domain focuses on creating software for businesses, like ERP systems, CRM
tools, and financial software.
• The use of computers for business applications began in the 1950s.
• Special computers were developed for this purpose, along with special languages.
• The first successful high-level language for business was COBOL
Common Tasks:
• Building large-scale applications
• Managing databases
• Automating business processes
Popular Languages:
• Java – Common in enterprise applications
• C# – Popular for Windows applications
• SQL – For managing databases
• Python – For automation and backend
• This domain focuses on creating intelligent systems that can learn from data.
• Symbolic rather than numeric computations are manipulated.
• Symbolic computation is more suitably done with linked lists than arrays.
• LISP was the first widely used AI programming language.
Common Tasks:
• Building AI models
• Implementing machine learning algorithms
• Natural language processing (NLP)
Popular Languages:
• Python – Most popular for AI/ML (TensorFlow, PyTorch)
• R – For data analysis and statistical modeling
• Java – Used in large-scale ML systems
• Lisp/Prolog – Used in AI research
• This domain involves creating low-level software that interacts with hardware.
• The O/S and all of the programming supports tools are collectively known as its
system software.
• Need efficiency because of continuous use.
Common Tasks:
• Building operating systems
• Developing device drivers
• Creating embedded systems
Popular Languages:
• C – Used for OS and hardware-level programming
• C++ – Advanced system programming
• Rust – A modern language for safe systems programming
• Assembly – For direct hardware interaction
Common Tasks:
• Designing user interfaces (UI)
• Creating backend servers
• Managing databases
Popular Languages:
• HTML/CSS/JavaScript – Frontend web development
• PHP – Backend development
• Python (with Django/Flask) – Backend development
• JavaScript (Node.js) – Full-stack development
• Ruby (Rails) – Backend framework
1. Readability
How easy is it to read and understand the code written in the language?
1. Overall Simplicity
o Fewer basic constructs improve readability. Complex languages with too
many features can confuse programmers, especially if subsets of features vary
between authors.
o Feature multiplicity (e.g., multiple ways to increment variables in Java:
count++, ++count, count += 1) and operator overloading (e.g., defining + for
unconventional uses) can hinder clarity.
2. Orthogonality
o Orthogonality refers to the ability to combine a small set of constructs in
consistent ways. For example, VAX assembly language allows operands in
both registers and memory, while IBM mainframes use separate instructions
for similar tasks.
o While orthogonality reduces exceptions and simplifies learning, excessive
orthogonality (as in ALGOL 68) can lead to unnecessary complexity by
enabling too many combinations.
3. Data Types
o Adequate data types enhance readability. For instance, Boolean types clarify
intent (timeOut = true) compared to numeric flags (timeOut = 1).
4. Syntax Design
o Clear and consistent syntax aids readability. Languages like Ada, which use
descriptive endings (end if, end loop), are more readable than those relying
on generic symbols like braces {} in C.
o Ambiguity in syntax (e.g., static in C having multiple meanings) or allowing
special words as variable names (e.g., Do in Fortran) can confuse readers.
2. Writability
How easy is it to write code in the language to solve a problem?
1. Simplicity and Orthogonality
o Fewer primitives and consistent rules improve writability. Programmers can
learn and use constructs effectively. However, excessive orthogonality can
allow errors to go undetected, making debugging harder.
2. Support for Abstraction
o Abstraction simplifies program development by hiding details. Languages
supporting process abstraction (e.g., subprograms for reusable operations)
and data abstraction (e.g., classes for binary trees in Java) improve writability
compared to those requiring repetitive and manual implementations (e.g.,
Fortran 77).
3. Expressivity
o Expressive languages provide convenient syntax for common tasks. For
instance, count++ in C is more concise than count = count + 1. Similarly,
constructs like Ada’s and then operator for short-circuit evaluation and Java’s
for loops enhance writability by reducing verbosity.
3. Reliability
Does the language allow you to write bug-free, secure, and stable code?
Type Checking
Detecting type errors during compilation or execution is essential. Compile-time type
checking, as seen in Java, reduces runtime errors. Older versions of C lacked type checking
for function parameters, leading to errors that were hard to diagnose. Modern C has
addressed this issue.
Exception Handling
The ability to intercept and handle runtime errors enhances reliability. Languages like Ada,
C++, Java, and C# provide robust exception-handling mechanisms, unlike C and Fortran.
Aliasing
Aliasing occurs when multiple names refer to the same memory location, increasing
complexity and error risks. Some languages limit aliasing to improve reliability.
Readability and Writability
Languages that allow natural expression of algorithms lead to fewer errors. Readable and
writable code is easier to maintain and modify, enhancing reliability.
4. Efficiency
How fast does the language execute programs? How much memory does it use?
5. Portability
Can the language run on different platforms and devices without much modification?
6. Cost
What is the cost of using the language.
Training Programmers
Simpler and more orthogonal languages reduce training costs.
Writing Programs
Languages closely aligned with application needs lower development costs.
Compilation Costs
Faster compilation with minimal optimization suits students, while optimized code is
preferred in production.
Execution Costs
Design influences runtime efficiency. Languages with fewer runtime checks execute faster.
Implementation System Cost
Free or affordable systems, as with Java, encourage adoption.
Reliability Costs
Failures in critical systems can lead to high financial and reputational losses.
Maintenance Costs
Readability is crucial for efficient maintenance, as it often involves developers other than the
original authors. Maintenance can cost up to four times the development expense.
The diagram shows the basic structure of the Von Neumann Architecture:
1. Memory stores both data and instructions.
2. The Processor includes:
o Control Unit: Manages instruction flow.
o Arithmetic Logic Unit (ALU): Performs calculations and logical operations,
storing results in the Accumulator.
3. Input and Output devices allow data to enter and leave the system.
Data flows between memory, the processor, and input/output, following a step-by-step
process.
How Does It Influence Language Design?
• Languages like C, Java, and Python are influenced by this architecture.
• These languages use variables to store data in memory and statements to execute
instructions in sequence.
• The concept of loops, conditional statements, and functions matches how a CPU
processes instructions.
Example:
When you write a for loop in C:
for (int i = 0; i < 10; i++) {
printf("%d\n", i);
}
This loop matches the step-by-step execution process of the CPU in Von Neumann
machines.
1. Procedural Programming
This was one of the first methodologies. It focuses on writing step-by-step instructions to
solve a problem.
Example:
In C:
void main() {
printf("Hello, World!");
}
3. Functional Programming
Functional programming focuses on writing code using functions without modifying
variables or changing state.
Language Categories
1. Imperative Language:
o Focuses on giving the computer step-by-step instructions to perform tasks.
o A procedural language follows a sequence of statements or commands in
order to achieve a desired output.
o Each series of steps is called a procedure, and a program written in one of
these languages will have one or more procedures within it.
o Example: C, Python
o How it works: You tell the program what to do in a sequence (e.g., loops,
conditionals).
2. Logical Language:
o Based on logic and rules, where the program is asked to solve problems using
facts and relationships.
o logic programming language expresses a series of facts and rules to instruct
the computer on how to make decisions.
o Example: Prolog, Datalog, ASP
o How it works: You define facts and rules, and the computer figures out how
to solve queries.
3. Functional Language:
o Focuses on using functions that take inputs and produce outputs, without
changing data and state.
o Example: Haskell, Lisp, Scala
o Recursion is used instead of loops.
o How it works: The program is written using pure functions that do not modify
state.
o Example (in Lisp) :
• factorial 0 = 1
• factorial n = n * factorial (n-1)
4. Object-Oriented Language:
o Organizes the program into objects that encapsulates both data and
methods.
o promotes reuse (through inheritance) and modularity.
o Example: Java, C++
o How it works: Programs are built around objects (like cars, books) that
interact with each other.
5. Scripting Language:
o Scripting languages are used for automating tasks or gluing together system
components.
o Lightweight and easy to write.
o Typically interpreted rather than compiled.
o Frequently used for quick development and automation.
o Examples: Python, JavaScript, Bash, PHP
Implementation Methods
1. Compilation Method:
o In this method, the entire program is translated into machine code (binary
code) all at once by a compiler before it is executed.
o The compiled program can run directly on the computer without needing the
source code anymore.
Lexical Analysis: The lexical analyzer gathers characters into lexical units (e.g., identifiers,
keywords, operators, punctuation). Comments are ignored since they are irrelevant to the
compilation process.
Syntax Analysis: Constructs parse trees, representing the syntactic structure of the program.
Semantic Analysis: Checks for complex errors (e.g., type mismatches) that cannot be caught
during syntax analysis.
Intermediate Code Generation: Produces an intermediate representation of the program,
often easier to optimize than machine code.
Optimization: Improves the program's performance by making it faster or smaller.
Code Generation: Translates optimized intermediate code into machine language.
Symbol Table: A database used throughout the compilation process to store the types and
attributes of user-defined names.
Example: C, C++
2.Pure Interpretation:
o In this method, the source code is executed line-by-line by an interpreter,
without first converting it to machine code.
o The program is not compiled; instead, the interpreter directly executes the
instructions while the program is running.
Languages Recognizers
A recognizer determines if a given input (sentence) belongs to the defined language. This
process involves checking if the sentence adheres to the language's syntax rules.
Example: In a compiler, the syntax analysis phase acts as a recognizer. It checks whether a
program's code (input strings) conforms to the syntax of the programming language.
How it Works:
• Input: A string of characters (e.g., a line of code).
• Process: The recognizer compares the input to predefined rules of the language.
• Output: Accept (valid sentence) or Reject (invalid sentence).
Languages Generators
A generator is a tool or device that creates valid sentences belonging to the language. By
producing sentences, it implicitly defines the structure and rules of the language.
Purpose:
To define what constitutes a valid sentence in the language. By comparing a given sentence
to the generator's rules, one can check its correctness.
How it Works:
• A generator applies a set of grammar rules to produce sentences.
Example:
In English, the grammar rule "Subject + Verb + Object" can generate valid sentences like:
• "The cat eats fish."
• "The dog barks loudly."
• Similarly, in programming, a generator could create valid Java statements like:
• int x = 5;
• System.out.println("Hello");
6. Parse Tree
• Explanation: A parse tree is a tree-like diagram that shows how a string of symbols is
derived from a grammar. It shows the structure of the expression and how each part
of the grammar applies to the input.
• Example: For the expression a + b, the parse tree would look something like this:
7. Ambiguity in Grammars
• Explanation: A grammar is said to be ambiguous if a single string can be parsed in
more than one way (i.e., it has multiple valid parse trees). Ambiguity can lead to
confusion or errors in interpretation, especially in programming languages.
• Example: The arithmetic expression a + b + c can be parsed in two ways:
• (a + b) + c or a + (b + c)
Both interpretations are valid according to the grammar, making the grammar ambiguous.
2.Rules: Each grammar rule also includes semantic rules that show how to calculate the
attributes.
For example, you can specify that two variables in an expression must have the same type.
Example – Simple Grammar
Let’s use this simple language:
<assign> → <var> = <expr>
<expr> → <var> + <var> | <var>
<var> → A | B | C
Here:
• <assign> represents an assignment, like A = B + C.
• <expr> represents an expression, like B + C.
• <var> represents a variable, like A, B, or C.
Adding Attributes
We want to add type information to these grammar symbols, to know whether variables
and expressions are compatible with each other in the program. For example, we want to
check if we’re trying to add two numbers or assign a number to a variable.
• actual_type: This tells us the actual type of a variable or expression (e.g., int or
float).
• expected_type: This tells us the expected type of an expression (e.g., when doing an
assignment).