Introduction To Compilers

Views:
 
Category: Education
     
 

Presentation Description

Compilers

Comments

Presentation Transcript

Chapter 1 Introduction:

Chapter 1 Introduction Eng Amr Zamel . 1

Outlines:

Outlines 2 1.1 Overview and History 1.2 What Do Compilers Do? 1.3 The Structure of a Compiler 1.4 The Syntax and Semantics of Programming Languages 1.5 Compiler Design and Programming Language Design 1.7 Computer Architecture and Compiler Design 1.8 Compiler Design Considerations

computer:

computer 3 Computer Software Hardware Ex: Program that run in your computer As word program Ex: Screen ,mouse ,keyboard Motherboard Processor Hard disk

Into. Programming language :

Into. Programming language 4 لغة التجميع Assembly lang. لغة المستوى الرفيع High level Lang. 011 0011 0001 Add R0 , R1 Z = x + y ; Ex: C ,FORTRAN ,C++,JAVA Compiler مترجم Assembler مترجم لغة الآلة Machine Lang.

What Do Compilers Do (1):

What Do Compilers Do (1) 5 A compiler acts as a translator, transforming human-oriented programming languages into computer-oriented machine languages . Ignore machine-dependen t details for programmer Programming Language ( Source ) Compiler Machine Language ( Target )

Overview and History:

Overview and History Cause Software for early computers was written in assembly language The benefits of reusing software on different CPUs started to become significantly greater than the cost of writing a compiler The first real compiler FORTRAN compilers of the late 1950s 18 person-years to build 6

The Structure of a Compiler (1):

7 Any compiler must perform two major tasks Analysis of the source program Synthesis of a machine-language program The Structure of a Compiler (1) Compiler Analysis Synthesis

The Structure of a Compiler (2):

The Structure of a Compiler (2) 8

Slide 9:

9

Slide 10:

10

Slide 11:

11

Slide 12:

12

The Structure of a Compiler (2):

The Structure of a Compiler (2) 13 Scanner Parser Semantic Routines Code Generator Optimizer Source Program Tokens Syntactic Structure Symbol and Attribute Tables (Used by all Phases of The Compiler) (Character Stream) Intermediate Representation Target machine code

The Structure of a Compiler (3):

The Structure of a Compiler (3) 14 Scanner Parser Semantic Routines Code Generator Optimizer Source Program Tokens Syntactic Structure Symbol and Attribute Tables (Used by all Phases of The Compiler) Scanner The scanner begins the analysis of the source program by reading the input, character by character, and grouping characters into individual words and symbols ( tokens ) RE ( Regular expression ) NFA ( Non-deterministic Finite Automata ) DFA ( Deterministic Finite Automata ) LEX (Character Stream) Intermediate Representation Target machine code

The Structure of a Compiler (4):

The Structure of a Compiler (4) 15 Scanner Parser Semantic Routines Code Generator Optimizer Source Program Tokens Syntactic Structure Symbol and Attribute Tables (Used by all Phases of The Compiler) Parser Given a formal syntax specification (typically as a context-free grammar [CFG] ), the parse reads tokens and groups them into units as specified by the productions of the CFG being used. As syntactic structure is recognized, the parser either calls corresponding semantic routines directly or builds a syntax tree . CFG ( Context-Free Grammar ) BNF ( Backus-Naur Form ) GAA ( Grammar Analysis Algorithms ) LL, LR, SLR, LALR Parsers YACC (Character Stream) Intermediate Representation Target machine code

The Structure of a Compiler (5):

The Structure of a Compiler (5) 16 Scanner Parser Semantic Routines Code Generator Optimizer Source Program (Character Stream) Tokens Syntactic Structure Intermediate Representation Symbol and Attribute Tables (Used by all Phases of The Compiler) Semantic Routines Perform two functions Check the static semantics of each construct Do the actual translation The heart of a compiler Syntax Directed Translation Semantic Processing Techniques IR (Intermediate Representation) Target machine code

The Structure of a Compiler (6):

The Structure of a Compiler (6) 17 Scanner Parser Semantic Routines Code Generator Optimizer Source Program Tokens Syntactic Structure Symbol and Attribute Tables (Used by all Phases of The Compiler) Optimizer The IR code generated by the semantic routines is analyzed and transformed into functionally equivalent but improved IR code This phase can be very complex and slow Peephole optimization loop optimization, register allocation, code scheduling Register and Temporary Management Peephole Optimization (Character Stream) Intermediate Representation Target machine code

The Structure of a Compiler (7):

The Structure of a Compiler (7) 18 Source Program (Character Stream) Scanner Tokens Parser Syntactic Structure Semantic Routines Intermediate Representation Optimizer Code Generator Code Generator Interpretive Code Generation Generating Code from Tree/Dag Grammar-Based Code Generator Target machine code

The Structure of a Compiler (8):

The Structure of a Compiler (8) 19 Scanner [Lexical Analyzer] Parser [Syntax Analyzer] Semantic Process [Semantic analyzer] Code Generator [Intermediate Code Generator] Code Optimizer Tokens Parse tree Abstract Syntax Tree w/ Attributes Non-optimized Intermediate Code Optimized Intermediate Code Code Optimizer Target machine code

Inside C Compiler:

Inside C Compiler symbol table abstract syntax tree Syntax analyser Lexical analyser Code generator p.c tokens a.s C Compiler

Slide 21:

21

Slide 22:

22

Slide 23:

23

Lexical Analysis:

Lexical Analysis 24

Lexical Analysis:

Lexical Analysis 25

Lexical Analysis:

Lexical Analysis 26

Lexical Analysis:

Lexical Analysis 27

authorStream Live Help