0% found this document useful (0 votes)
67 views28 pages

BKS UNIT I Lecture 1 Introduction To Compiler

Compiler notes

Uploaded by

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

BKS UNIT I Lecture 1 Introduction To Compiler

Compiler notes

Uploaded by

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

Learn Compiler Design: From B. K.

Sharma

UNIT I

Introduction to Compiler
Learn Compiler Design: From B. K. Sharma

Unit I: Syllabus
• Introduction to Compiler
• Structure of a compiler
• Lexical Analysis
• Role of Lexical Analyzer
• Input Buffering
• Specification of Tokens
• Recognition of Tokens
Learn Compiler Design: From B. K. Sharma

Unit I: Syllabus
• Lex
• Finite Automata
• Regular
• Expressions to Automata
• Minimizing DFA.
Learn Compiler Design: From B. K. Sharma

Mapping of Lesson with Course Outcome


(CO)
Lesson CO
Lesson 1: Introduction Apply the knowledge of
to Compiler theory of computation in
specifying and recognizing
tokens
Learn Compiler Design: From B. K. Sharma
Lesson 1: Learning Outcomes
At the end of this lesson, students will be able to
1: Define the compiler.

2: Identify Source Language, Target Language,


Target Machine Code.

3: Differentiate between Pure Machine code,


Augmented Machine code and Virtual Machine
Code.

4 : Justify “Why study Compiler Design?”

5 : List and explain different translators.


Learn Compiler Design: From B. K. Sharma

What is Compiler? (Original Meaning)

A compiler reads a program written in the


source language and translate it into an
equivalent program in machine language.

Source Machine
program Compiler code

Error
messages
Learn Compiler Design: From B. K. Sharma

What is Compiler? (Broader Meaning)

A compiler reads a program written in the


source language and translate it into an
equivalent program in target language.
Source Program Target Program
Compiler
High-Level Assembly or Machine
Language Language
Or
Error Intermediate Language
messages [Byte Code, MSIL]
Learn Compiler Design: From B. K. Sharma

What is Compiler? (Broader Meaning)

Is a program that translates one language


to another.

Takes as input a source program typically


written in a high level language.

Produces an equivalent target program


typically in assembly or machine language.

Reports error messages as part of the


translation process.
Learn Compiler Design: From B. K. Sharma

Examples of Source Language


Source language:
High-level programming languages (C, C++,
Java, C#)

Text-formatting languages (TROFF)

Query languages (SQL)

Logic-level specification languages(Prolog,


Answer-set-programming(ASP)
Learn Compiler Design: From B. K. Sharma

Examples of Target Language

Target language:

Machine language

Assembly Language

Intermediate Language [Byte Code,


MSIL]
Learn Compiler Design: From B. K. Sharma

Examples of Target Machine Code

Target machine code:

Pure machine code


Augmented machine code

Virtual machine code (e.g. byte code)


Learn Compiler Design: From B. K. Sharma

How does code execute?

Can the code be executed directly by CPU?

Can the code be executed collectively by


CPU
and Operating System?

Can the code be executed by Virtual


Machine?
(a machine that does not exist but is
simulated by another machine)
Learn Compiler Design: From B. K. Sharma

How does code execute?


Target machine code:
Pure machine code:
Compiler generated code for a particular machine
not assuming any OS or Library Routines.
This is pure code because it includes nothing
beyond the instruction set.

Pure code can execute on bare hardware without


dependence on any other software.
Each CPU has its own specific machine language
that produce pure machine code
Learn Compiler Design: From B. K. Sharma

How does code execute?


Target machine code:
Augmented machine code:
Compilers generate code for a machine
architecture augmented with operating system
routines and run-time language support routines.

To use such a program, a particular operating


system must be used and a collection of run-time
support routines (I/O, storage allocation,
mathematical functions, etc.) must be available.
Learn Compiler Design: From B. K. Sharma

How does code execute?


Target machine code:
Augmented machine code:
The combination of machine instruction and OS and
runtime routines define a virtual machine—a
computer that exists only as a hardware/software
combination.
Learn Compiler Design: From B. K. Sharma

How does code execute?


Target machine code:
Augmented machine code:

The compiler reads the C++ source code from a


file that ends with .cpp and produces a machine
code file that is executable
On Windows computers, executable files produced
by the C++ compiler end with .exe; other
operating systems follow different naming
conventions
Learn Compiler Design: From B. K. Sharma

How does code execute?


Target machine code:
Augmented machine code:

The operating system loads the machine code into


main memory where the hardware runs it directly
without further processing.
Learn Compiler Design: From B. K. Sharma

How does code execute?


Target machine code:
Virtual machine code:
Generated code can consist entirely of virtual
instructions (no native code at all).
This supports transportable code, that can run on
a variety of computers.
Java, with its JVM (Java Virtual Machine) is a
great example of this approach.
Learn Compiler Design: From B. K. Sharma

How does code execute?


Target machine code:
Virtual machine code:

Languages like Java and C# are hybrid languages


because they use both a compiler and a virtual
machine.
They first compile the source code to virtual
machine code, that is, to machine code for a
virtual computer (a computer that doesn't exist
but is simulated by another computer).
Learn Compiler Design: From B. K. Sharma

How does code execute?


Target machine code:
Virtual machine code:

After compiling the source code, a virtual machine


(VM) executes the code by simulating the actions
of a real computer.
The operating system loads the VM into main
memory and runs it; it is the VM that reads and
runs the virtual machine code.
Learn Compiler Design: From B. K. Sharma

Why study Compiler Design?


We can learn to drive a car without knowing the
mechanics of how the car works.
But never a good driver.
In long terms contributions to society and progress
and technology, who will score more:
Driver or People who designed the Car

?
Designing a compiler means designing / creating
the technology.
Learn Compiler Design: From B. K. Sharma

Why study Compiler Design?


Using something is usually enough when
everything goes as expected.

But if something goes wrong, only a true


understanding of the inner workings and
details will help to fix it.

Learning Compilers Design gives you with


both theoretical and practical knowledge
that is crucial in order to implement a
programming language.
Learn Compiler Design: From B. K. Sharma
What Do Compilers Do?

No matter what target code or target


machine code format are generated,
compilers do the same job: Translation.
Learn Compiler Design: From B. K. Sharma

A Translator

A program that accepts as input a program


written in a source language and translates
it into a target language.

Depending on the level of translation, we


have different names for translators:
Learn Compiler Design: From B. K. Sharma

Translator

Assembler Compiler Interpreter Preprocessor Editor Profiler

Assembler: translates from assembly code into a


machine language.

Compiler: translates from a high level language


(like C) to a low-level language
(machine code, virtual machine code,
assembly language).
Learn Compiler Design: From B. K. Sharma

Translator

Assembler Compiler Interpreter Preprocessor Editor Profiler

Interpreter: It reads a high-level language


program and data and it executes
the program immediately. No target
code is produced.

Preprocessor: Translates from a high-level


language into a related high-level
language.
Learn Compiler Design: From B. K. Sharma

Translator

Assembler Compiler Interpreter Preprocessor Editor Profiler

Editor: They can process text based on the


logical structure of the text(MS-Word’s
Bullets and Numbering).

Profiler:A program that analyzes a text or code


and produces some structural or/and
statistical information about the text.
Learn Compiler Design: From B. K. Sharma

Summary:
1:A compiler reads a program written in the source
language and translate it into an equivalent program in
target language.

2: Examples of source languages are High-level


programming languages (e.g., C, C++, Java)

3: Examples of target languages are machine language,


assembly language, Intermediate language (Byte Code,
MSIL)
4: Examples of Target Machine Code are Pure Machine
Code, Augmented Machine Code, Virtual Machine Code.

5: Different translators are Assemblers, compiler,


Interpreter pre-processor, Editor, profiler

You might also like