CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.

Slides:



Advertisements
Similar presentations
Lexical Analysis Dragon Book: chapter 3.
Advertisements

4b Lexical analysis Finite Automata
Lexical Analysis Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou.
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.
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
Lexical Analysis The Scanner Scanner 1. Introduction A scanner, sometimes called a lexical analyzer A scanner : – gets a stream of characters (source.
CS 540 Spring CS 540 Spring 2013 GMU2 The Course covers: Lexical Analysis Syntax Analysis Semantic Analysis Runtime environments Code Generation.
Topic #3: Lexical Analysis
CPSC 388 – Compiler Design and Construction Scanners – Finite State Automata.
Finite-State Machines with No Output Longin Jan Latecki Temple University Based on Slides by Elsa L Gunter, NJIT, and by Costas Busch Costas Busch.
Finite-State Machines with No Output
1 Lexical Analysis - An Introduction Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at.
1 Chapter 3 Scanning – Theory and Practice. 2 Overview Formal notations for specifying the precise structure of tokens are necessary –Quoted string in.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
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 Specifying Tokens Lecture 2 CS 4318/5531 Spring 2010 Apan Qasem Texas State University *some slides adopted from Cooper and Torczon.
Lexical Analyzer (Checker)
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
CS412/413 Introduction to Compilers Radu Rugina Lecture 4: Lexical Analyzers 28 Jan 02.
TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP.
1 November 1, November 1, 2015November 1, 2015November 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
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.
IN LINE FUNCTION AND MACRO Macro is processed at precompilation time. An Inline function is processed at compilation time. Example : let us consider this.
Introduction to Compiling
Overview of Previous Lesson(s) Over View  Symbol tables are data structures that are used by compilers to hold information about source-program constructs.
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.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
1st Phase Lexical Analysis
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 2: Lexical Analysis.
Chapter 2 Scanning. Dr.Manal AbdulazizCS463 Ch22 The Scanning Process Lexical analysis or scanning has the task of reading the source program as a file.
using Deterministic Finite Automata & Nondeterministic Finite Automata
Overview of Previous Lesson(s) Over View  A token is a pair consisting of a token name and an optional attribute value.  A pattern is a description.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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:
1 Compiler Construction Vana Doufexi office CS dept.
Deterministic Finite Automata Nondeterministic Finite Automata.
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
Department of Software & Media Technology
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
System Software Theory (5KS03).
Finite automate.
CS510 Compiler Lecture 2.
Scanner Scanner Introduction to Compilers.
CS 404 Introduction to Compiler Design
Chapter 2 Scanning – Part 1 June 10, 2018 Prof. Abdelaziz Khamis.
CSc 453 Lexical Analysis (Scanning)
Finite-State Machines (FSMs)
Lexical analysis Finite Automata
Compilers Welcome to a journey to CS419 Lecture5: Lexical Analysis:
Finite-State Machines (FSMs)
Review: Compiler Phases:
Recognition of Tokens.
Scanner Scanner Introduction to Compilers.
4b Lexical analysis Finite Automata
Scanner Scanner Introduction to Compilers.
4b Lexical analysis Finite Automata
Scanner Scanner Introduction to Compilers.
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Scanner Scanner Introduction to Compilers.
Lecture 5 Scanning.
Scanner Scanner Introduction to Compilers.
Presentation transcript:

CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat

CS 404Ahmd Ezzat 2 Lecture 1 Administration Introduction to Compilers – What is a compiler – Compiler phases Lexical Analysis – Tokens, lexemes – Regular expressions

CS 404Ahmd Ezzat 3 An Introduction to Compilers A compiler is a program translator Source language->Target language Compiler examples: cc, gcc, javac

CS 404Ahmd Ezzat 4 Phases of a Compiler Front end == analysis – Lexical analysis, Syntax analysis, Semantic analysis – Language dependent, machine independent Back end == synthesis – Intermediate code generation, Optimization, Target code generation – Language independent, machine dependent

CS 404Ahmd Ezzat 5 Examples of Compiler Phases Lexical analysis – Scanning: transforms characters into “tokens” Syntax analysis – Parsing: transforms token streams into “parse trees” – Structural analysis

CS 404Ahmd Ezzat 6 Examples of Compiler Phases (2) Semantic analysis: checks whether the input program “make sense” – Example: type checking Intermediate code generation – Example: three address code Code optimization Target code generation

CS 404Ahmd Ezzat 7 Compiler Issues Symbol table: a data structure containing a record for each identifier, with attributes of the identifier Error handling: detection, reporting, recovery Compiler passes: one pass versus multiple passes

CS 404Ahmd Ezzat 8 Working Together With Compilers Pre-processors: macros, file handling Assembler: from assembly code to machine code Loaders: place instructions and data in memory Linkers: link several target programs together

CS 404Ahmd Ezzat 9 Lexical Analysis Source language -> token streams Token: e.g., identifier, constant, keyword – Classes of sequence of characters – Satisfy certain patterns (or rules) – Data structure returned by lexical analyzer Lexeme: e.g. my_id, count2 – String matches a pattern

CS 404Ahmd Ezzat 10 Describe Patterns: Regular Expression Pattern or rules to identify lexemes Precise specification of sets of strings There exists a computational model to evaluate (Finite Automata) There exists tools to process them (LEX)

CS 404Ahmd Ezzat 11 Regular Expression Notations Symbols: e.g., a, b, c, 1, 2 Alphabet: finite set of symbols, Σ (sigma) – e.g., Σ = {a,b} String: a sequence of symbols – e.g., hello, ε (epsilon, empty string) Language: a set of strings over an alphabet – e.g., {a, ab, ba} – e.g., the set of all valid C programs

CS 404Ahmd Ezzat 12 Regular Expression Definition Every symbol of Σ U {ε} is a regular expression If r1 and r2 are regular expressions, so are – Concatenation: r1r2 – Alternation: r1 | r2 – Repetition: r1* Nothing else is a regular expression

CS 404Ahmd Ezzat 13 Regular Expression Extended a+ : one or more a’s a? : zero or one a a{n}: a repeats n times a{n,}: a repeats at least n times a{n,m}: a repeats at least n but no more than m times and more

CS 404Ahmd Ezzat 14 Regular Expressions Cannot Do Arithmetic expressions Set of strings over {(,)} with matched parentheses Strings over {a,b} with equal number of b’s following a’s

CS 404Ahmd Ezzat 15 Regular Definitions Give names to regular expressions and use them as shorthand Must avoid recursive definitions Example – digit -> 1 | 2 … 9 – int -> Digit+ – letter -> A | B | … Z – Id -> letter (letter | digit)*

CS 404Ahmd Ezzat 16 Finite Automata Evaluate regular expressions Recognize certain languages and reject others Two kinds of FA: – Non-deterministic FA (NFA) – Deterministic FA (DFA)

CS 404Ahmd Ezzat 17 An NFA Consists of An input alphabet, e.g., Σ = {a,b} A set of states, e.g., S = {s0, s1, s2} A set of transitions from states to states, labeled by elements of Σ or ε A start state, e.g., s0 A set of final states, e.g., F = {s1, s2}

CS 404Ahmd Ezzat 18 FA and Language An FA accepts string x if and only if there is some path in the transition graph from the start state to a final state, such that the edge labels along this path spells x The set of strings an FA accepts is said to be the language defined by this FA.

CS 404Ahmd Ezzat 19 Deterministic Finite Automata A DFA is a special case of NFA No states has an ε transition For each state s and input symbol a, there is at most one edge labeled a leaving s

CS 404Ahmd Ezzat 20 NFA, DFA and Regular Expressions A DFA is an NFA Each NFA can be converted into a DFA One can construct an NFA from a regular expression FAs are used by lexical analyzer to recognize tokens