Esempio n. 1
0
def test_build():
    graph = StateGraph(p.start_symbol, p.rules, 1)
    graph.build()
    st = SyntaxTable(1)
    st.build(graph)
    for key in syntaxtable.keys():
        assert st.table[key] == syntaxtable[key]
Esempio n. 2
0
def test_build():
    graph = StateGraph(p.start_symbol, p.rules, 1)
    graph.build()
    st = SyntaxTable(1)
    st.build(graph)
    for key in syntaxtable.keys():
        assert st.table[key] == syntaxtable[key]
Esempio n. 3
0
def test_build():
    graph = StateGraph(p.start_symbol, p.rules, 1)
    graph.build()
    st = SyntaxTable(None, 1)
    st.build(graph)
    for i in range(len(syntaxtable)):
        assert st.table[i] == syntaxtable[i]
Esempio n. 4
0
def test_build():
    pytest.skip("Due to nondeterminism this is difficult to test.")
    graph = StateGraph(p.start_symbol, p.rules, 1)
    graph.build()
    st = SyntaxTable(None, 1)
    st.build(graph)
    for i in range(len(syntaxtable)):
        assert st.table[i] == syntaxtable[i]
Esempio n. 5
0
a = Terminal("a")
b = Terminal("b")
c = Terminal("c")
Z = Nonterminal("Z")
S = Nonterminal("S")
A = Nonterminal("A")

None_S = State(Production(None, [S]), 0)
S_Sb = State(Production(S, [S, b]), 0)
S_bAa = State(Production(S, [b, A, a]), 0)
A_aSc = State(Production(A, [a, S, c]), 0)
A_a = State(Production(A, [a]), 0)
A_aSb = State(Production(A, [a, S, b]), 0)

graph = StateGraph(p.start_symbol, p.rules, 1)
graph.build()


def move_dot(state, i):
    temp = state.clone()
    temp.d += i
    return temp


def test_state_0():
    # State 0
    # Z ::= .S
    # S ::= .Sb
    # S ::= .bAa
    s = StateSet()
    s.add(None_S)
Esempio n. 6
0
a = Terminal("a")
b = Terminal("b")
c = Terminal("c")
Z = Nonterminal("Z")
S = Nonterminal("S")
A = Nonterminal("A")

None_S  = State(Production(None, [S]), 0)
S_Sb    = State(Production(S, [S, b]), 0)
S_bAa   = State(Production(S, [b, A, a]), 0)
A_aSc   = State(Production(A, [a, S, c]), 0)
A_a     = State(Production(A, [a]), 0)
A_aSb   = State(Production(A, [a, S, b]), 0)

graph = StateGraph(p.start_symbol, p.rules, 1)
graph.build()

def move_dot(state, i):
    temp = state.clone()
    temp.d += i
    return temp

def test_state_0():
    # State 0
    # Z ::= .S
    # S ::= .Sb
    # S ::= .bAa
    s = StateSet()
    s.add(None_S)
    s.add(S_Sb)
    s.add(S_bAa)
Esempio n. 7
0
def test_graph():
    pytest.skip("conversion is not working yet")
    graph = StateGraph(p.start_symbol, p.rules, 1)
    graph.build()

    s0 = StateSet([
        LR1Element(Production(None, [S]), 0, set([f])),
        LR1Element(Production(S, [b, A, c]), 0, set([f])),
    ])

    s1 = StateSet([
        LR1Element(Production(S, [b, A, c]), 1, set([f])),
        LR1Element(Production(A, [S]), 0, set([c])),
        LR1Element(Production(A, [a]), 0, set([c])),
        LR1Element(Production(S, [b, A, c]), 0, set([c])),
    ])

    s2 = StateSet([
        LR1Element(Production(S, [b, A, c]), 1, set([c])),
        LR1Element(Production(A, [S]), 0, set([c])),
        LR1Element(Production(A, [a]), 0, set([c])),
        LR1Element(Production(S, [b, A, c]), 0, set([c])),
    ])

    s3 = StateSet([
        LR1Element(Production(A, [a]), 1, set([c])),
    ])

    s4 = StateSet([
        LR1Element(Production(None, [S]), 1, set([f])),
    ])

    s5 = StateSet([
        LR1Element(Production(S, [b, A, c]), 2, set([f])),
    ])

    s6 = StateSet([
        LR1Element(Production(S, [b, A, c]), 2, set([c])),
    ])

    s7 = StateSet([
        LR1Element(Production(A, [S]), 1, set([c])),
    ])

    s8 = StateSet([
        LR1Element(Production(S, [b, A, c]), 3, set([f])),
    ])

    s9 = StateSet([
        LR1Element(Production(S, [b, A, c]), 3, set([c])),
    ])

    assert len(graph.state_sets) == 10
    assert s0 in graph.state_sets
    assert s1 in graph.state_sets
    assert s2 in graph.state_sets
    assert s3 in graph.state_sets
    assert s4 in graph.state_sets
    assert s6 in graph.state_sets
    assert s7 in graph.state_sets
    assert s8 in graph.state_sets
    assert s9 in graph.state_sets

    graph.convert_lalr()

    s0 = StateSet([
        LR1Element(Production(None, [S]), 0, set([f])),
        LR1Element(Production(S, [b, A, c]), 0, set([f])),
    ])

    s1 = StateSet([
        LR1Element(Production(S, [b, A, c]), 1, set([f, c])),
        LR1Element(Production(A, [S]), 0, set([c])),
        LR1Element(Production(A, [a]), 0, set([c])),
        LR1Element(Production(S, [b, A, c]), 0, set([c])),
    ])

    s2 = StateSet([
        LR1Element(Production(A, [a]), 1, set([c])),
    ])

    s3 = StateSet([
        LR1Element(Production(None, [S]), 1, set([f])),
    ])

    s4 = StateSet([
        LR1Element(Production(S, [b, A, c]), 2, set([f, c])),
    ])

    s5 = StateSet([
        LR1Element(Production(A, [S]), 1, set([c])),
    ])

    s6 = StateSet([
        LR1Element(Production(S, [b, A, c]), 3, set([f, c])),
    ])

    assert len(graph.state_sets) == 7
    assert s0 in graph.state_sets
    assert s1 in graph.state_sets
    assert s2 in graph.state_sets
    assert s3 in graph.state_sets
    assert s4 in graph.state_sets
    assert s6 in graph.state_sets