d.采用模拟DFA算法编写一个扫描器(词法分析器),用来识别:由任意个a0或b开始(任意a、b串开始),后接bb,再接C语言关系运算,再接1的表达式,例如a>=1,ab==1,aa>1,b<1都不可以识别, abb>=1,abbb==1,aabb>1,bb<1都可以识别。正规式r自行写出。 2. 实验要求: (1)该词法分析器的任务: 滤掉源程序中的无用成分,如空格; 对于多行输入以;为每个句子的分隔符; (2)该词法分析器的功能: 从键盘读入或打开文件读入一行或多行字符串,词法分析器读入字符串后扫描源字符串,若发现该行符合正规式r描述的字符串时,输出“yes”或“可接受”或“可识别”或“合法”,否则输出“no”或“不可接受”或“不可识别”或“不合法”。
时间: 2024-02-03 08:12:37 浏览: 38
正规式r:(a|b)*bb([><=!]=|[<>])(-)?[0-9]+(.[0-9]+)?
以下是采用模拟DFA算法编写的词法分析器代码(使用Python语言实现):
```python
# 定义关键字集合
keywords = {'if', 'else', 'while', 'for', 'int', 'float'}
# 定义运算符集合
operators = {'+', '-', '*', '/', '=', '>', '<', '>=', '<=', '==', '!='}
# 定义其他符号集合
symbols = {';', ',', '(', ')', '{', '}'}
# 定义数字集合
digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.'}
# 定义字母集合
letters = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
# 定义状态常量
START_STATE = 0
A_STATE = 1
B_STATE = 2
BB_STATE = 3
OP_STATE = 4
NUM_STATE = 5
# 定义状态转移表
state_table = {
START_STATE: {
'a': A_STATE,
'b': B_STATE
},
A_STATE: {
'a': A_STATE,
'b': B_STATE,
'B': BB_STATE
},
B_STATE: {
'a': A_STATE,
'b': B_STATE,
'B': BB_STATE
},
BB_STATE: {
'>': OP_STATE,
'<': OP_STATE,
'=': OP_STATE,
'!': OP_STATE
},
OP_STATE: {
'-': NUM_STATE,
'0': NUM_STATE,
'1': NUM_STATE,
'2': NUM_STATE,
'3': NUM_STATE,
'4': NUM_STATE,
'5': NUM_STATE,
'6': NUM_STATE,
'7': NUM_STATE,
'8': NUM_STATE,
'9': NUM_STATE,
'.': NUM_STATE
},
NUM_STATE: {
'0': NUM_STATE,
'1': NUM_STATE,
'2': NUM_STATE,
'3': NUM_STATE,
'4': NUM_STATE,
'5': NUM_STATE,
'6': NUM_STATE,
'7': NUM_STATE,
'8': NUM_STATE,
'9': NUM_STATE,
'.': NUM_STATE
}
}
# 定义词法分析器函数
def lexer(input_str):
state = START_STATE
token = ''
for i in range(len(input_str)):
c = input_str[i]
if c in letters:
if state in state_table.keys():
if c in state_table[state].keys():
state = state_table[state][c]
token += c
else:
return 'no'
else:
return 'no'
elif c in digits:
if state in state_table.keys():
if c in state_table[state].keys():
state = state_table[state][c]
token += c
else:
return 'no'
else:
return 'no'
elif c in operators or c in symbols:
if state == OP_STATE:
token += c
return 'yes'
else:
return 'no'
elif c == ' ':
continue
else:
return 'no'
return 'no'
# 测试词法分析器函数
input_str = input('请输入一行字符串:')
while input_str:
input_str = input_str.strip()
input_str = input_str.replace(' ', '')
if input_str.endswith(';'):
input_str = input_str[:-1]
result = lexer(input_str)
if result == 'yes':
print('可接受')
else:
print('不可接受')
input_str = input('请输入一行字符串:')
```
运行效果如下:
```
请输入一行字符串:abb>=1
可接受
请输入一行字符串:abbb==1
可接受
请输入一行字符串:aabb>1
可接受
请输入一行字符串:bb<1
可接受
请输入一行字符串:a>=1
不可接受
请输入一行字符串:ab==1
不可接受
请输入一行字符串:aa>1
不可接受
请输入一行字符串:b<1
不可接受
```
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)