Compiler Construction Dr. Naveed Ejaz Lecture 2. 2 Two-pass Compiler Front End Back End source code IR machine code errors.

Slides:



Advertisements
Similar presentations
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Advertisements

Lecture # 7 Chapter 4: Syntax Analysis. What is the job of Syntax Analysis? Syntax Analysis is also called Parsing or Hierarchical Analysis. A Parser.
Context-Free Grammars Lecture 7
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
Chapter 2 A Simple Compiler
From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)
Compiler Construction
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #5 Introduction.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
ParsingParsing. 2 Front-End: Parser  Checks the stream of words and their parts of speech for grammatical correctness scannerparser source code tokens.
Compiler Construction CS 606 Sohail Aslam Lecture 2.
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax Directed Definitions Synthesized Attributes
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.
COMPSCI 322: Language and Compilers Class Hour: Hyer Hall 210: TThu 9:30am – 10:15am.
1 Chapter 2 A Simple Compiler. 2 Outlines 2.1 The Structure of a Micro Compiler 2.2 A Micro Scanner 2.3 The Syntax of Micro 2.4 Recursive Descent Parsing.
COP4020 Programming Languages
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
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.
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
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.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
May 31, May 31, 2016May 31, 2016May 31, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa Pacific University,
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
1 Chapter 1 Introduction. 2 Outlines 1.1 Overview and History 1.2 What Do Compilers Do? 1.3 The Structure of a Compiler 1.4 The Syntax and Semantics of.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
D Goforth COSC Translating High Level Languages.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Introduction to Compiling
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
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 Composed By, Muhammad Bilal Qureshi.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Module 2 Compiler and their Working Software Construction Lecture 10,11 and 12.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
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.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
CMPSC 160 Translation of Programming Languages Fall 2002 Instructor: Hugh McGuire Lecture 2 Phases of a Compiler Lexical Analysis.
1 Compiler Construction Vana Doufexi office CS dept.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
Lecture 02: Compiler Overview Kwangman Man ( SangJi University.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Introduction to Parsing
1. Course Goals To provide students with an understanding of the major phases of a compiler. To introduce students to the theory behind the various phases,
Chapter 3 – Describing Syntax
Compiler Design (40-414) Main Text Book:
System Software Unit-1 (Language Processors) A TOY Compiler
A Simple Syntax-Directed Translator
PROGRAMMING LANGUAGES
Compiler Lecture 1 CS510.
Compiler Designs and Constructions
Compiler Design 4. Language Grammars
Lecture 2: General Structure of a Compiler
Introduction CI612 Compiler Design CI612 Compiler Design.
Lecture 02: Compiler Overview
Lecture 7: Introduction to Parsing (Syntax Analysis)
Compilers B V Sai Aravind (11CS10008).
CMPE 152: Compiler Design August 21/23 Lab
Introduction to Parsing
Introduction to Parsing
High-Level Programming Language
Chapter 10: Compilers and Language Translation
Presentation transcript:

Compiler Construction Dr. Naveed Ejaz Lecture 2

2 Two-pass Compiler Front End Back End source code IR machine code errors

3 Two-pass Compiler  Use an intermediate representation (IR)  Front end maps legal source code into IR

4 Two-pass Compiler  Back end translates IR into target machine code  Admits multiple front ends & multiple passes

5 Two-pass Compiler  Front end is O(n) or O(n log n)  Back end is NP-Complete (NPC)

6 Front End  Recognizes legal (& illegal) programs  Report errors in a useful way  Produce IR & preliminary storage map

7 The Front-End Modules  Scanner  Parser scannerparser source code tokensIR errors

8 ScannerScanner scannerparser source code tokens IR errors

9 ScannerScanner  Maps character stream into words – basic unit of syntax  Produces pairs – a word and its part of speech

10 ScannerScanner  Example x = x + y becomes token type word

11 ScannerScanner  we call the pair “ ” a “token”  typical tokens: number, identifier, +, -, new, while, if

12 ParserParser scannerparser source code tokensIR errors

13 ParserParser  Recognizes context-free syntax and reports errors  Guides context-sensitive (“semantic”) analysis  Builds IR for source program

14 Context-Free Grammars  Context-free syntax is specified with a grammar G=(S,N,T,P)  S is the start symbol  N is a set of non-terminal symbols  T is set of terminal symbols or words  P is a set of productions or rewrite rules

15 Context-Free Grammars Grammar for expressions 1.goal→expr 2.expr → expr op term 3. |term 4.term→ number 5. | id 6.op → + 7. | -

16 The Front End  For this CFG S = goal T = { number, id, +, -} N = { goal, expr, term, op} P = { 1, 2, 3, 4, 5, 6, 7}

17 The Front End  To recognize a valid sentence in some CFG, we reverse this process and build up a parse  A parse can be represented by a tree: parse tree or syntax tree

18 ParseParse Production Result goal 1 expr 2 expr op term 5 expr op y 7 expr – y 2 expr op term – y 4 expr op 2 – y 6 expr + 2 – y 3 term + 2 – y 5 x + 2 – y