本文整理汇总了Python中lexer.Lexer.showNext方法的典型用法代码示例。如果您正苦于以下问题:Python Lexer.showNext方法的具体用法?Python Lexer.showNext怎么用?Python Lexer.showNext使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lexer.Lexer
的用法示例。
在下文中一共展示了Lexer.showNext方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from lexer import Lexer [as 别名]
# 或者: from lexer.Lexer import showNext [as 别名]
class Parser:
def __init__(self):
self.lexer = Lexer()
def getLexer(self):
return self.lexer
def setLexer(self, val):
self.lexer = val
def parsePrgm(self, filename):
f = open(filename)
ch = f.readlines()
self.lexer.tokenize(ch)
print("+++ PARSER +++")
while (self.getLexer().getStream() != []):
self.parseInstruction()
def expect(self, token_kind):
next_tok = self.lexer.getNext()
if next_tok.kind != token_kind:
print("\nParsing error on line {0} pos {1}".format(next_tok.pos[0], next_tok.pos[1]))
print("Expecting "+token_kind+", got "+next_tok.kind)
sys.exit(1)
return next_tok
def showNext(self):
return self.lexer.showNext()
def acceptIt(self):
token = self.lexer.getNext()
return token
def parseInstruction(self):
print("Parse instruction")
if self.showNext().kind in ["pnt", "vect", "line", "seg", "ang", "scal", "string"]:
self.parseDeclaration()
#elif self.showNext().kind == 'ident':
#self.parseAffectation()
elif self.showNext().kind in ["print"]:
self.parsePrint()
else:
print ("GOSH! Parsing error for Instruction ligne : ", self.showNext().pos)
sys.exit(0)
def parseDeclaration(self):
print ("Parse declaration")
if self.showNext().kind == 'pnt':
self.parseDeclPnt()
elif self.showNext().kind == 'line':
self.parseDeclLine()
elif self.showNext().kind == 'vect':
self.parseDeclVect()
elif self.showNext().kind == 'seg':
self.parseDeclSeg()
elif self.showNext().kind == 'scal':
self.parseDeclScal()
elif self.showNext().kind == 'string':
self.parseDeclString()
else:
print ("GOSH! Parsing error for Declaration ligne : ", self.showNext().pos)
sys.exit(0)
def parseDeclPnt(self):
print ("Parse declaration point")
self.expect('pnt')
self.expect('ident')
if self.showNext().kind != 'excl':
self.expect('colon')
self.parseObjPnt()
self.expect('excl')
def parseDeclLine(self):
print ("Parse declaration line")
self.expect('line')
self.expect('ident')
if self.showNext().kind != 'excl':
self.expect('colon')
self.parseObjLine()
self.expect('excl')
def parseDeclVect(self):
print ("Parse declaration vector")
self.expect('vect')
self.expect('ident')
if self.showNext().kind != 'excl':
self.expect('colon')
self.parseObjVect()
self.expect('excl')
def parseDeclSeg(self):
print ("Parse declaration segment")
self.expect('seg')
self.expect('ident')
if self.showNext().kind != 'excl':
self.expect('colon')
self.parseObjSeg()
self.expect('excl')
def parseDeclScal(self):
#.........这里部分代码省略.........