/
main.py
executable file
·57 lines (55 loc) · 1.42 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#! /usr/bin/env
from grammar import Grammar
from cyk import CYKChart
from viz import genVIZ
from os import system
from os import sep
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)
if __name__ == '__main__':
import sys
main(sys.argv)