![Page 1: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/1.jpg)
1
CSC 338: Compiler design and implementation
Dr. Mohamed Ben Othman
![Page 2: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/2.jpg)
2
Goals:
- Compilers are used everyday in most computers.
- Allow students to implement big programming projects.
- Parts of most projects (specially those containing command language) can be built in the same way compilers are built.
![Page 3: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/3.jpg)
3
Introduction
1. Terminology
2. Compilers implementation languages
3. Compiler structure
4. Example
![Page 4: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/4.jpg)
4
Terminology
- Compilers: translate programs written in high level languages. The entire program has to be translated before execution (compared to a book translation).
- Interpreters: translate statement by statement (or line by line) and then this statement is given to the CPU to be executed before the next statement in being translated (compared to a man translator).
- High Level Language- Source Language- Source Code- Implementation language- Target machine: the machine to which the translation
will be done. 8086 Machine CodeC CompilerC Program
![Page 5: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/5.jpg)
5
How to choose the compiler implementation language
- The first compiler was written in assembly language
- The compiler of Fortran77 programming language is Pascal.
- It is preferable to write the compiler in the source language from which it will translate to machine language.
![Page 6: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/6.jpg)
6
Translation operation
The are some compilers do not respect this pattern. - Load-and-go Compilers: gives programs ready for
execution.- Cross-language compilers: compilers between
high level languages.
CompilerSource Code Executable CodeLinkerObject Code
![Page 7: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/7.jpg)
7
Compiler is divided into two parts: Front End and Back End.
The Front End translates a program from source language to an intermediate language.
The back End translates from the intermediate language to the machine language.
Compiler structure
Front
End
Source
Code
Machine
Code
Back
End
Intermediate
Object Code
![Page 8: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/8.jpg)
8
The Front End
Lexical analyzer
Syntactic analyzerSemantic analyzer
Intermediate code
generator
Source
Program
Tokens
Parse tree
Abstract
Syntax tree
Intermediate
code
![Page 9: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/9.jpg)
9
The Back End
Machine Independent optimizer
Machine dependent optimizerObject code Generator
Intermediate
code
Optimized
Intermediate
code
Object code
Optimized
Object code
![Page 10: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/10.jpg)
10
Lexical analysis
The lexical analyzer reads the input program as a character stream and produces a stream of lexemes (or token strings) as output.
The lexical analyzer reads the input program character by character until it reads a word (symbol).
The lexical analyzer searches the current work in a table (called symbol table) and adds it if not found. The lexical analyzer produces an output for each symbol called token. Tokens are generally integer numbers.
![Page 11: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/11.jpg)
11
Syntactic Analysis
Syntactic Analyzer (or parser): takes as input the Token stream produced by the lexical analyzer.
The parser produces a Parse Tree
![Page 12: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/12.jpg)
12
Semantic AnalysisThe semantic analyzer determines if the
meaning is respected in the user program.The syntax rules may be respected without
respecting the meaning.Semantic analysis is mainly to be sure that
data types are used properly. Semantic analysis is part of syntactic
analysis.
![Page 13: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/13.jpg)
13
Detecting errors in the source program
In all phases above the main goal is to determine if the program source respects the source language rules.
Example:for (int i $ 1; i<n; i++) x++;
if x > N Y -= 3 else Y += 3;
This error may be detected at lexical analysis
This is a syntax error
![Page 14: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/14.jpg)
14
Intermediate code generation
The intermediate code generator produces a code that is not related to the target machine. The intermediate code has to be very close to the and very easy to translate to machine language.
![Page 15: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/15.jpg)
15
Object Code
The target code is the machine code.
The machine code generation is not the same as the intermediate code generation.
A assembly language code generation may be done in the same time
![Page 16: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/16.jpg)
16
Lexical analysis example: (Pascal program)PROGRAM AverageNumbers(Input, Output);CONST Amount = 3;VAR Average : Real;
x : ARRAY[1..Amount] OF Integer;i, Sum : Integer;
BEGINx[1]:=3;x[2]:=6;x[3]:=10;Sum := 0;FOR i := 1 TO Amount DO Sum := Sum + x[i];Average := Sum/Amount
END. { AverageNumbers }
![Page 17: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/17.jpg)
17
Lexical analysis result
PROGRAM ID (ID , ID );CONST ID = NUMLITERAL;VAR ID : ID ;
ID : ARRAY[1 .. ID ] OF ID ; ID, ID : ID ;
BEGINand so on
![Page 18: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/18.jpg)
18
Miscellaneous
Symbol Table : contains all keywords and symbols.
Symbol Table Handler: manages the symbol table.Error Handling : gives a clear description of
errors.
![Page 19: 1 CSC 338: Compiler design and implementation Dr. Mohamed Ben Othman](https://reader036.vdocument.in/reader036/viewer/2022082820/56649e9c5503460f94b9d33c/html5/thumbnails/19.jpg)
19
Resume:
Lexical analysis
Source Program
Syntax analysis
Inter. code generation
code optimization
code generation
Error handlingSymbol table
Target program