本文整理汇总了Python中scanner.Scanner.next_token方法的典型用法代码示例。如果您正苦于以下问题:Python Scanner.next_token方法的具体用法?Python Scanner.next_token怎么用?Python Scanner.next_token使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scanner.Scanner
的用法示例。
在下文中一共展示了Scanner.next_token方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from scanner import Scanner [as 别名]
# 或者: from scanner.Scanner import next_token [as 别名]
class Parser:
def __init__(self, src_file, opened=False):
self.opened = opened
self.scanner = Scanner(src_file, opened)
self.look_ahead_one = lambda : self.scanner.look_ahead()[0]
self.result = None
self.pcode = Pcode()
self.table = Table()
self.error = None
self.tx = []
self.program()
def get_formated_ast(self):
ast = AST(self.result)
tree = ast.generate(self.result)
return ast.get_formated_ast(tree)
def get_formated_pcode(self):
result = ''
for key, val in enumerate(self.pcode.pcode):
result += str(key).zfill(4) + ' ' + val.f + ' ' + str(val.l).rjust(4) + ' ' + str(val.a).rjust(4) + '\n'
return result
def get_formated_table(self):
result = []
for i in self.table.table:
result.append([i.name, i.kind, str(i.val), str(i.level), str(i.addr)])
return result
def _expect(self, token_type):
next_token = self.scanner.next_token()
if next_token.token_type == token_type:
return next_token
else:
if not self.error:
self.pcode.error = True
self.error = 'line %d: expect %s, not %s(%s)' % (next_token.line_number, token_type, next_token.token_type, next_token.content)
else:
self.error += '\nline %d: expect %s, not %s(%s)' % (next_token.line_number, token_type, next_token.token_type, next_token.content)
def program(self, lev=0):
self.result = ['<program>', self.block(lev), self._expect(Token.DOT), self._expect(Token.EOF)]
def block(self, lev):
result = ['<block>']
dx = 3
tx = self.table.len()
cx = self.pcode.len()
self.pcode.add(PcodeItem(PcodeItem.JMP, 0, 0))
if self.look_ahead_one().token_type == Token.CONST:
result.extend(
map(self._expect, (Token.CONST, Token.IDENT, Token.EQ, Token.NUMBER))
)
self.table.add(TableItem(result[-3].content, 'const', int(result[-1].content)))
while self.look_ahead_one().token_type == Token.COMMA:
result.extend(
map(self._expect, (Token.COMMA, Token.IDENT, Token.EQ, Token.NUMBER))
)
self.table.add(TableItem(result[-3].content, 'const', int(result[-1].content)))
result.append(self._expect(Token.SEMICOLON))
if self.look_ahead_one().token_type == Token.VAR:
result.extend(
map(self._expect, (Token.VAR, Token.IDENT))
)
if result[-1]:
self.table.add(TableItem(result[-1].content, 'var', '', lev, dx))
dx += 1
while self.look_ahead_one().token_type == Token.COMMA:
result.extend(
map(self._expect, (Token.COMMA, Token.IDENT))
)
self.table.add(TableItem(result[-1].content, 'var', '', lev, dx))
dx += 1
result.append(self._expect(Token.SEMICOLON))
while self.look_ahead_one().token_type == Token.PROCEDURE:
result.extend(
map(self._expect, (Token.PROCEDURE, Token.IDENT, Token.SEMICOLON))
)
self.tx.append(self.table.len())
self.table.add(TableItem(result[-2].content, 'procedure', level=lev))
#self.table.add(TableItem(result[-2].content, 'procedure', level=lev, addr=self.pcode.len() + 1))
#self.table.add(TableItem(result[-2].content, 'procedure', level=lev, addr=dx))
#dx += 1
result.append(self.block(lev+1))
result.append(self._expect(Token.SEMICOLON))
if len(self.tx) != 0:
self.table.table[self.tx.pop()].addr = self.pcode.len()
self.pcode.pcode[cx].a = self.pcode.len() #return to main procedure
#if self.table.len() > tx:
#self.table.table[tx].addr = self.pcode.len()
#self.table.table[tx].size = dx
#.........这里部分代码省略.........