0% found this document useful (0 votes)
31 views34 pages

Compiler Construction Week 1

The document provides an overview of compiler construction including the prerequisites, contents, learning outcomes, teaching methodology, assessment, and recommended books for the course. It discusses topics like compilers vs interpreters, the structure of compilers including analysis and synthesis, and common compiler construction tools.

Uploaded by

Zulfiqar Ali
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)
31 views34 pages

Compiler Construction Week 1

The document provides an overview of compiler construction including the prerequisites, contents, learning outcomes, teaching methodology, assessment, and recommended books for the course. It discusses topics like compilers vs interpreters, the structure of compilers including analysis and synthesis, and common compiler construction tools.

Uploaded by

Zulfiqar Ali
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/ 34

Compiler Constructions

Zulfiqar Ali
UIT University
Syllabus
Prerequisite:
• Theory of Automata
Contents:
• Introduction to interpreter and compiler. Compiler techniques and
methodology, Organization of compilers; Lexical and syntax analysis;
Parsing techniques. Types of parsers, top-down parsing, bottom-up
parsing, Type checking, Semantic analyser, Object code generation and
optimization, detection and recovery from errors.
Course Learning Outcomes
1. Understand the basic techniques used in compiler construction such as
lexical analysis, top-down, bottom-up parsing, context-sensitive analysis,
and intermediate code generation

1. Understand the basic data structures used in compiler construction such


as abstract syntax trees, symbol tables, three-address code, and stack
machines
2. Design and implement a compiler using a software engineering approach
3. Use generators (e.g. Lex and Yacc)
Teaching Methodology:
• Lectures, Written Assignments, Semester Project,
Presentations

Course Assessment:
• Quizzes, Assignments, Mid Exam, Project, Presentations, Final
Exam
Recommended & Reference Books
• Compilers: Principles, Techniques, and Tools, A. V. Aho, R.
Sethi and J. D. Ullman, Addison-Wesley, 2nd ed., 2006.

• Modern Compiler Implementation in C ANDREW W. APPEL,


Princeton University with MAIA GINSBURG
Week 1 Outline
• Introduction
• Language Pre-Processors
• Compilers and Interpreters
• Structure of a Compiler
Introduction
• Programming languages are notations for describing
computations to people and to machines.
• A program can be run, 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.
Language Processors
• Simply stated, 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.
• An important role of the compiler is to report any
errors in the source program that it detects during
the translation process.
• If the target program is an executable machine-
language program, it can then b e called by the user
to process inputs and produce outputs.
• The machine-language target program produced by a
compiler is usually much faster than an interpreter at
mapping inputs to outputs . An interpreter, however,
can usually give better error diagnostics than a
compiler, because it executes the source program
statement by statement.
Example
• Java language processors combine compilation and
interpretation, as shown in Figure.
• A Java source program may first be compiled into an
intermediate form called byte codes. The byte codes
are then interpreted by a virtual machine
Why we use High Level Languages?
Using a high-level language for programming has a large
impact on how fast programs can be developed. The main
reasons for this are:
1. Compared to machine language, the notation used by programming
languages is closer to the way humans think about problems.
2. The compiler can spot some obvious programming mistakes.
3. Programs written in a high-level language tend to be shorter than
equivalent programs written in machine language.
4. The same program can be compiled to many different machine
languages and, hence, be brought to run on many different machines
Compilers
• A compiler is a program translates (or compiles) a
program written in a high-level programming
language (the source language) that is suitable for
human programmers into the low-level machine
language (target language) that is required by
computers.
• During this process, the compiler will also attempt to
spot and report obvious programmer mistakes that
detect during the translation process.
Intepreters
• Performing the operations implied by the
source program
Compilers vs Interpreters
Compilers Interpreter
• Takes Entire program as input • Take single instruction as input
• It is Faster • It is Slower
• Intermediate object code is generated. • No intermediate code is generated
• Required more memory Due to • Required less memory As no
intermediate object code intermediate code is generated
• Program not need compile every time • Every time higher level program is
• Errors are displayed after entire converted into lower level program.
program is checked. • Errors are displayed for every
• Debugging is comparatively hard. instruction interpreted.
• Ex: C, C++. • Debugging is easy.
• Ex: python, Ruby, basic.
Hybrid Compilers
• Compilation and interpretation may be
combined to implement a programming
language:
• The compiler may produce intermediate-level
code which is then interpreted rather than
compiled to machine code.
• Ex: java
Structure of a Compiler
• Up to this point we have treated a compiler as
a single box that maps a source program into a
semantically equivalent target program. If we
open up this box a little, we see that there are
two parts to this mapping: analysis and
synthesis.
Analysis
• Analysis determines the operations implied by
the source program which are recorded in a tree
structure

• The analysis part breaks up the source program


into constituent pieces and imposes a
grammatical structure on them.
• It then uses this structure to create an intermediate
representation of the source program.
• If the analysis part detects that the source program is
either syntactically ill formed or semantically
unsound, then it must provide informative messages,
so the user can take corrective action.
Syntheysis
• The synthesis part constructs the desired
target program from the intermediate
representation and the information in the
symbol table.
• The analysis part is often called the front end
of the compiler; the synthesis part is the back
end of the compiler.
Tools that Use the Analysis-Synthesis Model
• Editors (syntax highlighting)
• Pretty printers (e.g. doxygen)
• Static checkers (e.g. lint and splint)
• Interpreters
• Text formatters (e.g. TeX and LaTeX)
• Silicon compilers (e.g. VHDL)
• Query interpreters/compilers (Databases)
Compiler-Construction Tools
1. Parser generators that automatically produce syntax analyzers from a grammatical
description of a programming language.
2. Scanner generators that produce lexical analyzers from a regular-expression description of
the tokens of a language.
3. Syntax-directed translation engines that produce collections of routines for walking a parse
tree and generating intermediate code.
4. Code-generator generators that produce a code generator from a collection of rules for
translating each operation of the intermediate language into the machine language for a
target machine.
5. Data-flow analysis engines that facilitate the gathering of information about how values are
transmitted from one part of a program to each other part. Data-flow analysis is a key part of
code optimization.
6. Compiler-construction toolkits that provide an integrated set of routines for constructing
various phases of a compiler.
Why learn about compilers?
• It is considered a topic that you should know in order to be
“well-cultured” in computer science.
• A good craftsman should know his tools, and compilers are
important tools for programmers and computer scientists.
• The techniques used for constructing a compiler are useful
for other purposes as well.
• There is a good chance that a programmer or computer
scientist will need to write a compiler or interpreter for a
domain-specific language.
Evaluation of Programming Languages
• Classification by generation
• First-generation languages: machine languages
• Second-generation : assembly languages
• Third-generation : higher-level languages like Fortran, Cobol, Lisp, C,
C++, C#, and Java.
• Fourth-generation languages: languages designed for specific
applications like NOMAD 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.
Summary
• Language Processors: An integrated software development
environment includes many different kinds of language
processors such as compilers, interpreters, assemblers,
linkers, loaders, debuggers, profilers.
• Compilers and Interpreters
• Why learn about compilers
• Compiler Construction Tools
• Evaluation of Programming languages
References
• Compilers: Principles, Techniques, and Tools, A. V.
Aho, R. Sethi and J. D. Ullman, Addison-Wesley, 2nd
ed., 2006.
THANK YOU

You might also like