Compiler Design Overview. Compiler Design 1

Similar documents
CS131: Programming Languages and Compilers. Spring 2017

Principles of Compiler Design Presented by, R.Venkadeshan,M.Tech-IT, Lecturer /CSE Dept, Chettinad College of Engineering &Technology

Introduction to Syntax Analysis. Compiler Design Syntax Analysis s.l. dr. ing. Ciprian-Bogdan Chirila

CST-402(T): Language Processors

Compiler Design (40-414)

LECTURE NOTES ON COMPILER DESIGN P a g e 2

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Formal Languages and Compilers Lecture I: Introduction to Compilers

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY SCHOOL OF COMPUTING DEPARTMENT OF CSE COURSE PLAN

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Introduction to Compiler Construction

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY

Introduction to Compiler Construction

Pioneering Compiler Design

CS 314 Principles of Programming Languages. Lecture 3

CA Compiler Construction

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

COP 3402 Systems Software Syntax Analysis (Parser)

1. The output of lexical analyser is a) A set of RE b) Syntax Tree c) Set of Tokens d) String Character

Working of the Compilers

KALASALINGAM UNIVERSITY ANAND NAGAR, KRISHNAN KOIL DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING ODD SEMESTER COURSE PLAN

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Building Compilers with Phoenix

SRM UNIVERSITY FACULTY OF ENGINEERING AND TECHNOLOGY SCHOOL OF COMPUTER SCIENCE AND ENGINEERING COURSE PLAN

Introduction to Compiler Design

Lexical Scanning COMP360

A simple syntax-directed

A programming language requires two major definitions A simple one pass compiler

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Chapter 2 :: Programming Language Syntax

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

CS Compiler Construction West Virginia fall semester 2014 August 18, 2014 syllabus 1.0

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

CS 314 Principles of Programming Languages

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING ACADEMIC YEAR / EVEN SEMESTER

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

COMPILER DESIGN. For COMPUTER SCIENCE

Downloaded from Page 1. LR Parsing

G.PULLAIH COLLEGE OF ENGINEERING & TECHNOLOGY

List of Figures. About the Authors. Acknowledgments

Theory and Compiling COMP360

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, Pilani Pilani Campus Instruction Division

CS 4201 Compilers 2014/2015 Handout: Lab 1

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

Syntax Analysis. Chapter 4

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Name of chapter & details

3. Parsing. Oscar Nierstrasz

HOLY ANGEL UNIVERSITY COLLEGE OF INFORMATION AND COMMUNICATIONS TECHNOLOGY COMPILER THEORY COURSE SYLLABUS

Syntax. In Text: Chapter 3

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

Compiling Regular Expressions COMP360

CS426 Compiler Construction Fall 2006

ECE251 Midterm practice questions, Fall 2010

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

4. An interpreter is a program that

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

Principles of Programming Languages [PLP-2015] Detailed Syllabus

Syntax Analysis Part I

CS606- compiler instruction Solved MCQS From Midterm Papers

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

PRINCIPLES OF COMPILER DESIGN

COMP 3002: Compiler Construction. Pat Morin School of Computer Science

Homework & Announcements

CS 314 Principles of Programming Languages

LECTURE 3. Compiler Phases

Error Recovery during Top-Down Parsing: Acceptable-sets derived from continuation

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CPS 506 Comparative Programming Languages. Syntax Specification

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

GUJARAT TECHNOLOGICAL UNIVERSITY

Compiler Design Aug 1996

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

Question Bank. 10CS63:Compiler Design

2068 (I) Attempt all questions.

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Syntax-Directed Translation. Lecture 14

Last time. What are compilers? Phases of a compiler. Scanner. Parser. Semantic Routines. Optimizer. Code Generation. Sunday, August 29, 2010

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Context free grammars and predictive parsing

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

Lecture 4: Syntax Specification


LANGUAGE TRANSLATORS

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}


INSTITUTE OF AERONAUTICAL ENGINEERING (AUTONOMOUS)

Introduction to Lexing and Parsing

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Compiler Design Concepts. Syntax Analysis

UNIVERSITY OF CALIFORNIA

Principles of Programming Languages COMP251: Syntax and Grammars

CS5363 Final Review. cs5363 1

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013

Compilers. Computer Science 431

Transcription:

Compiler Design Overview Compiler Design 1

Preliminaries Required Basic knowledge of programming languages. Basic knowledge of FSA and CFG. Knowledge of a high programming language for the programming assignments. Textbook: Alfred V. Aho, Ravi Sethi, and Jeffrey D. Ullman, Compilers: Principles, Techniques, and Tools Addison-Wesley, 1986. 2

Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis Context Free Grammars Top-Down Parsing, LL Parsing Bottom-Up Parsing, LR Parsing Syntax-Directed Translation Attribute Definitions Evaluation of Attribute Definitions Semantic Analysis, Type Checking Run-Time Organization Intermediate Code Generation 3

COMPILERS A compiler is a program takes a program written in a source language and translates it into an equivalent program in a target language. source program COMPILER target program ( Normally a program written in a high-level programming language) error messages ( Normally the equivalent program in machine code relocatable object file) CS416 Compiler Design 4

Other Applications In addition to the development of a compiler, the techniques used in compiler design can be applicable to many problems in computer science. Techniques used in a lexical analyzer can be used in text editors, information retrieval system, and pattern recognition programs. Techniques used in a parser can be used in a query processing system such as SQL. Many software having a complex front-end may need techniques used in compiler design. A symbolic equation solver which takes an equation as input. That program should parse the given input equation. Most of the techniques used in compiler design can be used in Natural Language Processing (NLP) systems. 5

Major Parts of Compilers There are two major parts of a compiler: Analysis and Synthesis In analysis phase, an intermediate representation is created from the given source program. Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts of this phase. In synthesis phase, the equivalent target program is created from this intermediate representation. Intermediate Code Generator, Code Generator, and Code Optimizer are the parts of this phase. 6

Major Parts of Compilers 7

Phases of A Compiler Source Program Lexical Analyzer Syntax Analyzer Semantic Analyzer Intermediate Code Generator Code Optimizer Code Generator Target Program Each phase transforms the source program from one representation into another representation. They communicate with error handlers. They communicate with the symbol table. 8

Introduction to all phases CS416 Compiler Design 9

Lexical Analyzer Lexical Analyzer reads the source program character by character and returns the tokens of the source program. A token describes a pattern of characters having same meaning in the source program. (such as identifiers, operators, keywords, numbers, delimeters and so on). Smallest meaningful unit in a source program. Ex: newval := oldval + 12 => tokens: newval identifier := assignment operator oldval identifier + add operator 12 a number Puts information about identifiers into the symbol table. Regular expressions are used to describe tokens (lexical constructs). A Finite State Automaton can be used in the implementation of a lexical analyzer. 10

Syntax Analyzer A Syntax Analyzer creates the syntactic structure (generally a parse tree) of the given program. A syntax analyzer is also called as a parser. A parse tree describes a syntactic structure. In a parse tree, all terminals are at leaves. All inner nodes are non-terminals in a context free grammar. Symbol Table 11

Syntax Analyzer (CFG) The syntax of a language is specified by a context free grammar (CFG). The rules in a CFG are mostly recursive. A syntax analyzer checks whether a given program satisfies the rules implied by a CFG or not. If it satisfies, the syntax analyzer creates a parse tree for the given program. Ex: We use BNF (Backus Naur Form) to specify a CFG assgstmt -> identifier := expression expression -> identifier expression -> number expression -> expression + expression 12

Syntax Analyzer versus Lexical Analyzer Which constructs of a program should be recognized by the lexical analyzer, and which ones by the syntax analyzer? Both of them do similar things; But the lexical analyzer deals with simple non-recursive constructs of the language. The syntax analyzer deals with recursive constructs of the language. The lexical analyzer simplifies the job of the syntax analyzer. The lexical analyzer recognizes the smallest meaningful units (tokens) in a source program. The syntax analyzer works on the smallest meaningful units (tokens) in a source program to recognize meaningful structures in our programming language. 13

Parsing Techniques Depending on how the parse tree is created, there are different parsing techniques. These parsing techniques are categorized into two groups: Top-Down Parsing, Bottom-Up Parsing Top-Down Parsing: Construction of the parse tree starts at the root, and proceeds towards the leaves. Efficient top-down parsers can be easily constructed by hand. Recursive Predictive Parsing, Non-Recursive Predictive Parsing (LL Parsing). Bottom-Up Parsing: Construction of the parse tree starts at the leaves, and proceeds towards the root. Normally efficient bottom-up parsers are created with the help of some software tools. Bottom-up parsing is also known as shift-reduce parsing. Operator-Precedence Parsing simple, restrictive, easy to implement LR Parsing much general form of shift-reduce parsing, LR, SLR, LALR 14

Semantic Analyzer A semantic analyzer checks the source program for semantic errors and collects the type information for the code generation. Type-checking is an important part of semantic analyzer. Normally semantic information cannot be represented by a context-free language used in syntax analyzers. Context-free grammars used in the syntax analysis are integrated with attributes (semantic rules) Ex: the result is a syntax-directed translation, Attribute grammars newval := oldval + 12 The type of the identifier newval must match with type of the expression (oldval+12) 15

Semantic Analyzer Attribute fields are added to the nodes. One of the attributes in Type. Type of identifiers will be captured from declaration statements assignment real identifier := expression real expression + identifier expression inttoreal newval oldval 12 number int CS416 Compiler Design 16

Intermediate Code Generation A compiler may produce an explicit intermediate codes representing the source program. These intermediate codes are generally machine (architecture independent). But the level of intermediate codes is close to the level of machine codes. Ex: newval := oldval * fact + 1 id1 := id2 * id3 + 1 MULT id2,id3,temp1 Intermediates Codes (Quadruples) ADD temp1,#1,temp2 MOV temp2,,id1 17

Code Optimizer (for Intermediate Code Generator) The code optimizer optimizes the code produced by the intermediate code generator in the terms of time and space. Ex: MULT ADD id2,id3,temp1 temp1,#1,id1 18

Code Generator Produces the target language in a specific architecture. The target program is normally is a relocatable object file containing the machine codes. Ex: ( assume that we have an architecture with instructions whose at least one of its operands is a machine register) MOVE MULT ADD MOVE id2,r1 id3,r1 #1,R1 R1,id1 19