FABER Formal Languages, Automata and Models of Computation

Similar documents
CS415 Compilers. Lexical Analysis and. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Lecture 6 Regular Grammars

Normal Forms for Context-free Grammars

Formal languages, automata, and theory of computation

AUTOMATA AND LANGUAGES. Definition 1.5: Finite Automaton

Formal Languages Simplifications of CFGs

CMSC 330: Organization of Programming Languages

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. NFA for (a b)*abb.

Types of Finite Automata. CMSC 330: Organization of Programming Languages. Comparing DFAs and NFAs. Comparing DFAs and NFAs (cont.) Finite Automata 2

Closure Properties of Regular Languages

Finite Automata. Informatics 2A: Lecture 3. John Longley. 22 September School of Informatics University of Edinburgh

Lexical Analysis Finite Automate

Assignment 1 Automata, Languages, and Computability. 1 Finite State Automata and Regular Languages

Minimal DFA. minimal DFA for L starting from any other

Theory of Computation Regular Languages. (NTU EE) Regular Languages Fall / 38

1.4 Nonregular Languages

Java II Finite Automata I

Compiler Design. Fall Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Theory of Computation Regular Languages

NFA DFA Example 3 CMSC 330: Organization of Programming Languages. Equivalence of DFAs and NFAs. Equivalence of DFAs and NFAs (cont.

Table of contents: Lecture N Summary... 3 What does automata mean?... 3 Introduction to languages... 3 Alphabets... 3 Strings...

Homework 3 Solutions

Regular expressions, Finite Automata, transition graphs are all the same!!

Non-Deterministic Finite Automata. Fall 2018 Costas Busch - RPI 1

Thoery of Automata CS402

Lexical Analysis Part III

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Convert the NFA into DFA

Context-Free Grammars and Languages

Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Regular Expressions (RE) Kleene-*

State Minimization for DFAs

CSCI 340: Computational Models. Kleene s Theorem. Department of Computer Science

CMSC 330: Organization of Programming Languages. DFAs, and NFAs, and Regexps (Oh my!)

CS 314 Principles of Programming Languages

Chapter Five: Nondeterministic Finite Automata. Formal Language, chapter 5, slide 1

CS 301. Lecture 04 Regular Expressions. Stephen Checkoway. January 29, 2018

Lecture 08: Feb. 08, 2019

Anatomy of a Deterministic Finite Automaton. Deterministic Finite Automata. A machine so simple that you can understand it in less than one minute

CS 275 Automata and Formal Language Theory

First Midterm Examination

Chapter 2 Finite Automata

Finite Automata. Informatics 2A: Lecture 3. Mary Cryan. 21 September School of Informatics University of Edinburgh

Overview HC9. Parsing: Top-Down & LL(1) Context-Free Grammars (1) Introduction. CFGs (3) Context-Free Grammars (2) Vertalerbouw HC 9: Ch.

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, SPRING SEMESTER LANGUAGES AND COMPUTATION ANSWERS

Nondeterminism and Nodeterministic Automata

Worked out examples Finite Automata

Deterministic Finite Automata

Non-deterministic Finite Automata

NFAs continued, Closure Properties of Regular Languages

Scanner. Specifying patterns. Specifying patterns. Operations on languages. A scanner must recognize the units of syntax Some parts are easy:

Grammar. Languages. Content 5/10/16. Automata and Languages. Regular Languages. Regular Languages

Automata Theory 101. Introduction. Outline. Introduction Finite Automata Regular Expressions ω-automata. Ralf Huuck.

Review for the Midterm

CS 373, Spring Solutions to Mock midterm 1 (Based on first midterm in CS 273, Fall 2008.)

First Midterm Examination

Speech Recognition Lecture 2: Finite Automata and Finite-State Transducers

Non Deterministic Automata. Linz: Nondeterministic Finite Accepters, page 51

2. Lexical Analysis. Oscar Nierstrasz

Talen en Automaten Test 1, Mon 7 th Dec, h45 17h30

Harvard University Computer Science 121 Midterm October 23, 2012

More on automata. Michael George. March 24 April 7, 2014

Formal Languages and Automata

Chapter 4 Regular Grammar and Regular Sets. (Solutions / Hints)

Finite Automata-cont d

GNFA GNFA GNFA GNFA GNFA

CMPSCI 250: Introduction to Computation. Lecture #31: What DFA s Can and Can t Do David Mix Barrington 9 April 2014

a,b a 1 a 2 a 3 a,b 1 a,b a,b 2 3 a,b a,b a 2 a,b CS Determinisitic Finite Automata 1

CS:4330 Theory of Computation Spring Regular Languages. Equivalences between Finite automata and REs. Haniel Barbosa

Nondeterministic Automata vs Deterministic Automata

Speech Recognition Lecture 2: Finite Automata and Finite-State Transducers. Mehryar Mohri Courant Institute and Google Research

CSC 311 Theory of Computation

1 Nondeterministic Finite Automata

CHAPTER 1 Regular Languages. Contents

1.3 Regular Expressions

DFA Minimization and Applications

NFAs and Regular Expressions. NFA-ε, continued. Recall. Last class: Today: Fun:

Designing finite automata II

Non-deterministic Finite Automata

Tutorial Automata and formal Languages

Lecture 09: Myhill-Nerode Theorem

3 Regular expressions

CS311 Computational Structures Regular Languages and Regular Grammars. Lecture 6

Formal Language and Automata Theory (CS21004)

Homework 4. 0 ε 0. (00) ε 0 ε 0 (00) (11) CS 341: Foundations of Computer Science II Prof. Marvin Nakayama

5. (±±) Λ = fw j w is string of even lengthg [ 00 = f11,00g 7. (11 [ 00)± Λ = fw j w egins with either 11 or 00g 8. (0 [ ffl)1 Λ = 01 Λ [ 1 Λ 9.

CISC 4090 Theory of Computation

NFAs continued, Closure Properties of Regular Languages

CS 330 Formal Methods and Models

I. Theory of Automata II. Theory of Formal Languages III. Theory of Turing Machines

Some Theory of Computation Exercises Week 1

Agenda. Agenda. Regular Expressions. Examples of Regular Expressions. Regular Expressions (crash course) Computational Linguistics 1

input tape head moves current state

CSE : Exam 3-ANSWERS, Spring 2011 Time: 50 minutes

Revision Sheet. (a) Give a regular expression for each of the following languages:

NON-DETERMINISTIC FSA

Closure Properties of Regular Languages

CS 275 Automata and Formal Language Theory

Let's start with an example:

Fundamentals of Computer Science

CSC 473 Automata, Grammars & Languages 11/9/10

Transcription:

DVA337 FABER Forml Lnguges, Automt nd Models of Computtion Lecture 5 chool of Innovtion, Design nd Engineering Mälrdlen University 2015 1

Recp of lecture 4 y definition suset construction DFA NFA stte elimintion RE Thompson s construction (suset construction) nf-to-df (stte elimintion) nf-to-re (Thompson) re-to-nf implied: df-to-re(x) = nf-to-re(df) re-to-df(x) = nf-to-df(re-to-nf(x)) 2

Grmmrs Liner grmmrs right liner => NFA NFA => right liner Tody left liner => right liner Regulr grmmr left liner or right liner defines regulr lnguges DFA minimiztion stte reduction vi set prtitioning If time lexicl nlysis revisit 3

Grmmrs 4

Grmmrs Grmmrs express lnguges Exmple: the English lnguge sentence noun _ phrse predicte noun _ phrse rticle noun predicte ver 5

rticle rticle the noun noun ird dog ver ver sings rks 6

A derivtion of the ird sings : sentence noun _ phrse predicte rticle noun predicte rticle noun ver the noun ver the ird ver the ird sings 7

A derivtion of dog rks : sentence noun _ phrse predicte noun _ phrse ver rticle noun ver noun ver dog ver dog rks 8

The lnguge of the grmmr: L = {" " " " " " " " ird rks ", ird sings", the ird rks ", the ird sings", dog rks ", dog sings", the dog rks ", the dog sings" } } 9

Nottion noun noun ird dog Non-terminl (Vrile) Production rule Terminl 10

Exmple Grmmr: λ Derivtion of sentence: λ 11

Grmmr: λ Derivtion of sentence λ 12

Other derivtions 13

The lnguge of the grmmr λ L n n = { : n 0} 14

Forml Definition Grmmr G = ( V, T,, P) V T P : : : : et of vriles/non-terminls et of terminl symols trt symol et of production rules 15

Exmple Grmmr G λ G = ( V, T,, P) V = {} T = {, } P = {, λ} 16

ententil Form A sentence tht contins vriles nd terminls Exmple sententil forms entence 17

We write: * Insted of: 18

In generl we write w * 1 w n w 1 if w2 w3! w n By defult w * w ( ) 19

Exmple Grmmr Derivtions * λ λ * * * 20

Grmmr Exmple λ Derivtions * * 21

Another Grmmr Exmple Grmmr G Derivtions A A A A λ A A A A A 22

More Derivtions A A A A A n n 23

The Lnguge of Grmmr For grmmr G with strt vrile L( G) = { w: w} tring of terminls 24

For grmmr G Exmple A A A A λ n L( G) = { : n n 0} ince n n 25

Nottion A A λ A A A λ rticle rticle the rticle the 26

Liner Grmmrs 27

Liner Grmmrs Grmmrs with t most one vrile (non-terminl) t the right side of production Exmples: A λ A A A λ 28

A Non-Liner Grmmr Grmmr G λ L( G) = { w: n ( w) n ( w)} = 29

A Liner Grmmr Grmmr G A A B λ B A n n L( G) = { : n 0} 30

Right-Liner Grmmrs All productions hve form: A or xb A x Exmple 31

Left-Liner Grmmrs All productions hve form A or Bx A x Exmple A A A B B 32

Regulr Grmmrs A grmmr is regulr grmmr if nd only if it is right-liner or left-liner. 33

Regulr Grmmrs Generte Regulr Lnguges 34

Theorem Lnguges Generted y Regulr Grmmrs = Regulr Lnguges 35

Theorem - Prt 1 Lnguges Generted y Regulr Grmmrs Regulr Lnguges Any regulr grmmr genertes regulr lnguge 36

Theorem - Prt 2 Lnguges Generted y Regulr Grmmrs Regulr Lnguges Any regulr lnguge is generted y regulr grmmr 37

Proof Prt 1 Lnguges Generted y Regulr Grmmrs Regulr Lnguges The lnguge L(G) ny regulr grmmr generted y G is regulr 38

The cse of Right-Liner Grmmrs Let G e right-liner grmmr We will prove: L(G) is regulr Proof ide We will construct NFA with M ) L ( M ) = L( G 39

Grmmr G is right-liner Exmple A B A B B B 40

M Construct NFA such tht every stte is grmmr vrile: A B V F specil finl stte A B A B B B 41

Add edges for ech production: A V F B A B A B B B 42

A λ V F B A B A B B B 43

A λ V F A B B A B B B 44

A λ V F A B B A B B B 45

A λ V F A B B A B B B 46

A λ V F B A B B 47

NFA M A Grmmr G A B A B B B λ V F B L( M ) = L( G) = * + * 48

In Generl A right-liner grmmr G hs vriles: V0, V1, V2, nd productions: V! i or 1 2 m V j V i 1 2! m 49

We construct the NFA M such tht: ech vrile V i corresponds to node: V0 V1 V2. VF specil finl stte 50

For ech production: V! i 1 2 m V j we dd trnsitions nd intermedite nodes Vi 1 2 m V j Exmple: http://www.cs.duke.edu/csed/jflp/tutoril/grmmr/tofa/index.html Convert Right-Liner Grmmr to FA y JFLAP 51

Exmple V V V V V 0 1 2 3 4 V 1 2 5 9 0 1 V V 4 4 1 V 3 3 V 5 2 3 4 V 8 4 V 0 1 3 V 2 V 1 9 2 4 V 3 3 4 8 5 V 4 0 M 5 V F L ( G) = L( M ) 52

The cse of Left-Liner Grmmrs Let G e left-liner grmmr We will prove: L(G) is regulr Proof ide We will construct right-liner grmmr Gʹ with L ( G) = L( Gʹ ) R 53

ince G is left-liner grmmr the productions look like: A B! 1 2 k A 1 2! k 54

Construct right-liner grmmr Gʹ In G : A B! 1 2 k A vb In Gʹ : A k! 21B A v R B 55

Construct right-liner grmmr Gʹ In : G A 1 2! k A v Gʹ A k! 2 1 In : A v R 56

It is esy to see tht: L ( G) = L( Gʹ ) R ince Gʹ is right-liner, we hve: L(G ʹ R ) L ( Gʹ ) L(G) Regulr Lnguge Regulr Lnguge Regulr Lnguge 57

Proof - Prt 2 Lnguges Generted y Regulr Grmmrs Regulr Lnguges Any regulr lnguge L is generted y some regulr grmmr G 58

Any regulr lnguge L is generted y some regulr grmmr Proof ide ince L is regulr there is n NFA G M L = L(M ) such tht Construct from such tht M L ( M ) = L( G) regulr grmmr G 59

Exmple M q 0 q1 q2 λ L = * ( * )* q 3 L = L(M ) 60

Convert G M to right-liner grmmr q q 0 1 q q 1 1 M q0 q1 q2 q 1 q 2 λ q 2 q 3 q 3 q 3 q 1 L ( G) = L( M ) = L q 3 λ 61

In Generl For ny trnsition: q p Add production: q p vrile terminl vrile 62

For ny finl stte: q f Add production: q f λ 63

ince G is right-liner grmmr G is lso regulr grmmr with L ( G) = L( M ) = L 64

Regulr Grmmrs A regulr grmmr is ny right-liner or left-liner grmmr Exmples G1 G2 A A A B B 65

Oservtion Regulr grmmrs generte regulr lnguges Exmples G1 G2 A A A B B L ( G 1 ) = ( ) * L ( G2) = ( )* 66

The Chomsky Hierrchy 67

Chomsky s Lnguge Hierrchy Non-regulr lnguges Regulr Lnguges 68

Minimizing DFA y et Prtitioning 69

Minimiztion of DFA The deterministic finite utomt re not lwys the smllest possile ccepting the source lnguge. There my e sttes with the sme "cceptnce ehvior". This pplies to sttes p nd q, if for ll input words, the utomton lwys or never moves to finl stte from p nd q. 70

tte Reduction y et Prtitioning The set prtitioning technique is similr to one used for prtitioning people into groups sed on their responses to questionnire. The following slides show the detiled steps for computing equivlent stte sets of the strting DFA nd constructing the corresponding reduced DFA. 71

1 3 trting DFA 0 4 2 5 3 Reduced DFA 0, 1,2, 4,5 72

tte Reduction y et Prtitioning tep 0: Prtition the sttes into two groups ccepting nd non-ccepting. P1 P2 { 3, 4, 5 } { 0, 1, 2 } 0 1 2 3 4 5 73

tte Reduction y et Prtitioning tep 1: Get the response of ech stte for ech input symol. Notice tht ttes 3 nd 0 show different responses from the ones of the other sttes in the sme set. P 1 P 2 P2 P1 p 1 p 1 p 1 p 2 p 1 p 1 {3, 4, 5 } {0, 1, 2 } p 2 p 1 p 1 p 2 p 1 p 1 0 1 2 3 4 5 74

tte Reduction y et Prtitioning P22: 0, -> P21 0, -> P21 P21: 1, -> P12 1, -> P11 2, -> P12 2, -> P11 P12: 3, -> P12 3, -> P21 P11: 4, -> P11 4, -> P11 5, -> P11 5, -> P11 No more sudivision. Remining prtitions re the new sttes. {0},{1,2},{3},{4,5} P22 0 P21 1 2 P12 3 4 5 P11 75

Minimized DFA consists of four sttes of the finl prtition, nd the trnsitions re the one corresponding to the strting DFA. {4, 5} {3} {1, 2} {0} 0, 1,2 3 4,5, 0 1 2 3 4 5 Minimized DFA trting DFA 76

DFA Minimiztion Algorithm The lgorithm P { F, {Q-F}} while ( P is still chnging) T { } for ech set s P for ech α Σ prtition s y α into s 1, s 2,, s k T T s 1, s 2,, s k if T P then P T This is fixed-point lgorithm! Why does this work? Prtition P 2 Q (power set) trt off with 2 susets of Q {F} nd {Q-F} While loop tkes P i P i+1 y splitting one or more sets P i+1 is t lest one step closer to the prtition with Q sets Mximum of Q splits Prtitions re never comined Initil prtition ensures tht finl sttes re intct 77

et Prtition Algorithm (lling ook) Exmple 2.39 We pply step y step set prtition lgorithm on the following DFA: Two strings x,y re distinguished y lnguge (DFA) if there is (distinguishing) string z such s tht only one of strings xz, yz ends in ccepting stte (elongs to lnguge). 1 2 3 Σ = {, 4 5 6 } 78

We serch for strings tht re distinguished y DFA! Non-ccepting: 1 2 3 5 Wht hppens when we red? From 5 we end in 6 y, leving its group. 4 6 {1,2,3,4,5,6} Accepting: { 1,2,4,5,6} {3} Distinguishing string: Wht hppens when we red? From 1 nd 6 y we rech 3 which is ccepting. They form specil group. { 2,4,5} {1,6 } {2,4} {5} {1,6} or 79 {3} {3}

The miniml utomton hs four sttes: {2,4} { 5} {1,6 } {3} {2,4} { 1,6} {5} {3} 1 2 3 4 5 6 80

Regulr grmmrs ummry left liner right liner left liner => right liner (crete reverse regulr lnguges closed under reverse) right liner => NFA NFA => right liner Regulr grmmr ó regulr lnguge DFA minimiztion stte reduction vi set prtitioning 81

RE NFA DFA Miniml DFA Thompson s Contruction uset Contruction Hopcroft Minimiztion 82

Appliction: Compiler Lexicl Anlysis 83

Wht is compiler? A compiler is progrm tht trnsltes source lnguge into n equivlent trget lnguge while (i > 3) { [i] = [i]; i ++ } C progrm compiler does this mov ex, ex dd ex, 1 cmp ex, 3 jcc ex, edx ssemly progrm 84

Wht is compiler? compiler does this clss foo { int r;... } struct foo { int r;... } Jv progrm C progrm 85

Wht is compiler? compiler does this clss foo { int r;... }......... Jv progrm Jv virtul mchine progrm 86

Phses of compiler ource Progrm cnner Prser Lexicl Anlyzer yntx Anlyzer emntic Anlyzer Tokens Prse Tree Astrct yntx Tree with ttriutes 87

Compiltion in Nutshell ource code (chrcter strem) if ( == 0) = ; Lexicl nlysis Token strem if ( == 0 ) = ; Astrct syntx tree (AT) Decorted AT == 0 oolen == if int int 0 if = int = int lvlue int ; ; Prsing emntic Anlysis 88

tges of Anlysis Lexicl Anlysis reking the input up into individul words/tokens yntx Anlysis prsing the phrse structure of the progrm emntic Anlysis clculting the progrm s mening 89

Lexicl Anlyzer Input is strem of chrcters Produces strem of nmes, keywords & punctution mrks Discrds white spce nd comments 90

Lexicl Anlyzer Recognize tokens in progrm source code. The tokens cn e vrile nmes, reserved words, opertors, numers, etc. Ech kind of token cn e specified s n RE, e.g., vrile nme is of the form [A-Z-z] [A-Z-z0-9]*. We cn then construct n λ -NFA to recognize it utomticlly. 91

Lexicl Anlyzer By putting ll these λ-nfa s together, we otin one which cn recognize ll different kinds of tokens in the input string. We cn then convert this λ -NFA to NFA nd then to DFA, nd implement this DFA s deterministic progrm - the lexicl nlyzer. 92