## Part I
1. What's the purpose of finite automata in lexical analysis?
> sol:
> Finite automata (finite-state machines) are a mathematical way of describing particular kinds of algorithms.
> (
> - describe the process of recognizing patterns in input strings.
> - construct scanners.
> )

2. What's the main difference between NFA and DFA?
> sol:
> A deterministic finite automaton (DFA) is a special case of an NFA:
> - There are no moves on input $\epsilon$ 
> - For each state s and input symbol $a$, there is exactly one edge out of s labeled $a$

## Part II
给定字符集，写出符合要求的正则表达式
1. $\Sigma = \{a,b\}$ ,all strings of the digits that begin in $a$ and end in $b$
> 原题描述如上，感觉写的有问题，应该是 of the letters 
> sol: $a(a|b)^*b$

2. $\Sigma = \{a,b\}$ ,all strings of the letters that begin in $b$ and end in $aaa$
> sol: $b(a|b)^*aaa$

3. $\Sigma = \{a,b\}$ ,all strings that contain no three consecutive $a's$
> sol: $(b|ab|aab)^*$

4. $\Sigma = \{a,b,c\}$ ,all strings that contain at least one $a$ and at least one $c$
> sol: $(a|b|c)^* a (a|b|c)^* c (a|b|c)^* | (a|b|c)^* c (a|b|c)^* a (a|b|c)^*$ 

5. $\Sigma = \{a,b,c\}$ ,all strings that contain an even number of $a's$
> sol: $((b|c)^* a (b|c)^* a (b|c)^*)^*$

## Part III
1. Use Thompson's construction to convert the regular expression into an NFA.
- $(a|b|\epsilon)^*(a|bb)(ab|b)^*$
> Thompson构造只需要记住并和star运算，分别是
> ![[Pasted image 20250420233649.png]] ![[Pasted image 20250420233757.png]]
> sol:![[65fdf195f45b1aeafbf481712d55cee4_720.jpg]]
> 记得给状态标号，说明起始终止状态
2. 使用最小化DFA算法，留下计算过程
> 最小化DFA算法注意在划分过程中，对任意输入到达 $\Pi$ 同一组就仍在同一组，也就是说如果存在某个输入到达不了同一组，就将其划分开。
> sol: 
   最开始，按照初始和结束状态划分为$\Pi_{init} = \{\{1,2,3,4\},\{5,6,7\}\}$
> (1)对于 $\{1,2,3,4\}$，输入 $a$ 区分 $\{1,2\},\{3,4\}$ ，$\Pi_1 = \{\{1,2\},\{3,4\},\{5,6,7\}\}$
> (2)对于 $\{1,2\}$，不区分，$\Pi_2 =\Pi_1$
> (3)对于 $\{3,4\}$，输入 $a$ 区分 $\{\{3\},\{4\}\}$，$\Pi_3 = \{\{1,2\},\{3\},\{4\},\{5,6,7\}\}$
> (4)对于 $\{5,6,7\}$，输入 $a,b$ 同时区分 $\{\{5\},\{6,7\}\}$，$\Pi_4 = \{\{1,2\},\{3\},\{4\},\{5\},\{6,7\}\}$
> (5)对于 $\{1,2\}$，不区分，$\Pi_5 = \Pi_4$
> (6)对于 $\{3,4\}$，不区分，$\Pi_6 = \Pi_5$
> 综上，$\Pi_{final} = \{\{1,2\},\{3\},\{4\},\{5\},\{6,7\}\}$
> 选取 1,3,4,5,6 作为代表，重新构造 DFA，如下：
> ![[2e30cac5f4cdc36f375957eda575615c_720.jpg]]

## Part IV
给定正则表达式如下：
$(aa|b)^*a(a|b)^*$
1. 用T's 构造法将RE转为NFA
> sol: 
> ![[e752e16a1235557adba041966dfbdfd9_720.jpg]]

2. 用子集构造法将上述NFA构造对应的DFA
> 子集构造法就是
> ![[Pasted image 20250421004305.png]]
> sol:
> ![[bd95f800ea27d47937d023e4711a3d24_720.jpg]]
3. 用DFA最小化算法最小化
> sol:
> ![[3005147e273c437550ca275dd175c0a7_720.jpg]]

4. DFA转伪代码，其中第一层循环中case语句判定节点，第二层循环中case语句判定转移字符
```
bool isAccept() {
	curState = A, curChar = nxtChar();
	while (curChar != EOF) {
		// curState = trans(curState, curChar);
		switch(curState) {
			case A: 
				switch(curChar) {
					case a:
						curState = B;
						break;
					case b:
						curState = A;
						break;
					default:
						curState = NULL;
						break;
				}
				break;
			case B:
				switch(curChar) {
					case a:
						curState = B;
						break;
					case b:
						curState = B;
						break;
					default:
						curState = NULL;
						break;
				}
			default:
				curState = NULL;
				break;
		}
		if (curState == NULL) {
			return false;
		}
		curChar = nxtChar();
	}
	if (curState in F) {
		return true;	
	} else {
		return false;
	}
}
```
