Download presentation
Presentation is loading. Please wait.
Published byJonah Morrison Modified over 9 years ago
1
Compiler Construction CPCS302 Dr. Manal Abdulaziz
2
Chapter 1 Introduction
3
Dr.Manal AbdulazizCPCS302 Ch13 Definitions Compilers are computer programs that translate one language to another. Compiler input: Source language / high level language. Compiler output: Target language / object code or m/c code. Why compilers? Compiler Source Program Target Program
4
Dr.Manal AbdulazizCPCS302 Ch14 Chomsky hierarchy It is a classification of structure of natural languages. The classification is according to the complexity of the language’s grammar and the power of the algorithms needed to recognize them. It consists of four levels: type 0, type 1, type 2, type 3 grammars. Type 2, or context free, grammars proved to be the most useful for programming languages.
5
Dr.Manal AbdulazizCPCS302 Ch15 Parsing Problem It is the determination of efficient algorithms for the recognition of context free grammar. Closely related to context free grammars are finite automata and regular expression. Scanner generators developed from the study of finite automata. Parser generators automate the program development which is only one part of the compiler program.
6
Dr.Manal AbdulazizCPCS302 Ch16 Programs related to Compiler Interpreters: It is a translator like a compiler which executes the source code immediately rather than generating object code. Ex: LISP. Assemblers: It is a translator for the assembly language for a particular computer. Linkers: It is program collects code separately compiled or assembled in different object files into a file that is directly executable. Loaders: Codes produced by compilers are not completely fixed and ready to execute. Such code are said to be relocatable, and a loader will resolve all relocatable addresses relative to given base or starting address.
7
Dr.Manal AbdulazizCPCS302 Ch17 Programs related to Compiler: cont Preprocessors: It is called by the compiler program before actual translation begins. Editors: Source program is written using any editor that produces a standard file. Compilers have been bundled together with editors and other programs into an interactive development environment IDE Debuggers: It is a program that determines execution errors in a compiled program. Profilers: it is a program that collects statistics on the behavior of an object program during execution. Project Managers: It is a coordinator program that manages large programs worked with different people.
8
Dr.Manal AbdulazizCPCS302 Ch18 The Translation Process Scanner Parser Semantic Analyzer Source Code Optimizer Code Generator Target Code Optimizer Source Code Tokens Syntax Tree Annotated Tree Intermediate Code Target Code Target Code Error Handler Symbol Table Literal Table
9
Dr.Manal AbdulazizCPCS302 Ch19 The Translation Process Scanner Scanner phase does the actual reading of the source program, which is a stream of characters. The scanner program, called lexical analyzer, collects sequences of characters into meaningful units, called tokens. Ex: a [ index ] = 4 + 2 12 nonblank character & 8 tokens
10
Dr.Manal AbdulazizCPCS302 Ch110 The Translation Process Parser The parser receive the source code in token form and perform syntax analysis, which determines the structure of the source program. The results of syntax analysis are represented as parse tree or syntax tree. exp Assign exp = Subscript exp Addition exp [ index ] exp + id num Parse Tree
11
Dr.Manal AbdulazizCPCS302 Ch111 The Translation Process The Semantic Analyzer The semantics of a program determine its runtime behavior. Static semantics are some features that can be determined prior to execution. The result of semantic analyzer on the syntax tree represented by annotated parse tree. Assign exp Subscript expAddition exp id num integer aindex 4 2 Array of integer Abstract Parse Tree Annotated Parse Tree
12
Dr.Manal AbdulazizCPCS302 Ch112 The Translation Process The source Code Optimizer The intermediate code referred to a form of code representation intermediate between source code and object code, such as three address code. t = 4 +2 = 6 a [ index ] = t a [ index ] = 6 Subscript expnumber id integer aindex integer Array of integer 6 Assign exp
13
Dr.Manal AbdulazizCPCS302 Ch113 The Translation Process The Code Generator In this phase of compilation the properties of the target machine become the major factor. MOV R0, index value of index R0 MUL R0, 2 double value in R0 MOV R1, &a address of a R1 ADD R1, R0 add R0 to R1 MOV *R1, 6 constant 6 address in R1
14
Dr.Manal AbdulazizCPCS302 Ch114 The Translation Process The Target Code Optimizer In this phase, compiler attempts to improve the target code generated by the code generator. Such improvements include choosing addressing modes to improve performance, replacing slow instruction by faster ones, and eliminating redundant or unnecessary operations. MOV R0, index value of index R0 SLH R0 double value in R0 MOV &a[R0], 6 constant 6 address a + R0
15
Dr.Manal AbdulazizCPCS302 Ch115 Major Data Structure in the Compiler 1.Tokens: represented as a value of an enumerator data types representing the set of tokens in the source language. 2.The syntax tree: is constructed as a pointer- based structure. 3.The symbol table: keeps information associated with identifiers, functions, variables, constants, and data types. 4.The literal table: stores constants and strings used in a program for quick insertion and lookup. 5.Intermediate code: is kept as an array of text strings, a temporary text file, or as a linked list structures.
16
Dr.Manal AbdulazizCPCS302 Ch116 Front-end and Back-end This view regards the compiler as separated into those operations that depend only on the source language ( the front-end ) and those operations that depend only on the target language ( the back-end ). Front-endBack-end Intermediate Code Source Code Target Code
17
Dr.Manal AbdulazizCPCS302 Ch117 Error Handling One of the most important function of the compiler is its response to errors in the source program. Errors can be detected during almost every phase of compilation. These static errors must be reported by compiler. The compiler has to be able to generate meaningful error messages and resume compilation after each error.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.