COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Similar documents
UNIT II LEXICAL ANALYSIS

Lexical Analysis (ASU Ch 3, Fig 3.1)

COMPILER DESIGN UNIT I LEXICAL ANALYSIS. Translator: It is a program that translates one language to another Language.

Zhizheng Zhang. Southeast University

2. λ is a regular expression and denotes the set {λ} 4. If r and s are regular expressions denoting the languages R and S, respectively

Lexical Analysis. Prof. James L. Frankel Harvard University

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

Formal Languages and Compilers Lecture VI: Lexical Analysis

1. INTRODUCTION TO LANGUAGE PROCESSING The Language Processing System can be represented as shown figure below.

Formal Languages and Compilers Lecture IV: Regular Languages and Finite. Finite Automata

Figure 2.1: Role of Lexical Analyzer

Front End: Lexical Analysis. The Structure of a Compiler

Lexical Analysis. Introduction

SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT I - LEXICAL ANALYSIS PART - A

2. Lexical Analysis! Prof. O. Nierstrasz!

UNIT -2 LEXICAL ANALYSIS

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

David Griol Barres Computer Science Department Carlos III University of Madrid Leganés (Spain)

Lexical Analyzer Scanner

Lexical Analyzer Scanner

CSc 453 Lexical Analysis (Scanning)

[Lexical Analysis] Bikash Balami

Lexical Analysis. Sukree Sinthupinyo July Chulalongkorn University

CS308 Compiler Principles Lexical Analyzer Li Jiang

Languages and Compilers

PRINCIPLES OF COMPILER DESIGN UNIT II LEXICAL ANALYSIS 2.1 Lexical Analysis - The Role of the Lexical Analyzer

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

1. Lexical Analysis Phase

Compiler course. Chapter 3 Lexical Analysis

UNIT I- LEXICAL ANALYSIS. 1.Interpreter: It is one of the translators that translate high level language to low level language.

The Front End. The purpose of the front end is to deal with the input language. Perform a membership test: code source language?

Chapter 3 Lexical Analysis

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Chapter 4. Lexical analysis. Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Last lecture CMSC330. This lecture. Finite Automata: States. Finite Automata. Implementing Regular Expressions. Languages. Regular expressions

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Question Bank. 10CS63:Compiler Design

Introduction to Lexical Analysis

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

Dixita Kagathara Page 1

Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Lexical Analysis. Lecture 3. January 10, 2018

Implementation of Lexical Analysis

CS 314 Principles of Programming Languages

A simple syntax-directed

Lexical Analysis. Chapter 2

ECS 120 Lesson 7 Regular Expressions, Pt. 1

LANGUAGE TRANSLATORS

CS 314 Principles of Programming Languages. Lecture 3

Lexical Analysis. Lecture 2-4

Implementation of Lexical Analysis

Lexical Analysis. Implementation: Finite Automata

DVA337 HT17 - LECTURE 4. Languages and regular expressions

Implementation of Lexical Analysis

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

We use L i to stand for LL L (i times). It is logical to define L 0 to be { }. The union of languages L and M is given by

Part 5 Program Analysis Principles and Techniques

Lexical Analysis - An Introduction. Lecture 4 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Lexical Analysis. Lecture 3-4

Compiler Construction

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Regular Expressions. Regular Expressions. Regular Languages. Specifying Languages. Regular Expressions. Kleene Star Operation

CS415 Compilers. Lexical Analysis

Finite Automata. Dr. Nadeem Akhtar. Assistant Professor Department of Computer Science & IT The Islamia University of Bahawalpur

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

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

Compiler Construction

Decision, Computation and Language

Buffering Techniques: Buffer Pairs and Sentinels

TABLE OF CONTENTS S.No DATE TOPIC PAGE No UNIT I LEXICAL ANALYSIS 1 Introduction to Compiling-Compilers 6 2 Analysis of the source program 7 3 The

CSE302: Compiler Design

Converting a DFA to a Regular Expression JP

CSE 105 THEORY OF COMPUTATION

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

Non-deterministic Finite Automata (NFA)

Chapter 3: Lexical Analysis

Complexity Theory. Compiled By : Hari Prasad Pokhrel Page 1 of 20. ioenotes.edu.np

CS321 Languages and Compiler Design I. Winter 2012 Lecture 4

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

COP 3402 Systems Software Syntax Analysis (Parser)

Lexical Analysis/Scanning

Lexical Analysis. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

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

Dr. D.M. Akbar Hussain

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

Implementation of Lexical Analysis. Lecture 4

The Language for Specifying Lexical Analyzer

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

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

Lexical Analysis - 1. A. Overview A.a) Role of Lexical Analyzer

Syntax Analysis Part I

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

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

Introduction to Lexical Analysis

Lecture 4: Syntax Specification

CS 4120 Introduction to Compilers

CSE302: Compiler Design

Outline. 1 Scanning Tokens. 2 Regular Expresssions. 3 Finite State Automata

Transcription:

COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou

Administrative! [ALSU03] Chapter 3 - Lexical Analysis Sections 3.1-3.4, 3.6-3.7! Reading for next time [ALSU03] Chapter 3 Copyright (c) 2010 Ioanna Dionysiou 2

Lecture Outline! Role of lexical analyzer Issues, tokens, patterns, lexemes, attributes! Input Buffering Buffer pairs, sentinel! Specification of tokens Strings, languages, regular expressions and definitions! Recognition of tokens Transition diagrams! Finite Automata NFA, DFA Copyright (c) 2010 Ioanna Dionysiou 3

Role of Lexical Analyzer Source Program Lexical Analyzer token get next token Syntactic Analyzer (parser). Symbol Table First phase of a compiler read input characters until it identifies the next token Copyright (c) 2010 Ioanna Dionysiou 4

Lexical Analyzer Phases! Sometimes, are divided into two phases Scanning Simple tasks Eliminating white spaces and comments Lexical analysis More complex tasks Copyright (c) 2010 Ioanna Dionysiou 5

Lexical and Syntax Analysis! Why separating lexical analysis from syntax analysis? Simple design is the most important consideration Low coupling, high cohesion Compiler efficiency is improved Compiler portability is enhanced Copyright (c) 2010 Ioanna Dionysiou 6

Tokens, patterns, lexemes pi is a lexeme for the token identifier id The pattern for token id matches the string pi The pattern for token id is a sequence of letters and\or digits, where the sequence always start with a letter Copyright (c) 2010 Ioanna Dionysiou 7

! Token Tokens, lexemes, patterns Terminals in the grammar for the source language! Lexeme Sequence of characters in the source program that is matched by the pattern for a token! Pattern Rule describing the set of lexemes that can represent a particular token in source programs Copyright (c) 2010 Ioanna Dionysiou 8

Attributes for tokens What happens when more than one lexemes is matched by a pattern? Lexeme 0 Lexeme 1 Pattern for token num matches both lexemes 0 and 1 Copyright (c) 2010 Ioanna Dionysiou 9

Attributes for tokens! It is essential for the code generator to know what string was actually matched Token Attributes Information about tokens A token has a single attribute Pointer to the symbol-table entry» <token, pointer> Lexeme and line number Question: Do all tokens need to have an entry in the symbol-table? Copyright (c) 2010 Ioanna Dionysiou 10

In-class Exercise if A < B Identify the tokens and their associated attribute-values Copyright (c) 2010 Ioanna Dionysiou 11

Solution if A < B <if,null > <id, pointer to symbol-table entry for A> <relation, pointer to symbol-table entry for < > <id, pointer to symbol-table entry for B> Copyright (c) 2010 Ioanna Dionysiou 12

! fi (0) Lexical Errors misspelling for the keyword if function identifier! There are cases where the error is clear None of the patterns for tokens matches the remaining input Error-recovery actions Examples? Copyright (c) 2010 Ioanna Dionysiou 13

Lecture Outline Copyright (c) 2010 Ioanna Dionysiou 14

Input Buffering Issues! Three approaches to the implementation of a lexical analyzer Use a lexical-analyzer generator Write a lexical analyzer in a systems programming language using the I/O provided Write a lexical analyzer in assembly and explicitly manage the reading of input Copyright (c) 2010 Ioanna Dionysiou 15

Buffering! Lexical analyzer may need to look ahead several characters beyond the lexeme for pattern before a match can be announced ungetc pushes lookahead characters back into the input stream Other buffering schemes to minimize the overhead Dividing a buffer into 2 N-character halves Load N characters into each buffer half using a single read command Use eof special character to signal the end of the source program Copyright (c) 2010 Ioanna Dionysiou 16

Lecture Outline Copyright (c) 2010 Ioanna Dionysiou 17

Specification of Tokens! Strings and languages Alphabet, character class Finite set of symbols {0,1} is the binary alphabet String, sentence, word.over some alphabet is a finite sequence of symbols drawn from that alphabet 0100001 is a string over the binary alphabet of length 7» 230001 is not a string over the binary alphabet Empty string ε Language Set of strings over fixed alphabet Copyright (c) 2010 Ioanna Dionysiou 18

More on strings! Suppose x, y are strings Concatenation of x and y x = school y = work xy = schoolwork x ε = ε x = x Exponentiation of x x 0 = ε x 1 = x x 2 = xx x i = x i-1 x Copyright (c) 2010 Ioanna Dionysiou 19

! Consider s = school What is. Prefix of s Suffix of s Substring of s Subsequence of s For every string More on strings both s and ε are prefixes, suffixes, and substrings of s Copyright (c) 2010 Ioanna Dionysiou 20

Operations on Languages! For lexical analysis, we are interested in the following: operations Union Concatenation Closure Exponentiation A new language is created by applying the operations on existing languages Copyright (c) 2010 Ioanna Dionysiou 21

Union Operation! Consider Languages L= {a,b}, M = {1,2} Union of L and M is written as L M L M = {s s is in L or s is in M} L M = {a,b,1,2} Copyright (c) 2010 Ioanna Dionysiou 22

Concatenation Operation! Consider Languages L= {a,b}, M = {1,2} Concatenation of L and M is written as LM L M = {st s is in L and t is in M} LM = {a1, a2, b1, b2} Copyright (c) 2010 Ioanna Dionysiou 23

Exponentiation Operation Consider Language L = {a,b} L 0 = {ε} L 1 = L = {a,b} L 2 = LL = {a,b}{a,b}={aa,ab,ba,bb} L i = L i-1 L Copyright (c) 2010 Ioanna Dionysiou 24

Kleene closure Operation! Consider Language L = {a,b} Kleene-closure of L is written as L* L* = L i with i=0 to (union of zero or more concatenations of L) L* = {ε,a,b,aa,ab,ba,bb, } L 0 = {ε} L 1 = {a,b} L 0 L 1 = {ε, a,b} L 2 = {a,b} {a,b} = {aa,ab,ba,bb} L 0 L 1 L 2 = {ε, a,b, aa,ab,ba,bb} Copyright (c) 2010 Ioanna Dionysiou 25

In-class Exercise! Consider L = {0,1,2} and M ={A,B}. Describe the language that is created from L and M when applying Union Concatenation (LM, ML) Kleene Closure (L) Copyright (c) 2010 Ioanna Dionysiou 26

Solution L M = {0,1,2,A,B} LM = {0A, 0B, 1A, 1B, 2A, 2B} ML = {A0, A1, A2, B0, B1, B2} L* = {ε,0,1,2,00,01,02,10,11, 12, 20, 21,22, } Copyright (c) 2010 Ioanna Dionysiou 27

! r is about Regular Expressions (r) notation patterns expression that describes a set of strings a precise description of a set Copyright (c) 2010 Ioanna Dionysiou 28

Regular Expressions Examples! Examples of r a b ab {a,b} {ab} a (ab) {a,ab} a(a b) a* {aa,ab} {ε,a,aa,aaa, } Copyright (c) 2010 Ioanna Dionysiou 29

r and L(r)! A regular expression is built up by simpler regular expressions using a set of rules! Each regular expression r denotes a language L(r) A language denoted by a regular expression is said to be a regular set Copyright (c) 2010 Ioanna Dionysiou 30

Rules that define r over alphabet Σ 1) ε is a regular expression that denotes {ε} - that is the set containing the empty string 2) If α is a symbol in Σ then α is a regular expression that denotes {α} - that is the set containing the string α Copyright (c) 2010 Ioanna Dionysiou 31

Rules that define r over alphabet Σ 3) Suppose that r and s are regular expressions denoting languages L(r) and L(s). Then, (r) (s) is a regular expression denoting L(r) L(s) (r)(s) is a regular expression denoting L(r)L(s) (r)* is a regular expression denoting (L(r))* (r) is a regular expression denoting L(r) Rules 1 and 2 form the basis of a recursive definition. Rule 3 provides the inductive step. Copyright (c) 2010 Ioanna Dionysiou 32

Conventions! The unary operator * has the highest precedence and is left associative! Concatenation has the second highest precedence and is left associative! has the lowest precedence and is left associative (a) ((b)*(c)) is equivalent to a b*c Copyright (c) 2010 Ioanna Dionysiou 33

! Let Σ = {a,b} a b denotes (a b) (a b) denotes a* denotes b* denotes (a b)* denotes (ab)* denotes In-class Exercise Copyright (c) 2010 Ioanna Dionysiou 34

Algebraic Properties of r AXIOM r s = s r r (s t) = (r s) t (rs)t = r(st) DESCRIPTION is commutative is associative concatenation is associative r(s t) = rs rt concatenation distributes over εr = r ε is the identity element of concatenation r* = (r ε)* relation between ε,* r** = r* * is idempotent Copyright (c) 2010 Ioanna Dionysiou 35

Regular Definitions! If Σ is an alphabet of basic symbols, then a regular definition is a sequence of definitions of the following form d 1 r 1 d 2 r 2 d i is a distinct name r 1 is a regular expression d n r n Copyright (c) 2010 Ioanna Dionysiou 36

Example! The set of Pascal identifiers is the set of strings of letters and digits beginning with a letter. A regular definition of this set is: letter A B Z a z digit 0 1 2 9 id letter(letter digit)* Copyright (c) 2010 Ioanna Dionysiou 37

In-class Exercise! Give the regular definition for Pascal real numbers. Examples of real numbers are 1.23 888.0 Copyright (c) 2010 Ioanna Dionysiou 38

Solution digit digits fraction real 0 1 9 digit digit*. digits digits fraction Copyright (c) 2010 Ioanna Dionysiou 39

Notational shorthand! Certain constructs occur frequently in regular expressions that is convenient to introduce shorthand One or more instances (operator +) a+ is the set of strings of one or more a s Zero or one instances (operator?) a? is the set of the empty string or one a Character classes ([ ]) [a-z] is the set that consists of a,b,,z [a-z]* is the set of the empty string or set consisting of a,b,.,z Copyright (c) 2010 Ioanna Dionysiou 40

Lecture Outline Copyright (c) 2010 Ioanna Dionysiou 41

Transition Diagrams! We considered the problem of how to specify tokens. Next question is How to recognize them? Transition diagrams Depict actions that take place when a lexical analyzer is called by the parser to the get the next token start o > 1 = 3 return(relop, GE) < 2 return(relop, LT) Copyright (c) 2010 Ioanna Dionysiou 42

In-class Exercise! Try to draw the transition diagrams for: Constants If Then Pi Identifiers Start with a letter, followed by a sequence of letters and digits Relational operators = <= Copyright (c) 2010 Ioanna Dionysiou 43

Lecture Outline Copyright (c) 2010 Ioanna Dionysiou 44

Finite Automate (FA)! Finite Automata Recognizer for a language Generalized transition diagram Takes as an input string x Returns Yes if x is a sentence of the language No otherwise! There are two types Nondeterministic finite automata (NFA) Deterministic finite automata (DFA) Copyright (c) 2010 Ioanna Dionysiou 45

Finite Automata! Both NFA and DFA recognize regular sets! Time-space tradeoff DFA is faster than NFA DFA can be bigger than NFA Copyright (c) 2010 Ioanna Dionysiou 46

Nondeterministic FA (NFA)! NFA is a model that consists of Set of states Input symbol alphabet Σ A transition function move that maps state-symbol pairs to sets of states A state s 0 that is distinguished as the start (or initial) state A set of states F distinguished as accepting (or final) states Copyright (c) 2010 Ioanna Dionysiou 47

NFA as a labeled directed graph STATE SYMBOL start o a 1 b 3 a b 0 {1,2} _ a 2 a 1 _ {3} States: 0,1,2,3 Initial state: 0 Final state: 3 Input alphabet: {a,b} 2 {3} _ Transition table for NFA Copyright (c) 2010 Ioanna Dionysiou 48

NFA! A NFA accepts an input string x iff there is some path in the graph from the initial to the some accepting state, such that the edge labels along the path spell out string x Path is a sequence of state transitions called moves Copyright (c) 2010 Ioanna Dionysiou 49

NFA start o a 1 b 3 a 2 a Moves for accepting string ab Moves for accepting string aa 0 a 1 b 3 0 a 2 a 3 Copyright (c) 2010 Ioanna Dionysiou 50

Another NFA start a o b b 1 b 2 3 States: 0,1,2,3 Initial state: 0 Final states: 1,3 Input alphabet: {a,b} a Transition table? What input strings does it accept? Copyright (c) 2010 Ioanna Dionysiou 51

Transition Table for NFA start a o b b 1 b 2 3 STATE SYMBOL a b 0 {0} {1,2} a 2 {2} {3} Copyright (c) 2010 Ioanna Dionysiou 52

Other NFAs start o ε ε 1 a 2 b 3 3 a start b o ε ε c 1 a 2 b 3 3 a b Copyright (c) 2010 Ioanna Dionysiou 53

Deterministic FA (DFA)! It is a special case of NFA in which No state has an ε-transition For each state s and input symbol a, there is at most one edge labeled a leaving s! In other words, there is at most one transition from each input on any input Each entry in the transition table is a single entry At most one path from the initial state labeled by that string Copyright (c) 2010 Ioanna Dionysiou 54

DFA STATE SYMBOL a b start o a 1 b 3 0 {1} {2} b 2 a 1 _ {3} 2 {3} _ Copyright (c) 2010 Ioanna Dionysiou 55

In-class Exercise! Construct an NFA that accepts (a b)*abb and draw the transition table! Can you construct a DFA that accepts the same string? Copyright (c) 2010 Ioanna Dionysiou 56

Solution! Solution in [ALSU07], page 148, 151 Copyright (c) 2010 Ioanna Dionysiou 57