×
Expect problems and eat them for breakfast.
--Your friends at LectureNotes
Close

Compiler Design

by Dr. D. JagadeesanDr. D. Jagadeesan
Type: NoteInstitute: ANNA UNIVERISTY Specialization: Computer Science EngineeringOffline Downloads: 72Views: 1936Uploaded: 5 months ago

Share it with your friends

Suggested Materials

Leave your Comments

Contributors

Dr. D. Jagadeesan
Dr. D. Jagadeesan
Anna University – B.E -VI Sem CSE CS2352 – Principles of Compiler Design Dr. D. Jagadeesan, B.E., M.Tech., Ph.D., MISTE., Professor in CSE, APCE Unit – I Syllabus : Unit – I : LEXICAL ANALYSIS Introduction to Compiling- Compilers-Analysis of the source program- The phasesCousins-The grouping of phases-Compiler construction tools. The role of the lexical analyzer- Input buffering-Specification of tokens-Recognition of tokens-A language for specifying lexical analyzer. Compiler : A Compiler is a program that reads a program written in one language (Source Language like C,C++,etc…) and translate it into an equivalent program in another language (Target Language like Machine Language) and the Compiler reports to its user the presence of errors in the source program. Source Program (High Level Language) Complier Target Program (Low Level Language) Error Message Classification of Compiler : 1. Single Pass Compiler 2. Multi-Pass Compiler 3. Load and Go Compiler 4. Debugging or Optimizing Compiler. Software Tools : Many software tools that manipulate source programs first perform some kind of analysis. Some examples of such tools include:  Structure Editors :  A structure editor takes as input a sequence of commands to build a source program.  The structure editor not only performs the text-creation and modification functions of an ordinary text editor, but it also analyzes the program text, putting an appropriate hierarchical structure on the source program.  Example – while …. do and begin….. end.  Pretty printers :  A pretty printer analyzes a program and prints it in such a way that the structure of the program becomes clearly visible.  Static Checkers :  A static checker reads a program, analyzes it, and attempts to discover potential bugs without running the program. 1 / 17
Anna University – B.E -VI Sem CSE CS2352 – Principles of Compiler Design Dr. D. Jagadeesan, B.E., M.Tech., Ph.D., MISTE., Professor in CSE, APCE Unit – I  Interpreters :  Translate from high level language ( BASIC, FORTRAN, etc..) into assembly or machine language.  Interpreters are frequently used to execute command language, since each operator executed in a command language is usually an invocation of a complex routine such as an editor or Compiler.  The analysis portion in each of the following examples is similar to that of a conventional Compiler.  Text formatters.  Silicon Compiler.  Query interpreters. Analysis of Source Program : The analysis phase breaks up the source program into constituent pieces and creates an intermediate representation of the source program. Analysis consists of three phases:  Linear analysis (Lexical analysis or Scanning)) :  The lexical analysis phase reads the characters in the source program and grouped into them tokens that are sequence of characters having a collective meaning.  Example : position : = initial + rate * 60  Identifiers – position, initial, rate.  Assignment symbol - : =  Operators - + , *  Number - 60  Blanks – eliminated.  Hierarchical analysis (Syntax analysis or Parsing) :  It involves grouping the tokens of the source program into grammatical phrases that are used by the Compiler to synthesize output.  Example : position : = initial + rate * 60 Assignment statement | := Identifier | position Expression | + Expression | identifier | initial Expression | * Expression | identifier | rate Expression | number | 60 2 / 17
Anna University – B.E -VI Sem CSE CS2352 – Principles of Compiler Design Dr. D. Jagadeesan, B.E., M.Tech., Ph.D., MISTE., Professor in CSE, APCE Unit – I  Semantic analysis :  In this phase checks the source program for semantic errors and gathers type information for subsequent code generation phase.  An important component of semantic analysis is type checking.  Example : int to real conversion. Expression | * Expression | identifier | rate Expression | number | inttoreal | 60 Phases of Compiler: A Compiler operates in phases, each of which transforms the source program from one representation to another.  Two parts (Six Phases) of compilation. They are,  Analysis Phase ( Three Phases)  Lexical Analysis  Syntax Analysis  Semantic Analysis  Synthesis Phase ( Three Phases)  Intermediate Code Generation  Code Optimizer  Code Generator  Two other activities are  Symbol Table Management  Error Handler Lexical Analysis :  It is also called scanner.  The lexical analysis phase reads the characters in the source program and grouped into them tokens that are sequence of characters having a collective meaning. Such as an Identifier, a Keyword, a Punctuation, an operator or multi character operator like ++.  The character sequence forming a token is called the lexeme for the token. Certain tokens will be augmented by a lexical value.  Example : position : = initial + rate * 60  id1 := id2 + id3 * 60  Blanks – eliminated. 3 / 17
Anna University – B.E -VI Sem CSE CS2352 – Principles of Compiler Design Dr. D. Jagadeesan, B.E., M.Tech., Ph.D., MISTE., Professor in CSE, APCE Unit – I Source Program Lexical Analyzer Syntax Analyzer Symbol Table Management Error Handler Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator Target Program Syntax analysis:  It processes the string of descriptors (tokens), synthesized by the lexical analyzer to determine the syntactic structure of an input statement. This process is known as parsing.  Output of the parsing step is a representation of the syntactic structure of a statement. A convenient representation is in the form of a syntax tree.  Example : position : = initial + rate * 60 := + id1 id2 * id3 60 Semantic analysis :  In this phase checks the source program for semantic errors and gathers type information for subsequent code generation phase.  An important component of semantic analysis is type checking.  Example : int to real conversion. 4 / 17

Lecture Notes