# S ::= A eof
# A ::= E a
#     | E b
# E ::= c E d
#     | e

ra = rule("A")
rs = rule("S").ele("A").ele("eof")
print rs

re = rule("E")
ra.ele("E").ele("a")
ra.alt().ele("E").ele("b")
print ra

re.ele("c").ele("E").ele("d")
re.alt().ele("e")
print re

globals_holder.a_net = atn()

rs.merge_to_atn(globals_holder.a_net)
ra.merge_to_atn(globals_holder.a_net)
re.merge_to_atn(globals_holder.a_net)

d_a = create_dfa(ra.get_start_state(globals_holder.a_net))

globals_holder.a_net.to_png("A")

d_a.to_png("A_dfa")
Beispiel #2
0
# multi-rule LL(1)
# S ::= A $
# A ::= a c+
#     | B
# B ::= b c+
#     | d c*
ra = rule("A")
rs = rule("S").ele("A").ele("eof")
rb = rule("B")

ra.ele("a").kc("c")
ra.alt().ele("B")

rb.ele("b").kc("c")
rb.alt().ele("d").ks("c")

globals_holder.a_net = atn()
ra.merge_to_atn(globals_holder.a_net)
globals_holder.a_net = rb.merge_to_atn(globals_holder.a_net)
rs.merge_to_atn(globals_holder.a_net)

s_dnet = create_dfa(rs.get_start_state(globals_holder.a_net))
a_dnet = create_dfa(ra.get_start_state(globals_holder.a_net))
b_dnet = create_dfa(rb.get_start_state(globals_holder.a_net))

globals_holder.a_net.to_png("A")
s_dnet.to_png("S_dfa")
a_dnet.to_png("A_dfa")
b_dnet.to_png("B_dfa")
Beispiel #3
0
from algos import create_dfa, globals_holder
from atn_creation import rule
from datastructure import atn

# LL(3) grammar
# S ::= A $
# A ::= a b c d?
#     | a b e d+
r = rule('A')
rs = rule('S').ele('A').ele('eof')
print rs
r.ele('a').ele('b').ele('c').opt('d')
r.alt().ele('a').ele('b').ele('e').kc('d')
print r
globals_holder.a_net = atn()
r.merge_to_atn(globals_holder.a_net)
rs.merge_to_atn(globals_holder.a_net)

d_s = create_dfa(rs.get_start_state(globals_holder.a_net))
d_net = create_dfa(r.get_start_state(globals_holder.a_net))

globals_holder.a_net.to_png('A')
d_s.to_png("S_dfa")
d_net.to_png('A_dfa')
Beispiel #4
0
rc.ele('e').ele('f').ele('g').ele('D')
rc.alt().ele('e').ele('f').ele('g').ele('h')
print rc

rd.ele('i').ele('j').ele('k').ele('l')
rd.alt().ele('i').ele('j').ele('k').ele('m')
print rd

# gen atns
globals_holder.a_net = atn()
ra.merge_to_atn(globals_holder.a_net)
rb.merge_to_atn(globals_holder.a_net)
rc.merge_to_atn(globals_holder.a_net)
rd.merge_to_atn(globals_holder.a_net)
rs.merge_to_atn(globals_holder.a_net)

# gen dfas
d_a = create_dfa(ra.get_start_state(globals_holder.a_net))
d_b = create_dfa(rb.get_start_state(globals_holder.a_net))
d_c = create_dfa(rc.get_start_state(globals_holder.a_net))
d_d = create_dfa(rd.get_start_state(globals_holder.a_net))
d_s = create_dfa(rs.get_start_state(globals_holder.a_net))

globals_holder.a_net.to_png("A")

d_s.to_png("S_dfa")
d_a.to_png("A_dfa")
d_b.to_png("B_dfa")
d_c.to_png("C_dfa")
d_d.to_png("D_dfa")