1 Compiler 3.2 A Level computing. 2 So, Sir what is a compiler?

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

CS 31003: Compilers Introduction to Phases of Compiler.
Compilers and Language Translation
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
CPSC Compiler Tutorial 9 Review of Compiler.
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
1 Foundations of Software Design Lecture 24: Compilers, Lexers, and Parsers; Intro to Graphs Marti Hearst Fall 2002.
Writing software for embedded systems Part I: Introduction, design flow Part II: The role of compilers Alessandro Dalla Torre, Andrea Marongiu {alessandro.dallatorre,
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
Saman Amarasinghe ©MIT Fall 1998 Outline Course Administration Information Introduction to computer language engineering –What are compilers? –
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Compiled by Benjamin Muganzi 3.2 Functions and Purposes of Translators Computing 9691 Paper 3 1.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
Invitation to Computer Science 5th Edition
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
COP4020 Programming Languages
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
CSC 338: Compiler design and implementation
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
國立台灣大學 資訊工程學系 薛智文 98 Spring Compiler TH 234, DTH 103.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
Compilers I CNS History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Compilers Computer Symbol Table Output Scanner (lexical analysis)
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Language Implementation Overview John Keyser Spring 2016.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
System Software Theory (5KS03).
Introduction to Compiler Construction
Finite-State Machines (FSMs)
-by Nisarg Vasavada (Compiled*)
Chapter 1: Introduction to Compiling (Cont.)
CS 536 / Fall 2017 Introduction to programming languages and compilers
Compiler Construction
Compiler Designs and Constructions
Basic Program Analysis: AST
Course supervisor: Lubna Siddiqui
Compiler 薛智文 TH 6 7 8, DTH Spring.
CSE401 Introduction to Compiler Construction
Compilers B V Sai Aravind (11CS10008).
CMPE 152: Compiler Design August 21/23 Lab
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compiler design.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Compiler 薛智文 M 2 3 4, DTH Spring.
Presentation transcript:

1 Compiler 3.2 A Level computing

2 So, Sir what is a compiler?

3 The Structure of a Compiler SourceTokensInterm. Language Lexical analysis Parsing Code Gen. Machine Code Today we start Optimization

4 Lexical Analysis (scanner) Scanner reads characters from the source program Scanner groups the characters into lexemes The scanner is called by parser Each lexeme corresponds to a token Example: if (i == j) z = 0; else z = 1; The input is just a sequence of characters: \tif (i == j)\n\t\tz = 0;\n\t\telse\n\t\tz = 1; lexemes

5 Sir, what the hell then you mean by Token and Lexeme ? A token is a syntactic category –In English: noun, verb, adjective, … –In a programming language: Identifier, Integer, Keyword, Whitespace, … –Given the source code I = I + rate * 60; A Java scanner would return the following sequence of tokens… IDENT ASSIGN IDENT PLUS IDENT TIMES INT-LIT SEMI-COLON tokens

6 The Parser Group of tokens into “grammatical phrases”, discovering the underlying structure of the source program Finds syntax errors. For example, in java source code I = * 5; Corresponds to the following sequence of tokens IDENT ASSIGN TIMES INT-LIT SEMI COLON All are legal tokens, but sequence of token is erroneous.

7 Once again Sir, what the hell does the parser do finally? Might find some “static semantic” errors, e.g., use of an undeclared variable, or operands that are wrongly used with variables. Might generate code, or build some intermediate representation of the program such as an abstract-syntax tree.

8 num + ()* num ‘*’ ‘(‘ num ‘+’ num ‘)’ Syntax Analyzer (Parser)

9 Semantic Analyzer Consider the Source code: I = I + rate * 60; = (float) I + (float) (float) I + (float) rate* 60 (float) rate * 60 (int)  The semantic analyzer checks for more “static semantic” errors. It may also annotate and or change the abstract syntax tree.

10 The Intermediate Code Generator The ICG translates from abstract-syntax tree to intermediate code. One possibility is 3- address code. Example: temp1 = 60 temp2 = rate * temp1 temp3 = I + temp2 I = temp3

11 The Optimizer The Optimizer tries to improve code generated by the intermediate code generator. The optimizer may also try to reduce the code smaller So.. The previous intermediate code thus becomes temp2 = rate * temp1 I = I + temp2

12 The Code Generator generates object code finally from the (optimized) intermediate code. Example: sumcalc: xorl %r8d, %r8d xorl %ecx, %ecx movl %edx, %r9d cmpl %edx, %r8d jg.L7 sall $2, %edi.L5: movl %edi, %eax cltd idivl %esi leal 1(%rcx), %edx movl %eax, %r10d imull %ecx, %r10d movl %edx, %ecx imull %edx, %ecx leal (%r10,%rcx), %eax movl %edx, %ecx addl %eax, %r8d cmpl %r9d, %edx jle.L5.L7: movl %r8d, %eax ret int sumcalc(int a, int b, int N) { int i; int x, t, u, v; x = 0; u = ((a<<2)/b); v = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + v + t*t; v = v + u; } return x; }

13 So what? So the compiler converts the source program into object program. When doing this process of converting (source to object) it takes the sources code and passes it to different stages: lexical analysis, which reads the source Code character by character and coins it to form the lexemes and these lexemes then are framed into bits of tokens which are then called by Parser. The parser then groups these token into meaningful “grammatical phrases”, finds syntax errors if any and then built a intermediate representation of the program as an “abstract-syntax” tree. This now is passed through another process called Semantic analyzer, which further annotates and augments the “abstract-syntax tree”. Now this “abstract-syntax” tree is translated to an intermediate code by the Intermediate Code Generator. The outcome of the Intermediate code is further optimized by Optimizer. The optimizer can further simplify the codes and passes it to the Code Generator, which generates the desired Object code. These object code are then linked by the linkers and loaded into the memory for execution by the loader.

14 Anatomy of a Computer Step by Step (We shall see what happens) Lexical Analyzer (Scanner) Token Stream Program (character stream) Lexical Analyzer (Scanner) Token Stream Program (character stream)

15 Lexical Analyzer (Scanner) Num(234)mul_oplpar_opNum(11)add_oprpar_op 2 34 *( ) Num(-22)

16 Lexical Analyzer (Scanner) val#ue Num(234)mul_oplpar_opNum(11)add_oprpar_op 2 34 *( ) Num(-22) Not a number Variable names cannot have ‘#’ character

17 Anatomy of a Computer Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Token Stream Parse Tree Program (character stream) Syntax Analyzer (Parser) Token StreamParse Tree

18 Syntax Analyzer (Parser) num + ()* num ‘*’ ‘(‘ num ‘+’ num ‘)’

19 Syntax Analyzer (Parser) int * foo(i, j, k)) int i; int j; { for(i=0; i j) { fi(i>j) return j; } Extra parentheses Missing increment Not an expressionNot a keyword

20 Anatomy of a Computer Intermediate Representation Semantic Analyzer Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Token Stream Parse Tree Program (character stream) Intermediate Representation Semantic Analyzer Parse Tree

21 Semantic Analyzer int * foo(i, j, k) int i; int j; { int x; x = x + j + N; return j; } Type not declared Mismatched return type Uninitialized variable used Undeclared variable

22 Anatomy of a Computer Code Optimizer Optimized Intermediate Representation Intermediate Representation Semantic Analyzer Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Token Stream Parse Tree Program (character stream) Code Optimizer Optimized Intermediate RepresentationIntermediate Representation

23 Optimizer int sumcalc(int a, int b, int N) { int i; int x, t, u, v; x = 0; u = ((4*a)b*i); v = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + u + t*t; } return x; } int sumcalc(int a, int b, int N) { int i; int x, y; x = 0; y = 0; for(i = 0; i <= N; i++) { x = x+4*a/b*i+(i+1)*(i+1); } return x; }

24 Anatomy of a Computer Code Optimizer Code Generator Optimized Intermediate Representation Assembly code Intermediate Representation Semantic Analyzer Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Token Stream Parse Tree Program (character stream) Code Generator Optimized Intermediate RepresentationAssembly code

25 Code Generator sumcalc: xorl %r8d, %r8d xorl %ecx, %ecx movl %edx, %r9d cmpl %edx, %r8d jg.L7 sall $2, %edi.L5: movl %edi, %eax cltd idivl %esi leal 1(%rcx), %edx movl %eax, %r10d imull %ecx, %r10d movl %edx, %ecx imull %edx, %ecx leal (%r10,%rcx), %eax movl %edx, %ecx addl %eax, %r8d cmpl %r9d, %edx jle.L5.L7: movl %r8d, %eax ret int sumcalc(int a, int b, int N) { int i; int x, t, u, v; x = 0; u = ((a<<2)/b); v = 0; for(i = 0; i <= N; i++) { t = i+1; x = x + v + t*t; v = v + u; } return x; }