Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.

Similar presentations


Presentation on theme: "Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases."— Presentation transcript:

1 Compiler Introduction 1 Kavita Patel

2 Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases of Compiler  1.5 Process of Compiling  1.6 The Grouping of Phases  1.7 Compiler-Construction Tools  1.8 Compiler vs. Interpreter

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

4 What Do Compilers Do (2)  Another way that compilers differ from one another is in the format of the target machine code they generate :  Assembly or other source format  Relocatable binary  Relative address  A linkage step is required  Absolute binary  Absolute address  Can be executed directly 4

5 5  Any compiler must perform two major tasks  Analysis of the source program  Synthesis of a machine-language program The Structure of a Compiler Compiler AnalysisSynthesis

6 Compilation Process: Phases of Compiler 6 Lexical Analysis Syntax Analysis Semantic Analysis Semantic Analysis Code Generator Code Generator Code Optimizer Source Program TokensSyntactic Structure Symbol and Attribute Tables Symbol and Attribute Tables (Used by all Phases of The Compiler) (Character Stream) Intermediate Representation Target machine code

7 Phases of Compiler(2) 7 Scanner Parser Semantic Routines Semantic Routines Code Generator Code Generator Optimizer Source Program TokensSyntactic Structure Symbol and Attribute Tables Symbol and Attribute Tables (Used by all Phases of The Compiler) Lexical Analyzer(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 Lexical Analyzer(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

8 Phases of Compiler(3) 8 Scanner Parser Semantic Routines Semantic Routines Code Generator Code Generator Optimizer Source Program TokensSyntactic Structure Symbol and Attribute Tables Symbol and Attribute Tables (Used by all Phases of The Compiler) Syntax Analyzer(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 )  GAA ( Grammar Analysis Algorithms )  LL, LR, SLR, LALR Parsers  YACC Syntax Analyzer(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 )  GAA ( Grammar Analysis Algorithms )  LL, LR, SLR, LALR Parsers  YACC (Character Stream) Intermediate Representation Target machine code

9 Phases of Compiler(4) 9 Scanner Parser Semantic Routines Semantic Routines Code Generator Code Generator Optimizer Source Program (Character Stream) TokensSyntactic Structure Intermediate Representation Symbol and Attribute Tables 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) 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

10 Phases of Compiler(5) 10 Scanner Parser Semantic Routines Semantic Routines Code Generator Code Generator Optimizer Source Program TokensSyntactic Structure Symbol and Attribute Tables 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  loop optimization, register allocation, code scheduling  Register and Temporary Management 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  loop optimization, register allocation, code scheduling  Register and Temporary Management (Character Stream) Intermediate Representation Target machine code

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

12 Phases of Compiler(7) 12 Scanner [Lexical Analyzer] Parser [Syntax Analyzer] Semantic Process [Semantic analyzer] Code Generator [Intermediate Code Generator] 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

13 13 Process of Compiling scanner parser Semantic analyzer Intermediate code generator Code optimization Code generator Stream of characters Stream of tokens Parse/syntax tree Annotated tree Intermediate code Target code Error Handler Symbol Table Manager

14 14 The Grouping of Phases  Compiler front and back ends:  Front end:  Analysis steps + Intermediate code generation  Depends primarily on the source language  Machine independent  Back end:  Code optimization and generation  Independent of source language  Machine dependent

15 15 Compiler-Construction Tools  Software development tools are available to implement one or more compiler phases  Scanner generators (LEX)  Parser generators (YACC)  Syntax-directed translation engines  Automatic code generators  Data-flow engines

16 16

17 Thank You 17


Download ppt "Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases."

Similar presentations


Ads by Google