0% found this document useful (0 votes)
53 views24 pages

CS2352 - Principles of Compiler Design

The document outlines the phases and components of a compiler, including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, code generation, and error handling. It describes the functions of key compiler components like the lexical analyzer, syntax analyzer, semantic analyzer, intermediate code generator, code optimizer, code generator, and symbol table. The overall compilation process involves translating a source program through various representations and analyses into an equivalent target program.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
53 views24 pages

CS2352 - Principles of Compiler Design

The document outlines the phases and components of a compiler, including lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, code generation, and error handling. It describes the functions of key compiler components like the lexical analyzer, syntax analyzer, semantic analyzer, intermediate code generator, code optimizer, code generator, and symbol table. The overall compilation process involves translating a source program through various representations and analyses into an equivalent target program.
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 24

CS2352 - Principles of Compiler Design Introduction

20-Dec-10

CS2352

Course Outline
Introduction to Compiling Lexical Analysis Syntax Analysis
Context Free Grammars Top-Down Parsing, LL Parsing Bottom-Up Parsing, LR Parsing

Syntax-Directed Translation Run-Time Organization Intermediate Code Generation Code Optimization Code Generation
20-Dec-10 CS2352 2

Introduction to Compiling
Translators

Source Language

Translator

Target Language

20-Dec-10

CS2352

Introduction to Compiling
Compiler

High Level Language FORTRAN, PASCAL C, C++ etc

Compiler

Target Language M/c Lang. or Assembly Lang

Error Messages

20-Dec-10

CS2352

Introduction to Compiling
Assembler

Assembly Language

Assembler

M/c Language

20-Dec-10

CS2352

Introduction to Compiling
Preprocessor

High Level Language

Compiler

Another High Level Language

20-Dec-10

CS2352

Introduction to Compiling
Interpreter
Input Intermediate code Instead of output, performs operations implied by the source program.

20-Dec-10

CS2352

Phases of a Compiler
Analysis Phase

Synthesis Phase

20-Dec-10

CS2352

Major parts of Compiler


Analysis Phase and Synthesis Phase Analysis phase
an intermediate representation is created from the given source program. Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts of this phase.

Synthesis phase
the equivalent target program is created from this intermediate representation. Intermediate Code Generator, Code Generator, and Code Optimizer are the parts of this phase.
20-Dec-10 CS2352 9

Applications
Techniques used in a lexical analyzer can be used in text editors, information retrieval system, and pattern recognition programs. Parser query processing system such as SQL. Many software having a complex front-end may need techniques used in compiler design. Most of the techniques used in compiler design can be used in Natural Language Processing (NLP) systems.

20-Dec-10

CS2352

10

Overview of Compilation

20-Dec-10

CS2352

11

Lexical Analyzer
Reads Characters from left to right Tokens identifiers, keywords, operators, punctuation symbols, multi character operators
Source Program

Lexical Analyzer

Stream of Tokens

20-Dec-10

CS2352

12

Lexical Analyzer Cont


position := initial + rate * 60
The identifier position. The assignment symbol :=. The identifier initial. The plus sign. The identifier rate. The multiplication sign. The number 60.

20-Dec-10

CS2352

13

Syntax Analyzer
Parser also know as Hierarchical analysis
Stream of Tokens

Syntax Analyzer

Parse Tree

Performs 2 functions
Checks the token if they occur in patterns specified by the source language Construct a tree like structure that can be used by the subsequent phases

20-Dec-10

CS2352

14

Syntax Analyzer Cont


The hierarchical structure of programs is usually described by recursive rules like the following ones that describe expressions: 1. Any Identifier is an expression. 2. Any Number is an expression. 3. If Expression1 and Expression2 are expressions, then so are:
a. Expression1+Expression2 b. Expression1*Expression2 c. ( Expression1)

4. If Identifier1 is an identifier and Expression2 is an expression, then


Identifier1 := Expression2 is a statement.

5. If Expression1 is an expression and Statement2 is a statement, then


a. while (Expression1) doStatement2 b. if (Expression1) thenStatement2 are statements.
20-Dec-10 CS2352 15

Syntax Analyzer Cont


According to rule (1) position, initial and rate are expressions. Rule (2) states that 60 in an expression. Rule (3) says that rate * 60 is an expression Finally Rule (4) says that initial + rate * 60 is an expression.

20-Dec-10

CS2352

16

Syntax Analyzer Cont


:=

position

initial

rate
20-Dec-10 CS2352

60
17

Semantic Analyzer
Checks for (more) "static semantic" errors
Parse Tree

Semantic Analyzer

Modified / Annotated Parse Tree


:= (float)

position

+ (float)

initial

* (float)

rate

intToFloat (float) 60

20-Dec-10

CS2352

18

Intermediate Code Generator


Variety of Intermediate Representations

Parse Tree

I.C.G

Intermediate Code

temp1 := inttofloat(60) temp2 := rate * temp1 temp3 := initial + temp2 position := temp3

20-Dec-10

CS2352

19

Intermediate Code Generator


Properties of 3 Address Code
Atmost one operator other than assignment operator. Compiler must generate a temporary name to hold the value computed by each instruction. Some 3 address instruction can have less than 3 operands.

20-Dec-10

CS2352

20

Code Optimizer
Tries to improve code to
Run faster Be smaller Consume less energy

Intermediate Code

Code Optimizer

Optimized Code

temp1:=id3*60.0 id1:=id2+temp1

20-Dec-10

CS2352

21

Code Generator
Optimized Code

Code Generator

Target Program

MOVF id3,R2 MULF #60.0,R2 MOVF id2,R1 ADDF R2,R1 MOVF R1,id1

20-Dec-10

CS2352

22

Symbol Table
Keep track of names declared in the program Separate level for each scope Linear List Slow but easy to implement Hash table Complex to implement but fast.

20-Dec-10

CS2352

23

Error Handler
Involves
Detection of errors Reporting Errors Recovery of Errors.

20-Dec-10

CS2352

24

You might also like