本文整理汇总了Python中SymbolTable.SymbolTable.get方法的典型用法代码示例。如果您正苦于以下问题:Python SymbolTable.get方法的具体用法?Python SymbolTable.get怎么用?Python SymbolTable.get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SymbolTable.SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.get方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import get [as 别名]
class TypeChecker(NodeVisitor):
def __init__(self):
self.symbol_table = SymbolTable(None, "TypeChecker", {})
self.ttypes = defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: None)))
self.fill_ttypes()
def fill_ttypes(self):
# arithmetic int operations
self.add_ttype('+', 'int', 'int', 'int')
self.add_ttype('-', 'int', 'int', 'int')
self.add_ttype('*', 'int', 'int', 'int')
self.add_ttype('/', 'int', 'int', 'int')
self.add_ttype('%', 'int', 'int', 'int')
# binary int operations
self.add_ttype('&', 'int', 'int', 'int')
self.add_ttype('|', 'int', 'int', 'int')
self.add_ttype('^', 'int', 'int', 'int')
self.add_ttype('<<', 'int', 'int', 'int')
self.add_ttype('>>', 'int', 'int', 'int')
# arithmetic float operations
self.add_ttype('+', 'float', 'float', 'float')
self.add_ttype('-', 'float', 'float', 'float')
self.add_ttype('*', 'float', 'float', 'float')
self.add_ttype('/', 'float', 'float', 'float')
self.add_ttype('%', 'float', 'float', 'float')
self.add_ttype('+', 'int', 'float', 'float')
self.add_ttype('-', 'int', 'float', 'float')
self.add_ttype('*', 'int', 'float', 'float')
self.add_ttype('/', 'int', 'float', 'float')
self.add_ttype('%', 'int', 'float', 'float')
self.add_ttype('+', 'float', 'int', 'float')
self.add_ttype('-', 'float', 'int', 'float')
self.add_ttype('*', 'float', 'int', 'float')
self.add_ttype('/', 'float', 'int', 'float')
self.add_ttype('%', 'float', 'int', 'float')
# relational int operations
self.add_ttype('==', 'int', 'int', 'int')
self.add_ttype('!=', 'int', 'int', 'int')
self.add_ttype('<', 'int', 'int', 'int')
self.add_ttype('>', 'int', 'int', 'int')
self.add_ttype('<=', 'int', 'int', 'int')
self.add_ttype('>=', 'int', 'int', 'int')
# relational float operations
self.add_ttype('==', 'float', 'float', 'float')
self.add_ttype('!=', 'float', 'float', 'float')
self.add_ttype('<', 'float', 'float', 'float')
self.add_ttype('>', 'float', 'float', 'float')
self.add_ttype('<=', 'float', 'float', 'float')
self.add_ttype('>=', 'float', 'float', 'float')
self.add_ttype('==', 'int', 'float', 'float')
self.add_ttype('!=', 'int', 'float', 'float')
self.add_ttype('<', 'int', 'float', 'float')
self.add_ttype('>', 'int', 'float', 'float')
self.add_ttype('<=', 'int', 'float', 'float')
self.add_ttype('>=', 'int', 'float', 'float')
self.add_ttype('==', 'float', 'int', 'float')
self.add_ttype('!=', 'float', 'int', 'float')
self.add_ttype('<', 'float', 'int', 'float')
self.add_ttype('>', 'float', 'int', 'float')
self.add_ttype('<=', 'float', 'int', 'float')
self.add_ttype('>=', 'float', 'int', 'float')
# string operations
self.add_ttype('+', 'string', 'string', 'string')
self.add_ttype('*', 'string', 'int', 'string')
self.add_ttype('==', 'string', 'string', 'string')
self.add_ttype('!=', 'string', 'string', 'string')
self.add_ttype('<', 'string', 'string', 'string')
self.add_ttype('>', 'string', 'string', 'string')
self.add_ttype('<=', 'string', 'string', 'string')
self.add_ttype('>=', 'string', 'string', 'string')
def add_ttype(self, operation, operand1, operand2, returned):
self.ttypes[operation][operand1][operand2] = returned
def visit_Name(self, node):
return node.name
def visit_CheckedName(self, node):
if self.symbol_table.get(node.name):
return node.name
else:
print("Error: Usage of undeclared variable '%s': line %s" % (node.name, node.lineno))
return node.name
def visit_Operator(self, node):
return node.op
def visit_Integer(self, node):
return 'int'
def visit_Float(self, node):
return 'float'
def visit_String(self, node):
return 'string'
def visit_Program(self, node):
self.symbol_table = self.symbol_table.push_scope("Program")
for item in node.body:
#.........这里部分代码省略.........
示例2: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import get [as 别名]
#.........这里部分代码省略.........
def error(self,text,line):
self.errorsOcurred=True
print("********************************")
print("Error: " + text)
print("Line " + str(line))
exc_type, exc_obj, exc_tb = sys.exc_info()
print(exc_type, exc_tb.tb_lineno)
print("********************************")
def visit_Program(self,node):
try:
# print "visiting Program"
self.symbolTable=SymbolTable(None,'main')
node.classdefs.accept(self)
node.declarations.accept(self)
node.fundefs.accept(self)
node.instructions.accept(self)
except:
self.error("could not continue parsing, correct errors first",0)
def visit_Declarations(self,node):
# print "visiting Declarations"
for element in node.list :
element.accept(self)
def visit_Declaration(self,node):
# print "visiting Declaration"
toReturn = []
declType = node.typeOrId
allInits = node.initsOrClassinits.accept(self)
if(declType in self.types):
for element in allInits:
[typeOrId,id] = element
if self.symbolTable.get(id.value) != None:
self.error("Symbol: "+id.value+", was previusly declared",id.line)
try:
self.ttype['f'][declType][typeOrId]
except:
self.error("cannot initialize symbol of type: "+declType+", with expression of type: "+typeOrId,id.value)
self.symbolTable.put(id.value,typeOrId)
toReturn.append(id.value)
else:
typeOrId=declType.accept(self)
for id in allInits:
if self.symbolTable.get(id.value) != None:
self.error("Symbol: "+id.value+", was previusly declared",id.line)
self.symbolTable.put(id.value,typeOrId)
tmp = typeOrId
while tmp!=None:
classTable = self.classTables[tmp.id]
for element in classTable.map:
self.symbolTable.put(self.makeClassContentName(typeOrId.id,id.value,element), classTable.get(element))
tmp=tmp.parentClass
toReturn.append(id.value)
return toReturn
def makeClassContentName(self, className, objectName, fieldName):
return "__"+className+objectName+fieldName
def visit_Inits(self,node):
# print "visiting Inits"
toReturn=[]
for element in node.list:
toReturn.append(element.accept(self))
return toReturn
示例3: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import get [as 别名]
#.........这里部分代码省略.........
self.ttype['f']['string']['string'] = 'string'
self.ttype['f']['int']['int'] = 'int'
self.ttype['f']['float']['float'] = 'float'
self.ttype['f']['float']['int'] = 'float'
def error(self,text,line):
self.errorsOcurred=True
print "********************************"
print "Error: "+text
print "Line " +str(line)
print "********************************"
def visit_Program(self,node):
try:
#print "visiting Program"
self.symbolTable=SymbolTable(None,'main')
node.declarations.accept(self)
node.fundefs.accept(self)
node.instructions.accept(self)
except:
self.error("could not continue parsing, correct errors first",0)
def visit_Declarations(self,node):
#print "visiting Declarations"
for element in node.list :
element.accept(self)
def visit_Declaration(self,node):
#print "visiting Declaration"
declType = node.type
allInits = node.inits.accept(self)
for element in allInits:
[type,id] = element
if self.symbolTable.get(id.value) != None:
self.error("Symbol: "+id.value+", was previusly declared",id.line)
try:
self.ttype['f'][declType][type]
except:
self.error("cannot initialize symbol of type: "+declType+", with expression of type: "+type,id.value)
self.symbolTable.put(id.value,type)
def visit_Inits(self,node):
#print "visiting Inits"
toReturn=[]
for element in node.list:
toReturn.append(element.accept(self))
return toReturn
def visit_Init(self,node):
#print "visiting Init"
return [node.expression.accept(self),node.id]
def visit_Instructions(self,node):
#print "visiting Instructions"
self.symbolTable = SymbolTable(self.symbolTable,'instructions')
for element in node.list :
element.accept(self)
self.symbolTable = self.symbolTable.getParentScope()
def visit_PrintInstr(self,node):
#print "visiting PrintInstr"
if node.expression.accept(self) not in ['string','int','float']:
self.error("cannot print expression of that type",node.line)
def visit_LabeledInstr(self,node):
#print "visiting LabeledInstr"
示例4: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import get [as 别名]
class TypeChecker(NodeVisitor):
def __init__(self):
self.symbolTable = SymbolTable(None, 'root')
self.declType = ''
self.curFunc = None
def visit_BinExpr(self, node):
type1 = self.visit(node.left)
type2 = self.visit(node.right)
oper = node.op
if ttype[oper][type1][type2] is None:
print "Invalid binary expression {}. Line: {}".format(oper, node.line)
return ttype[oper][type1][type2]
def visit_Integer(self, node):
return 'int'
def visit_Float(self, node):
return 'float'
def visit_String(self, node):
return 'string'
def visit_Variable(self, node):
definition = self.symbolTable.getFromAnyEnclosingScope(node.name)
if definition is None:
print "Undefined symbol {}. Line: {}".format(node.name, node.line)
else:
return definition.type
def visit_Program(self, node):
self.visit(node.segments)
def visit_Declaration(self, node):
self.declType = node.type
self.visit(node.inits)
self.declType = ''
def visit_Init(self, node):
initType = self.visit(node.expression)
if initType == self.declType or (initType == "int" and self.declType == "float") or (
initType == "float" and self.declType == "int"):
if self.symbolTable.get(node.name) is not None:
print "Symbol {} in line {} is already defined in this scope".format(node.name, node.line)
else:
self.symbolTable.put(node.name, VariableSymbol(node.name, self.declType))
else:
print "Type mismatch in assignment of {} to {}. Line {}".format(initType, self.declType, node.line)
def visit_Assignment(self, node):
definition = self.symbolTable.getFromAnyEnclosingScope(node.id)
type = self.visit(node.expression)
if definition is None:
print "Assignment to undefined symbol {}. Line {}".format(node.id, node.line)
elif type != definition.type and (definition.type != "float" and definition != "int"):
print "Type mismatch in assignment of {} to {}. Line {}.".format(type, definition.type, node.line)
def visit_ParenExpression(self, node):
return self.visit(node.expression)
def visit_Fundef(self, node):
if self.symbolTable.get(node.id) is not None:
print "Function {} in line {} is already defined in this scope".format(node.id, node.line)
else:
func = FunctionSymbol(node.id, node.type, SymbolTable(self.symbolTable, node.id))
self.symbolTable.put(node.id, func)
self.curFunc = func
self.symbolTable = func.table
if node.args_list is not None:
self.visit(node.args_list)
func.setParamTypesFromTable()
self.visit(node.compound_instr)
self.symbolTable = self.symbolTable.getParentScope()
self.curFunc = None
def visit_CompoundInstruction(self, node):
innerScope = SymbolTable(self.symbolTable, "innerScope")
self.symbolTable = innerScope
self.visit(node.segments)
self.symbolTable = self.symbolTable.getParentScope()
def visit_PrintInstruction(self, node):
self.visit(node.expr_list)
def visit_LabeledInstruction(self, node):
self.visit(node.instruction)
def visit_ChoiceInstruction(self, node):
self.visit(node.condition)
self.visit(node.instruction)
self.visit(node.else_instruction)
def visit_WhileInstruction(self, node):
self.visit(node.condition)
self.visit(node.instruction)
def visit_RepeatInstruction(self, node):
#.........这里部分代码省略.........
示例5: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import get [as 别名]
class TypeChecker(NodeVisitor):
def __init__(self):
self.table = SymbolTable(None, "root")
self.actType = ""
self.isValid = True
def visit_Integer(self, node):
return "int"
def visit_Float(self, node):
return "float"
def visit_String(self, node):
return "string"
def visit_Variable(self, node):
definition = self.table.getGlobal(node.name)
if definition is None:
self.isValid = False
print "Undefined symbol {} in line {}".format(node.name, node.line)
else:
return definition.type
def visit_BinExpr(self, node):
lhs = self.visit(node.lhs)
rhs = self.visit(node.rhs)
op = node.op
if ttype[op][lhs][rhs] is None:
self.isValid = False
print "Bad expression {} in line {}".format(node.op, node.line)
return ttype[op][lhs][rhs]
def visit_AssignmentInstruction(self, node):
definition = self.table.getGlobal(node.id)
type = self.visit(node.expr)
if definition is None:
self.isValid = False
print "Used undefined symbol {} in line {}".format(node.id, node.line)
elif type != definition.type and (definition.type != "float" and definition != "int"):
self.isValid = False
print "Bad assignment of {} to {} in line {}.".format(type, definition.type, node.line)
def visit_GroupedExpression(self, node):
return self.visit(node.interior)
def visit_FunctionExpression(self, node):
if self.table.get(node.name):
self.isValid = False
print "Function {} already defined. Line: {}".format(node.name, node.line)
else:
function = FunctionSymbol(node.name, node.retType, SymbolTable(self.table, node.name))
self.table.put(node.name, function)
self.actFunc = function
self.table = self.actFunc.table
if node.args is not None:
self.visit(node.args)
self.visit(node.body)
self.table = self.table.getParentScope()
self.actFunc = None
def visit_CompoundInstruction(self, node):
innerScope = SymbolTable(self.table, "innerScope")
self.table = innerScope
if node.declarations is not None:
self.visit(node.declarations)
self.visit(node.instructions)
self.table = self.table.getParentScope()
def visit_ArgumentList(self, node):
for arg in node.children:
self.visit(arg)
self.actFunc.extractParams()
def visit_Argument(self, node):
if self.table.get(node.name) is not None:
self.isValid = False
print "Argument {} already defined. Line: {}".format(node.name, node.line)
else:
self.table.put(node.name, VariableSymbol(node.name, node.type))
def visit_InvocationExpression(self, node):
funDef = self.table.getGlobal(node.name)
if funDef is None or not isinstance(funDef, FunctionSymbol):
self.isValid = False
print "Function {} not defined. Line: {}".format(node.name, node.line)
else:
if (node.args is None and funDef.params != []) or len(node.args.children) != len(funDef.params):
self.isValid = False
print "Invalid number of arguments in line {}. Expected {}".format(node.line, len(funDef.params))
else:
types = [self.visit(x) for x in node.args.children]
expectedTypes = funDef.params
for actual, expected in zip(types, expectedTypes):
if actual != expected and not (actual == "int" and expected == "float"):
self.isValid = False
print "Mismatching argument types in line {}. Expected {}, got {}".format(
node.line, expected, actual
)
return funDef.type
#.........这里部分代码省略.........
示例6: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import get [as 别名]
class TypeChecker(NodeVisitor):
def __init__(self):
self.table = SymbolTable(None, "root")
self.current_type = ""
self.current_func = None
self.current_loop = None
self.errors = False
def visit_BinExpr(self, node):
left = self.visit(node.left)
right = self.visit(node.right)
op = node.op
t = types_table[op][left][right]
if t is None:
self.errors = True
print "Cannot perform operation {0} between types {1} and {2} at line {3}".format(
op, left, right, node.lineno
)
return t
def visit_Integer(self, node):
return 'int'
def visit_Float(self, node):
return 'float'
def visit_String(self, node):
return 'string'
def visit_Variable(self, node):
definition = self.table.getGlobal(node.id)
if definition is None:
self.errors = True
print "Undefined symbol {0} in line {1}.".format(node.id, node.lineno)
else:
return definition.type
def visit_Fundef(self, node):
f = self.table.get(node.id)
if f:
self.errors = True
if type(f) == FunctionSymbol:
print "Function {0} is already defined at line {1}. Redefinition at line {2}.".format(
node.id, f.lineno, node.lineno
)
else:
print "Name {0} is already defined at line {1}. Redefinition at line {2}.".format(
node.id, f.lineno, node.lineno
)
else:
f = FunctionSymbol(node.id, node.t, SymbolTable(self.table, node.id), node.lineno)
self.table.put(node.id, f)
self.current_func = f
globalTable = self.table
self.table = self.current_func.table
if node.args_list is not None:
self.visit(node.args_list)
self.visit(node.comp_instr)
self.table = globalTable
self.current_func = None
def visit_Assignment(self, node):
definition = self.table.getGlobal(node.var)
t = self.visit(node.expr)
if definition is None:
self.errors = True
print "Undefined symbol: {0} at line {1}".format(node.var, node.lineno)
elif t != definition.type:
warning = False
if definition.type == "float" and t == "int":
warning = True
self.errors = self.errors or not warning
print "{0}Wrong assignment type for symbol: {1}: symbol's type is {2}," \
" tried to assign type {3} at line {4}.".format("Warning: " if warning else "",
node.var, definition.type,
t if t is not None else "undefined",
node.lineno)
def visit_Declaration(self, node):
self.current_type = node.declaration_type
self.visit(node.inits)
self.current_type = ""
def visit_Init(self, node):
t = self.visit(node.expression)
if t != self.current_type:
warning = False
if t == "int" and self.current_type == "float":
warning = True
self.errors = self.errors or not warning
print "{0}Initialization to symbol {1}: symbol's type is {2}," \
" tried to assign type {3} at line {4}".format("Warning: " if Warning else "",
node.var_name, self.current_type,
t, node.lineno)
definition = self.table.get(node.var_name)
if definition is not None:
self.errors = True
print "Variable {0} is already defined at line {1}. Redefinition at line {2}.".format(
node.var_name, definition.lineno, node.lineno
)
#.........这里部分代码省略.........
示例7: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import get [as 别名]
class TypeChecker(object):
ttype = {}
ttype[('+', 'int', 'int')] = 'int'
ttype[('-', 'int', 'int')] = 'int'
ttype[('*', 'int', 'int')] = 'int'
ttype[('/', 'int', 'int')] = 'int'
ttype[('%', 'int', 'int')] = 'int'
ttype[('^', 'int', 'int')] = 'int'
ttype[('|', 'int', 'int')] = 'int'
ttype[('&', 'int', 'int')] = 'int'
ttype[('<', 'int', 'int')] = 'int'
ttype[('>', 'int', 'int')] = 'int'
ttype[('<=', 'int', 'int')] = 'int'
ttype[('>=', 'int', 'int')] = 'int'
ttype[('==', 'int', 'int')] = 'int'
ttype[('!=', 'int', 'int')] = 'int'
ttype[('>>', 'int', 'int')] = 'int'
ttype[('<<', 'int', 'int')] = 'int'
ttype[('&&', 'int', 'int')] = 'int'
ttype[('||', 'int', 'int')] = 'int'
ttype[('+', 'float', 'float')] = 'float'
ttype[('-', 'float', 'float')] = 'float'
ttype[('*', 'float', 'float')] = 'float'
ttype[('/', 'float', 'float')] = 'float'
ttype[('<', 'float', 'float')] = 'int'
ttype[('>', 'float', 'float')] = 'int'
ttype[('<=', 'float', 'float')] = 'int'
ttype[('>=', 'float', 'float')] = 'int'
ttype[('==', 'float', 'float')] = 'int'
ttype[('!=', 'float', 'float')] = 'int'
ttype[('+', 'float', 'int')] = 'float'
ttype[('-', 'float', 'int')] = 'float'
ttype[('*', 'float', 'int')] = 'float'
ttype[('/', 'float', 'int')] = 'float'
ttype[('+', 'int', 'float')] = 'float'
ttype[('-', 'int', 'float')] = 'float'
ttype[('*', 'int', 'float')] = 'float'
ttype[('/', 'int', 'float')] = 'float'
ttype[('<', 'float', 'int')] = 'int'
ttype[('>', 'float', 'int')] = 'int'
ttype[('<=', 'float', 'int')] = 'int'
ttype[('>=', 'float', 'int')] = 'int'
ttype[('==', 'float', 'int')] = 'int'
ttype[('!=', 'float', 'int')] = 'int'
ttype[('<', 'int', 'float')] = 'int'
ttype[('>', 'int', 'float')] = 'int'
ttype[('<=', 'int', 'float')] = 'int'
ttype[('>=', 'int', 'float')] = 'int'
ttype[('==', 'int', 'float')] = 'int'
ttype[('!=', 'int', 'float')] = 'int'
ttype[('+', 'string', 'string')] = 'string'
ttype[('*', 'string', 'int')] = 'string'
ttype[('<', 'string', 'string')] = 'int'
ttype[('>', 'string', 'string')] = 'int'
ttype[('<=', 'string', 'string')] = 'int'
ttype[('>=', 'string', 'string')] = 'int'
ttype[('!=', 'string', 'string')] = 'int'
ttype[('==', 'string', 'string')] = 'int'
tconv = {}
tconv[('int', 'float')] = True
@staticmethod
def conversion_possible(type1, type2):
return TypeChecker.tconv.get((type1, type2)) is not None
def __init__(self):
self.scope = SymbolTable(None, 'global')
self.return_type = None
self.returned = None
self.breakable = False
def visit_BinExpr(self, node):
type1 = type2 = None
type1 = node.left.accept(self)
type2 = node.right.accept(self)
op = node.operator
if type1 is None or type2 is None:
return None
try:
return TypeChecker.ttype[(op, type1, type2)]
except(KeyError):
print("Cannot evaluate {0} {1} {2} - incompatible types at {3}:{4}".format(type1, op, type2, node.pos[0], node.pos[1]))
return None
def visit_Variable(self, node):
value = self.scope.get(node.name)
if value != None:
#.........这里部分代码省略.........