index.md (3550B)
1 +++ 2 title = "Lecture 3" 3 template = "page-math.html" 4 +++ 5 6 ## Grammars (contd) 7 8 ### Right linear grammars (contd) 9 10 1. Strict right linear grammars 11 12 Let G be right linear grammar G = (V, T, S, P) There is a strictly 13 right linear grammar H such that L(G) = L(H): 14 15 1. Assume we have production rule γ of form A → u(B) with A,B ∈ V 16 and u ∈ T<sup>\*</sup> with \|u\| \> 1. Then u = aw for some a ∈ T and w 17 ∈ T\^+. 18 2. Let X be a fresh variable, add X to V and split rule γ into: 19 - A → aX 20 - X → w(B) 21 22 Then A → aX → aw(B) = u(B). So L(G) = L(H). 23 3. Repeat splitting until \|u\| ≤ 1 for all rules. 24 25 2. Prove right linear grammars ⇔ regular languages 26 27 Thm: language L is regular ⇔ there is right linear grammar G with 28 L(G) = L 29 30 Proof ⇒: 31 32 - translate NFA/DFAs into right linear grammars: use states as 33 nonterminals and transitions as rules 34 35 Proof ⇐: 36 37 - build NFA from right linear grammar 38 39 ### Left linear grammars 40 41 Left linear: all production rules only have non-terminals on left side 42 43 Thm: language L is regular iff there is left linear grammar G with L(G) = L. 44 45 Proof: L regular ⇔ L<sup>R</sup> is regular ⇔ right linear grammar for L<sup>R</sup> ⇔ 46 left linear grammar for L (reverse all production rules) 47 48 Mixing right and left linear rules doesn't mean that the generated 49 language will be regular! 50 51 ## Regular expressions 52 53 Definition of regex over alphabet Σ: 54 55 - ∅, λ are regular expressions 56 - a is regular expression for ever a ∈ Σ 57 - r<sub>1</sub> + r<sub>2</sub> is regular expression for all regexes r<sub>1</sub> and r<sub>2</sub> 58 - $r_1 \cdot r_2$ is regex for all regexes r<sub>1</sub> and r<sub>2</sub> 59 - r<sup>*</sup> is regex for all regexes r 60 61 ### Regex to NFA 62 63 For every regex r, build NFA such that: 64 65 - L(M) = L(r) 66 - M has precisely one final state (different from the starting state) 67 68 ![](86315079efdd412f82bc1195a96910da.png) 69 70 ### NFA to regex 71 72 1. Transform M st there is only one final state. If M has multiple 73 final states: 74 75 - add fresh state q<sub>f</sub> to M 76 - for every final state q in M, add arrow $q \xrightarrow{\lambda} q_f$ to M 77 - define q<sub>f</sub> as only final state 78 79 2. Remove all double arrows. Use transition graphs with regexes as 80 labels. If there are e2 arrows from state q<sub>1</sub> to q<sub>2</sub> with labels 81 r<sub>1</sub> and r<sub>2</sub>, replace them by one arrow with label r<sub>1</sub> + r<sub>2</sub>. It 82 might be that q<sub>1</sub> == q<sub>2</sub>, then arrows are loops. 83 84 ![](576b1d4dbafe45399c236e5b6f40b60d.png) 85 86 3. As long as there are states other than starting and final state: 87 88 - remove one state q 89 90 - for all states q<sub>1</sub>,q<sub>2</sub> and arrows $q_1 \xrightarrow{r_1} q$ and $q \xrightarrow{r_2} q_2$, 91 add arrow from q<sub>1</sub> to q<sub>2</sub> as follows: 92 93 - if arrow $q \xrightarrow{r} q$: 94 95 ![](cb193f47c584401799c6bdedeb273245.png) 96 97 - otherwise: 98 99 ![](5f853b93cdad4c28bb70b297332f39a7.png) 100 101 4. If F ≠ *q*<sub>0</sub>, then transition graph is reduced: 102 103 ![](9fa836b5b1d246f4a1ad9c0ce6cb42d5.png) 104 105 ## Alternative descriptions of regular languages 106 107 These statements mean the same thing: 108 109 - there is DFA M with L(M) = L 110 - there is NFA M with L(M) = L 111 - there is right linear grammar G with L(G) = L 112 - there is left linear grammar G with L(G) = L 113 - there is regular expression r with L(r) = L