Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Compiler Construction

What is a compiler?
Covering topics

● Introduction
● Language Processors

● Compiler

● Interpreter

● Questions?
Introduction

● Language is a way of communication between two


parties(Humans,Computers)
● Programming languages are representation of computations between human
and computer.
● Software running in a computer is written in some language, before running it
must be converted to a form understandable by computer.
● This task is done by the translators or language processors
Language Processors

● Compiler

Program Compiler Executable Output

Data
Errors

● Interpreter

Program
Interpreter Output
Data
A language processing system
Source program

Preprocessor

Modified source program

Compiler

Target assembly language

Assembler

Relocatable machine code

Linker/Loader Library files relocatable


object files

Target machine code


Basic Structure of Compiler

● Modern compilers preserve the outline of FORTRAN 1

– Lexical Analysis
– Parsing
– Semantic Analysis
– Code Optimization
– Code Generation
Basic Structure of Compiler

● Lexical Analysis: recognize words


This is a compiler construction subject

– Divides program into “words” or “tokens”


if x == y then z = 1; else z = 2;
Basic Structure of Compiler

● Parsing: diagramming sentence


– Diagram is a tree

This is a compiler construction subject

article verb article noun noun noun

subject object

sentence
Basic Structure of Compiler

● Parsing: program sentence


if x == y then z = 1; else z = 2;
x == y z 1 z 2

relation assign assign

predicate then-statement else-statement

if-then-else-statement
Basic Structure of Compiler

● Semantic Analysis: meaning


Ahmad said Naveed left his assignment at home.
– Ambiguity
– Programming languages define strict rules to avoid it
{
int count = 10;
{
int count=50;
cout<<count;
}
}
– Perform type checking
Basic Structure of Compiler

● Code Optimization: automatically modifies program so that


– run faster
– use less memory

x=x*0 is same as x=0;


Basic Structure of Compiler

● Code Generation: produces assembly code(usually)


– translation into another language
Economy of Programming Languages

● Why are there so many languages?


● Why new programming languages?
● What is a good programming language?
Why are there so many languages?
● Application domain have distinctive/conflicting needs.
● Scientific computing
– good floating pointing
– good arrays
– parallelism
● Business application
– persistence
– data analysis
– consistency
● System programming
– resource management
– real time constraints
Why new programming languages?

● Programmer training is the dominant cost for a programming language


– widely used languages are slow to change
– easy to start a new language
What is a good programming language?

● No universally accepted metric for language design


– a language that is used by most people
What is a good programming language?

● No universally accepted metric for language design


– a language that is used by most people
References
● Compilers Principals Techniques and Tools (Alfred V. Aho Colombia University,
Monica S.Lam Stanford Univeristy, Ravi Sethi Avaya, Jeffrey D.Ullman Stanford
University)
● Compiler course (Alex Icon Stanford University)

You might also like