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