Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "COMP3190: Principle of Programming Languages DFA and its equivalent, scanner."— Presentation transcript:

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

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

3 - 2 - Example of DFA q1 q2 1 0 01 δ01 q1 q2 q1q2

4 - 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.

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

6 - 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

7 - 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.

8 - 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?

9 - 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

10 - 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).

11 - 10 - 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  23 4 5 7 8 a a    

12 - 11 - 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  51 4 2 3 6 a b   a b a b a b

13 - 12 - Iab 012 132 214 334 465 565 634 0 1 2 3 5 4 6 a ab b b a b a a b a b a b

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

15 - 14 - Class Problem 01 4 2 6 3 5 9 7 εε ε ε ε ε ε ε a a b 8 b Convert this NFA to a DFA

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

17 - 16 - 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

18 - 17 - 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.

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

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

21 - 20 - Minimization Example 20 1-label splits up NONACCEPT's

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

23 - 22 - 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.

24 - 23 - Minimization Example. Compare 23 100100101 10000

25 - 24 - a e b f c g d h 0 0 0 0 0 0 0 0 1 1 11 1 1 1 1 Class Exercise

26 - 25 - 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

27 - 26 - Outline v DFA & NFA v Regular expression v Regular languages v Scanner

28 - 27 - 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 ).

29 - 28 - 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!!!

30 - 29 - 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 "[a-z]+@([a-z]+\.)+(edu|com)" rs@cs.princeton.edu true

31 - 30 - 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

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

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

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

35 - 34 - Minimize DFA

36 - 35 - Outline v DFA & NFA v Regular expression v Regular languages v Scanner

37 - 36 - 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

38 - 37 - 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 ) | ε )

39 - 38 - 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.

40 - 39 - Outline v DFA & NFA v Regular expression v Regular languages v Scanner

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

42 - 41 - Scanner for Pascal

43 - 42 - Scanner for Pascal(case Statements)

44 - 43 - 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.


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

Similar presentations


Ads by Google