本文整理汇总了Python中grammar.Grammar.add_production方法的典型用法代码示例。如果您正苦于以下问题:Python Grammar.add_production方法的具体用法?Python Grammar.add_production怎么用?Python Grammar.add_production使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类grammar.Grammar
的用法示例。
在下文中一共展示了Grammar.add_production方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: buildgrammar
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import add_production [as 别名]
def buildgrammar(self):
g = Grammar()
g.nonterminals = self.states
g.terminals = self.symbols
g.startsymbol = str(self.initialstate)
tf = self.transitions
if len(tf) > 0:
for t in tf:
if len(t) == 3:
g.add_production(t[0], t[1] + t[2])
if t[2] in self.finalstates:
g.add_production(t[0], t[1] + '')
if g.starsymbol in self.finalstates:
g.add_production(g.starsymbol, 'e')
self.grammar = g
print 'Nonterminals: ', self.grammar.nonterminals
print 'Terminals: ', self.grammar.terminals
print 'Start symbol: ', self.grammar.startsymbol
print 'Productions: ', self.grammar.productions
return
示例2: __init__
# 需要导入模块: from grammar import Grammar [as 别名]
# 或者: from grammar.Grammar import add_production [as 别名]
class Parser:
def __init__(self, source):
self.token = ''
self.rule = []
self.rule_list = []
self.terminal_name = {}
self.terminal_group = {}
self.start_token = ''
self.grammar = None
self.l = Lexer(source)
self.parse()
def match(self, m):
if m == self.token[0]:
self.token = self.l.next()
else:
print 'match error', m
def parse(self):
self.grammar = Grammar()
self.token = self.l.next()
self.terminals()
self.start()
self.productions()
def terminals(self):
group = 0
while self.token[0] == 'TOKEN':
self.match('TOKEN')
while self.token[0] == 'TERM':
self.grammar.add_terminal(self.token[1], group)
self.match('TERM')
group += 1
def start(self):
self.match('START')
self.start_token = self.token[1]
self.match('NONTERM')
def productions(self):
self.match('BLOCK')
while self.token[0] != 'BLOCK':
self.left_side()
self.grammar.start_token = self.start_token
def left_side(self):
self.rule_list = []
production_name = self.token[1]
self.match('NONTERM')
self.match('COLON')
self.right_side()
self.grammar.add_production(production_name, self.rule_list)
self.match('SEMI')
def right_side(self):
rule = []
first = True
while self.token[0] != 'SEMI':
if self.token[0] == 'TERM':
if first == True:
first = False
rule.append(self.token[1])
self.match('TERM')
elif self.token[0] == 'NONTERM':
if first == True:
first = False
rule.append(self.token[1])
self.match('NONTERM')
elif self.token[0] == 'CHAR':
if first == True:
first = False
rule.append(self.token[1])
self.match('CHAR')
elif self.token[0] == 'PIPE':
if first == True:
self.rule_list.append(Rule(['']))
first = False
self.match('PIPE')
else:
self.rule_list.append(Rule(rule))
rule = []
self.match('PIPE')
if self.token[0] == 'PIPE':
self.rule_list.append(Rule(['']))
self.match(self.token[0])
elif self.token[0] == 'SEMI':
rule = ['']
else:
print 'right_side:', self.token
self.rule_list.append(Rule(rule))
def printer(self, fo = None):
pp = PrettyPrint(self.grammar)
pp.printer(fo)