CD Finalized Notes
CD Finalized Notes
MODULE-2
INRODUCTION TO COMPILER DESIGN
1.1 Language Processors:
✓ Compiler: A compiler is a program that can read a program in one language — the source
language — and translate it into an equivalent program in another language — the target
language. See Fig. 1.1.
✓ An important role of the compiler is to report any errors in the source program that it detects during
the translation process.
Page 1
SYSTEM SOFTWARE & COMPILERS 18CS61
COMPILER INTERPRETER
1. It works on complete program at once 1. It works line-by-line (it takes one statement
(entire program as input). at a time as input).
2. It generates Intermediate code called 2. It doesn’t generate Intermediate Object code
Object code or Machine code. or Machine code.
3. It executes Conditional control statements 3. It executes Conditional control statements
(if-else, switch-case) & logic Constructs faster (if-else, switch-case) & logic Constructs
than Interpreter. Slower than Compiler.
4. The Compiled program takes more 4. The interpreted programs are more Memory
memory (entire object code has to reside in efficient (it doesn’t generate object code).
memory).
5. Compile the Program once and run any 5. Program are interpreted line-by-line every
time. time when they run.
6. Errors are reported after entire program is 6. Error is reported as soon as the first error is
checked. encountered, rest of program will not bechecked
until error is removed.
7. Compiled language is difficult to debug. 7. It is easy to debug, because it stops & reports
errors as it encounters them.
8.Examples:C,C++,COBOL 8.Examples:BASIC,PHP,MATLAB,LISP
✓ A language-processing system:
1. A source program may be divided into modules stored in separate files. The task of collecting
the source program is sometimes entrusted to a separate program, called a preprocessor.
2. The compiler may produce an assembly-language program as its output, because assembly
language is easier to produce as output and is easier to debug.
3. The assembly language is then processed by a program called an assembler that produces
relocatable machine code as its output.
4. Large programs are often compiled in pieces, so the relocatable machine code may have to be
linked together with other relocatable object files and library files into the code that actually
runs on the machine.
5. The linker resolves external memory addresses, where the code in one file may refer to a location
in another file.
6. The loader then puts together the entire executable object files into memory for execution.
Page 2
SYSTEM SOFTWARE & COMPILERS 18CS61
Page 3
SYSTEM SOFTWARE & COMPILERS 18CS61
Page 4
SYSTEM SOFTWARE & COMPILERS 18CS61
✓ The node labeled * makes it explicit that we must first multiply the value of rate by 60. The node
labeled + indicates that we must add the result of this multiplication to the value of initial.
✓ The root of the tree, labeled =, indicates that we must store the result of this addition into the location
for the identifier position.
Page 5
SYSTEM SOFTWARE & COMPILERS 18CS61
b) The compiler must generate a temporary name to hold the value computed by a three-address
instruction.
c) Some "three-address instructions" like the first and last in the sequence (1.3), above, have
fewer than three operands.
Page 6