II. Language Processing ystem skeletal source program preprocessor source program compiler target object assembly program assembler relocatable machin

Similar documents

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

Compiler Design (40-414)

Crafting a Compiler with C (II) Compiler V. S. Interpreter

CS131: Programming Languages and Compilers. Spring 2017

COMPILER DESIGN. For COMPUTER SCIENCE

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Intermediate Code Generation

CST-402(T): Language Processors

Introduction to Compiler Design

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

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Parsing Techniques. AST Review. AST Data Structures. Implicit AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

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

Introduction to Compiler Construction

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

Compiler Design Overview. Compiler Design 1

COMPILER DESIGN LECTURE NOTES

A Simple Syntax-Directed Translator

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Yacc: A Syntactic Analysers Generator

Compiler, Assembler, and Linker

Compiling Regular Expressions COMP360

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.


Using an LALR(1) Parser Generator

QUESTION BANK CHAPTER 1 : OVERVIEW OF SYSTEM SOFTWARE. CHAPTER 2: Overview of Language Processors. CHAPTER 3: Assemblers

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

UNIT III. The following section deals with the compilation procedure of any program.

Formal Languages and Compilers Lecture I: Introduction to Compilers

tokens parser 1. postfix notation, 2. graphical representation (such as syntax tree or dag), 3. three address code

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CSE302: Compiler Design

CMPT 379 Compilers. Parse trees

Compiler Design. Subject Code: 6CS63/06IS662. Part A UNIT 1. Chapter Introduction. 1.1 Language Processors

2068 (I) Attempt all questions.

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Introduction to Compiler Construction

Introduction. Compilers and Interpreters

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

LECTURE NOTES ON COMPILER DESIGN P a g e 2

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

Compiler Code Generation COMP360

Principles of Compiler Design

CPS 506 Comparative Programming Languages. Syntax Specification

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

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

Syntax-Directed Translation

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

Programming Language Syntax and Analysis

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Syntax Analysis. Chapter 4

CS 4201 Compilers 2014/2015 Handout: Lab 1

Syntax. In Text: Chapter 3

Time : 1 Hour Max Marks : 30

Introduction. Interpreters High-level language intermediate code which is interpreted. Translators. e.g.

CS 6353 Compiler Construction Project Assignments

Writing Evaluators MIF08. Laure Gonnord

CS 6353 Compiler Construction Project Assignments

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Alternatives for semantic processing

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

SOFTWARE ARCHITECTURE 5. COMPILER

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

1. INTRODUCTION TO LANGUAGE PROCESSING The Language Processing System can be represented as shown figure below.

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Context-free grammars

VETRI VINAYAHA COLLEGE OF ENGINEERING AND TECHNOLOGY

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

UNIT III & IV. Bottom up parsing

Syntax and Grammars 1 / 21

SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT I - LEXICAL ANALYSIS PART - A

Syntax Errors; Static Semantics

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

Compiler Construction: Parsing

Syntax. A. Bellaachia Page: 1

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

Parsing Techniques. AST Review. AST Data Structures. LL AST Construction. AST Construction CS412/CS413. Introduction to Compilers Tim Teitelbaum

Group B Assignment 9. Code generation using DAG. Title of Assignment: Problem Definition: Code generation using DAG / labeled tree.

Chapter 4. Lexical and Syntax Analysis

COMPILER DESIGN UNIT I LEXICAL ANALYSIS. Translator: It is a program that translates one language to another Language.

Question Bank. 10CS63:Compiler Design

Compilers. Prerequisites

B The SLLGEN Parsing System

ICOM 4036 Spring 2004

Syntax-Directed Translation. Introduction

Compiler Lab. Introduction to tools Lex and Yacc

PRINCIPLES OF COMPILER DESIGN

Dixita Kagathara Page 1

CMSC 330: Organization of Programming Languages

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

4. Lexical and Syntax Analysis

CS426 Compiler Construction Fall 2006

Transcription:

CP 140 - Mathematical Foundations of C Dr.. Rodger ection: The tructure of a Compiler 1.1 What is a Compiler I. Translator Deænition: program in translator program in language! for! language X X Y Examples: ource Object Language Language Name Example High Level High Level preprocessor ratfor! f77 Assembly Machine assembler as High Level Machine compiler f77 cc pc Any executes interpretor BAIC often immediately c shell apl lisp æ Preprocessor For i=1 to n do stmts end for i=1 While ié=n do stmts i=i+1 end while 1

II. Language Processing ystem skeletal source program preprocessor source program compiler target object assembly program assembler relocatable machine code loaderlink-editor absolute machine code III. Compiler program in program in high level! compiler! machine language X for X language Y 2

1.2 TRUCTURE OF A COMPILER General Overview ç ç ç çç ç çç ymbol Table Management l ll l ll l l ource Code Lexical Analysis tokens yntax Analysis parse trees Intermediate Code Generation Code Optimization @ @@ l ll intermediate code @ @@ l ll @ @@ l l intermediate code Code Generation @ @@ Error @ @ Handling Object Program Figure 1: 3

1.3 PHAE OF COMPILATION 1.3.1 Lexical Analysis canner a. Purpose: Read the same program character by character grouping them into atomic units called ëtokens." b. Tokens: æ depend on language and compiler writer æ Examples: reserved words if for operators +; ; é; = constants 0 4.89 punctuation g ë identiæers sb ch æ treated as a pair: token.type and token.value í token type is a mnemonic integer í some tokens have no value c. Example if x é= 0x=y+z when put through lexical analyzer produces: token type value if 25 28 id 23 ëx" é= 27 int constant 22 0 38 id 23 ëx" = assgnment 4 id 23 ëy" + 34 id 23 ëz" 4

d. How does one build a lexical analyzer æ from scratch æ lex e. Preview of Lex æ idea: tokens described by regular expressions æ basic syntax: regular expression action æ basic semantics: if match regular expression then do action. æ Example: ëif" ëë ë0-9ë+ return25; return28; return22; f. Remarks Besides returning token types and values the lexical analyzer might a print error messages b insert identiæers in the symbol table 1.3.2 yntax Analysis Parsing a. Purpose: Accepts the sequence of tokens generated by the lexical analyzer checks whether the program is syntactically correct and generates a parse tree. b. yntax: formally described by a context free grammar. 5

c. Parse Tree if xé=0x=y+z statement if-statement ç P A PPP ç ççç ç A if condition hhhhhhhhhhhhhhhh statement P C PPPPP!!! relation! C expression relop expression L L assg. stmt Z ç ççç ææ Z lhs = rhs id é= constant id expr D H HHH D expr + expr C C id id Figure 2 is the parse tree for this statement. d. How does one build a parser Figure 2: Parse tree æ from scratch æ using a parser generator such as yacc 1.3.3 Intermediate Code Generator a. Purpose: Traverse the parse tree producing simple intermediate code. b. Three-Address Code: Instructions: 1. id := id op id 2. goto label 3. if condition goto label 6

Example: if xé=0 x = x + z if xé=0 goto L1 goto L2 L1: x:=y+z L2: 1.3.4 Intermediate Code Generation a. Purpose: Transform the intermediate code into ëbetter" code. b. Examples 1 Rearrangement ofcode if xé=0 goto L1 if xé0 goto L2 goto L2! x = y + z L1: x=y+z L2: L2: 2 Redundancy Elimination a=w+x+y b=x+y+z T1=x+y! a=w+t1 b=t1+z 3 trength Reduction x 2! x æ x expensive! cheap operator operator 4 Frequency Reduction for i=1; ién; i=i+1 f T1 = sqrt26 x = sqrt26! for i=1; ién; i=i+1 f g x=t1 g 7

c. Remarks: 1 Main criteria for optimization is speed. 1.3.5 Code Generation a. Purpose: Transform intermediate code to machine code assembler b. Example: a=b+c mov add mov b R1 c R1 R1 a c. Remarks 1 completely machine dependent whereas other phases are not 2 ëregister allocation" is the most diæcult task æ idea - use registers fast access to avoid memory use slow access æ problem - only a ænite number of registers during intermediate code phase one assumes an inænite number 1.4 ymbol Table a. Purpose: record information about various objects in the source program b. Examples æ procedure - no. and type of arguments æ simple variable - type æ array - type size c. Use - information is required during æ parsing æ code generation 8

1.5 Error Handler a. Errors - all errors should be æ detected æ detected correctly æ detected as soon as possible æ reported at the appropriate place and in a helpful manner b. Purpose æ report errors æ ëerror recovery" - proceed with processing c. Note: Errors can occur in each phase æ misspelled token æ wrong syntax æ improper procedure call æ statements that cannot be reached 9