0% found this document useful (0 votes)
5 views

ch1-intro1

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)
5 views

ch1-intro1

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/ 27

Principles of Compiler

Construction
Dr. Preeti Kaur
Syllabus
• Prerequisites: Computer Architecture, Data Structure, Microprocessor

• Textbook: “Compilers: Principles, Techniques, and Tools” by Aho,


Ullman and Sethi

2
COURSE CONTENTS
Unit 1
Introduction: Language processors, structure of a compiler, compiler-construction tools, evolution of programming
languages, applications of compiler technology, Transition diagrams, bootstrapping, just-in-time compilation.

Unit 2

Lexical analysis: Input buffering, specification and recognition of tokens, lexical analyzer generator.

Unit 3

Syntax analysis: Specification of syntax using grammar. Top-down parsing – recursive-descent, predictive. Bottom-up
parsing – shift-reduce, SLR, CLR, LALR. Parser generator.

Unit 4

Intermediate-code generation: Syntax-directed translation. Three-address code. Translation of declarations,


expressions, control flow. Backpatching. Runtime environment: Activation trees and records.

Unit 5
Code optimization: Sources of optimization, basic blocks, optimization of basic blocks, data-flow analysis, loop
optimizations. Code generation: Issues, register allocation and assignment, peephole optimization

3
Practicals
• Develop simple language processors like desk calculator and
assembler.
• Design a small high-level language.
• Develop a lexical analyzer and a syntax analyzer for the same using
the LEX and YACC tools. Also implement the bookkeeper module.
• Design a small high-level language and implement a compiler for the
same. If the target machine of the compiler is a hypothetical machine,
then implement a simulator for it.
• Develop a simple calculator using LEX and YACC tools.
• Implement a program for symbol table using hashing
• Implement a two-pass assembler
• Implement a bottom-up parser using YACC tool.
• Represent ‘C’ language using Context Free Grammar
• Add assignment statement, If then else statement and while loop to the
calculator and generate the three address code for the same.

4
SUGGESTED READINGS
• Aho, A. V., Lam, M. S., Sethi, R. and Ullman J. D.,
“Compilers – Principles,Techniques and Tools (2nd ed.)”,
Pearson.
• Chattopadhyay, S. 2005, “Compiler Design, PHI”.
• Appel, A. W. 200, “Modern Compiler Implementation in
C", Cambridge University Press.
• Kenneth C. Louden (1997), Compiler Construction–
Principles and Practice, 1st edition, PWS Publishing.

5
Objectives
• To learn about translators
• To build a compiler for a programming language
• To learn and use compiler construction tools, such as generators of
scanners and parsers
• To learn about grammars and to define LL(1), LR(1), and LALR(1)
grammars
• To learn compiler analysis and optimization techniques

6
What do we expect to achieve by the end of
the course?
• Knowledge to design, develop, understand, modify/enhance, and
maintain compilers for (even complex!) programming languages
• Confidence to use language processing technology for software
development
Organization of the course
CACSC14 is a 3 2 0 course i.e.
Marks for
• TCA Theory Continuous assesment-15
5 (class test)+5(assignment)+5(attendance)
• TMS Mid semester exam -15
• TES End semester exam -40
• PCA Practical Continuous assesment-15
• PES Practical End semester exam-15
Compilers and Interpreters
• “Compilation”
• Translation of a program written in a source language into a semantically
equivalent program written in a target language
• simplified view:

Input

Source Target
Compiler
Program Program

Error messages Output 9


Compilers and Interpreters (cont’d)
• “Interpretation”
• Performing the operations implied by the source program
• simplified view:

Source
Program
Interpreter Output
Input

Error messages 10
Compiler vs. Interpreter

◼ Compilers: Translate a source (human-writable) program to


an executable (machine-readable) program

◼ Interpreters: Convert a source program and execute it at the


same time.

11
Compiler vs. Interpreter
Ideal concept:

Source code Compiler Executable

Input data Executable Output data

Source code
Interpreter Output data
Input data

12
Compiler vs. Interpreter
◼ Most languages are usually thought of as using either one or
the other:
◼ Compilers: FORTRAN, COBOL, C, C++, Pascal, PL/1
◼ Interpreters: Lisp, scheme, BASIC, Perl, Python, Smalltalk

◼ BUT: not always implemented this way


◼ Virtual Machines (e.g., Java)
◼ Linking of executables at runtime
◼ JIT (Just-in-time) compiling

13
Compiler vs. Interpreter
Compiler Interpreter
◼ Pros ◼ Pros
◼ Less space ◼ Easy debugging
◼ Fast execution ◼ Fast Development

◼ Cons ◼ Cons
◼ Slow processing ◼ Not for large projects
◼ Partly Solved ◼ Exceptions: Perl, Python
(Separate compilation) ◼ Requires more space
◼ Debugging ◼ Slower execution
◼ Improved thru IDEs(integrated ◼ Interpreter in memory all the time
development environment)

14
Compiler vs. Interpreter

15
The Analysis-Synthesis Model of Compilation

• There are two parts to compilation:


• Analysis determines the operations implied by the source program which are
recorded in a tree structure
• Synthesis takes the tree structure and translates the operations therein into
the target program

16
Other 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)

17
Preprocessors, Compilers, Assemblers, and
Linkers
Skeletal Source Program

Preprocessor
Source Program
Compiler
Target Assembly Program
Assembler
Relocatable Object Code
Linker Libraries and
Relocatable Object Files
18
Absolute Machine Code
The Phases of a Compiler
Phase Output Sample
Programmer (source code producer) Source string A=B+C;
Scanner (performs lexical analysis) Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’
And symbol table with names
Parser (performs syntax analysis Parse tree or abstract syntax tree ;
|
based on the grammar of the =
programming language) / \
A +
/ \
B C

Semantic analyzer (type checking, Annotated parse tree or abstract


etc) syntax tree
Intermediate code generator Three-address code, quads, or int2fp B t1
RTL register transfer language. + t1 C t2
:= t2 A
Optimizer Three-address code, quads, or int2fp B t1
RTL + t1 #2.3 A
Code generator Assembly code MOVF #2.3,r1
ADDF2 r1,r2
MOVF r2,A
Peephole optimizer Assembly code ADDF2 #2.3,r2
MOVF r2,A 22
The Grouping of Phases
• Compiler front and back ends:
• Front end: analysis (machine independent)
• Back end: synthesis (machine dependent)
• Compiler passes:
• A collection of phases is done only once (single pass) or multiple times (multi
pass)
• Single pass: usually requires everything to be defined before being used in source
program
• Multi pass: compiler may have to keep entire program representation in memory

23
Compiler-Construction Tools
• Software development tools are available to implement one or more
compiler phases
• Scanner generators
• Parser generators
• Syntax-directed translation engines
• Automatic code generators
• Data-flow engines

24
Outline
• Introduction
• Lexical Analysis and Lex/Flex
• Syntax Analysis and Yacc/Bison
• Syntax-Directed Translation
• Type Checking
• Intermediate Code Generation
• Target Code Generation
• Code Optimization

25
• Symbol Table
• Error Recovery

26
Learning in this course
• High level languages to machine code conversion

• Both theoretical and practical components are covered to implement programming languages

• Algorithms and data structures are studied which are required to understand the process of compilation

• Theory of lexical analysis, parsing, type checking, runtime system, code generation, optimization

• Use of tools and specifications for developing various parts of compilers are also covered in the course

27

You might also like