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

Size: px
Start display at page:

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

Transcription

1 Formal Languages and Compilers Lecture IV: Regular Languages and Finite Automata Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: artale/ Formal Languages and Compilers BSc course 2017/18 First Semester

2 Summary of Lecture IV Regular Expressions (RE). Implementing a Recognizer of RE s: Automata. Deterministic Finite Automata (DFA). Nondeterministic Finite Automata (NFA). From Regular Expressions to NFA. ϵ-nfa: NFA with ϵ Transitions. From NFA to DFA.

3 Regular Expressions Each Regular Expression, say R, denotes a Language, L(R). The following are the rules to build them over an alphabet V: 1 If a V {ϵ} then a is a Regular Expression denoting the language {a}; 2 If R, S are Regular Expressions denoting the Languages L(R) and L(S) then: 1 R S is a Regular Expression denoting L(R) L(S); 2 R S is a Regular Expression denoting the concatenation L(R) L(S), i.e., L(R) L(S) = {r s r L(R) and s L(S)}; 3 R (Kleen closure) is a Regular Expression denoting L(R), zero or more concatenations of L(R), i.e., L(R) = i=0 L(R)i where L(R) 0 = {ϵ}; 4 (R) is a Regular Expression denoting L(R). Precedence of Operators: > > E F G = E (F (G ))

4 Regular Expressions: Examples Example. Let V = {a, b}. 1 The Regular Expression a b denotes the Language {a, b}. 2 The Regular Expression (a b)(a b) denotes the Language {aa, ab, ba, bb}. 3 The Regular Expression a denotes the Language of all strings of zero or more a s, {ϵ, a, aa, aaa,...}. 4 The Regular Expression (a b) denotes the Language of all strings of a s and b s.

5 Regular Expressions: Shorthands Notational shorthands are introduced for frequently used constructors. 1 +: One or more instances. If R is a Regular Expression then R + RR. 2?: Zero or one instance. If R is a Regular Expression then R? ϵ R. 3 Character Classes. If a, b,..., z V then [a, b, c] a b c, and [a z] a b... z.

6 Regular Definitions Regular Definitions are used to give names to regular Expressions and then to re-use these names to build new Regular Expressions. A Regular Definition is a sequence of definitions of the form: D 1 R 1 D 2 R 2... D n R n Where each D i is a distinct name and each R i is a Regular Expression over the extended alphabet V {D 1, D 2,..., D i 1 }. Note: Such names for Regular Expression will be often the Tokens returned by the Lexical Analyzer. As a convention, names are printed in boldface.

7 Regular Definitions: Examples Example 1. Identifiers are usually strings of letters and digits beginning with a letter: letter A B... Z a b... z digit id letter(letter digit) Using Character Classes we can define identifiers as: id [A Za z][a Za z0 9]

8 Regular Definitions: Examples (Cont.) Example 2. Numbers are usually strings such as 5230, 3.14, 6.45E4, 1.84E-4. digit digits digit + optional-fraction (.digits)? optional-exponent (E(+ )?digits)? num digits optional-fraction optional-exponent

9 Regular Expressions VS. Regular Grammars Languages captured by Regular Expressions could be captured by Regular Grammars (Type 3 Grammars). Regular Expressions are a notational variant of Regular Grammars: Usually they give a more compact representation. Example. The Regular Expression for numbers can be captured by a Regular Grammar with the following Productions (num is the scope and digit is a terminal symbol): num digit digit Z Z digit digit Z. Frac-Exp E Exp-Num Frac-Exp digit digit Frac-Exp digit Exp Exp E Exp-Num Exp-Num +Digits Digits digit digit Digits Digits digit digit Digits

10 Summary Regular Expressions. Implementing a Recognizer of RE s: Automata. Deterministic Finite Automata (DFA). Nondeterministic Finite Automata (NFA). From Regular Expressions to NFA. ϵ-nfa: NFA with ϵ Transitions. From NFA to DFA.

11 Finite Automata We need a mechanism to recognize Regular Expressions. While Regular Expressions are a specification language, Finite Automata are their implementation. Given an input string, x, and a Regular Language, L, they answer yes if x L and no otherwise.

12 Deterministic Finite Automata A Deterministic Finite Automata, DFA for short, is a tuple: A = (S, V, δ, s 0, F ): S is a finite non empty set of states; V is the input symbol alphabet; δ : S V S is a total function called the Transition Function; s 0 S is the initial state; F S is the set of final states.

13 Transition Graphs A DFA can be represented by Transition Graphs where the nodes are the states and each labeled edge represents the transition function. The initial state has an input arch marked start. Final states are indicated by double circles. Example. DFA that accepts strings in the Language L((a b) abb). b b start 0 a 1 b 2 b 3 a a a

14 Transition Table Transition Tables implement transition graphs, and thus Automata. A Transition Table has a row for each state and a column for each input symbol. The value of the cell (s i, a j ) is the state that can be reached from state s i with input a j. Example. The table implementing the previous transition graph will have 4 rows and 2 columns, let us call the table δ, then: δ(0, a) = 1 δ(0, b) = 0 δ(1, a) = 1 δ(1, b) = 2 δ(2, a) = 1 δ(2, b) = 3 δ(3, a) = 1 δ(3, b) = 0

15 Deterministic Finite Automata Vs. Languages To define when an Automaton accepts a string we extend the transition function, δ, to a multiple transition function ˆδ : S V S: ˆδ(s, ϵ) = s ˆδ(s, xa) = δ(ˆδ(s, x), a); x V, a V A DFA accepts an input string, w, if starting from the initial state with w as input the Automaton stops in a final state: ˆδ(s 0, w) = f, and f F. Language accepted by a DFA, A = (S, V, δ, s 0, F ): L(A) = {w V ˆδ(s 0, w) F }

16 Summary Regular Expressions. Implementing a Recognizer of RE s: Automata. Deterministic Finite Automata (DFA). Nondeterministic Finite Automata (NFA). From Regular Expressions to NFA. ϵ-nfa: NFA with ϵ Transitions. From NFA to DFA.

17 Nondeterministic Finite Automata A Finite Automaton is said Nondeterministic if we could have more than one transition with a given input symbol. A Nondeterministic Finite Automata, NFA, is a tuple: A = (S, V, δ, s 0, F ): S is a finite non empty set of states; V is the input symbol alphabet; δ : S V 2 S is a total function called the Transition Function; s 0 S is the initial state; F S is the set of final states. Note 1. Values in Transition Tables for NFA will be set of states. Note 2. δ(s, a) may be the empty set, i.e., the NFA makes no transition on that input.

18 NFA: An Example Given an input string and an NFA there will be, in general, more then one path that can be followed: An NFA accepts an input string if there is at least one path ending in a final state. Example. NFA that accepts strings in the Language L((a b) abb). b start a b b a Exercise. Check that δ(0, aaabb) is accepted by the above NFA. Given an input, w, we can represent the computation of a NFA as a tree of possible execution, and check for acceptance looking for at least one path that ends in a final state.

19 Nondeterministic Finite Automata (Cont.) To formally define when an NFA accepts a string we extend the transition function, δ, to the domain S V : ˆδ(s, ϵ) = {s} ˆδ(s, xa) = s i ˆδ(s,x) δ(s i, a); x V, a V An NFA accepts an input string, w, if starting from the initial state with w as input the Automaton reaches a final state: s.s ˆδ(s 0, w), and s F. Language accepted by a NFA, A = (S, V, δ, s 0, F ): L(A) = {w V ˆδ(s 0, w) F }

20 DFA Vs. NFA Both DFA and NFA are capable of recognizing all Regular Languages/Expressions: L(NFA) = L(DFA) The main difference is a Space Vs. Time tradeoff: DFA are faster than NFA; DFA are bigger (exponentially larger) than NFA.

21 Summary Regular Expressions. Implementing a Recognizer of RE s: Automata. Deterministic Finite Automata (DFA). Nondeterministic Finite Automata (NFA). From Regular Expressions to NFA. ϵ-nfa: NFA with ϵ Transitions. From NFA to DFA.

22 From Regular Expressions to NFA (1) The algorithm that generates an NFA for a given Regular Expression (RE) is guided by the syntactic structure of the RE. Given a RE, say r, the Thompson s construction generates an NFA accepting L(r). The Thompson s construction is a recursive procedure guided by the structure of the regular expression.

23 rom Regular Expressions to NFA (2) The NFA resulting from the Thompson s construction has important properties: 1 It is an ϵ-nfa: The automaton can make a transition without consuming an input symbol the automaton can non-deterministically change state; 2 It has exactly one final state; 3 No edge enters the start state; 4 No edge leaves the final state.

24 From Regular Expressions to NFA (3) Notation: if r is a RE then N(r) is its NFA with transition graph: N(r) Algorithm RE to NFA: Thompson s Construction 1 For ϵ, the NFA is start i ϵ f Where i is the new start state and f the new final state.

25 From Regular Expressions to NFA (4) 2 For a V, the NFA is start i a f Where i is the new start state and f the new final state. 3 Suppose now that N(s) and N(t) are NFA s then 1 For RE s t, the NFA is ϵ N(s) ϵ start i ϵ ϵ f N(t) Where i and f are the new starting and accepting states, respectively.

26 From Regular Expressions to NFA (5) 4 (Cont.) 2 For RE st, the NFA is start N(s) N(t) f Where the start state of N(s) is the start state of the new NFA, the final state of N(t) is the final state of the new NFA, the final state of N(s) is merged with the initial state of N(t). 3 For RE s, the NFA is ϵ start ϵ ϵ i N(s) f ϵ Where i is the new start state and f the new final state.

27 From Regular Expressions to NFA (6) 5 (Cont.) end. Remarks. 4 For RE (s) use the same NFA as N(s). Every time we insert new states we introduce a new name for them to maintain all the states distinct. Even if a symbol appears many times we construct a new NFA for each instance.

28 RE to NFA: An Example Exercise. Build the NFA for the RE (a b) abb.

29 Summary Regular Expressions. Implementing a Recognizer of RE s: Automata. Deterministic Finite Automata (DFA). Nondeterministic Finite Automata (NFA). From Regular Expressions to NFA. ϵ-nfa: NFA with ϵ Transitions. From NFA to DFA.

30 NFAs with ϵ-transitions We can allow state-to-state transitions on ϵ input. These transitions are done spontaneously, without looking at the input string. Useful to compose NFA s (as showed in the Thompson-construction). A convenience at times, but still only regular languages are accepted. Note: See also Prof. J. Ullman slides.

31 NFAs with ϵ-transitions (cont.) An ϵ-nfa is a tuple: A ϵ = (S, V, δ, s 0, F ), where S, V, s 0, F are as for an NFA, and: δ : S (V {ϵ}) 2 S

32 ϵ-closure Definition [ϵ-closure]. For s S, ϵ-closure(s) is the set of all states reachable from s using a sequence of ϵ-moves. Inductive Definition. Base. s ϵ-closure(s); Induction. If q ϵ-closure(s) and q δ(q, ϵ), then, q ϵ-closure(s). We can extend this notion to set of states, Q S: ϵ-closure(q) = ϵ-closure(q i ) q i Q

33 Extended Delta for ϵ-nfa We need to define ˆδ(s, w), for w V : Base: ˆδ(s, ϵ) = ϵ-closure(s). Induction: ˆδ(s, x a) = s i ˆδ(s,x) ϵ-closure(δ(s i, a)). Intuition: ˆδ(s, w) is the set of all states reachable from s along paths whose labels on arcs, apart from ϵ-lables, yield w. Note: The Language recognized by an ϵ-nfa is still defined in the same way as for NFA: L(A ϵ) = {w V ˆδ(s 0, w) F }

34 Equivalence of NFA and ϵ-nfa Every NFA is an ϵ-nfa: It just has no transitions on ϵ. Converse: It requires us to take an ϵ-nfa and construct an NFA that accepts the same language. See Lecture Notes by J. Ullman for transforming an ϵ-nfa into an NFA.

35 Summary Regular Expressions. Implementing a Recognizer of RE s: Automata. Deterministic Finite Automata (DFA). Nondeterministic Finite Automata (NFA). From Regular Expressions to NFA. ϵ-nfa: NFA with ϵ Transitions. From NFA to DFA.

36 From NFA to DFA NFA are hard to simulate with a computer program. 1 There are many possible paths for a given input string caused by the nondeterminism; 2 The acceptance condition says that there must be at least one path ending with a final state; 3 We may need to find all the paths before accepting/excluding a string (BackTracking). To map an NFA into an equivalent DFA we use the so called Subset Construction. Main Idea: Each DFA state corresponds to a set of NFA states, thus encoding all the possible states an NFA could reach after reading an input symbol.

37 From NFA to DFA: Subset Construction We define the Subset Construction for transforming an NFA to an equivalent DFA. Definition. [NFA to DFA] Let NFA = (S N, V, δ N, s 0, F N ) then the equivalent DFA = (S D, V, δ D, s 0, F D ) where: S D = 2 S N ; Note: with [s 1,..., s n] we denote an element in S D, which stands for the set of states {s 1,..., s n}. s 0 = [s 0]; F D is the set of states in S D containing at least one element from F N ; δ D ([s 1,..., s n], a) = [q 1,..., q m] iff {q 1,..., q m} = n i=1 δ N(s i, a).

38 ϵ-nfa to NFA to DFA: Exercise a 2 3 ϵ start ϵ 0 1 ϵ ϵ ϵ ϵ ϵ a b b b 4 5 ϵ

39 Summary of Lecture IV Regular Expressions. Implementing a Recognizer of RE s: Automata. Deterministic Finite Automata (DFA). Nondeterministic Finite Automata (NFA). From Regular Expressions to NFA. ϵ-nfa: NFA with ϵ Transitions. From NFA to DFA.

### Formal Languages and Compilers Lecture VI: Lexical Analysis

Formal Languages and Compilers Lecture VI: Lexical Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/ artale/ Formal

### COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

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

Last lecture CMSC330 Finite Automata Languages Sets of strings Operations on languages Regular expressions Constants Operators Precedence 1 2 Finite automata States Transitions Examples Types This lecture

### Lexical Analysis. Prof. James L. Frankel Harvard University

Lexical Analysis Prof. James L. Frankel Harvard University Version of 5:37 PM 30-Jan-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Regular Expression Notation We will develop a

### Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Formal Languages and Compilers Lecture VII Part 3: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

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

Lexical Analysis Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata Phase Ordering of Front-Ends Lexical analysis (lexer) Break input string

### Lexical Analysis. Implementation: Finite Automata

Lexical Analysis Implementation: Finite Automata Outline Specifying lexical structure using regular expressions Finite automata Deterministic Finite Automata (DFAs) Non-deterministic Finite Automata (NFAs)

### Zhizheng Zhang. Southeast University

Zhizheng Zhang Southeast University 2016/10/5 Lexical Analysis 1 1. The Role of Lexical Analyzer 2016/10/5 Lexical Analysis 2 2016/10/5 Lexical Analysis 3 Example. position = initial + rate * 60 2016/10/5

### Regular Languages and Regular Expressions

Regular Languages and Regular Expressions According to our definition, a language is regular if there exists a finite state automaton that accepts it. Therefore every regular language can be described

### Dr. D.M. Akbar Hussain

1 2 Compiler Construction F6S Lecture - 2 1 3 4 Compiler Construction F6S Lecture - 2 2 5 #include.. #include main() { char in; in = getch ( ); if ( isalpha (in) ) in = getch ( ); else error (); while

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

Concepts Introduced in Chapter 3 Lexical Analysis Regular Expressions (REs) Nondeterministic Finite Automata (NFA) Converting an RE to an NFA Deterministic Finite Automatic (DFA) Lexical Analysis Why separate

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

CSE45 Translation of Programming Languages Lecture 2: Automata and Regular Expressions Finite Automata Regular Expression = Specification Finite Automata = Implementation A finite automaton consists of:

### ECS 120 Lesson 7 Regular Expressions, Pt. 1

ECS 120 Lesson 7 Regular Expressions, Pt. 1 Oliver Kreylos Friday, April 13th, 2001 1 Outline Thus far, we have been discussing one way to specify a (regular) language: Giving a machine that reads a word

### CT32 COMPUTER NETWORKS DEC 2015

Q.2 a. Using the principle of mathematical induction, prove that (10 (2n-1) +1) is divisible by 11 for all n N (8) Let P(n): (10 (2n-1) +1) is divisible by 11 For n = 1, the given expression becomes (10

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

Regular expressions: a regular expression is built up out of simpler regular expressions using a set of defining rules. Regular expressions allows us to define tokens of programming languages such as identifiers.

### Lexical Analysis - 2

Lexical Analysis - 2 More regular expressions Finite Automata NFAs and DFAs Scanners JLex - a scanner generator 1 Regular Expressions in JLex Symbol - Meaning. Matches a single character (not newline)

### Implementation of Lexical Analysis. Lecture 4

Implementation of Lexical Analysis Lecture 4 1 Tips on Building Large Systems KISS (Keep It Simple, Stupid!) Don t optimize prematurely Design systems that can be tested It is easier to modify a working

### Lexical Analysis. Lecture 3-4

Lexical Analysis Lecture 3-4 Notes by G. Necula, with additions by P. Hilfinger Prof. Hilfinger CS 164 Lecture 3-4 1 Administrivia I suggest you start looking at Python (see link on class home page). Please

### Front End: Lexical Analysis. The Structure of a Compiler

Front End: Lexical Analysis The Structure of a Compiler Constructing a Lexical Analyser By hand: Identify lexemes in input and return tokens Automatically: Lexical-Analyser generator We will learn about

### Implementation of Lexical Analysis

Implementation of Lexical Analysis Lecture 4 (Modified by Professor Vijay Ganesh) Tips on Building Large Systems KISS (Keep It Simple, Stupid!) Don t optimize prematurely Design systems that can be tested

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

2010: Compilers Lexical Analysis: Finite State Automata Dr. Licia Capra UCL/CS REVIEW: REGULAR EXPRESSIONS a Character in A Empty string R S Alternation (either R or S) RS Concatenation (R followed by

### Implementation of Lexical Analysis

Implementation of Lexical Analysis Outline Specifying lexical structure using regular expressions Finite automata Deterministic Finite Automata (DFAs) Non-deterministic Finite Automata (NFAs) Implementation

### (Refer Slide Time: 0:19)

Theory of Computation. Professor somenath Biswas. Department of Computer Science & Engineering. Indian Institute of Technology, Kanpur. Lecture-15. Decision Problems for Regular Languages. (Refer Slide

### Compiler Construction

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Conceptual Structure of a Compiler Source code x1 := y2

### UNIT -2 LEXICAL ANALYSIS

OVER VIEW OF LEXICAL ANALYSIS UNIT -2 LEXICAL ANALYSIS o To identify the tokens we need some method of describing the possible tokens that can appear in the input stream. For this purpose we introduce

### Automating Construction of Lexers

Automating Construction of Lexers Regular Expression to Programs Not all regular expressions are simple. How can we write a lexer for (a*b aaa)? Tokenizing aaaab Vs aaaaaa Regular Expression Finite state

### Regular Languages. MACM 300 Formal Languages and Automata. Formal Languages: Recap. Regular Languages

Regular Languages MACM 3 Formal Languages and Automata Anoop Sarkar http://www.cs.sfu.ca/~anoop The set of regular languages: each element is a regular language Each regular language is an example of a

### CS308 Compiler Principles Lexical Analyzer Li Jiang

CS308 Lexical Analyzer Li Jiang Department of Computer Science and Engineering Shanghai Jiao Tong University Content: Outline Basic concepts: pattern, lexeme, and token. Operations on languages, and regular

### MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT 6.035 Specifying Languages with Regular essions and Context-Free Grammars Martin Rinard Massachusetts Institute of Technology Language Definition Problem How to precisely define language Layered structure

### Lexical Analysis. Lecture 2-4

Lexical Analysis Lecture 2-4 Notes by G. Necula, with additions by P. Hilfinger Prof. Hilfinger CS 164 Lecture 2 1 Administrivia Moving to 60 Evans on Wednesday HW1 available Pyth manual available on line.

### Languages and Compilers

Principles of Software Engineering and Operational Systems Languages and Compilers SDAGE: Level I 2012-13 3. Formal Languages, Grammars and Automata Dr Valery Adzhiev vadzhiev@bournemouth.ac.uk Office:

Nondeterministic Finite Automata (NFA): Nondeterministic Finite Automata (NFA) states of an automaton of this kind may or may not have a transition for each symbol in the alphabet, or can even have multiple

### Lexical Analysis. Chapter 2

Lexical Analysis Chapter 2 1 Outline Informal sketch of lexical analysis Identifies tokens in input string Issues in lexical analysis Lookahead Ambiguities Specifying lexers Regular expressions Examples

### MIT Specifying Languages with Regular Expressions and Context-Free Grammars

MIT 6.035 Specifying Languages with Regular essions and Context-Free Grammars Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Language Definition Problem How to precisely

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 2: Syntax Analysis Zheng (Eddy) Zhang Rutgers University January 22, 2018 Announcement First recitation starts this Wednesday Homework 1 will be release

### Figure 2.1: Role of Lexical Analyzer

Chapter 2 Lexical Analysis Lexical analysis or scanning is the process which reads the stream of characters making up the source program from left-to-right and groups them into tokens. The lexical analyzer

### Implementation of Lexical Analysis

Implementation of Lexical Analysis Outline Specifying lexical structure using regular expressions Finite automata Deterministic Finite Automata (DFAs) Non-deterministic Finite Automata (NFAs) Implementation

### CSE450. Translation of Programming Languages. Automata, Simple Language Design Principles

CSE45 Translation of Programming Languages Automata, Simple Language Design Principles Finite Automata State Graphs A state: The start state: An accepting state: A transition: a A Simple Example A finite

### Formal Languages and Automata

Mobile Computing and Software Engineering p. 1/3 Formal Languages and Automata Chapter 3 Regular languages and Regular Grammars Chuan-Ming Liu cmliu@csie.ntut.edu.tw Department of Computer Science and

### Chapter 3 Lexical Analysis

Chapter 3 Lexical Analysis Outline Role of lexical analyzer Specification of tokens Recognition of tokens Lexical analyzer generator Finite automata Design of lexical analyzer generator The role of lexical

### CS 314 Principles of Programming Languages. Lecture 3

CS 314 Principles of Programming Languages Lecture 3 Zheng Zhang Department of Computer Science Rutgers University Wednesday 14 th September, 2016 Zheng Zhang 1 CS@Rutgers University Class Information

### CS5371 Theory of Computation. Lecture 8: Automata Theory VI (PDA, PDA = CFG)

CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG) Objectives Introduce Pushdown Automaton (PDA) Show that PDA = CFG In terms of descriptive power Pushdown Automaton (PDA) Roughly

### CS 310: State Transition Diagrams

CS 30: State Transition Diagrams Stefan D. Bruda Winter 207 STATE TRANSITION DIAGRAMS Finite directed graph Edges (transitions) labeled with symbols from an alphabet Nodes (states) labeled only for convenience

### Compiler Construction

Compiler Construction Lecture 2: Lexical Analysis I (Introduction) Thomas Noll Lehrstuhl für Informatik 2 (Software Modeling and Verification) noll@cs.rwth-aachen.de http://moves.rwth-aachen.de/teaching/ss-14/cc14/

### Compiler course. Chapter 3 Lexical Analysis

Compiler course Chapter 3 Lexical Analysis 1 A. A. Pourhaji Kazem, Spring 2009 Outline Role of lexical analyzer Specification of tokens Recognition of tokens Lexical analyzer generator Finite automata

### CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION Spring 2017 http://cseweb.ucsd.edu/classes/sp17/cse105-ab/ Today's learning goals Sipser Ch 1.2, 1.3 Design NFA recognizing a given language Convert an NFA (with or without

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

CS 1622 Lecture 2 Lexical Analysis CS 1622 Lecture 2 1 Lecture 2 Review of last lecture and finish up overview The first compiler phase: lexical analysis Reading: Chapter 2 in text (by 1/18) CS 1622 Lecture

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

CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2] 1 What is Lexical Analysis? First step of a compiler. Reads/scans/identify the characters in the program and groups

### Lexical Analyzer Scanner

Lexical Analyzer Scanner ASU Textbook Chapter 3.1, 3.3, 3.4, 3.6, 3.7, 3.5 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 Main tasks Read the input characters and produce

### 6 NFA and Regular Expressions

Formal Language and Automata Theory: CS21004 6 NFA and Regular Expressions 6.1 Nondeterministic Finite Automata A nondeterministic finite automata (NFA) is a 5-tuple where 1. is a finite set of states

### CS415 Compilers. Lexical Analysis

CS415 Compilers Lexical Analysis These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Lecture 7 1 Announcements First project and second homework

### CS2 Language Processing note 3

CS2 Language Processing note 3 CS2Ah 5..4 CS2 Language Processing note 3 Nondeterministic finite automata In this lecture we look at nondeterministic finite automata and prove the Conversion Theorem, which

### Finite automata. We have looked at using Lex to build a scanner on the basis of regular expressions.

Finite automata We have looked at using Lex to build a scanner on the basis of regular expressions. Now we begin to consider the results from automata theory that make Lex possible. Recall: An alphabet

### Lexical Analyzer Scanner

Lexical Analyzer Scanner ASU Textbook Chapter 3.1, 3.3, 3.4, 3.6, 3.7, 3.5 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 Main tasks Read the input characters and produce

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

Outline 1 2 Regular Expresssions Lexical Analysis 3 Finite State Automata 4 Non-deterministic (NFA) Versus Deterministic Finite State Automata (DFA) 5 Regular Expresssions to NFA 6 NFA to DFA 7 8 JavaCC:

### lec3:nondeterministic finite state automata

lec3:nondeterministic finite state automata 1 1.introduction Nondeterminism is a useful concept that has great impact on the theory of computation. When the machine is in a given state and reads the next

### 2. Lexical Analysis! Prof. O. Nierstrasz!

2. Lexical Analysis! Prof. O. Nierstrasz! Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.! http://www.cs.ucla.edu/~palsberg/! http://www.cs.purdue.edu/homes/hosking/!

### Lexical Analysis 1 / 52

Lexical Analysis 1 / 52 Outline 1 Scanning Tokens 2 Regular Expresssions 3 Finite State Automata 4 Non-deterministic (NFA) Versus Deterministic Finite State Automata (DFA) 5 Regular Expresssions to NFA

### Lexical Analysis/Scanning

Compiler Design 1 Lexical Analysis/Scanning Compiler Design 2 Input and Output The input is a stream of characters (ASCII codes) of the source program. The output is a stream of tokens or symbols corresponding

### Compiler Construction I

TECHNISCHE UNIVERSITÄT MÜNCHEN FAKULTÄT FÜR INFORMATIK Compiler Construction I Dr. Michael Petter SoSe 2015 1 / 58 Organizing Master or Bachelor in the 6th Semester with 5 ECTS Prerequisites Informatik

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

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres dgriol@inf.uc3m.es Introduction: Definitions Lexical analysis or scanning: To read from left-to-right a source

### Lexical Analysis. Sukree Sinthupinyo July Chulalongkorn University

Sukree Sinthupinyo 1 1 Department of Computer Engineering Chulalongkorn University 14 July 2012 Outline Introduction 1 Introduction 2 3 4 Transition Diagrams Learning Objectives Understand definition of

### DVA337 HT17 - LECTURE 4. Languages and regular expressions

DVA337 HT17 - LECTURE 4 Languages and regular expressions 1 SO FAR 2 TODAY Formal definition of languages in terms of strings Operations on strings and languages Definition of regular expressions Meaning

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

Finite Automata Dr. Nadeem Akhtar Assistant Professor Department of Computer Science & IT The Islamia University of Bahawalpur PhD Laboratory IRISA-UBS University of South Brittany European University

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

David Griol Barres dgriol@inf.uc3m.es Computer Science Department Carlos III University of Madrid Leganés (Spain) OUTLINE Introduction: Definitions The role of the Lexical Analyzer Scanner Implementation

### Lexical Analysis (ASU Ch 3, Fig 3.1)

Lexical Analysis (ASU Ch 3, Fig 3.1) Implementation by hand automatically ((F)Lex) Lex generates a finite automaton recogniser uses regular expressions Tasks remove white space (ws) display source program

### CMPSCI 250: Introduction to Computation. Lecture 20: Deterministic and Nondeterministic Finite Automata David Mix Barrington 16 April 2013

CMPSCI 250: Introduction to Computation Lecture 20: Deterministic and Nondeterministic Finite Automata David Mix Barrington 16 April 2013 Deterministic and Nondeterministic Finite Automata Deterministic

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

Chapter 4 Lexical analysis Lexical scanning Regular expressions DFAs and FSAs Lex Concepts CMSC 331, Some material 1998 by Addison Wesley Longman, Inc. 1 CMSC 331, Some material 1998 by Addison Wesley

### UNION-FREE DECOMPOSITION OF REGULAR LANGUAGES

UNION-FREE DECOMPOSITION OF REGULAR LANGUAGES Thesis submitted in partial fulfillment of the requirements for the award of degree of Master of Engineering in Computer Science and Engineering Submitted

### CS402 Theory of Automata Solved Subjective From Midterm Papers. MIDTERM SPRING 2012 CS402 Theory of Automata

Solved Subjective From Midterm Papers Dec 07,2012 MC100401285 Moaaz.pk@gmail.com Mc100401285@gmail.com PSMD01 MIDTERM SPRING 2012 Q. Point of Kleen Theory. Answer:- (Page 25) 1. If a language can be accepted

### J. Xue. Tutorials. Tutorials to start in week 3 (i.e., next week) Tutorial questions are already available on-line

Tutorials Tutorials to start in week 3 (i.e., next week) Tutorial questions are already available on-line COMP3131/9102 Page 65 March 4, 2018 Assignment 1: Scanner +5 = two tokens: + and 5 the scanner

### Formal Languages. Formal Languages

Regular expressions Formal Languages Finite state automata Deterministic Non-deterministic Review of BNF Introduction to Grammars Regular grammars Formal Languages, CS34 Fall2 BGRyder Formal Languages

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

Lexical Analysis Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast! Compiler Passes Analysis of input program (front-end) character stream

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

CSEP 501 Compilers Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter 2008 1/8/2008 2002-08 Hal Perkins & UW CSE B-1 Agenda Basic concepts of formal grammars (review) Regular expressions

### Lexical Analysis. Introduction

Lexical Analysis Introduction Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission to make copies

### Introduction to Lexical Analysis

Introduction to Lexical Analysis Outline Informal sketch of lexical analysis Identifies tokens in input string Issues in lexical analysis Lookahead Ambiguities Specifying lexical analyzers (lexers) Regular

### Compiler Design. 2. Regular Expressions & Finite State Automata (FSA) Kanat Bolazar January 21, 2010

Compiler Design. Regular Expressions & Finite State Automata (FSA) Kanat Bolazar January 1, 010 Contents In these slides we will see 1.Introduction, Concepts and Notations.Regular Expressions, Regular

### Compiler Construction I

TECHNISCHE UNIVERSITÄT MÜNCHEN FAKULTÄT FÜR INFORMATIK Compiler Construction I Dr. Michael Petter, Dr. Axel Simon SoSe 2014 1 / 59 Organizing Master or Bachelor in the 6th Semester with 5 ECTS Prerequisites

### CSE 431S Scanning. Washington University Spring 2013

CSE 431S Scanning Washington University Spring 2013 Regular Languages Three ways to describe regular languages FSA Right-linear grammars Regular expressions Regular Expressions A regular expression is

### Administrivia. Lexical Analysis. Lecture 2-4. Outline. The Structure of a Compiler. Informal sketch of lexical analysis. Issues in lexical analysis

dministrivia Lexical nalysis Lecture 2-4 Notes by G. Necula, with additions by P. Hilfinger Moving to 6 Evans on Wednesday HW available Pyth manual available on line. Please log into your account and electronically

### Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Formal Languages and Compilers Lecture VII Part 4: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

### Structure of Programming Languages Lecture 3

Structure of Programming Languages Lecture 3 CSCI 6636 4536 Spring 2017 CSCI 6636 4536 Lecture 3... 1/25 Spring 2017 1 / 25 Outline 1 Finite Languages Deterministic Finite State Machines Lexical Analysis

### COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview n Tokens and regular expressions n Syntax and context-free grammars n Grammar derivations n More about parse trees n Top-down and

### Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Grammars Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

### UNIT II LEXICAL ANALYSIS

UNIT II LEXICAL ANALYSIS 2 Marks 1. What are the issues in lexical analysis? Simpler design Compiler efficiency is improved Compiler portability is enhanced. 2. Define patterns/lexeme/tokens? This set

### ECS 120 Lesson 16 Turing Machines, Pt. 2

ECS 120 Lesson 16 Turing Machines, Pt. 2 Oliver Kreylos Friday, May 4th, 2001 In the last lesson, we looked at Turing Machines, their differences to finite state machines and pushdown automata, and their

### Chapter Seven: Regular Expressions

Chapter Seven: Regular Expressions Regular Expressions We have seen that DFAs and NFAs have equal definitional power. It turns out that regular expressions also have exactly that same definitional power:

### [Lexical Analysis] Bikash Balami

1 [Lexical Analysis] Compiler Design and Construction (CSc 352) Compiled By Central Department of Computer Science and Information Technology (CDCSIT) Tribhuvan University, Kirtipur Kathmandu, Nepal 2

### Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2016 Lecture 15 Ana Bove May 23rd 2016 More on Turing machines; Summary of the course. Overview of today s lecture: Recap: PDA, TM Push-down

### 8 ε. Figure 1: An NFA-ǫ

0 1 2 3 4 a 6 5 7 8 9 10 LECTURE 27 Figure 1: An FA-ǫ 12.1 ǫ Transitions In all automata that we have seen so far, every time that it has to change from one state to another, it must use one input symol.

### Lecture 4: Syntax Specification

The University of North Carolina at Chapel Hill Spring 2002 Lecture 4: Syntax Specification Jan 16 1 Phases of Compilation 2 1 Syntax Analysis Syntax: Webster s definition: 1 a : the way in which linguistic

### COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview Tokens and regular expressions Syntax and context-free grammars Grammar derivations More about parse trees Top-down and bottom-up

### 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 Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

### HKN CS 374 Midterm 1 Review. Tim Klem Noah Mathes Mahir Morshed

HKN CS 374 Midterm 1 Review Tim Klem Noah Mathes Mahir Morshed Midterm topics It s all about recognizing sets of strings! 1. String Induction 2. Regular languages a. DFA b. NFA c. Regular expressions 3.

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

CS412/413 Introduction to Compilers Tim Teitelbaum Lecture 2: Lexical Analysis 23 Jan 08 Outline Review compiler structure What is lexical analysis? Writing a lexer Specifying tokens: regular expressions

### Compiler Construction

Compiler Construction Exercises 1 Review of some Topics in Formal Languages 1. (a) Prove that two words x, y commute (i.e., satisfy xy = yx) if and only if there exists a word w such that x = w m, y =

### Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov 2014 Compiler Design (170701) Question Bank / Assignment Unit 1: INTRODUCTION TO COMPILING

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

Concepts Lexical scanning Regular expressions DFAs and FSAs Lex CMSC 331, Some material 1998 by Addison Wesley Longman, Inc. 1 CMSC 331, Some material 1998 by Addison Wesley Longman, Inc. 2 Lexical analysis