Compiler Construction. Lecture 9

Similar documents
Compiler Construction(CS606)- Lecture Handouts

Course Organization. The grade distribution will be. Theory Homeworks 10% Exams 50% Practice Project 40%

Compiler Construction. Lecture 10

Lexical Analysis - Flex

Compiler phases. Non-tokens

Lexical Analysis. Implementing Scanners & LEX: A Lexical Analyzer Tool

2010: Compilers REVIEW: REGULAR EXPRESSIONS HOW TO USE REGULAR EXPRESSIONS

Lexical and Parser Tools

Lex Spec Example. Int installid() {/* code to put id lexeme into string table*/}

Introduction to Compiler Design

MidTerm Papers Solved MCQS with Reference (1 to 22 lectures)

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

CMSC 350: COMPILER DESIGN

Lecture Outline. COMP-421 Compiler Design. What is Lex? Lex Specification. ! Lexical Analyzer Lex. ! Lex Examples. Presented by Dr Ioanna Dionysiou

Implementation of Lexical Analysis

Module 8 - Lexical Analyzer Generator. 8.1 Need for a Tool. 8.2 Lexical Analyzer Generator Tool

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

LECTURE 6 Scanning Part 2

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 2: Lexical Analysis 23 Jan 08

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

CS415 Compilers. Lexical Analysis

Assignment 1 (Lexical Analyzer)

Announcements! P1 part 1 due next Tuesday P1 part 2 due next Friday

Assignment 1 (Lexical Analyzer)

Monday, August 26, 13. Scanners

Wednesday, September 3, 14. Scanners

CS 301. Lecture 05 Applications of Regular Languages. Stephen Checkoway. January 31, 2018

LEX/Flex Scanner Generator

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

Scanners. Xiaokang Qiu Purdue University. August 24, ECE 468 Adapted from Kulkarni 2012

Implementation of Lexical Analysis

Lexical Analysis. Chapter 1, Section Chapter 3, Section 3.1, 3.3, 3.4, 3.5 JFlex Manual

CSc 453 Lexical Analysis (Scanning)

Finite Automata and Scanners

Compilation 2014 Warm-up project

CSC 467 Lecture 3: Regular Expressions

Formal Languages and Compilers Lecture VI: Lexical Analysis

CSE 401 Midterm Exam Sample Solution 2/11/15

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Compiler Construction LECTURE # 3

CSE302: Compiler Design

Implementation of Lexical Analysis

Languages, Automata, Regular Expressions & Scanners. Winter /8/ Hal Perkins & UW CSE B-1

Dr. D.M. Akbar Hussain

Outline CS4120/4121. Compilation in a Nutshell 1. Administration. Introduction to Compilers Andrew Myers. HW1 out later today due next Monday.

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

CSE 3302 Programming Languages Lecture 2: Syntax

Figure 2.1: Role of Lexical Analyzer

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

An introduction to Flex

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Lexical and Syntax Analysis

Chapter 3 Lexical Analysis

Edited by Himanshu Mittal. Lexical Analysis Phase

Compilers and Interpreters

Lexical Analysis. Textbook:Modern Compiler Design Chapter 2.1.

Compiler course. Chapter 3 Lexical Analysis

LECTURE 3. Compiler Phases

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Lexical analysis. Syntactical analysis. Semantical analysis. Intermediate code generation. Optimization. Code generation. Target specific optimization

Front End: Lexical Analysis. The Structure of a Compiler

UNIT -2 LEXICAL ANALYSIS

Using Lex or Flex. Prof. James L. Frankel Harvard University

Compiler Construction

Lexical Analyzer Scanner

Overview of Compiler. A. Introduction

Compiling Regular Expressions COMP360

Lexical Analyzer Scanner

The Language for Specifying Lexical Analyzer

Writing a Lexical Analyzer in Haskell (part II)

Compiler Construction D7011E

Yacc: A Syntactic Analysers Generator

JFlex Regular Expressions

CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2]

Some Thoughts on Grad School. Undergraduate Compilers Review

WARNING for Autumn 2004:

CSE450. Translation of Programming Languages. Lecture 20: Automata and Regular Expressions

2068 (I) Attempt all questions.

Lexical Analysis. Introduction

Lecture 9 CIS 341: COMPILERS

CSCI312 Principles of Programming Languages!

Alternation. Kleene Closure. Definition of Regular Expressions

I. OVERVIEW 1 II. INTRODUCTION 3 III. OPERATING PROCEDURE 5 IV. PCLEX 10 V. PCYACC 21. Table of Contents

Structure of Programming Languages Lecture 3

Lexical Analysis. Lecture 2-4

Outline. Lecture 17: Putting it all together. Example (input program) How to make the computer understand? Example (Output assembly code) Fall 2002

Introduction to Lexical Analysis

Automated Tools. The Compilation Task. Automated? Automated? Easier ways to create parsers. The final stages of compilation are language dependant

EXPERIMENT NO : M/C Lenovo Think center M700 Ci3,6100,6th Gen. H81, 4GB RAM,500GB HDD

Compiler construction lecture 3

EECS483 D1: Project 1 Overview

A Scanner should create a token stream from the source code. Here are 4 ways to do this:

Lexical Analysis. Chapter 2

Lexical Analysis. Implementation: Finite Automata

CS143 Handout 04 Summer 2011 June 22, 2011 flex In A Nutshell

Revisit the example. Transformed DFA 10/1/16 A B C D E. Start

More Examples. Lex/Flex/JLex

CS606- compiler instruction Solved MCQS From Midterm Papers

JFlex. Lecture 16 Section 3.5, JFlex Manual. Robb T. Koether. Hampden-Sydney College. Mon, Feb 23, 2015

Transcription:

Compiler Construction Lecture 9

DFA Minimization The generated DFA may have a large number of states. Hopcroft s algorithm: minimizes DFA states 2

DFA Minimization The generated DFA may have a large number of states. Hopcroft s algorithm: minimizes DFA states 3

DFA Minimization Idea: find groups of equivalent states. All transitions from states in one group G 1 go to states in the same group G 2 4

DFA Minimization Idea: find groups of equivalent states. All transitions from states in one group G 1 go to states in the same group G 2 5

DFA Minimization Construct the minimized DFA such that there is one state for each group of states from the initial DFA. 6

DFA Minimization a a A a B b D b E b a a b C b DFA for (a b )*abb 7

DFA Minimization b a a A,C a B b D b E a b Minimized DFA for (a b )*abb 8

Optimized Acceptor RE R RE=>NFA NFA=>DFA Min. DFA input string w Simulate DFA yes, if w e L(R) no, if w e L(R) 9

Lexical Analyzers Lexical analyzers (scanners) use the same mechanism but they: Have multiple RE descriptions for multiple tokens Have a character stream at the input 10

Lexical Analyzers Lexical analyzers (scanners) use the same mechanism but they: Have multiple RE descriptions for multiple tokens Have a character stream at the input 11

Lexical Analyzers Lexical analyzers (scanners) use the same mechanism but they: Have multiple RE descriptions for multiple tokens Have a character stream at the input 12

Lexical Analyzers Return a sequence of matching tokens at the output (or an error) Always return the longest matching token 13

Lexical Analyzers Return a sequence of matching tokens at the output (or an error) Always return the longest matching token 14

Lexical Analyzers R 1 R 2 RE=>NFA character stream NFA=>DFA Min. DFA Simulate DFA Token stream 15

Lexical Analyzer Generators The lexical analysis process can automated We only need to specify Regular expressions for tokens Rule priorities for multiple longest match cases 16

Lexical Analyzer Generators The lexical analysis process can automated We only need to specify Regular expressions for tokens Rule priorities for multiple longest match cases 17

Lexical Analyzer Generators Flex generates lexical analyzer in C or C++ Jlex written in Java. Generates lexical analyzer in Java 18

Lexical Analyzer Generators Flex generates lexical analyzer in C or C++ Jlex written in Java. Generates lexical analyzer in Java 19

Using Flex Provide a specification file Flex reads this file and produces C or C++ output file contains the scanner. The file consists of three sections 20

Using Flex Provide a specification file Flex reads this file and produces C or C++ output file contains the scanner. The file consists of three sections 21

Using Flex Provide a specification file Flex reads this file and produces C or C++ output file contains the scanner. The file consists of three sections 22

Flex Specification File 1 C or C++ and flex definitions 23

Flex Specification File 1 2 C or C++ and flex definitions %% token definitions and actions 24

Flex Specification File 1 2 3 C or C++ and flex definitions %% token definitions and actions %% user code 25

Specification File lex.l %{ #include tokdefs.h %} D [0-9] L [a-za-z_] id {L}({L} {D})* %% "void" {return(tok_void);} "int" {return(tok_int);} "if" {return(tok_if);} 26

Specification File lex.l "else" {return(tok_else);} "while"{return(tok_while)}; "<=" {return(tok_le);} ">=" {return(tok_ge);} "==" {return(tok_eq);} "!=" {return(tok_ne);} {D}+ {return(tok_int);} {id} {return(tok_id);} [\n] [\t] [ ] ; %% 27

File tokdefs.h #define TOK_VOID 1 #define TOK_INT 2 #define TOK_IF 3 #define TOK_ELSE 4 #define TOK_WHILE 5 #define TOK_LE 6 #define TOK_GE 7 #define TOK_EQ 8 #define TOK_NE 9 #define TOK_INT 10 #define TOK_ID 111 28

Invoking Flex lex.l flex lex.cpp 29

Using Generated Scanner void main() { FlexLexer lex; int tc = lex.yylex(); while(tc!= 0) cout << tc <<, <<lex.yytext() << endl; tc = lex.yylex(); } 30

Creating Scanner EXE flex lex.l g++ c lex.cpp g++ c main.cpp g++ o lex.exe lex.o main.o lex <main.cpp 31