本文整理汇总了Python中Analyzer.Analyzer.getLabel方法的典型用法代码示例。如果您正苦于以下问题:Python Analyzer.getLabel方法的具体用法?Python Analyzer.getLabel怎么用?Python Analyzer.getLabel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Analyzer.Analyzer
的用法示例。
在下文中一共展示了Analyzer.getLabel方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Parser
# 需要导入模块: from Analyzer import Analyzer [as 别名]
# 或者: from Analyzer.Analyzer import getLabel [as 别名]
class Parser(object):
scanner = None
analyzer = None
sourceFile = None
symbolTableStack = None
lookahead = ''
firstIdFlag = False
# Constructor
def __init__(self, fileName):
try:
self.sourceFile = open(fileName, 'r')
except IOError:
sys.exit("Source file not found")
self.scanner = Scanner(self.sourceFile)
self.symbolTableStack = SymbolTableStack()
self.analyzer = Analyzer(fileName, self.symbolTableStack)
def parse(self):
self.lookahead = self.scanner.getNextToken()
self.systemGoal()
print "Parsing Successful"
self.sourceFile.close()
def match(self, toMatch):
lexeme = self.scanner.lexeme
if(self.lookahead == toMatch):
self.lookahead = self.scanner.getNextToken()
return lexeme
else:
# print the caller
# print inspect.stack()[1][3]
self.matchError(toMatch)
def systemGoal(self):
if self.lookahead is "MP_PROGRAM": # 1 SystemGoal -> Program eof
self.program()
else:
self.error("MP_PROGRAM")
def program(self):
if self.lookahead is "MP_PROGRAM": # 2 Program -> ProgramHeading ";" Block "."
self.programHeading()
self.match("MP_SCOLON")
self.block()
self.match("MP_PERIOD")
else:
self.error("MP_PROGRAM")
def programHeading(self):
if self.lookahead is "MP_PROGRAM": # 3 ProgramHeading -> "program" ProgramIdentifier
self.match("MP_PROGRAM")
self.programIdentifier()
self.symbolTableStack.addTable('Main', self.analyzer.getLabel())
self.analyzer.genBranch(self.analyzer.getLabel())
else:
self.error("MP_PROGRAM")
def block(self):
if self.lookahead in ["MP_VAR", "MP_BEGIN", "MP_FUNCTION", "MP_PROCEDURE"]: # 4 Block -> VariableDeclarationPart ProcedureAndFunctionDeclarationPart StatementPart
self.variableDeclarationPart()
self.procedureAndFunctionDeclarationPart()
self.statementPart()
else:
self.error('"MP_VAR", "MP_BEGIN", "MP_FUNCTION", "MP_PROCEDURE"')
def variableDeclarationPart(self):
if self.lookahead is "MP_VAR": # 5 VariableDeclarationPart -> "var" VariableDeclaration ";" VariableDeclarationTail
self.match("MP_VAR")
self.firstIdFlag = True
self.variableDeclaration()
self.match("MP_SCOLON")
self.variableDeclarationTail()
elif self.lookahead in ["MP_BEGIN", "MP_FUNCTION", "MP_PROCEDURE"]:
return
else:
self.error('"MP_VAR", "MP_BEGIN", "MP_FUNCTION", "MP_PROCEDURE"')
def variableDeclarationTail(self):
if self.lookahead in ["MP_PROCEDURE", "MP_FUNCTION", "MP_BEGIN"]: # 7 VariableDeclarationTail -> lambda
return
elif self.lookahead is "MP_IDENTIFIER": # 6 VariableDeclarationTail -> VariableDeclaration ";" VariableDeclarationTail
self.variableDeclaration()
self.match("MP_SCOLON")
self.variableDeclarationTail()
else:
self.error('"MP_PROCEDURE", "MP_FUNCTION", "MP_BEGIN", "MP_IDENTIFIER", "MP_SCOLON"')
def variableDeclaration(self):
#.........这里部分代码省略.........