本文整理汇总了Python中grammar.Grammar.generate方法的典型用法代码示例。如果您正苦于以下问题:Python Grammar.generate方法的具体用法?Python Grammar.generate怎么用?Python Grammar.generate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类grammar.Grammar
的用法示例。
在下文中一共展示了Grammar.generate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import generate [as 别名]
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
示例2: generate
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import generate [as 别名]
def generate(input_str):
''' Parses an input string and returns another one
containing the generated program skeleton.
'''
HEADER, L, ENDCODE = parser.parse(input_str)
result = 'from skel import Grammar\n'
if HEADER is not None:
result += HEADER + '\n'
result = result + """
def generate(self):
"""
result = ''
grammar = Grammar(Parser.START_SYMBOL)
if L:
for T in L:
grammar.addRule(T)
result += grammar.generate(Parser.START_SYMBOL)
if ENDCODE is not None:
result += ENDCODE + '\n'
return result
示例3: solve
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import generate [as 别名]
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"
示例4: main
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import generate [as 别名]
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)
示例5: solve
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import generate [as 别名]
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"