编译原理第三次作业

3.3.5

(1) 正则表达式:

define) other -> {bcdfghjklmnpqrstvwxyz}

answer:other* a(other|a)* e(other|e)* i(other|i)* o(other|o)* u(other|u)*

NFA: (由python代码生成,除了这个下面的DFA是基于开源工具绘制的)

from graphviz import Digraph

def create_nfa():
    nfa = Digraph(format="png", node_attr={"shape": "circle"}, edge_attr={"label": "ε"})
    nfa.attr(rankdir="LR")  # 水平布局

   
    nfa.attr(size="64,64")  
    nfa.attr(ranksep="2")  
    nfa.attr(nodesep="1")  
    nfa.attr(fontsize="12")  

    # 定义状态
    states = ["start", "q0", "q1", "q2", "q3", "q4", "q5", "final"]
    for state in states:
        nfa.node(state)
    
    # 定义其他字符集合
    other = "bcdfghjklmnpqrstvwxyz"
    
    # 初始状态到q0的ε转移
    nfa.edge("start", "q0", label="ε")
    
    # q0: other* -> ε转移
    nfa.edge("q0", "q0", label=f"{other}|ε")
    
    # q0 -> q1: a(other|a)*
    nfa.edge("q0", "q1", label="a")
    nfa.edge("q1", "q1", label=f"{other}|a")
    
    # q1 -> q2: e(other|e)*
    nfa.edge("q1", "q2", label="e")
    nfa.edge("q2", "q2", label=f"{other}|e")
    
    # q2 -> q3: i(other|i)*
    nfa.edge("q2", "q3", label="i")
    nfa.edge("q3", "q3", label=f"{other}|i")
    
    # q3 -> q4: o(other|o)*
    nfa.edge("q3", "q4", label="o")
    nfa.edge("q4", "q4", label=f"{other}|o")
    
    # q4 -> q5: u(other|u)*
    nfa.edge("q4", "q5", label="u")
    nfa.edge("q5", "q5", label=f"{other}|u")
    
    # q5 -> final: ε转移
    nfa.edge("q5", "final", label="ε")
    
    return nfa


nfa = create_nfa()
nfa.render("nfa_diagram", view=True)

NFA转DFA表格:

l la le li lo lu lother
q0 q1 q0
q1 q1 q2 q1
q2 q2 q3 q2
q3 q3 q4 q3
q4 q4 q5 q4
q5 q5 q5

(6)
正则表达式:
(b|a (bb|aa)* (ba|ab)) (bb|aa|(ba|ab) (bb|aa)* (ba|ab)) *
NFA:

NFA转DFA表格:

l la lb
23 0 2 23 0 2 3 13 12 22 4 8 23 0 2 1 62
... ... ...

(8)
正则表达式:
b* (a+b?)*
NFA:

NFA转DFA表格:

l la lb
2 0 9 5 3 2 0 9 5 3 4 8 6 2 0 9 5 3 1
2 0 9 5 3 4 8 6 2 0 9 5 3 4 8 6 2 0 9 5 3 4 8 6 7 1
2 0 9 5 3 1 2 0 9 5 3 4 8 6 1 2 0 9 5 3 1
2 0 9 5 3 4 8 6 1 2 0 9 5 3 4 8 6 1 7

(9)
正则表达式:
b* | b* a + | b* a + b a*
NFA:

NFA转DFA表格:

l la lb
21 9 12 2 5 0 3 10 8 6 15 13 21 9 12 2 5 0 3 10 8 6 15 13 1 11
21 9 12 2 5 0 3 10 8 6 15 13 1 11 21 9 12 2 5 0 3 10 8 6 15 13 1 11 7 14 16 21 9 12 2 5 0 3 10 8 6 15 13 1 11
21 9 12 2 5 0 3 10 8 6 15 13 1 11 7 14 16 21 9 12 2 5 0 3 10 8 6 15 13 1 11 7 14 16 21 9 12 2 5 0 3 10 8 6 15 13 1 11 7 14 16 17 20 18
21 9 12 2 5 0 3 10 8 6 15 13 1 11 7 14 16 17 20 18 21 9 12 2 5 0 3 10 8 6 15 13 1 11 7 14 16 17 20 18 19 21 9 12 2 5 0 3 10 8 6 15 13 1 11 7 14 16 17 20 18
posted @ 2025-03-13 22:39  哎哎呦呦喂喂0211  阅读(19)  评论(0)    收藏  举报