Compiler Design Introduction Prepared By Prof G Greeshma

Compiler Design - Introduction Prepared By Prof. G. Greeshma AP / CSE JCT College of Engineering and Technology 1

Outlines 1. 1 1. 2 1. 3 1. 4 1. 5 1. 7 1. 8 2 Overview and History What Do Compilers Do? The Structure of a Compiler The Syntax and Semantics of Programming Languages Compiler Design and Programming Language Design Computer Architecture and Compiler Design Considerations

Overview and History (1) 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 3 FORTRAN compilers of the late 1950 s 18 person-years to build

Overview and History (2) Compiler technology is more broadly applicable and has been employed in rather unexpected areas. 4 Text-formatting languages, like nroff and troff; preprocessor packages like eqn, tbl, pic Silicon compiler for the creation of VLSI circuits Command languages of OS Query languages of Database systems

What Do Compilers Do (1) 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) 5 Compiler Machine Language (Target)

What Do Compilers Do (2) Compilers may generate three types of code: Pure Machine Code Augmented Machine Code with OS routines and runtime support routines. More often Virtual Machine Code 6 Machine instruction set without assuming the existence of any operating system or library. Mostly being OS or embedded applications. Virtual instructions, can be run on any architecture with a virtual machine interpreter or a just-in-time compiler Ex. Java

What Do Compilers Do (3) 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 Absolute binary 7 Relative address A linkage step is required Absolute address Can be executed directly

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

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

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

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

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

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

The Structure of a Compiler (7) Source Program (Character Stream) Tokens Scanner Code Generator p Interpretive Code Generation p Generating Code from Tree/Dag p Grammar-Based Code Generator Syntactic Parser Semantic Structure Routines Intermediate Representation Optimizer Code Generator 14 Target machine code
![The Structure of a Compiler (8) Code Generator [Intermediate Code Generator] Scanner [Lexical Analyzer] The Structure of a Compiler (8) Code Generator [Intermediate Code Generator] Scanner [Lexical Analyzer]](http://slidetodoc.com/presentation_image_h2/116fd154be784ee9ec12722b9b6dd518/image-15.jpg)
The Structure of a Compiler (8) Code Generator [Intermediate Code Generator] Scanner [Lexical Analyzer] Non-optimized Intermediate Code Tokens Code Optimizer Parser [Syntax Analyzer] Optimized Intermediate Code Parse tree Code Optimizer Semantic Process [Semantic analyzer] Abstract Syntax Tree w/ Attributes 15 Target machine code
- Slides: 15