Ejemplo n.º 1
0
def test(string='balance.txt', spec='g1.txt'):
    from balance import BalanceLexer
    G = Grammar()
    source = open(spec,'r')
    G.generate(source)
    G.bnf2cnf()
    print "grammer==",G
    lexer= BalanceLexer()
    balance=open(string,'r')
    lexer.scanFile(balance)
    S=lexer.getStream()
    print "stream ===",S
    C=CYKChart()
    C.Build_CYK_Chart(G,S)
    print C
Ejemplo n.º 2
0
def solve():
    G = Grammar()
    source = open("cky.txt",'r')
    G.generate(source)
    G.bnf2cnf()
    print "grammer==",G
    lexer= Telescope()
    balance=open('telescope','r')
    lexer.scanFile(balance)
    S=lexer.getStream()
    print "stream ===",S
    C=CYKChart()
    C.Build_CYK_Chart(G,S)
    print C
    genDot(C,"cky.dot")
    system("dot -Tjpg cky.dot -o cky.jpg")
    print "cky.jpg created"
Ejemplo n.º 3
0
def main(args):
    lexarname =None
    gramarspec =None
    inputfile =None
    outputtype =None
    outputfile =None
    argc= len(args)
    if argc == 1: 
        print "usage: main.py  lexarname, gramarspec, inputfile,[output-type] [output-file ]\n"
        return
    if argc > 1 : 
        lexarname = args[1]
    if argc > 2 :
        gramarspec = args[2]
    if argc > 3 : 
        inputfile = args[3]
    if argc > 4 :
        outputtype=args[4]
    else:
        outputtype="dot"
    if argc > 5 :
        outputfile = args[5]
    else:
        outputfile = inputfile
    G = Grammar()
    source = open(gramarspec,'r')
    G.generate(source)
    G.bnf2cnf()
    print "grammer==",G
    if sep in lexarname:
        lexarname = lexarname.replace(sep,".")
    lexerclass=__import__(lexarname)
    lexer=lexerclass.Lexer()
    lexer.scan(inputfile)
    S=lexer.getStream()
    print "stream ===",S
    C=CYKChart()
    C.Build_CYK_Chart(G,S)
    print C
    print C.graph
    if outputtype=="dot":
        genDot(C,outputfile)
        system("dot -Tjpg %s  -o %s "%(outputfile, outputfile)) # todo, see if dot takes STDIN so I can pipe this to it 
        print "%s generated"%(outputfile)
    elif outputtype=="js":
       genVIZ(C,outputfile)
Ejemplo n.º 4
0
def solve():
    from os import system
    G = Grammar()
    source = open("infix.txt",'r')
    G.generate(source)
    G.bnf2cnf()
    print "grammer==",G
    lexer= Infix()
    balance=open('input.txt','r')
    lexer.scanFile(balance)
    S=lexer.getStream()
    print "stream ===",S
    C=CYKChart()
    C.Build_CYK_Chart(G,S)
    print C
    print C.graph
    genDot(C,"infix.dot")
    system("dot -Tjpg infix.dot -o infix.jpg")
    print "infix.jpg created"