COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.

Slides:



Advertisements
Similar presentations
4b Lexical analysis Finite Automata
Advertisements

CSC 361NFA vs. DFA1. CSC 361NFA vs. DFA2 NFAs vs. DFAs NFAs can be constructed from DFAs using transitions: Called NFA- Suppose M 1 accepts L 1, M 2 accepts.
Lecture 6 Nondeterministic Finite Automata (NFA)
CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
Lexical Analysis Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
Regular Expressions Finite State Automaton. Programming Languages2 Regular expressions  Terminology on Formal languages: –alphabet : a finite set of.
Lexical Analysis III Recognizing Tokens Lecture 4 CS 4318/5331 Apan Qasem Texas State University Spring 2015.
Courtesy Costas Busch - RPI1 Non Deterministic Automata.
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.
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Regular.
1 Single Final State for NFAs and DFAs. 2 Observation Any Finite Automaton (NFA or DFA) can be converted to an equivalent NFA with a single final state.
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
Fall 2006Costas Busch - RPI1 Non-Deterministic Finite Automata.
FSA Lecture 1 Finite State Machines. Creating a Automaton  Given a language L over an alphabet , design a deterministic finite automaton (DFA) M such.
1 Non-Deterministic Automata Regular Expressions.
1.Defs. a)Finite Automaton: A Finite Automaton ( FA ) has finite set of ‘states’ ( Q={q 0, q 1, q 2, ….. ) and its ‘control’ moves from state to state.
CS Chapter 2. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
Fall 2004COMP 3351 Another NFA Example. Fall 2004COMP 3352 Language accepted (redundant state)
Costas Busch - LSU1 Non-Deterministic Finite Automata.
1 A Single Final State for Finite Accepters. 2 Observation Any Finite Accepter (NFA or DFA) can be converted to an equivalent NFA with a single final.
CPSC 388 – Compiler Design and Construction
Regular Expressions (RE) Empty set Φ A RE denotes the empty set Empty string λ A RE denotes the set {λ} Symbol a A RE denotes the set {a} Alternation M.
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 Syntax Specification Regular Expressions. 2 Phases of Compilation.
Theory of Languages and Automata
Theory of Computation, Feodor F. Dragan, Kent State University 1 Regular expressions: definition An algebraic equivalent to finite automata. We can build.
어휘분석 (Lexical Analysis). Overview Main task: to read input characters and group them into “ tokens. ” Secondary tasks: –Skip comments and whitespace;
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
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.
1 Chapter 2 Finite Automata (part b) Windmills in Holland.
Lexical Analysis Constructing a Scanner from Regular Expressions.
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.
CS412/413 Introduction to Compilers Radu Rugina Lecture 4: Lexical Analyzers 28 Jan 02.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
CHAPTER 1 Regular Languages
Overview of Previous Lesson(s) Over View  Symbol tables are data structures that are used by compilers to hold information about source-program constructs.
CMSC 330: Organization of Programming Languages Theory of Regular Expressions Finite Automata.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
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.
Algorithms for hard problems Automata and tree automata Juris Viksna, 2015.
CS 154 Formal Languages and Computability February 11 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
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:
CSCI 2670 Introduction to Theory of Computing September 11, 2007.
Complexity and Computability Theory I Lecture #5 Rina Zviel-Girshin Leah Epstein Winter
1 Lexical Analysis Uses formalism of Regular Languages Uses formalism of Regular Languages Regular Expressions Regular Expressions Deterministic Finite.
Deterministic Finite Automata Nondeterministic Finite Automata.
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
COMP3190: Principle of Programming Languages DFA and its equivalent, scanner.
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
1/29/02CSE460 - MSU1 Nondeterminism-NFA Section 4.1 of Martin Textbook CSE460 – Computability & Formal Language Theory Comp. Science & Engineering Michigan.
Topic 3: Automata Theory 1. OutlineOutline Finite state machine, Regular expressions, DFA, NDFA, and their equivalence, Grammars and Chomsky hierarchy.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Finite automate.
Non Deterministic Automata
Some slides by Elsa L Gunter, NJIT, and by Costas Busch
Non-Deterministic Finite Automata
Non Deterministic Automata
Chapter 1 Regular Language
Lecture 5 Scanning.
Announcements - P1 part 1 due Today - P1 part 2 due on Friday Feb 1st
Lexical Analysis Uses formalism of Regular Languages
Presentation transcript:

COMP3190: Principle of Programming Languages DFA and its equivalent, scanner

- 1 - Outline v DFA & NFA »DFA »NFA »NFA →DFA »Minimize DFA v Regular expression v Regular languages v Scanner

- 2 - Example of DFA q1 q δ01 q1 q2 q1q2

- 3 - Deterministic Finite Automata (DFA) v 5-tuple: »Q: finite set of states »Σ: finite set of “letters” (alphabet) »δ: Q × Σ → Q (transition function) »q 0 : start state (in Q) »F : set of accept states (subset of Q) v Acceptance: Given an input string, it is consumed with the automata in a final state.

- 4 - Another Example of a DFA S q1 q2 r1 r2 a b a ab b b ab a

- 5 - Outline v DFA & NFA »DFA »NFA »NFA →DFA »Minimize DFA v Regular expression v Regular languages v Context free languages &PDA v Scanner v Parser

- 6 - Non-deterministic Finite Automata (NFA) Transition function is different v δ: Q × Σ ε → P(Q) v P(Q) is the powerset of Q (set of all subsets) v Σ ε is the union of Σ and the special symbol ε (denoting empty) String is accepted if there is at least one path leading to an accept state, and input consumed.

- 7 - Example of an NFA q1q2q3q4 0, 1 1 0, ε1 0, 1 δ01ε q1{q1}{q1, q2} q2{q3} q3{q4} q4{q4} What strings does this NFA accept?

- 8 - Outline v DFA & NFA »DFA »NFA »NFA →DFA »Minimize DFA v Regular expression v Regular languages v Context free languages &PDA v Scanner v Parser

- 9 - Converting an NFA to a DFA v For set of states S,  - closure(S) is the set of states that can be reached from S without consuming any input. v For a set of states S, S c is the set of states that can be reached from S by consuming input symbol c. v Each set of NFA states corresponds to one DFA state (hence at most 2 n states).

v  -closure({1})={1 , 2}=I v I a =  -closure({5,4,3}) v J={5 , 4 , 3}  -closure(J)=  -closure({5 , 4 , 3}) ={5 , 4 , 3 , 6 , 2 , 7 , 8} v J a ={3} 6 1 a  a a    

IIaIa IbIb {X,5,1}{5,3,1}{5,4,1} {5,3,1}{5,2,3,1,6,Y}{5,4,1} {5,3,1}{5,2,4,1,6,Y} {5,2,3,1,6,Y} {5,4,6,1,Y} {5,3,6,1,Y}{5,2,4,1,6,Y} {5,3,6,1,Y}{5,2,4,1,6,Y} {5,3,6,1,Y}{5,2,3,1,6,Y}{5,4,6,1,Y} X Y  a b   a b a b a b

Iab a ab b b a b a a b a b a b

Excercise 1 2 3start a a b a 4 65 ε ε ε a b b AB 4 6 a a C a,b b

Class Problem εε ε ε ε ε ε ε a a b 8 b Convert this NFA to a DFA

Outline v DFA & NFA »DFA »NFA »NFA →DFA »Minimize DFA v Regular expression v Regular languages v Scanner

State Minimization v Resulting DFA can be quite large »Contains redundant or equivalent states 2 5 b start 1 3 b a b a a 4 b a 123 aa b b Both DFAs accept b*ab*a C a,b C

Obtaining the minimal equivalent DFA v Initially two equivalence classes: accept and nonaccept states.  Search for an equivalence class C and an input letter a such that with a as input, the states in C make transitions to states in k>1 different equivalence classes. v Partition C into k classes accordingly v Repeat until unable to find a class to partition.

Minimization Example 18 Split into two teams. ACCEPT vs. NONACCEPT

Minimization Example 19 0-label doesn’t split up any teams

Minimization Example 20 1-label splits up NONACCEPT's

Minimization Example 21 No further splits. HALT! Start team contains original start

Minimization Example. End Result 22 States of the minimal automata are remaining teams. Edges are consolidated across each team. Accept states are break-offs from original ACCEPT team.

Minimization Example. Compare

a e b f c g d h Class Exercise

Exercise v How to minimize the following DFA? 2 5 b start 1 3 b a b a a 4 b a 123 aa b b Both DFAs accept b*ab*a

Outline v DFA & NFA v Regular expression v Regular languages v Scanner

Regular Expressions R is a regular expression if R is v “a” for some a in Σ. v ε (the empty string). v member of the empty language. v the union of two regular expressions. v the concatenation of two regular expr. v R 1 * (Kleene closure: zero or more repetitions of R 1 ).

Examples of Regular Expressions {0, 1}* 0 all strings that end in 0 {0, 1} 0* string that start with 1 or 0 followed by zero or more 0s. {0, 1}* all strings {0 n 1 n, n >=0} not a regular expression!!!

Regular Expressions in Java v Ex: pattern match. v Is text in the set described by the pattern? v public class RE { public static void main(String[] args) { String pattern = args[0]; String text = args[1]; System.out.println(text.matches(pattern)); } v % java RE "..oo..oo." bloodroot true v % java RE "[$_A-Za-z][$_A-Za-z0-9]*" ident123 true v % java RE true

Regular Expression Notation in Java v a: an ordinary letter v ε: the empty string v M | N: choosing from M or N v MN: concatenation of M and N v M*: zero or more times (Kleene star) v M + : one or more times v M?: zero or one occurence v [a-zA-Z] character set alternation (choice) v. period stands for any single char exc. newline

Converting a regular expression to a NFA v Empty string v Single character v union operator v Concatenation v Kleene closure

Regular expression→NFA Language: Strings of 0s and 1s in which the number of 0s is even Regular expression: (1*01*0)*1*

NFA → DFA Initial classes: {A, B, E}, {C, D} No class requires partitioning! Hence a two-state DFA is obtained.

Minimize DFA

Outline v DFA & NFA v Regular expression v Regular languages v Scanner

Regular language v a formal language »a set of finite sequences of symbols from a finite alphabet v it can be generated by a regular grammar

Regular Grammar v Later definitions build on earlier ones v Nothing defined in terms of itself (no recursion) Regular grammar for numeric literals in Pascal: digit → 0|1|2|...|8|9 unsigned_integer → digit digit* unsigned_number → unsigned_integer ((. unsigned_integer) | ε ) (( e (+ | - | ε ) unsigned_integer ) | ε )

Important Theorems v A language is regular if a regular expression describes it. v A language is regular if a finite automata recognizes it. v DFAs and NFAs are equally powerful.

Outline v DFA & NFA v Regular expression v Regular languages v Scanner

Scanning v Accept the longest possible token in each invocation of the scanner. v Implementation. »Capture finite automata.  Case(switch) statements.  Table and driver.

Scanner for Pascal

Scanner for Pascal(case Statements)

Scanner Generators v Start with a regular expression. v Construct an NFA from it. v Use a set of subsets construction to obtain an equivalent DFA. v Construct the minimal equivalent DFA.