Post on 27-May-2015
description
Compilers
WELCOME TO A JOURNEY TO
CS419Spring 2014
Lecture1: Introduction
Cairo UniversityFCI
Dr. Hussien SharafComputer Science Departmentdr.sharaf@from-masr.com
LECTURE OUTLINE
Grading and policy Course outline and
references Chapter one
2Dr. Hussien M. Sharaf
GRADING AND POLICY
3Dr. Hussien M. Sharaf
Grades will be based on 100 possible points, using the following distribution schedule:Assignments: 5% Mid-term Exam : 10% Group Project: 10%Practical: 10%Attendance: 5%Final Exam: 60%
COURSE OUTLINE AND REFERENCES
Chapter one – Introduction Chapter three- Lexical analysis
definition - using Regular Expression Chapter three- Lexical analysis using
DFA Chapter three- Lexical analysis using
NFA and Transfer NFA to DFA.
4Dr. Hussien M. Sharaf
COURSE OUTLINE AND REFERENCES Chapter four- Syntax analysis using
CFG. Chapter four- Syntax analysis, Parsing
trees and Ambiguity. Chapter four - Removing Left
Recursion and Left Factoring. Chapter four - Syntax analysis (CFG)
using Top-down parsing.
5Dr. Hussien M. Sharaf
COURSE OUTLINE AND REFERENCES Chapter four - First and Follow
operators. Chapter four - Syntax analysis (CFG)
using Bottom-Up (predictive/LR) parsing.
Chapter four - Construction of “LR Parsing Tables” / “parsing Table LL1”
Semantic analysis Intermediate code and code generation.
6Dr. Hussien M. Sharaf
LECTURE1 OUTLINE Introduction is split into two lectures: Lec1: Overview
What are compilers Phases ( architecture ) of a Compiler. Some Data structures that are required for
compiler’s work: Token Symbol table. Literal table Parse tree
7Dr. Hussien M. Sharaf
LECTURE2 OUTLINE Lec2: Overview
Phases of a Compiler: Scanning. Parsing. Semantic analysis. Intermediate code generation. Code generation.
8Dr. Hussien M. Sharaf
WHAT ARE COMPILERS?A program that translates one language to another.
Responsibility: 1. Accepts a source program
typically written in a high- level language.
2. Produces an equivalent target program typically in assembly or machine language.
3. Reports error messages as part of the translation process.
compilerSource programUsually High level language
Target programUsually machine language
Error message
9Dr. Hussien M. Sharaf
COUSINS OF COMPILERS
1. Interpreter: is a program that ultimately performs the same function as a compiler, but in a different manner. It works by scanning through the source program instruction by instruction. As each instruction is encountered, the interpreter translates it into machine code and executes it directly.
2. Assembler: is a program that automatically translates the source program written in assembly language and to produce as output an object code written in binary machine code.
3. Linker: is a program that takes one or more objects generated by compilers and assembles them into a single executable program.
4. Loader: (is a routine that) loads an object program into memory and prepares it for execution
10Dr. Hussien M. Sharaf
DIFFERENT ARCHITECTURAL VIEWS
1. Functional view : 6 phases.2. Logical view: the 6 phases are grouped into two
main categories A. Analysis VS synthesis.B. Front end VS back end.
3. Operations view: execute one or more phase into one pass. Each pass builds or updates the output of the previous pass.
A. Scanning & parsing.B. Sematic analysis.C. Code generation & optimization.
11Dr. Hussien M. Sharaf
ARCHITECTURE/PHASES OF A COMPILER
Scanner/lexical analyzer
Parser/ syntax analyzer
Semantic analyzer
Source Code optimization
Code generator
Target Code optimization
Stream of characters
Stream of tokens
Parse/syntax tree
Annotated tree
Intermediate code
Target code
Target code
Literal Table
Symbol Table
Dr. Hussien M. Sharaf
12
SOME DATA STRUCTURES
1. Token
2. Symbol table
3. Literal table
4. Parse tree
5. Semantic parse tree
6. Intermediate code
13Dr. Hussien M. Sharaf
1. TOKEN
Single Symbol ahead: In most languages the scanner needs to generate only one token ahead at a time.
In this case you don’t need a collection/array of tokens, only one global variable can be used.
Dr. Hussien M. Sharaf
2. SYMBOL TABLE1. Stores information associated with
identifiers. Information associated with variables like [name, type, address, size (for array), etc.]
2. Stores Information associated with functions like [name, type of return value, parameters, address, etc.]
name Type address size (for array)
x int OxA300 n/a
y int OxA304 n/a
c char OxA308 10
Sample code:
int x, y;char c[10];x = 5;
15Dr. Hussien M. Sharaf
2. SYMBOL TABLE (CONT’D)
3. Use defined data types like structs, enums and classes.
The symbol table is modified by the scanner, parser, and semantic analyzer.
The information at the symbol table is used by intermediate code generator phase and machine code generator phase.
Mostly use hash table for efficiency. Because access time is O(k) and space consumption is not a concern.
16Dr. Hussien M. Sharaf
3. LITERAL TABLE
Store constants and strings used in program reduce the memory size by reusing
constants and strings Can be combined with symbol table in
some implementations.
17Dr. Hussien M. Sharaf
4. PARSE TREE Dynamically-allocated, pointer-based
TREE structure Sample code
18Dr. Hussien M. Sharaf
5. SEMANTIC PARSE TREE Usually the same parse tree is used
and annotations are added for each node.
19Dr. Hussien M. Sharaf
6. INTERMEDIATE CODE The structure of the
code is kept as simple as possible usually three-address code.
Each instruction is allows only three addresses (variables).
Each instruction is added as an entry into a linked list that allows dynamic growth.
20Dr. Hussien M. Sharaf
Var2 Var3opVar1
Var2 Var3opVar1
…. …op….
NULL
End of chapter 1
Please write your ID, Name and email address on the link given below:
http://tinyurl.com/mkwg48a
21Dr. Hussien M. Sharaf