CS2352 - Principles of Compiler Design
CS2352 - Principles of Compiler Design
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
Compiler
Error Messages
20-Dec-10
CS2352
Introduction to Compiling
Assembler
Assembly Language
Assembler
M/c Language
20-Dec-10
CS2352
Introduction to Compiling
Preprocessor
Compiler
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
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
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
20-Dec-10
CS2352
16
position
initial
rate
20-Dec-10 CS2352
60
17
Semantic Analyzer
Checks for (more) "static semantic" errors
Parse Tree
Semantic Analyzer
position
+ (float)
initial
* (float)
rate
intToFloat (float) 60
20-Dec-10
CS2352
18
Parse Tree
I.C.G
Intermediate Code
temp1 := inttofloat(60) temp2 := rate * temp1 temp3 := initial + temp2 position := temp3
20-Dec-10
CS2352
19
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