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

Similar documents
CST-402(T): Language Processors

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

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

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



COMPILER DESIGN LECTURE NOTES

Introduction to Compiler Construction

Compilers. Prerequisites

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Introduction to Compiler Construction

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

Compiler, Assembler, and Linker

LECTURE 3. Compiler Phases

Introduction to Compiler Construction

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Compiler Design (40-414)

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Programming Languages Third Edition. Chapter 7 Basic Semantics

CS 4201 Compilers 2014/2015 Handout: Lab 1

The role of semantic analysis in a compiler

Informatica 3 Syntax and Semantics

Compilers and Interpreters

Chapter 10 Language Translation

This book is licensed under a Creative Commons Attribution 3.0 License

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

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

Why are there so many programming languages? Why do we have programming languages? What is a language for? What makes a language successful?

Concepts of Programming Languages

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

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Working of the Compilers

Syntax. A. Bellaachia Page: 1

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

Syntactic Analysis. The Big Picture Again. Grammar. ICS312 Machine-Level and Systems Programming

SOFTWARE ARCHITECTURE 5. COMPILER

COSC121: Computer Systems: Runtime Stack

A Simple Syntax-Directed Translator

A simple syntax-directed

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

COP 3402 Systems Software. Lecture 4: Compilers. Interpreters

Compiling Techniques

SLIDE 2. At the beginning of the lecture, we answer question: On what platform the system will work when discussing this subject?

CS 314 Principles of Programming Languages. Lecture 3

Dixita Kagathara Page 1

MIDTERM EXAM (Solutions)

CS 321 IV. Overview of Compilation

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Stating the obvious, people and computers do not speak the same language.

Introduction to Compiler Construction

Life Cycle of Source Program - Compiler Design

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

Early computers (1940s) cost millions of dollars and were programmed in machine language. less error-prone method needed

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

COMPILER DESIGN. For COMPUTER SCIENCE

CS Exam #1-100 points Spring 2011

Programmiersprachen (Programming Languages)

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

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

A Pascal program. Input from the file is read to a buffer program buffer. program xyz(input, output) --- begin A := B + C * 2 end.

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

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

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

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

Software II: Principles of Programming Languages

Introduction to Compiler Construction

Compilers and Code Optimization EDOARDO FUSELLA

Compiler Structure. Lexical. Scanning/ Screening. Analysis. Syntax. Parsing. Analysis. Semantic. Context Analysis. Analysis.

CS426 Compiler Construction Fall 2006

VIVA QUESTIONS WITH ANSWERS

What do Compilers Produce?

CSC488S/CSC2107S - Compilers and Interpreters. CSC 488S/CSC 2107S Lecture Notes

Compilers and interpreters

22c:111 Programming Language Concepts. Fall Syntax III

Part 5 Program Analysis Principles and Techniques

What is a compiler? Xiaokang Qiu Purdue University. August 21, 2017 ECE 573

Alternatives for semantic processing

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

Chapter 1. Preliminaries

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

G Programming Languages - Fall 2012

Chapter 1 Preliminaries

When do We Run a Compiler?

Compilers. Lecture 2 Overview. (original slides by Sam

4. An interpreter is a program that

Principles of Programming Languages. Lecture Outline

LANGUAGE PROCESSORS. Presented By: Prof. S.J. Soni, SPCE Visnagar.

Introduction to Scientific Computing Languages

Why are there so many programming languages?

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

names names identifiers variables subroutines constants

The Structure of a Syntax-Directed Compiler

Organization of Programming Languages (CSE452) Why are there so many programming languages? What makes a language successful?

Transcription:

Introduction Translators Interpreters High-level intermediate code which is interpreted e.g. Program in a representation translator Program in another representation BASIC, LISP, APL command s, e.g. UNIX-shell query s for databases error messages Input Compiler High-level machine or assembly Source Interpreter error messages e.g. Pascal, Ada, Fortran Three phases of execution: Result does not translate, interprets directly "Compile time" 1. Source object (compiling) 2. Linking, loading absolute "Run-time" 3. Input output Assembler Symbolic machine code machine code e.g. MOVE R1,SUM 01..101 Lecture 1 Compilers, Introduction Page 3 Lecture 1 Compilers, Introduction Page 4 Simulator, Emulator Machine code is interpreted machine code e.g. Simulate a processor on an existing processor. Preprocessor Extended ("sugared") high-level high-level Natural translators e.g. Chinese English Very difficult problem, especially to include context. Visiting relatives can be hard work. - To go and visit relatives... - Relatives who are visiting... Example1: IF THEN ELSE in FORTRAN: Before preprocessing: IF A < B THEN Z=A ELSE Z=B After preprocessing: IF (A.LT.B) THEN GOTO 99 Z=B GOTO 100 99 Z=A 100 CONTINUE I saw a man with a telescope Example 2: "File inclusion" #include "fil1.h" Lecture 1 Compilers, Introduction Page 5 Lecture 1 Compilers, Introduction Page 6

Why high-level s? The structure of the compiler Understandability (readability) Naturalness (s for different applications) Portability (machine-independent) Efficient to use (development time) due to separation of data and instructions typing data-structures blocks -flow primitives subroutines Source Analysis intermediate Synthesis Object Logical organisation Analysis ("front-end"): Pull apart the text string (the ) to internal structures, reveal the structure and meaning of the source. Synthesis ("back-end"): Construct an object using information from the analysis. Lecture 1 Compilers, Introduction Page 7 Lecture 1 Compilers, Introduction Page 8 The phases of the compiler Source sequence of chars: IF sum=5 THEN.. Pass: Physical organisation (phase to phase) dependent on and compromises. Lexical analysis sequence of tokens : IF sum = 5... Available memory space, efficiency (time taken), forward references, portability- and modularityrequirements determine the number of passes. Table management Syntactic analysis Semantic analysis and intermediate code parse tree, derivation tree Error management The number of passes The number of times the is written in a file (or is read from a file). Several phases can be gathered together in one pass. internal form, intermediate code Code internal form Code generation Object machine code/assembly code Lecture 1 Compilers, Introduction Page 9 Lecture 1 Compilers, Introduction Page 10

Lexical analysis (scanner) Sequence of characters Tokens (basic symbols, groups of successive characters which belong together logically). 1. In the source text isolate and classify the basic elements that form the : Token Example Identifiers SUM, A Constants 556, 1.5E-5 Strings Provide a number Keywords, reserved words WHILE, IF Operators + - * / Others. ; ^, 3. Distinguish homonyms. Homonyms: Words that are pronounced and/or are written alike but which have different meanings. feet, feat; spoke (of a wheel), spoke (talked) coach: football coach, journey by coach Example1: FORTRAN: DO 10 I=1,15 DO 10 I=1.15 is a loop, but is an assignment. NB! Blanks have no meaning in FORTRAN. Example 2: Pascal VAR i: 15..25; 2. Construct tables (symbol table, constant table, string table etc.). Lecture 1 Compilers, Introduction Page 11 Lecture 1 Compilers, Introduction Page 12 The scanner returns values in the form <type, value> Example: IF sum < 15 THEN z := 153 < 5, 0 > 5 = IF, 0 = lacks value < 7, 14 > 7 = code for identifier, 14 = entry to symbol table < 9, 1 > 9 = relational operator, 1 = < < 1, 15> 1 = code for constant, 15 = value < 2, 0 > 2 = THEN, 0 = lacks value < 7, 9 > 7 = code for identifier, 9 = entry to symbol table < 3, 0 > 3 = :=, 0 = lacks value < 1, 153 > 1 = code for constant, 153 = value Syntax analysis (parsing) Sequence of tokens Parse tree, error messages 1. Determine whether the input sequence forms a structure which is legal according to the definition of the. Example1: OK. IF X = 1 THEN X := 1 Index Symbol table Example 2: Not OK. 9 z.. 14 sum Regular expressions are used to describe tokens. IFF X = 1 THEN X := 1 which produces the sequence of tokens: < 7, 23 > < 7, 16 > {Two identifiersinarow wrong!} < 9, 0 >... Lecture 1 Compilers, Introduction Page 13 Lecture 1 Compilers, Introduction Page 14

2. Group tokens into syntactic units and construct parse trees which exhibit the structure. Example: A/B*C Semantic analysis and intermediate code generation Parse tree + symbol table intermediate code + symbol table temp.variables, information on their type... / 1. Semantic analysis checks items which a grammar can not describe: A B * C type compatibility a := i * 1.5 correct number and type of parameters in calls to procedures as specified in the procedure declaration. represents A/(B*C) i.e. right-associative (is this desirable?) The syntax of a is described using a context-free grammar. 2. Generate intermediate code. Example: A + B * C Produces in reverse Polish notation: A B C * + Or three-address code: T1 := B * C T2 := A + T1 in the form of a parse tree Lecture 1 Compilers, Introduction Page 15 Lecture 1 Compilers, Introduction Page 16 Or abstract syntax tree: + A * B C Code optimisation (more appropriately: Code improvement ) Internal form Internal form, hopefully improved. The intermediate form is used because it is: 1. Simpler than the high-level (fewer and simpler operations). 2. Not profiled for a given machine (portability). 3. Suitable for optimisation. Syntax-directed translation schemes are used to attach semantic routines (rules) to syntactic constructions. Machine-independent code optimisation: In some way make the machine code faster or more compact by transforming the internal form. Example: Eliminating common sub-expressions Stepwise improvement A:=B+C*I T1:=C*I T1:=C*I D:=C*I+E T2:=B+T1 A:=B+T1 A:=T2 D:=T1+E T3:=C*I T4:=T3+E D:=T4 Example: Code with no effect!? (two assignments to the same variable) A:=B*C*D+4; removed (or error message)a:=5*k; logical error perhaps? Lecture 1 Compilers, Introduction Page 17 Lecture 1 Compilers, Introduction Page 18

Code generation Internal form Machine code/assembly code 1. Register allocation and machine code generation (or assembly code). 2. Instruction scheduling (specially important for RISC) 3. Machine-dependent code optimisation (so-called peephole optimisation ). Table management Updating and search in tables Symbol table (for identifiers) String table Constant table Help for other phases during compilation. Example: Z := A+B*C is translated to: MOVE 1, B IMUL 1, C ADD 1, A MOVEM 1, Z Lecture 1 Compilers, Introduction Page 19 Lecture 1 Compilers, Introduction Page 20 Error management 1. Discover an error. 2. Write an error message. 3. Correct the error (or guess, very difficult!) 4. Restart from the error (try to continue). Examples of error messages: Lexical analysis: Faulty sequence of characters which does not result in a token, e.g. Ö, 5EL, %K, string Syntax analysis: Syntax error (e.g. missing semicolon). Semantic analysis: Type conflict, e.g. HEJ +5 Code optimisation: Uninitialised variables, anomaly detection. Code generation: Too large integers, run out of memory. Table management: Double declaration, table overflow. Lecture 1 Compilers, Introduction Page 21