-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautomata_theory.html
More file actions
4517 lines (2642 loc) · 209 KB
/
Copy pathautomata_theory.html
File metadata and controls
4517 lines (2642 loc) · 209 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html><html><body><title>Automata Theory Tutorial</title>
<h1>Automata Theory Tutorial</h1>
<p><b>Automata Theory</b> is a branch of computer science that deals with designing abstract selfpropelled computing devices that follow a predetermined sequence of operations automatically. An automaton with a finite number of states is called a <b>Finite Automaton</b>. This is a brief and concise tutorial that introduces the fundamental concepts of Finite Automata, Regular Languages, and Pushdown Automata before moving onto Turing machines and Decidability.</p>
<h1>Audience</h1>
<p>This tutorial has been prepared for students pursuing a degree in any information technology or computer science related field. It attempts to help students grasp the essential concepts involved in automata theory.</p>
<h1>Prerequisites</h1>
<p>This tutorial has a good balance between theory and mathematical rigor. The readers are expected to have a basic understanding of discrete mathematical structures.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Automata Theory Introduction</title>
<h1>Automata Theory Introduction</h1>
<h2>Automata – What is it?</h2>
<p>The term "Automata" is derived from the Greek word "αὐτόματα" which means "self-acting". An automaton (Automata in plural) is an abstract self-propelled computing device which follows a predetermined sequence of operations automatically.</p>
<p>An automaton with a finite number of states is called a <b>Finite Automaton</b> (FA) or <b>Finite State Machine</b> (FSM).</p>
<h3>Formal definition of a Finite Automaton</h3>
<p>An automaton can be represented by a 5-tuple (Q, ∑, δ, q<sub>0</sub>, F), where −</p>
<p><b>Q</b> is a finite set of states.</p>
<p><b>∑</b> is a finite set of symbols, called the <b>alphabet</b> of the automaton.</p>
<p><b>δ</b> is the transition function.</p>
<p><b>q<sub>0</sub></b> is the initial state from where any input is processed (q<sub>0</sub> ∈ Q).</p>
<p><b>F</b> is a set of final state/states of Q (F ⊆ Q).</p>
<h2>Related Terminologies </h2>
<h3>Alphabet</h3>
<p><b>Definition</b> − An <b>alphabet</b> is any finite set of symbols.</p>
<p><b>Example</b> − ∑ = {a, b, c, d} is an <b>alphabet set</b> where ‘a’, ‘b’, ‘c’, and ‘d’ are <b>symbols</b>.</p>
<h3>String</h3>
<p><b>Definition</b> − A <b>string</b> is a finite sequence of symbols taken from ∑.</p>
<p><b>Example</b> − ‘cabcad’ is a valid string on the alphabet set ∑ = {a, b, c, d}</p>
<h3>Length of a String</h3>
<p><b>Definition</b> − It is the number of symbols present in a string. (Denoted by <b>|S|</b>).</p>
<p><b>Examples</b> −</p>
<p>If S = ‘cabcad’, |S|= 6</p>
<p>If |S|= 0, it is called an <b>empty string</b> (Denoted by <b>λ</b> or <b>ε</b>)</p>
<h3>Kleene Star</h3>
<p><b>Definition</b> − The Kleene star, <b>∑*</b>, is a unary operator on a set of symbols or strings, <b>∑</b>, that gives the infinite set of all possible strings of all possible lengths over <b>∑</b> including <b>λ</b>.</p>
<p><b>Representation</b> − ∑* = ∑<sub>0</sub> ∪ ∑<sub>1</sub> ∪ ∑<sub>2</sub> ∪……. where ∑<sub>p</sub> is the set of all possible strings of length p.</p>
<p><b>Example</b> − If ∑ = {a, b}, ∑* = {λ, a, b, aa, ab, ba, bb,………..}</p>
<h3>Kleene Closure / Plus</h3>
<p><b>Definition</b> − The set <b>∑<sup>+</sup></b> is the infinite set of all possible strings of all possible lengths over ∑ excluding λ.</p>
<p><b>Representation</b> − ∑<sup>+</sup> = ∑<sub>1</sub> ∪ ∑<sub>2</sub> ∪ ∑<sub>3</sub> ∪…….</p>
<p><b>Example</b> − If ∑ = { a, b } , ∑<sup>+</sup> = { a, b, aa, ab, ba, bb,………..}</p>
<h3>Language</h3>
<p><b>Definition</b> − A language is a subset of ∑* for some alphabet ∑. It can be finite or infinite.</p>
<p><b>Example</b> − If the language takes all possible strings of length 2 over ∑ = {a, b}, then L = { ab, bb, ba, bb}</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Deterministic Finite Automaton</title>
<h1>Deterministic Finite Automaton</h1>
<p>Finite Automaton can be classified into two types −</p>
<h2>Deterministic Finite Automaton (DFA)</h2>
<p>In DFA, for each input symbol, one can determine the state to which the machine will move. Hence, it is called <b>Deterministic Automaton</b>. As it has a finite number of states, the machine is called <b>Deterministic Finite Machine</b> or <b>Deterministic Finite Automaton.</b></p>
<h2>Formal Definition of a DFA</h2>
<p>A DFA can be represented by a 5-tuple (Q, ∑, δ, q<sub>0</sub>, F) where −</p>
<p><b>Q</b> is a finite set of states.</p>
<p><b>∑</b> is a finite set of symbols called the alphabet.</p>
<p><b>δ</b> is the transition function where δ: Q × ∑ → Q </p>
<p><b>q<sub>0</sub></b> is the initial state from where any input is processed (q<sub>0</sub> ∈ Q).</p>
<p><b>F</b> is a set of final state/states of Q (F ⊆ Q).</p>
<h2>Graphical Representation of a DFA</h2>
<p>A DFA is represented by digraphs called <b>state diagram</b>.</b></p>
<h3>Example</h3>
<p>Let a deterministic finite automaton be →</p>
<p>Transition function δ as shown by the following table −</p>
<p>Its graphical representation would be as follows −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Non-deterministic Finite Automaton</title>
<h1>Non-deterministic Finite Automaton</h1>
<p>In NDFA, for a particular input symbol, the machine can move to any combination of the states in the machine. In other words, the exact state to which the machine moves cannot be determined. Hence, it is called <b>Non-deterministic Automaton</b>. As it has finite number of states, the machine is called <b>Non-deterministic Finite Machine</b> or <b>Non-deterministic Finite Automaton</b>.</p>
<h3>Formal Definition of an NDFA</h3>
<p>An NDFA can be represented by a 5-tuple (Q, ∑, δ, q<sub>0</sub>, F) where −</p>
<p><b>Q</b> is a finite set of states.</p>
<p><b>∑</b> is a finite set of symbols called the alphabets.</p>
<p><b>δ</b> is the transition function where δ: Q × ∑ → 2<sup>Q</sup></p>
<p>(Here the power set of Q (2<sup>Q</sup>) has been taken because in case of NDFA, from a state, transition can occur to any combination of Q states)</p>
<p><b>q<sub>0</sub></b> is the initial state from where any input is processed (q<sub>0</sub> ∈ Q).</p>
<p><b>F</b> is a set of final state/states of Q (F ⊆ Q).</p>
<h3>Graphical Representation of an NDFA: (same as DFA)</h3>
<p>An NDFA is represented by digraphs called state diagram.</p>
<p><b>Example</b></p>
<p>Let a non-deterministic finite automaton be →</p>
<p>The transition function δ as shown below −</p>
<p>Its graphical representation would be as follows −</p>
<h2>DFA vs NDFA</h2>
<p>The following table lists the differences between DFA and NDFA.</p>
<h2>Acceptors, Classifiers, and Transducers</h2>
<h3>Acceptor (Recognizer)</h3>
<p>An automaton that computes a Boolean function is called an <b>acceptor</b>. All the states of an acceptor is either accepting or rejecting the inputs given to it.</p>
<h3>Classifier</h3>
<p>A <b>classifier</b> has more than two final states and it gives a single output when it terminates.</p>
<h3>Transducer</h3>
<p>An automaton that produces outputs based on current input and/or previous state is called a <b>transducer</b>. Transducers can be of two types −</p>
<p><b>Mealy Machine</b> − The output depends both on the current state and the current input.</p>
<p><b>Moore Machine</b> − The output depends only on the current state.</p>
<h2>Acceptability by DFA and NDFA</h2>
<p>A string is accepted by a DFA/NDFA iff the DFA/NDFA starting at the initial state ends in an accepting state (any of the final states) after reading the string wholly.</p>
<p>A string S is accepted by a DFA/NDFA (Q, ∑, δ, q<sub>0</sub>, F), iff</p>
<p>The language <b>L</b> accepted by DFA/NDFA is</p>
<p>A string S′ is not accepted by a DFA/NDFA (Q, ∑, δ, q<sub>0</sub>, F), iff</p>
<p>The language L′ not accepted by DFA/NDFA (Complement of accepted language L) is</p>
<p><b>Example</b></p>
<p>Let us consider the DFA shown in Figure 1.3. From the DFA, the acceptable strings can be derived.</p>
<p>Strings accepted by the above DFA: {0, 00, 11, 010, 101, ...........}</p>
<p>Strings not accepted by the above DFA: {1, 011, 111, ........}</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>NDFA to DFA Conversion</title>
<h1>NDFA to DFA Conversion</h1>
<h2>Problem Statement</h2>
<p>Let <b>X = (Q<sub>x</sub>, ∑, δ<sub>x</sub>, q<sub>0</sub>, F<sub>x</sub>)</b> be an NDFA which accepts the language L(X). We have to design an equivalent DFA <b>Y = (Q<sub>y</sub>, ∑, δ<sub>y</sub>, q<sub>0</sub>, F<sub>y</sub>)</b> such that <b>L(Y) = L(X)</b>. The following procedure converts the NDFA to its equivalent DFA −</p>
<h2>Algorithm</h2>
<p><b>Input</b> − An NDFA</p>
<p><b>Output</b> − An equivalent DFA</p>
<p><b>Step 1</b> − Create state table from the given NDFA.</p>
<p><b>Step 2</b> − Create a blank state table under possible input alphabets for the equivalent DFA.</p>
<p><b>Step 3</b> − Mark the start state of the DFA by q0 (Same as the NDFA).</p>
<p><b>Step 4</b> − Find out the combination of States {Q<sub>0</sub>, Q<sub>1</sub>,... , Q<sub>n</sub>} for each possible input alphabet.</p>
<p><b>Step 5</b> − Each time we generate a new DFA state under the input alphabet columns, we have to apply step 4 again, otherwise go to step 6.</p>
<p><b>Step 6</b> − The states which contain any of the final states of the NDFA are the final states of the equivalent DFA.</p>
<h2>Example</h2>
<p>Let us consider the NDFA shown in the figure below.</p>
<p>Using the above algorithm, we find its equivalent DFA. The state table of the DFA is shown in below.</p>
<p>The state diagram of the DFA is as follows −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>DFA Minimization</title>
<h1>DFA Minimization</h1>
<h2>DFA Minimization using Myphill-Nerode Theorem</h2>
<h3>Algorithm</h3>
<p><b>Input</b> − DFA</p>
<p><b>Output</b> − Minimized DFA</p>
<p><b>Step 1</b> − Draw a table for all pairs of states (Q<sub>i</sub>, Q<sub>j</sub>) not necessarily connected directly [All are unmarked initially]</p>
<p><b>Step 2</b> − Consider every state pair (Q<sub>i</sub>, Q<sub>j</sub>) in the DFA where Q<sub>i</sub> ∈ F and Q<sub>j</sub> ∉ F or vice versa and mark them. [Here F is the set of final states]</p>
<p><b>Step 3</b> − Repeat this step until we cannot mark anymore states −</p>
<p>If there is an unmarked pair (Q<sub>i</sub>, Q<sub>j</sub>), mark it if the pair {δ (Q<sub>i</sub>, A), δ (Q<sub>i</sub>, A)} is marked for some input alphabet.</p>
<p><b>Step 4</b> − Combine all the unmarked pair (Q<sub>i</sub>, Q<sub>j</sub>) and make them a single state in the reduced DFA.</p>
<h3>Example</h3>
<p>Let us use Algorithm 2 to minimize the DFA shown below.</p>
<p><b>Step 1</b> − We draw a table for all pair of states.</p>
<p><b>Step 2</b> − We mark the state pairs.</p>
<p><b>Step 3</b> − We will try to mark the state pairs, with green colored check mark, transitively. If we input 1 to state ‘a’ and ‘f’, it will go to state ‘c’ and ‘f’ respectively. (c, f) is already marked, hence we will mark pair (a, f). Now, we input 1 to state ‘b’ and ‘f’; it will go to state ‘d’ and ‘f’ respectively. (d, f) is already marked, hence we will mark pair (b, f).</p>
<p>After step 3, we have got state combinations {a, b} {c, d} {c, e} {d, e} that are unmarked.</p>
<p>We can recombine {c, d} {c, e} {d, e} into {c, d, e}</p>
<p>Hence we got two combined states as − {a, b} and {c, d, e}</p>
<p>So the final minimized DFA will contain three states {f}, {a, b} and {c, d, e}</p>
<h2>DFA Minimization using Equivalence Theorem</h2>
<p>If X and Y are two states in a DFA, we can combine these two states into {X, Y} if they are not distinguishable. Two states are distinguishable, if there is at least one string S, such that one of δ (X, S) and δ (Y, S) is accepting and another is not accepting. Hence, a DFA is minimal if and only if all the states are distinguishable.</p>
<h3>Algorithm 3</h3>
<p><b>Step 1</b> − All the states <b>Q</b> are divided in two partitions − <b>final states</b> and <b>non-final states</b> and are denoted by <b>P<sub>0</sub></b>. All the states in a partition are 0<sup>th</sup> equivalent. Take a counter <b>k</b> and initialize it with 0.</p>
<p><b>Step 2</b> − Increment k by 1. For each partition in P<sub>k</sub>, divide the states in P<sub>k</sub> into two partitions if they are k-distinguishable. Two states within this partition X and Y are k-distinguishable if there is an input <b>S</b> such that <b>δ(X, S)</b> and <b>δ(Y, S)</b> are (k-1)-distinguishable.</p>
<p><b>Step 3</b> − If P<sub>k</sub> ≠ P<sub>k-1</sub>, repeat Step 2, otherwise go to Step 4.</p>
<p><b>Step 4</b> − Combine k<sup>th</sup> equivalent sets and make them the new states of the reduced DFA.</p>
<h3>Example</h3>
<p>Let us consider the following DFA −</p>
<p>Let us apply the above algorithm to the above DFA −</p>
<p>Hence, P<sub>1</sub> = P<sub>2</sub>.</p>
<p>There are three states in the reduced DFA. The reduced DFA is as follows −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Moore and Mealy Machines</title>
<h1>Moore and Mealy Machines</h1>
<p>Finite automata may have outputs corresponding to each transition. There are two types of finite state machines that generate output −</p>
<h3>Mealy Machine</h3>
<p>A Mealy Machine is an FSM whose output depends on the present state as well as the present input.</p>
<p>It can be described by a 6 tuple (Q, ∑, O, δ, X, q<sub>0</sub>) where −</p>
<p><b>Q</b> is a finite set of states.</p>
<p><b>∑</b> is a finite set of symbols called the input alphabet.</p>
<p><b>O</b> is a finite set of symbols called the output alphabet.</p>
<p><b>δ</b> is the input transition function where δ: Q × ∑ → Q</p>
<p><b>X</b> is the output transition function where X: Q × ∑ → O</p>
<p><b>q<sub>0</sub></b> is the initial state from where any input is processed (q<sub>0</sub> ∈ Q).</p>
<p>The state table of a Mealy Machine is shown below −</p>
<p>The state diagram of the above Mealy Machine is −</p>
<h3>Moore Machine</h3>
<p>Moore machine is an FSM whose outputs depend on only the present state.</p>
<p>A Moore machine can be described by a 6 tuple (Q, ∑, O, δ, X, q<sub>0</sub>) where −</p>
<p><b>Q</b> is a finite set of states.</p>
<p><b>∑</b> is a finite set of symbols called the input alphabet.</p>
<p><b>O</b> is a finite set of symbols called the output alphabet.</p>
<p><b>δ</b> is the input transition function where δ: Q × ∑ → Q</p>
<p><b>X</b> is the output transition function where X: Q → O</p>
<p><b>q<sub>0</sub></b> is the initial state from where any input is processed (q<sub>0</sub> ∈ Q).</p>
<p>The state table of a Moore Machine is shown below −</p>
<p>The state diagram of the above Moore Machine is −</p>
<h3>Mealy Machine vs. Moore Machine</h3>
<p>The following table highlights the points that differentiate a Mealy Machine from a Moore Machine.</p>
<h2>Moore Machine to Mealy Machine</h2>
<h3>Algorithm 4</h3>
<p><b>Input</b> − Moore Machine</p>
<p><b>Output</b> − Mealy Machine</p>
<p><b>Step 1</b> − Take a blank Mealy Machine transition table format.</p>
<p><b>Step 2</b> − Copy all the Moore Machine transition states into this table format.</p>
<p><b>Step 3</b> − Check the present states and their corresponding outputs in the Moore Machine state table; if for a state Q<sub>i</sub> output is m, copy it into the output columns of the Mealy Machine state table wherever Q<sub>i</sub> appears in the next state.</p>
<h3>Example</h3>
<p>Let us consider the following Moore machine −</p>
<p>Now we apply Algorithm 4 to convert it to Mealy Machine.</p>
<p><b>Step 1 & 2</b> −</p>
<p><b>Step 3</b> −</p>
<h2>Mealy Machine to Moore Machine</h2>
<h3>Algorithm 5</h3>
<p><b>Input</b> − Mealy Machine</p>
<p><b>Output</b> − Moore Machine</p>
<p><b>Step 1</b> − Calculate the number of different outputs for each state (Q<sub>i</sub>) that are available in the state table of the Mealy machine.</p>
<p><b>Step 2</b> − If all the outputs of Qi are same, copy state Q<sub>i</sub>. If it has n distinct outputs, break Q<sub>i</sub> into n states as Q<sub>in</sub> where <b>n</b> = 0, 1, 2.......</p>
<p><b>Step 3</b> − If the output of the initial state is 1, insert a new initial state at the beginning which gives 0 output.</p>
<h3>Example</h3>
<p>Let us consider the following Mealy Machine −</p>
<p>Here, states ‘a’ and ‘d’ give only 1 and 0 outputs respectively, so we retain states ‘a’ and ‘d’. But states ‘b’ and ‘c’ produce different outputs (1 and 0). So, we divide <b>b</b> into <b>b<sub>0</sub>, b<sub>1</sub></b> and <b>c</b> into <b>c<sub>0</sub>, c<sub>1</sub></b>.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Introduction to Grammars</title>
<h1>Introduction to Grammars</h1>
<p>n the literary sense of the term, grammars denote syntactical rules for conversation in natural languages. Linguistics have attempted to define grammars since the inception of natural languages like English, Sanskrit, Mandarin, etc.</p>
<p>The theory of formal languages finds its applicability extensively in the fields of Computer Science. <b>Noam Chomsky</b> gave a mathematical model of grammar in 1956 which is effective for writing computer languages.</p>
<h2>Grammar</h2>
<p>A grammar <b>G</b> can be formally written as a 4-tuple (N, T, S, P) where −</p>
<p><b>N</b> or <b>V<sub><i><small>N</small></i></sub></b> is a set of variables or non-terminal symbols.</p>
<p><b>T</b> or <b>∑</b> is a set of Terminal symbols.</p>
<p><b>S</b> is a special variable called the Start symbol, S ∈ N</p>
<p><b>P</b> is Production rules for Terminals and Non-terminals. A production rule has the form α → β, where α and β are strings on V<sub><i><small>N</small></i></sub> ∪ ∑ and least one symbol of α belongs to V<sub>N</sub>.</p>
<h3>Example</h3>
<p>Grammar G1 −</p>
<p>Here,</p>
<p><b>S, A,</b> and <b>B</b> are Non-terminal symbols;</p>
<p><b>a</b> and <b>b</b> are Terminal symbols</p>
<p><b>S</b> is the Start symbol, S ∈ N</p>
<p>Productions, <b>P : S → AB, A → a, B → b</b></p>
<h3>Example</h3>
<p>Grammar G2 −</p>
<p>Here,</p>
<p><b>S</b> and <b>A</b> are Non-terminal symbols.</p>
<p><b>a</b> and <b>b</b> are Terminal symbols.</p>
<p><b>ε</b> is an empty string.</p>
<p><b>S</b> is the Start symbol, S ∈ N</p>
<p>Production <b>P : S → aAb, aA → aaAb, A → ε</b></p>
<h2>Derivations from a Grammar</h2>
<p>Strings may be derived from other strings using the productions in a grammar. If a grammar <b>G</b> has a production <b>α → β</b>, we can say that <b>x α y</b> derives <b>x β y</b> in <b>G</b>. This derivation is written as −</p>
<h3>Example</h3>
<p>Let us consider the grammar −</p>
<p>Some of the strings that can be derived are −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Language Generated by a Grammar</title>
<h1>Language Generated by a Grammar</h1>
<p>The set of all strings that can be derived from a grammar is said to be the language generated from that grammar. A language generated by a grammar <b>G</b> is a subset formally defined by</p>
<p>If <b>L(G1) = L(G2)</b>, the Grammar <b>G1</b> is equivalent to the Grammar <b>G2</b>.</p>
<h3>Example</h3>
<p>If there is a grammar</p>
<p>Here <b>S</b> produces <b>AB</b>, and we can replace <b>A</b> by <b>a</b>, and <b>B</b> by <b>b</b>. Here, the only accepted string is <b>ab</b>, i.e.,</p>
<h3>Example</h3>
<p>Suppose we have the following grammar −</p>
<p>The language generated by this grammar −</p>
<h2>Construction of a Grammar Generating a Language</h2>
<p>We’ll consider some languages and convert it into a grammar G which produces those languages.</p>
<h3>Example</h3>
<p><b><i>Problem</i></b> − Suppose, L (G) = {a<sup>m</sup> b<sup>n</sup> | m ≥ 0 and n > 0}. We have to find out the grammar <b>G</b> which produces <b>L(G)</b>.</p>
<p><b><i>Solution</i></b></p>
<p>Since L(G) = {a<sup>m</sup> b<sup>n</sup> | m ≥ 0 and n > 0}</p>
<p>the set of strings accepted can be rewritten as −</p>
<p>Here, the start symbol has to take at least one ‘b’ preceded by any number of ‘a’ including null.</p>
<p>To accept the string set {b, ab, bb, aab, abb, …….}, we have taken the productions −</p>
<p>Thus, we can prove every single string in L(G) is accepted by the language generated by the production set.</p>
<p>Hence the grammar −</p>
<h3>Example</h3>
<p><b><i>Problem</i></b> − Suppose, L (G) = {a<sup>m</sup> b<sup>n</sup> | m > 0 and n ≥ 0}. We have to find out the grammar G which produces L(G).</p>
<p><b><i>Solution</i></b> −</p>
<p>Since L(G) = {a<sup>m</sup> b<sup>n</sup> | m > 0 and n ≥ 0}, the set of strings accepted can be rewritten as −</p>
<p>Here, the start symbol has to take at least one ‘a’ followed by any number of ‘b’ including null.</p>
<p>To accept the string set {a, aa, ab, aaa, aab, abb, …….}, we have taken the productions −</p>
<p>Thus, we can prove every single string in L(G) is accepted by the language generated by the production set.</p>
<p>Hence the grammar −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Chomsky Classification of Grammars</title>
<h1>Chomsky Classification of Grammars</h1>
<p>According to Noam Chomosky, there are four types of grammars − Type 0, Type 1, Type 2, and Type 3. The following table shows how they differ from each other −</p>
<p>Take a look at the following illustration. It shows the scope of each type of grammar −</p>
<h2>Type - 3 Grammar</h2>
<p><b>Type-3 grammars</b> generate regular languages. Type-3 grammars must have a single non-terminal on the left-hand side and a right-hand side consisting of a single terminal or single terminal followed by a single non-terminal.</p>
<p>The productions must be in the form <b>X → a or X → aY</b></p>
<p>where <span style="padding-left:2%;"><b>X, Y ∈ N</b> (Non terminal)</span></p>
<p>and <span style="padding-left:2%;"><b>a ∈ T</b> (Terminal)</span></p>
<p>The rule <b>S → ε</b> is allowed if <b>S</b> does not appear on the right side of any rule.</p>
<h3>Example</h3>
<h2>Type - 2 Grammar</h2>
<p><b>Type-2 grammars</b> generate context-free languages.</p>
<p>The productions must be in the form <b>A → γ</b></p>
<p>where <span style="padding-left:2%;"><b> A ∈ N</b> (Non terminal)</span></p>
<p>and <span style="padding-left:2%;"><b>γ ∈ (T ∪ N)*</b> (String of terminals and non-terminals).</span></p>
<p>These languages generated by these grammars are be recognized by a non-deterministic pushdown automaton.</p>
<h3>Example</h3>
<h2>Type - 1 Grammar</h2>
<p><b>Type-1 grammars</b> generate context-sensitive languages. The productions must be in the form</p>
<p>where <span style="padding-left:2%;"><b>A ∈ N</b> (Non-terminal)</span></p>
<p>and <span style="padding-left:2%;"><b>α, β, γ ∈ (T ∪ N)*</b> (Strings of terminals and non-terminals)</span></p>
<p>The strings <b>α</b> and <b>β</b> may be empty, but <b>γ</b> must be non-empty.</p>
<p>The rule <b>S → ε</b> is allowed if S does not appear on the right side of any rule. The languages generated by these grammars are recognized by a linear bounded automaton.</p>
<h3>Example</h3>
<h2>Type - 0 Grammar</h2>
<p><b>Type-0 grammars</b> generate recursively enumerable languages. The productions have no restrictions. They are any phase structure grammar including all formal grammars.</p>
<p>They generate the languages that are recognized by a Turing machine.</p>
<p>The productions can be in the form of <b>α → β</b> where <b>α</b> is a string of terminals and nonterminals with at least one non-terminal and <b>α</b> cannot be null. <b>β</b> is a string of terminals and non-terminals.</p>
<h3>Example</h3>
<p>© Copyright 2017. All Rights Reserved.</p>
<pre class="result notranslate">
X → ε
X → a | aY
Y → b
</pre>
<h2>Type - 2 Grammar</h2>
<p><b>Type-2 grammars</b> generate context-free languages.</p>
<p>The productions must be in the form <b>A → γ</b></p>
<p>where <span style="padding-left:2%;"><b> A ∈ N</b> (Non terminal)</span></p>
<p>and <span style="padding-left:2%;"><b>γ ∈ (T ∪ N)*</b> (String of terminals and non-terminals).</span></p>
<p>These languages generated by these grammars are be recognized by a non-deterministic pushdown automaton.</p>
<h3>Example</h3>
<pre class="result notranslate">
S → X a
X → a
X → aX
X → abc
X → ε
</pre>
<h2>Type - 1 Grammar</h2>
<p><b>Type-1 grammars</b> generate context-sensitive languages. The productions must be in the form</p>
<p style="padding-left:12%;"><b>α A β → α γ β</b></p>
<p>where <span style="padding-left:2%;"><b>A ∈ N</b> (Non-terminal)</span></p>
<p>and <span style="padding-left:2%;"><b>α, β, γ ∈ (T ∪ N)*</b> (Strings of terminals and non-terminals)</span></p>
<p>The strings <b>α</b> and <b>β</b> may be empty, but <b>γ</b> must be non-empty.</p>
<p>The rule <b>S → ε</b> is allowed if S does not appear on the right side of any rule. The languages generated by these grammars are recognized by a linear bounded automaton.</p>
<h3>Example</h3>
<pre class="result notranslate">
AB → AbBc
A → bcA
B → b
</pre>
<h2>Type - 0 Grammar</h2>
<p><b>Type-0 grammars</b> generate recursively enumerable languages. The productions have no restrictions. They are any phase structure grammar including all formal grammars.</p>
<p>They generate the languages that are recognized by a Turing machine.</p>
<p>The productions can be in the form of <b>α → β</b> where <b>α</b> is a string of terminals and nonterminals with at least one non-terminal and <b>α</b> cannot be null. <b>β</b> is a string of terminals and non-terminals.</p>
<h3>Example</h3>
<pre class="result notranslate">
S → ACaB
Bc → acB
CB → DB
aD → Db
</pre>
<title>Regular Expressions</title>
<h1>Regular Expressions</h1>
<p>A <b>Regular Expression</b> can be recursively defined as follows −</p>
<p><b>ε</b> is a Regular Expression indicates the language containing an empty string. <b>(L (ε) = {ε})</b></p>
<p><b>φ</b> is a Regular Expression denoting an empty language. <b>(L (φ) = { })</b></p>
<p><b>x</b> is a Regular Expression where <b>L = {x}</b></p>
<p>If <b>X</b> is a Regular Expression denoting the language <b>L(X)</b> and <b>Y</b> is a Regular Expression denoting the language <b>L(Y)</b>, then</p>
<p><b>X + Y</b> is a Regular Expression corresponding to the language <b>L(X) ∪ L(Y)</b> where <b>L(X+Y) = L(X) ∪ L(Y)</b>.</p>
<p><b>X . Y</b> is a Regular Expression corresponding to the language <b>L(X) . L(Y)</b> where <b>L(X.Y) = L(X) . L(Y)</b></p>
<p><b>R*</b> is a Regular Expression corresponding to the language <b>L(R*)</b>where <b>L(R*) = (L(R))*</b></p>
<p>If we apply any of the rules several times from 1 to 5, they are Regular Expressions.</p>
<h2>Some RE Examples</h2>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Regular Sets</title>
<h1>Regular Sets</h1>
<p>Any set that represents the value of the Regular Expression is called a <b>Regular Set.</b></p>
<h3>Properties of Regular Sets</h3>
<p><b>Property 1</b>. <i>The union of two regular set is regular.</i></p>
<p><b>Proof</b> −</p>
<p>Let us take two regular expressions</p>
<p>So, <span style="padding-left:2%">L<sub>1</sub> = {a, aaa, aaaaa,.....} (Strings of odd length excluding Null)</span></p>
<p>and <span style="padding-left:2%">L<sub>2</sub> ={ ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)</span></p>
<p><b>Hence, proved.</b></p>
<p><b>Property 2.</b> <i>The intersection of two regular set is regular.</i></p>
<p><b>Proof</b> −</p>
<p>Let us take two regular expressions</p>
<p>So, <span style="padding-left:2%">L<sub>1</sub> = { a,aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)</span></p>
<p><b>Hence, proved.</b></p>
<p><b>Property 3.</b> <i>The complement of a regular set is regular.</i></p>
<p><b>Proof</b> −</p>
<p>Let us take a regular expression −</p>
<p>So, <span style="padding-left:2%;">L = {ε, aa, aaaa, aaaaaa, .......} (Strings of even length including Null)</span></p>
<p>Complement of <b>L</b> is all the strings that is not in <b>L</b>.</p>
<p>So, <span style="padding-left:2%;">L’ = {a, aaa, aaaaa, .....} (Strings of odd length excluding Null)</span></p>
<p><b>Hence, proved.</b></p>
<p><b>Property 4.</b> <i>The difference of two regular set is regular.</i></p>
<p><b>Proof</b> −</p>
<p>Let us take two regular expressions −</p>
<p>So, <span style="padding-left:2%;">L<sub>1</sub> = {a, aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)</span></p>
<p><b>Hence, proved.</b></p>
<p><b>Property 5.</b> <i>The reversal of a regular set is regular.</i></p>
<p><b>Proof</b> −</p>
<p>We have to prove <b>L<sup>R</sup></b> is also regular if <b>L</b> is a regular set.</p>
<p>Let, <span style="padding-left:2%">L = {01, 10, 11, 10}</span></p>
<p><b>Hence, proved.</b></p>
<p><b>Property 6.</b> <i>The closure of a regular set is regular.</i></p>
<p><b>Proof</b> −</p>
<p>i.e.,<span style="padding-left:2%;"> RE (L) = a (aa)*</span></p>
<p>RE (L*) = a (a)*</p>
<p><b>Hence, proved.</b></p>
<p><b>Property 7.</b> <i>The concatenation of two regular sets is regular.</i></p>
<p><b>Proof −</b></p>
<p>Let <span style="padding-left:4%">RE<sub>1</sub> = (0+1)*0 and RE<sub>2</sub> = 01(0+1)*</span></p>
<p>Here,<span style="padding-left:2%"> L<sub>1</sub> = {0, 00, 10, 000, 010, ......} <span style="padding-left:5%;">(Set of strings ending in 0)</span></span></p>
<p>and <span style="padding-left:4%">L<sub>2</sub> = {01, 010,011,.....} <span style="padding-left:15%;">(Set of strings beginning with 01)</span></span></p>
<p>Then,<span style="padding-left:2%"> L<sub>1</sub> L<sub>2</sub> = {001,0010,0011,0001,00010,00011,1001,10010,.............}</span></p>
<p>Set of strings containing 001 as a substring which can be represented by an RE − (0 + 1)*001(0 + 1)*</p>
<p>Hence, proved.</p>
<h2>Identities Related to Regular Expressions</h2>
<p>Given R, P, L, Q as regular expressions, the following identities hold −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Arden's Theorem</title>
<h1>Arden's Theorem</h1>
<p>In order to find out a regular expression of a Finite Automaton, we use Arden’s Theorem along with the properties of regular expressions.</p>
<p><b><i>Statement</i></b> −</p>
<p><b>Proof</b> −</p>
<p>When we put the value of <b>R</b> recursively again and again, we get the following equation −</p>
<p>Hence, proved.</p>
<h2>Assumptions for Applying Arden’s Theorem</h2>
<h3>Method</h3>
<p><b>Step 1</b> − Create equations as the following form for all the states of the DFA having n states with initial state q<sub>1</sub>.</p>
<p><b>R<sub>ij</sub></b> represents the set of labels of edges from <b>q<sub>i</sub></b> to <b>q<sub>j</sub></b>, if no such edge exists, then <b>R<sub>ij</sub> = ∅</b></p>
<p><b>Step 2</b> − Solve these equations to get the equation for the final state in terms of <b>R<sub>ij</sub></b></p>
<p><b>Problem</b></p>
<p>Construct a regular expression corresponding to the automata given below −</p>
<p><b>Solution</b> −</p>
<p>Here the initial state is <b>q<sub>2</sub></b> and the final state is <b>q<sub>1</sub></b>.</p>
<p>The equations for the three states q1, q2, and q3 are as follows −</p>
<p>Now, we will solve these three equations −</p>
<p>Hence, the regular expression is (a + b(b + ab)*aa)*.</p>
<p><b>Problem</b></p>
<p>Construct a regular expression corresponding to the automata given below −</p>
<p><b>Solution</b> −</p>
<p>Here the initial state is q<sub>1</sub> and the final state is q<sub>2</sub></p>
<p>Now we write down the equations −</p>
<p>Now, we will solve these three equations −</p>
<p>So, <span style="padding-left:4%;">q<sub>1</sub> = 0*</span></p>
<p>So, <span style="padding-left:4%;">q<sub>2</sub> = 0*1(0)* [By Arden’s theorem]</span></p>
<p>Hence, the regular expression is 0*10*.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Construction of an FA from an RE</title>
<h1>Construction of an FA from an RE</h1>
<p>We can use Thompson's Construction to find out a Finite Automaton from a Regular Expression. We will reduce the regular expression into smallest regular expressions and converting these to NFA and finally to DFA.</p>
<p>Some basic RA expressions are the following −</p>
<p><b><i>Case 1</i></b> − For a regular expression ‘a’, we can construct the following FA −</p>
<p><b><i>Case 2</i></b> − For a regular expression ‘ab’, we can construct the following FA −</p>
<p><b><i>Case 3</i></b> − For a regular expression (a+b), we can construct the following FA −</p>
<p><b><i>Case 4</i></b> − For a regular expression (a+b)*, we can construct the following FA −</p>
<h3>Method</h3>
<p><b>Step 1</b> <span style="padding-left:3%;">Construct an NFA with Null moves from the given regular expression.</span></p>
<p><b>Step 2</b> <span style="padding-left:3%;">Remove Null transition from the NFA and convert it into its equivalent DFA.</span></p>
<p><b>Problem</b></p>
<p>Convert the following RA into its equivalent DFA − 1 (0 + 1)* 0</p>
<p><b><i>Solution</i></b></p>
<p>We will concatenate three expressions "1", "(0 + 1)*" and "0"</p>
<p>Now we will remove the <b>ε</b> transitions. After we remove the <b>ε</b> transitions from the NDFA, we get the following −</p>
<p>It is an NDFA corresponding to the RE − 1 (0 + 1)* 0. If you want to convert it into a DFA, simply apply the method of converting NDFA to DFA discussed in Chapter 1.</p>
<h2>Finite Automata with Null Moves (NFA-ε)</h2>
<p>A Finite Automaton with null moves (FA-ε) does transit not only after giving input from the alphabet set but also without any input symbol. This transition without input is called a <b>null move</b>.</p>
<p>An NFA-ε is represented formally by a 5-tuple (Q, ∑, δ, q<sub>0</sub>, F), consisting of</p>
<p><b>Q</b> − a finite set of states</p>
<p><b>∑</b> − a finite set of input symbols</p>
<p><b>δ</b> − a transition function δ : Q × (∑ ∪ {ε}) → 2<sup>Q</sup></p>
<p><b>q<sub>0</sub></b> − an initial state q<sub>0</sub> ∈ Q</p>
<p><b>F</b> − a set of final state/states of Q (F⊆Q).</p>
<p>The above <b>(FA-ε)</b> accepts a string set − {0, 1, 01}</p>
<h2>Removal of Null Moves from Finite Automata</h2>
<p>If in an NDFA, there is ϵ-move between vertex X to vertex Y, we can remove it using the following steps −</p>
<p><b>Problem</b></p>
<p>Convert the following NFA-ε to NFA without Null move.</p>
<p><b><i>Solution</i></b></p>
<p><b>Step 1</b> −</p>
<p><b>Step 2</b> −</p>
<p><b>Step 3</b> −</p>
<p><b>Step 4</b> −</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Pumping Lemma For Regular Grammars</title>
<h1>Pumping Lemma For Regular Grammars</h1>
<h3>Theorem</h3>
<p>Let L be a regular language. Then there exists a constant <b>‘c’</b> such that for every string <b>w</b> in <b>L</b> −</p>
<p>We can break <b>w</b> into three strings, <b>w = xyz</b>, such that −</p>
<h2>Applications of Pumping Lemma</h2>
<p>Pumping Lemma is to be applied to show that certain languages are not regular. It should never be used to show a language is regular.</p>
<p>If <b>L</b> is regular, it satisfies Pumping Lemma.</p>
<p>If <b>L</b> does not satisfy Pumping Lemma, it is non-regular.</p>
<h2>Method to prove that a language L is not regular</h2>
<p>At first, we have to assume that <b>L</b> is regular.</p>
<p>So, the pumping lemma should hold for <b>L</b>.</p>
<p>Use the pumping lemma to obtain a contradiction −</p>
<p>Select <b>w</b> such that <b>|w| ≥ c</b></p>
<p>Select <b>y</b> such that <b>|y| ≥ 1</b></p>
<p>Select <b>x</b> such that <b>|xy| ≤ c</b></p>
<p>Assign the remaining string to <b>z.</b></p>
<p>Select <b>k</b> such that the resulting string is not in <b>L.</b></p>
<p><b>Hence L is not regular.</b></p>
<p><b>Problem</b></p>
<p>Prove that <b>L = {a<sup>i</sup>b<sup>i</sup> | i ≥ 0}</b> is not regular.</p>
<p><b><i>Solution</i></b> −</p>
<p>At first, we assume that <b>L</b> is regular and n is the number of states.</p>
<p>Let w = <i>a<sup>n</sup>b<sup>n</sup></i>. Thus |w| = 2n ≥ n.</p>
<p>By pumping lemma, let w = xyz, where |xy| ≤ n.</p>
<p>Let x = a<sup>p</sup>, y = a<sup>q</sup>, and z = a<sup>r</sup>b<sup>n</sup>, where p + q + r = n, p ≠ 0, q ≠ 0, r ≠ 0. Thus |y| ≠ 0.</p>
<p>Let k = 2. Then xy<sup>2</sup>z = a<sup>p</sup>a<sup>2q</sup>a<sup>r</sup>b<sup>n</sup>.</p>
<p>Number of as = (p + 2q + r) = (p + q + r) + q = n + q</p>
<p>Hence, xy<sup>2</sup>z = a<sup>n+q</sup> b<sup>n</sup>. Since q ≠ 0, xy<sup>2</sup>z is not of the form a<sup>n</sup>b<sup>n</sup>.</p>
<p>Thus, xy<sup>2</sup>z is not in L. Hence L is not regular.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>DFA Complement</title>
<h1>DFA Complement</h1>
<p>If (Q, ∑, δ, q<sub>0</sub>, F) be a DFA that accepts a language L, then the complement of the DFA can be obtained by swapping its accepting states with its non-accepting states and vice versa.</p>
<p>We will take an example and elaborate this below −</p>
<p>This DFA accepts the language</p>
<p>over the alphabet</p>
<p>So, RE = a<sup>+</sup>.</p>
<p>Now we will swap its accepting states with its non-accepting states and vice versa and will get the following −</p>
<p>This DFA accepts the language</p>
<p>over the alphabet</p>
<p><b>Note</b> − If we want to complement an NFA, we have to first convert it to DFA and then have to swap states as in the previous method.</p>
<p>© Copyright 2017. All Rights Reserved.</p>
<title>Context-Free Grammar Introduction</title>
<h1>Context-Free Grammar Introduction</h1>
<p><b><i>Definition</i></b> − A context-free grammar (CFG) consisting of a finite set of grammar rules is a quadruple <b>(N, T, P, S)</b> where</p>
<p><b>N</b> is a set of non-terminal symbols.</p>
<p><b>T</b> is a set of terminals where <b>N ∩ T = NULL.</b></p>
<p><b>P</b> is a set of rules, <b>P: N → (N ∪ T)*</b>, i.e., the left-hand side of the production rule <b>P</b> does have any right context or left context.</p>
<p><b>S</b> is the start symbol.</p>
<p><b>Example</b></p>
<h2>Generation of Derivation Tree</h2>
<p>A derivation tree or parse tree is an ordered rooted tree that graphically represents the semantic information a string derived from a context-free grammar.</p>
<h3>Representation Technique</h3>
<p><b>Root vertex</b> − Must be labeled by the start symbol.</p>
<p><b>Vertex</b> − Labeled by a non-terminal symbol.</p>
<p><b>Leaves</b> − Labeled by a terminal symbol or ε.</p>