Introduction Lecture 1 Wed, Jan 12, 2005. The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

CS 31003: Compilers Introduction to Phases of Compiler.
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.
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.
Chapter 1: Introduction to Compiling
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
Compiler Design Nai-Wei Lin Department of Computer Science National Chung Cheng University.
CPSC 388 – Compiler Design and Construction Lecture: MWF 11:00am-12:20pm, Room 106 Colton.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
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.
Parser-Driven Games Tool programming © Allan C. Milne Abertay University v
CSC 338: Compiler design and implementation
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
Compiler Construction1 COMP Compiler Construction Lecturer: Dr. Arthur Cater Teaching Assistant:
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
RIT 08/11/47Chapter 11 Chapter 1: Introduction to Compiling Dr. Winai Wichaipanitch Rajamangala Institute of Technology Klong 6 Thanyaburi Pathumthani.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
COMPILER OVERVIEW. Compiler Phases  Syntactic Analysis (Lexing, Parsing)  c = (a + b) * (a + b);
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.
1 Compiler 3.2 A Level computing. 2 So, Sir what is a compiler?
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
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.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
Introduction to Compiling
YACC. Introduction What is YACC ? a tool for automatically generating a parser given a grammar written in a yacc specification (.y file) YACC (Yet Another.
Introduction CPSC 388 Ellen Walker Hiram College.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
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.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
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.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
More yacc. What is yacc – Tool to produce a parser given a grammar – YACC (Yet Another Compiler Compiler) is a program designed to compile a LALR(1) grammar.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
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 *
Compilers Design Chapter1 : Introduction, Structure of a Compiler
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Compiler Design (40-414) Main Text Book:
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
Lexical and Syntax Analysis
Introduction.
PROGRAMMING LANGUAGES
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
Compiler Construction
Compiler Designs and Constructions
Basic Program Analysis: AST
Course supervisor: Lubna Siddiqui
Introduction CI612 Compiler Design CI612 Compiler Design.
Compiler 薛智文 TH 6 7 8, DTH Spring.
Compilers B V Sai Aravind (11CS10008).
Compiler design.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Compiler Structures 1. Overview Objective
Presentation transcript:

Introduction Lecture 1 Wed, Jan 12, 2005

The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation. Optimization. Machine code generation.

Lexical Analysis The lexical analyzer is also called a lexer or a scanner. The lexer receives a stream of characters from the source program. It groups them into tokens. Source Program Stream of Tokens Lexical Analyzer

Tokens A token is the smallest meaningful component in a program. Each token has a type and a value. For example, The variable count has type ID and value "count". The number 123 has type NUM and value "123". The keyword if has type IF and value "if".

Example: Lexical Analysis What are the tokens in the following program? int main() { int a = 123; return 0; }

Example: Lexical Analysis The following statement position = initial + rate * 60; would be viewed as id 1 = id 2 + id 3 * num ; by the lexer.

Lexical Analysis Tools There are tools available to assist in the writing of lexical analyzers. lex – produces C source code (UNIX). flex – produces C source code (gnu). JLex – produces Java source code. We will use JLex.

Syntax Analysis A syntax analyzer is called a parser. The parser receives a stream of tokens from the lexer. It groups them into phrases that match grammatical patterns. Stream of Tokens Abstract Syntax Tree Parser

Syntactic Analysis The parser outputs an abstract syntax tree representing the syntactical structure of the pattern.

Grammatical Patterns Grammatical patterns are described by a context-free grammar. For example, an assignment statement may be defined as stmt  id = expr ; expr  expr + expr | expr * expr | id | num

Example: Syntactic Analysis The tokens id 1 = id 2 + id 3 * num ; may be represented by the following tree. = id 1 + id 2 * id 3 num

Syntax Analysis Tools There are tools available to assist in the writing of parsers. yacc – produces C source code (UNIX). bison – produces C source code (gnu). CUP – produces Java source code. We will use CUP.

Semantic Analyzer An semantic analyzer traverses the abstract syntax tree checking that each node is appropriate for its context. That is, it checks for semantic errors. It outputs a refined abstract syntax tree. Abstract Syntax Tree Abstract Syntax Tree Semantic Analyzer

Example: Semantic Analysis The tree may be modified to the tree = id 1 + id 2 * id 3 num = id 1 + id 2 * id 3 inttoreal num

Intermediate Code Generation An intermediate code generator receives the abstract syntax tree from the semantic analyzer. It outputs intermediate code that semantically corresponds to the abstract syntax tree. Intermediate Code Intermediate Code Generator Abstract Syntax Tree

Intermediate Code Intermediate code is code that represents the semantics of a program, but is machine- independent. This marks the boundary between the front end and the back end. The front end is language-specific and machine-independent. The back end is language-independent and machine-specific.

Intermediate Code C Program Java Program Pascal Program Intermediate Code x86 Code MC Code Front End Back End

Example: Intermediate Code Generation The tree temp1 = inttoreal(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3 may be expressed in intermediate code as = id 1 + id 2 * id 3 inttoreal num

Code Optimizer A program may be optimized for speed or for size. An optimizer reviews the code, looking for ways to reduce the number of operations and the memory requirements. Often there is a trade-off between speed and size. Intermediate Code Optimizer Intermediate Code

Example: Optimization The intermediate code in this example may be optimized. temp1 = id3 * 60.0 id1 = id2 + temp1 temp1 = inttoreal(60) temp2 = id3 * temp1 temp3 = id2 + temp2 id1 = temp3

Machine Code Generation The code generator receives the (optimized) intermediate code. It produces either machine code for a specific machine, or assembly code for a specific machine and assembler. If it produces assembly code, then an assembler is used to produce the machine code.

Machine Code Generation Intermediate Code Assembly Code Generator Machine Code Assembler

Example: Machine Code Generation The intermediate code may be translated into the assembly code movf id3,R2 mulf #60.0,R2 movf id2,R1 addf R2,R1 movf R1,id1 temp1 = id3 * 60.0 id1 = id2 + temp1