本文整理汇总了Python中SymbolTable.SymbolTable.put方法的典型用法代码示例。如果您正苦于以下问题:Python SymbolTable.put方法的具体用法?Python SymbolTable.put怎么用?Python SymbolTable.put使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SymbolTable.SymbolTable
的用法示例。
在下文中一共展示了SymbolTable.put方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: visit_Fundef
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import put [as 别名]
def visit_Fundef(self, node):
node.Functions.putNewFun(node.id, node.type)
Functions = FunctionsTable(node.Functions, "Functions")
Variables = SymbolTable(node.Variables, "Variables")
node.argList.Functions = Functions
node.argList.Variables = Variables
listOfArguments = node.argList.accept(self)
for element in listOfArguments:
if element!= None:
node.Functions.put(node.id, element[1])
if Variables.put(element[0], element[1])==-1:
self.errors.append("In line "+ str(node.lineno) + ": variable "+ element.name + " was initialized")
node.compoundInstr.Functions = Functions
node.compoundInstr.Variables = Variables
node.compoundInstr.accept(self)
示例2: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import put [as 别名]
#.........这里部分代码省略.........
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
def visit_Init(self,node):
# print "visiting Init"
return [node.expression.accept(self),node.id]
def visit_Classinits(self,node):
示例3: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import put [as 别名]
#.........这里部分代码省略.........
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:
self.visit(item)
self.symbol_table = self.symbol_table.pop_scope()
def visit_Declaration(self, node):
for init in node.inits:
name = self.visit(init.name)
var = self.symbol_table.get_declared_var(name)
if var:
if isinstance(var, FunctionDefSymbol):
print("Error: Function identifier '%s' used as a variable: line %d" % (name, node.lineno))
else:
print("Error: Variable '%s' already declared: line %d" % (name, node.lineno))
else:
self.symbol_table.put(name, VariableSymbol(init.name, node.var_type))
self.visit(init)
def visit_Initializer(self, node):
expression_ret_type = self.get_return_type(node.expression)
declared_type = self.get_return_type(node.name)
if expression_ret_type and declared_type:
TypeChecker.check_type_consistency(node, declared_type, expression_ret_type)
def visit_PrintInstr(self, node):
for item in node.expr_list:
self.visit(item)
def visit_LabeledInstr(self, node):
self.visit(node.instruction)
def visit_Assignment(self, node):
declared_type = self.get_return_type(node.target)
expression_ret_type = self.get_return_type(node.value)
if not declared_type:
print("Error: Variable '%s' undefined in current scope: line %d" % (self.visit(node.target), node.lineno))
elif expression_ret_type:
TypeChecker.check_type_consistency(node, declared_type, expression_ret_type)
def visit_IfInstr(self, node):
self.visit(node.condition)
self.visit(node.body)
if node.else_body:
self.visit(node.else_body)
def visit_WhileInstr(self, node):
self.visit(node.condition)
self.symbol_table.set_inside_loop(1)
示例4: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import put [as 别名]
#.........这里部分代码省略.........
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"
node.instruction.accept(self)
def visit_Assignment(self,node):
#print "visiting Assignment"
if self.symbolTable.getIncludingParents(node.id.value) == None:
self.error("unknown symbol name: "+id.value,id.line)
示例5: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import put [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):
#.........这里部分代码省略.........
示例6: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import put [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
#.........这里部分代码省略.........
示例7: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import put [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
)
#.........这里部分代码省略.........
示例8: TypeChecker
# 需要导入模块: from SymbolTable import SymbolTable [as 别名]
# 或者: from SymbolTable.SymbolTable import put [as 别名]
#.........这里部分代码省略.........
print("Undefined variable {0} at {1}:{2}".format(node.name, node.pos[0], node.pos[1]))
return None
def visit_If(self, node):
condition_type = node.condition.accept(self)
if condition_type != 'int':
print("Condition must evaluate to integer at {0}:{1}".format(node.condition.pos[0], node.condition.pos[1]))
node.block_if.accept(self)
if node.block_else != None:
node.block_else.accept(self)
def visit_While(self, node):
condition_type = node.condition.accept(self)
if condition_type != 'int':
print("Condition must evaluate to integer at {0}:{1}".format(node.condition.pos[0], node.condition.pos[1]))
old_breakable = self.breakable
self.breakable = True
node.body.accept(self)
self.breakable = old_breakable
def visit_Repeat(self, node):
condition_type = node.condition.accept(self)
if condition_type != 'int':
print("Condition must evaluate to integer at {0}:{1}".format(node.condition.pos[0], node.condition.pos[1]))
old_breakable = self.breakable
self.breakable = True
node.body.accept(self)
self.breakable = old_breakable
def visit_Fundef(self, node):
symbol = self.scope.getDirect(node.name)
if symbol is None:
self.scope.put(node.name, node)
else:
print("Symbol {0} already defined at {1}:{2}. First defined at {3}:{4}".format(
node.name, node.pos[0], node.pos[1], symbol.pos[0], symbol.pos[1]))
# Create new scope for function
self.scope = SymbolTable(self.scope, node.name)
# Leave information about return type of the function
self.return_type = node.return_type
self.returned = False
for argument in node.arguments:
argument.accept(self)
node.body.accept(self)
if not self.returned:
print("No return statement found in function {0} defined at {1}:{2}".format(node.name, node.pos[0], node.pos[1]))
# Clear information about return type
self.return_type = None
self.returned = False
# Get the hell out of function scope, after its done
self.scope = self.scope.parent
def visit_Arg(self, node):
self.scope.put(node.id, node)
return node.type
def visit_Declaration(self, node):
for init in node.inits:
init.type = node.type
defined_var = self.scope.getDirect(init.name.name)
if defined_var is not None:
print("Variable {0} already defined at {1}:{2}. First defined at {3}:{4}.".format(
init.name, init.pos[0], init.pos[1], defined_var.pos[0], defined_var.pos[1]))