1 Compiler Construction Vana Doufexi office CS dept.

Slides:



Advertisements
Similar presentations
Lexical Analysis Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!
Advertisements

From Cooper & Torczon1 The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language?
Regular Expressions Finite State Automaton. Programming Languages2 Regular expressions  Terminology on Formal languages: –alphabet : a finite set of.
Winter 2007SEG2101 Chapter 81 Chapter 8 Lexical Analysis.
Lexical Analysis III Recognizing Tokens Lecture 4 CS 4318/5331 Apan Qasem Texas State University Spring 2015.
1 The scanning process Main goal: recognize words/tokens Snapshot: At any point in time, the scanner has read some input and is on the way to identifying.
1 The scanning process Goal: automate the process Idea: –Start with an RE –Build a DFA How? –We can build a non-deterministic finite automaton (Thompson's.
2. Lexical Analysis Prof. O. Nierstrasz
From Cooper & Torczon1 Implications Must recognize legal (and illegal) programs Must generate correct code Must manage storage of all variables (and code)
Compiler Construction
Scanner Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source language? Is the.
1 Scanning Aaron Bloomfield CS 415 Fall Parsing & Scanning In real compilers the recognizer is split into two phases –Scanner: translate input.
CS 540 Spring CS 540 Spring 2013 GMU2 The Course covers: Lexical Analysis Syntax Analysis Semantic Analysis Runtime environments Code Generation.
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.
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.
어휘분석 (Lexical Analysis). Overview Main task: to read input characters and group them into “ tokens. ” Secondary tasks: –Skip comments and whitespace;
Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
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.
Lecture # 3 Chapter #3: Lexical Analysis. Role of Lexical Analyzer It is the first phase of compiler Its main task is to read the input characters and.
Lexical Analysis (I) Compiler Baojian Hua
Lexical Analysis Constructing a Scanner from Regular Expressions.
Lexical Analysis I Specifying Tokens Lecture 2 CS 4318/5531 Spring 2010 Apan Qasem Texas State University *some slides adopted from Cooper and Torczon.
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 3, 09/11/2003 Prof. Roy Levow.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
CS412/413 Introduction to Compilers Radu Rugina Lecture 4: Lexical Analyzers 28 Jan 02.
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,
1 November 1, November 1, 2015November 1, 2015November 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 Languages and Compilers (SProg og Oversættere) Lexical analysis.
Lexical Analysis: Finite Automata CS 471 September 5, 2007.
Review: Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Intermediate code generator Code optimizer Code generator Symbol.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
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 By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Lexical Analysis – Part II EECS 483 – Lecture 3 University of Michigan Wednesday, September 13, 2006.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 2: Lexical Analysis.
CSC 4181 Compiler Construction
CMPSC 160 Translation of Programming Languages Fall 2002 Instructor: Hugh McGuire Lecture 2 Phases of a Compiler Lexical Analysis.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
LECTURE 5 Scanning. SYNTAX ANALYSIS We know from our previous lectures that the process of verifying the syntax of the program is performed in two stages:
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
Topic 3: Automata Theory 1. OutlineOutline Finite state machine, Regular expressions, DFA, NDFA, and their equivalence, Grammars and Chomsky hierarchy.
Intro to compilers Based on end of Ch. 1 and start of Ch. 2 of textbook, plus a few additional references.
Compiler Design (40-414) Main Text Book:
CS 3304 Comparative Languages
Finite-State Machines (FSMs)
Finite-State Machines (FSMs)
课程名 编译原理 Compiling Techniques
Compiler Lecture 1 CS510.
CS416 Compiler Design lec00-outline September 19, 2018
CPSC 388 – Compiler Design and Construction
Lexical Analysis - An Introduction
Review: Compiler Phases:
Lecture 5: Lexical Analysis III: The final bits
CSE401 Introduction to Compiler Construction
CS 3304 Comparative Languages
CS416 Compiler Design lec00-outline February 23, 2019
Lexical Analysis - An Introduction
Compiler Construction
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Lecture 5 Scanning.
Presentation transcript:

1 Compiler Construction Vana Doufexi office CS dept

2 Administrative info class webpage – –contains: news staff information lecture notes & other handouts homeworks & manuals policies, grades newsgroup portal useful links

3 What is a compiler A program that reads a program written in some language and translates it into a program written in some other language –Modula-2 to C –Java to bytecodes –COOL to MIPS code

4 Why study compilers? Application of a wide range of theoretical techniques Good SW engineering experience Better understand languages

5 Features of compilers Correctness –preserve the meaning of the code Speed of target code –vs. speed of compilation? Good use of resources (size, power) Good error reporting/handling

6 Compiler structure Use intermediate representation –Why? source code target code Front EndBack End IR

7 Compiler Structure Front end –Recognize legal/illegal programs report/handle errors –Generate IR –The process can be automated Back end –Translate IR into target code instruction selection register allocation instruction scheduling lots of NPC problems -- use approximations

8 Compiler Structure Optimization: Middle stage –goals improve running time of generated code improve space, power consumption, etc. –how? perform a number of transformations on the IR multiple passes –important: preserve meaning of code

9 The Front End Scanning ( a.k.a. lexical analysis ) –recognize "words" Parsing ( a.k.a. syntax analysis ) –check syntax Semantic analysis –examine meaning (e.g. type checking) Other issues: –symbol table (to keep track of identifiers) –error detection/reporting/recovery

10 The Scanner Its job: –given a character stream, recognize words (tokens) e.g. x = 1 becomes IDENTIFIER EQUAL INTEGER –collect identifier information e.g. IDENTIFIER corresponds to a lexeme (the actual word x) and its type (acquired from the declaration of x). –ignore white space and comments –report errors Good news –the process can be automated

11 The Parser Its job: –Check and verify syntax based on specified syntax rules e.g. IDENTIFIER LPAREN RPAREN make up an EXPRESSION. Coming soon: how context-free grammars specify syntax –Report errors –Build IR often a syntax tree Good news –the process can be automated

12 Semantic analysis Its job: –Check the meaning of the program e.g. In x=y, is y defined before being used? Are x and y declared? e.g. In x=y, are the types of x and y such that you can assign one to the other? –Meaning may depend on context –Report errors

13 IRs Graphical –e.g. parse tree, DAG Linear –e.g. three-address code Hybrid –e.g. linear for blocks of straight-line code, a graph to connect blocks Low-level or high-level

14 The scanning process Main goal: recognize words How? by recognizing patterns –e.g. an identifier is a sequence of letters or digits that starts with a letter. Lexical patterns form a regular language Regular languages are described using regular expressions (REs) Can we create an automatic RE recognizer? –Yes! (Hold that thought)

15 The scanning process Definition: Regular expressions (over alphabet  ) –  is an RE denoting {  } –If , then  is an RE denoting {  } –If r and s are REs, then (r) is an RE denoting L(r) r|s is an RE denoting L(r)  L(s) rs is an RE denoting L(r)L(s) r* is an RE denoting the Kleene closure of L(r) Property: REs are closed under many operations –This allows us to build complex REs.

16 The scanning process Definition: Deterministic Finite Automaton –a five-tuple ( , S, , s 0, F) where  is the alphabet S is the set of states  is the transition function (S  S) s 0 is the starting state F is the set of final states (F  S) Notation: –Use a transition diagram to describe a DFA DFAs are equivalent to REs –Hey! We just came up with a recognizer!

17 The scanning process Goal: automate the process Idea: –Start with an RE –Build a DFA How? –We can build a non-deterministic finite automaton (Thompson's construction) –Convert that to a deterministic one (Subset construction) –Minimize the DFA (Hopcroft's algorithm) –Implement it Existing scanner generator: flex