DEPARTMENT OF COMPUTER SCIENCE AND BUSINESS SYSTEMS
III YEAR – VI SEMESTER
21CS1602-COMPILER DESIGN
UNIT I - INTRODUCTION TO COMPILERS 9
Structure of a compiler – Lexical Analysis – Role of Lexical Analyzer – Input Buffering –
Specification of Tokens – Recognition of Tokens – Lex – Finite Automata – Regular
Expressions to Automata – Minimizing DFA.
INTRODUCTION TO COMPILERS
TRANSLATORS-COMPILATION AND INTERPRETATION
TRANSLATOR
A translator is a program that takes as input a program written in one
language and produces as output a program in another language. Beside
program translation, the translator performs another very important role,
the error-detection. Any violation of d HLL(High Level Language)
specification would be detected and reported to the programmers.
Important Role of Translator are:
Translating the HLL program input into an equivalent ml program.
Providing diagnostic messages wherever the programmer violates
specification of the HLL.
A translator or language processor is a program that translates an
input program written in a programming language into an equivalent
program in another language.
Source Code Translator Target Code
Execution in Translator
Types of Translators:
a. Interpreter
b. Assembler
c. Compiler
1.1.1.a INTERPRETER
An interpreter is a program that appears to execute a source program
as if it were machine language. It is one of the translators that translate
high level language to low level language.
Source Program Program Output
Interpreter
Data
Execution in Interpreter
, During execution, it checks line by line for errors. Languages such as
BASIC, SNOBOL, LISP can be translated using interpreters. JAVA also uses
interpreter. The process of interpretation can be carried out in following
phases.
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
4. Direct Execution
Example: BASIC , Lower Version of Pascal, SNOBOL, LISP & JAVA
Advantages:
Modification of user program can be easily made and implemented as
execution proceeds.
Type of object that denotes various may change dynamically.
Debugging a program and finding errors is simplified task for a
program used for interpretation.
The interpreter for the language makes it machine independent.
Disadvantages:
The execution of the program is slower.
Memory consumption is more.
1.1.1.b. ASSEMBLER
Programmers found it difficult to write or read programs in machine
language. They begin to use a mnemonic (symbols) for each machine
instruction, which they would subsequently translate into machine
language. Such a mnemonic machine language is now called an assembly
language. Programs known as assembler were written to automate the
translation of assembly language in to machine language. The input to an
assembler program is called source program, the output is a machine
language translation (object program).
It translates assembly level language to machine code.
Assembly Language Assembler Machine Code
Fig1.1.3: Execution in Assembler
Example: Microprocessor 8085, 8086.
Advantages:
Debugging and verifying
Making compilers->Understanding assembly coding techniques is
necessary for making compilers, debuggers and other development
tools.
Optimizing code for size
Optimizing code for speed.
Disadvantages:
Development time. Writing code in assembly language takes much
longer than writing in a high-level language.
Reliability and security. It is easy to make errors in assembly code.
Debugging and verifying. Assembly code is more difficult to debug and
verify because there are more possibilities for errors than in high-level
code.
, Portability. Assembly code is platform-specific. Porting to a different
platform is difficult
1.1.1.c. COMPILER
Compiler is a translator program that translates a program written in
(HLL) the source program and translate it into an equivalent program in
(MLL) the target program. As an important role of a compiler is error
showing to the programmer.
Source Code Compiler Target Code
Error Message
Fig1.1.4: Execution in Compiler
Executing a program written in HLL programming language is
basically of two parts. The source program must first be compiled translated
into a object program. Then the results object program is loaded into a
memory executed.
Source Code Compiler Object Code
Object Program Object Program Object Program
Input Output
Fig1.1.5: Execution Process of Source Program
in Compiler
Example: C, C++, COBOL, higher version of Pascal.
List of Compilers :
Ada compilers
ALGOL compilers
BASIC compilers
C# compilers
C compilers
C++ compilers
COBOL compilers
D compilers
Common Lisp compilers
Fortran compilers
Java compilers
Pascal compilers
PL/I compilers
Python
, Difference between Compiler and
Interpreter
Sl.No Compiler Interpreter
1 Compiler works on the Interpreter program works
complete program at once. It line-by-line. It takes one
takes the entire program as statement at a time as input.
input.
2 Compiler generates Interpreter does not generate
intermediate code, called the intermediate object code or
object code or machine code. machine code .
3 Compiler executes conditional Interpreter executes
control statements (like if-else conditional control statements
and switch-case) and logical at a much slower speed.
constructs faster than
interpreter.
4 Compiled programs take more Interpreter does not generate
memory because the entire intermediate object code. As a
object code has to reside in result, interpreted programs
memory. are more memory efficient.
5 Compile once and run 5 Interpreted programs are
anytime. Compiled program interpreted line-by-line every
does not need to be compiled time they are run.
every time.
6 Compiler does not allow a Interpreter runs the program
program to run until it is from first line and stops
completely error-free. execution only if it encounters
an error.
7 Compiled languages are more Interpreted languages are less
efficient but difficult to debug. efficient but easier to debug.
This makes such languages
an ideal choice for new
students
8 Example: C, C++, COBOL Example: BASIC, Visual
Basic, Python, Ruby, PHP,
Perl, MATLAB, Lisp
Language processors (COUSINS OF THE COMPLIER or Language
Processing System) :
1. Preprocessors :
It produces input to Compiler. They may perform the following
functions.
Macro Processing:
A preprocessor may allow a user to define macros that are
shorthand’s for longer constructs.
File inclusion:
A preprocessor may include header files into the program text. For
example, the C preprocessor causes the contents of the file <global.h> to replace
the statement #include<global.h> when it processes a file containing this
statement.