0% found this document useful (0 votes)
19 views

Compiler CH-1

Uploaded by

Yohannes Dereje
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Compiler CH-1

Uploaded by

Yohannes Dereje
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Principles of Compiler Design

Chapter One:
Introduction to Programming
Languages
Contents
 Definitions of programming languages
 The lexical and syntactic structure of a language
 Evolution of Programming Languages
 High-level programming languages
 Elements of Programming languages:
– Data elements, Data structures, Operators, Assignment,
Statements, Program units, Data environments, Parameter
transmission, Storage management.
 Programming Language basics
Programming Languages
 Programming languages are notations for describing
computations to people and to machines.
 The world as we know it depends on programming languages,
because all the software running on all the computers was written
in some programming language.
 But, before a program can be run, first it must be translated into a
form in which it can be executed by a computer.
 The software systems that do this translation are
called compilers.
Con…
 The study of compiler touches upon programming languages,
machine architecture, language theory, algorithms, and
software engineering.
 In this preliminary course,
– We introduce the different forms of language translators, give a high
level overview of the structure of a typical compiler, and
– The trends in programming languages and machine architecture that
are shaping compilers.
The Lexical and Syntactic Structure of a
Language
 Language is a complex system with various levels of structure.
 The lexical and syntactic structure are fundamental
components of language.
 In computer science, a lexical grammar or lexical structure is a
formal grammar defining the syntax of tokens.
 The program is written using characters that are defined by the
lexical structure of the language used.
The lexical and syntactic structure of a
language
 Lexical Structure
 Lexicon
– The lexicon is the mental dictionary of words and their meanings.
– It includes all the words, their forms, and their meanings in a language.
– Lexemes are the basic units of the lexicon.
 Syntactic Structure
 Syntax
– Syntax is the study of sentence structure and how words are combined
to create meaningful sentences.
– It involves the arrangement of words and phrases to convey meaning.
Evolution of Programming Languages
 The evolution of programming languages continues with
ongoing advancements and new languages emerging to address
specific needs in various domains.
 These languages have shaped the way software is developed,
making it more accessible, efficient, and powerful.
 The first step towards more people-friendly programming
languages was the development of mnemonic assembly
languages in the early 1950's.
Evolution of Programming Languages
 The key milestones in the evolution of programming
languages:
1. Machine Language (1940s-1950s):

– Early computers were programmed directly in machine language, which


consisted of binary instructions.
– Programmers had to understand the hardware intimately, making
programming tedious and error-prone.
2. Assembly Language (1940s-1950s):
– Assembly languages provided a more human-readable representation of
machine code, with mnemonics for instructions.
– Improved programmer productivity but still required a deep understanding
of hardware.
Evolution of Programming Languages
3. Fortran (1957):
– Developed by IBM, Fortran (Formula Translation) was the first high-level
programming language.
– Designed for scientific and engineering applications, it allowed easier
mathematical expression and improved productivity.
4. 4. LISP (1958):
– LISP (short for "List Processing") was created by John McCarthy and was
one of the earliest high-level programming languages.
– Focused on symbolic processing and artificial intelligence, making it
influential in those domains.
Evolution of Programming Languages
5. COBOL (1959):
– COBOL (COmmon Business-Oriented Language) was designed for business
and data processing.
– It introduced many language features that are still in use today, such as record
structures and file handling.
6. ALGOL (1958-60):
– ALGOL (ALGOrithmic Language) was designed for scientific computing
and had a significant influence on later languages.
– It introduced structured programming concepts and influenced languages like
Pascal and C.
7. BASIC (1964):
– BASIC (Beginner's All-purpose Symbolic Instruction Code) was designed for
educational purposes and made programming accessible to beginners.
Evolution of Programming Languages
8. C (1972):
– developed by Dennis Ritchie at Bell Labs, was a breakthrough language with
strong low-level capabilities.
– It influenced many subsequent languages and operating systems and is still
widely used today.
9. Pascal (1970):
– Created by Niklaus Wirth, Pascal was designed for teaching structured
programming.
– It introduced strong typing and other features later seen in languages like Ada
and Modula-2.
10. C++ (1983):
– C++ was developed as an extension of C, adding OO programming features.
– It has been a widely used language for systems programming and software
development.
Evolution of Programming Languages
11. Python (1989):
– Python, created by Guido van Rossum, focused on readability and ease of
use.
– It gained popularity for web development, data analysis, and scripting.
12. Java (1995):
– Java, developed by James Gosling at Sun Microsystems, was designed for
cross-platform compatibility.
– It has been used extensively for web and mobile applications.
13. JavaScript (1995):
– JavaScript, not to be confused with Java, is a scripting language for web
development, enabling dynamic content and interactivity in browsers.
Evolution of Programming Languages
14. 15. PHP (1995):
– PHP (Hypertext Preprocessor) was developed for server-side web
development, enabling dynamic web content.

15. C# (2000):
– C#, developed by Microsoft, was designed for Windows application
development and integrated with the .NET framework.

16. Swift (2014):


– Apple introduced Swift as a modern, safe, and efficient language for iOS
and macOS app development, aiming to replace Objective-C.
Evolution of Programming Languages
 Today, there are thousands of programming languages.
 They can be classified in a variety of ways.

1. Based on Generation
 First-generation languages are the machine languages,
 Second -generation the assembly languages
 Third -generation the higher-level languages like Fortran,
Cobol, Lisp, C, C++, C#, and Java.
Evolution of Programming Languages
 Fourth-generation languages are languages designed for
specific applications like
– NOMAD(Novel Materials Discovery) for report generation,
– SQL for database queries, and
– Postscript for text formatting.

 Fifth -generation language has been applied to logic- and


constraint-based languages like
– Prolog and OPS5 (Official Production System).
 First language to be used in a successful expert
system, the R1/XCON system
2. Based on programming paradigm
 Imperative Languages
– in which a program specifies how a computation is to be done.
– Languages such as C, C++, C#, and Java are imperative languages
 Declarative languages
– in which a program specifies what computation is to be done.
– A programming language where the programmer specifies the goal or what
should be achieved, rather than how a goal should be achieved.

– Functional languages such as ML and Haskell and constraint logic


languages such as Prolog are declarative languages.
Con….
 Object-oriented language is one that supports object-oriented
programming, a programming style in which a program consists of a
collection of objects that interact with one another.
 Simula67 and Smalltalk are the earliest major object-oriented languages.

 Languages such as C++, C#, Java, and Ruby are more recent object-
oriented languages.
 Scripting languages are interpreted languages with high-level operators
designed for " gluing together" computations.
 Awk, JavaScript, Perl, PHP, Python, Ruby, and Tcl are popular examples of
scripting languages.
High-level programming languages
 High-level programming languages are computer programming
languages that are designed to be easier for humans to understand
and use compared to low-level languages.
 These languages provide abstractions and a higher level of
functionality, making it more straightforward for programmers to
write code and solve complex problems.
Key Characteristics of High-level Programming Languages:

 Abstraction
– High-level languages provide a level of abstraction that
allows programmers to work with concepts and operations
that are closer to human thought processes, rather than
dealing with the intricacies of a computer's hardware.

 Readability
– High-level languages use human-readable syntax, making
it easier for programmers to write, read, and understand
code.

– This promotes code maintainability and collaboration.


Key Characteristics of High-level Programming Languages:
 Portability
– Programs written in high-level languages are generally more
portable, as they are not tied to specific computer
architectures.
– They can be run on different platforms with minimal or no
modification.
 Productivity
– High-level languages often come with built-in libraries and
frameworks that simplify common tasks and reduce the
amount of code a programmer needs to write.
– This can lead to increased productivity.
Key Characteristics of High-level Programming Languages:
 Memory Management
– handle memory management automatically, reducing the risk of
memory-related errors such as buffer overflows or memory leaks.
 Hardware Independence
– away hardware-specific details, enabling programmers to write
code without concern for the underlying computer's architecture.
 Error Handling
– Provide built-in error handling mechanisms, making it easier to
identify and manage runtime errors.
Elements of Programming
languages:
– Data elements
– Data structures
– Operators
– Assignment
– Statements
– Program units
– Data environments
– Parameter transmission
– Storage management.
Data Elements:
 Data elements are the basic building blocks for representing
information in a programming language.
 They include fundamental data types such as,
– Integers,
– Floating-point numbers,
– Characters, and
– Booleans.
 Data elements can also be user-defined types, like structures or
objects.
Data Structures:
 Data structures are collections of data elements organized and
stored for efficient manipulation.
 Common data structures include
– Arrays,
– Lists,
– Sets,
– Stacks,
– Queues, and
– Trees.

 They enable programmers to manage and manipulate data in


various ways.
Operators:
 Operators are symbols or keywords that perform operations on data
elements or data structures.
 Common operators includes;
– Arithmetic operators (+, -, *, /),
– Comparison operators (==, !=, <, >), and
– Logical operators (&&, ||).

Assignment:
 Assignment is the process of storing a value in a variable.
– It typically involves using the assignment operator (=) to assign a value to
a variable.
Statements:
 Statements are the individual instructions or commands that
make up a program.
 Statements can include
– Assignment Statements
– Control Flow Statements (If, While, For)
– Function Calls.
Program Units:
 Program units are larger organizational structures within a
program.
 They can be;
– Functions
– Methods
– Classes
– Modules, or procedures, depending on the programming
language.
 Program units help break down a program into manageable and
reusable components.
Data Environments:
 The data environment refers to the scope and lifetime of
variables and data elements in a program.
 It determines where variables are visible and how long they
exist.
 Variables can have
– Local Scope (only accessible in a specific function or block)
– Global Scope (accessible throughout the entire program).
Parameter Transmission:
 Parameter transmission refers to how data is passed to functions or
methods.
 It includes concepts like pass by value and pass by reference.

Storage Management:
– Storage management is the process of allocating and deallocating
memory for data structures.
– It ensures that memory is efficiently used and released when no longer
needed.
– Garbage collection is a common technique for managing memory in
languages like Java and Python.
Programming Language basics
 Programming languages are used to instruct computers to
perform specific tasks.
 They consist of a set of rules and syntax that programmers use
to write code.
 Some fundamental concepts and terms related to programming
languages are:
 Syntax: refers to the set of rules that dictate how a program must be
written in a particular programming language.
 It includes rules for keywords, Punctuation, and structure.
Con…
 Variables: Variables are used to store data.
 They have a name and a data type (e.g., integers, floating-point numbers,
strings).

 Control Structures: are used to control the flow of a program.


 Common control structures include conditional statements (if, else,
switch), loops (for, while, do-while), and branching statements (break,
continue, return).

 Functions/Methods: are used to group a set of statements into a


reusable block.
 They take inputs (parameters) and may return a value.
Con…
 Comments: are used to add explanations or notes to the code.
 Comments are ignored by the compiler or interpreter and are there for
human readers.

 Libraries and Frameworks: Many programming languages have


libraries and frameworks that provide pre-written code for common tasks.

 These can save you time and effort when developing software.

 Error Handling:
 Most languages provide mechanisms to catch and handle errors,

preventing crashes and unexpected behavior.


Con…
 IDE (Integrated Development Environment ):
 An IDE is a software application that provides tools for writing,

testing, and debugging code.

 It includes a code editor, compiler/interpreter, and debugging tools.

 Paradigms:
 Different programming languages support various programming

paradigms, such As Procedural, Object-oriented, Functional, and

Logic Programming.

You might also like