CSE – Solved

$ 29.99
Category:

Description

/ Đại Học Chính Qui (Bacherlor program (Full-time study))
/ Khoa Khoa học và Kỹ thuật Máy tính (Faculty of Computer Science and Engineering ) / Khoa Học Máy Tính
/ Nguyên lý ngôn ngữ lập trình (CO3005)_Nguyễn Hứa Phùng (DH_HK211) / 9-Sequence Control (Expressions, Statements) / Sequence Programming
Đã bắt đầu vào
Tình trạng Đã hoàn thành
Hoàn thành vào
Thời gian thực
hiện 6 ngày 14 giờ
Điểm 3,00/3,00
Điểm 10,00 của 10,00 (100%)
1
class ClassDecl:#name:str,parent:str,mem:List[Decl] class VarDecl(Decl): #name:str,typ:Type class FuncDecl(Decl): #name:str,param:List[VarDecl],body:Tuple(List[Decl],List[Expr]) class Type(ABC): #abstract class class IntType(Type) class FloatType(Type) class ClassType(Type):#name:str class Expr(ABC): #abstract class class Lit(Expr): #abstract class class IntLit(Lit): #val:int class Id(Expr): #name:str and exceptions:
class RedeclaredField(Exception): #name:str class UndeclaredMethod(Exception): #name:str
Implement the methods of the following class Visitor to travel on the above AST to detect undeclared declarations (throw the exception UndeclaredIdentifier). Note that the redeclared declarations exception also is thrown if a redeclared declaration is detected: class StaticCheck(Visitor):
def visitProgram(self,ctx:Program,o:object): pass
def visitClassDecl(self,ctx:ClassDecl,o:object):pass
def visitVarDecl(self,ctx:VarDecl,o:object):pass
def visitFuncDecl(self,ctx:FuncDecl,o:object):pass
def visitIntType(self,ctx:IntType,o:object):pass
def visitFloatType(self,ctx:FloatType,o:object):pass
def visitClassType(self,ctx:ClassType,o:object):pass
def visitIntLit(self,ctx:IntLit,o:object):pass
def visitId(self,ctx:Id,o:object):pass
def visitFieldAccess(self,ctx:FieldAccess,o:object):pass
Your code starts at line 65 For example:
Test Result
Program([ClassDecl(“x”,””,[VarDecl(“a”,IntType()),FuncDecl(“a”,[],([VarDecl(“m”,ClassType(“x”))],
[FieldAccess(Id(“m”),”a”),FieldAccess(Id(“m”),”b”)]))])]) Redeclared
Method: a
Answer: (penalty regime: 0 %)
3 self.visit(i,o)
14
15 ▼
16 ▼
17
18
19
20 ▼ 21
22 ▼
23 def visitVarDecl(self,ctx:VarDecl,o:object): if ctx.name in o: raise RedeclaredField(ctx.name)
o.append(ctx.name)
def visitFuncDecl(self,ctx:FuncDecl,o:object):
lst = [] if ctx.name in o:
raise RedeclaredMethod(ctx.name)
( )
24
25
26
27
28
29
30
31
32
33
34
35
36 o.append(ctx.name) def visitIntType(self,ctx:IntType,o:object):pass def visitFloatType(self,ctx:FloatType,o:object):pass def visitClassType(self,ctx:ClassType,o:object):pass def visitIntLit(self,ctx:IntLit,o:object):pass def visitId(self,ctx:Id,o:object):pass def visitFieldAccess(self,ctx:FieldAccess,o:object):pass

Test Expected Got
 Program([ClassDecl(“x”,””,[VarDecl(“a”,IntType()),FuncDecl(“a”,[],
([VarDecl(“m”,ClassType(“x”))],
[FieldAccess(Id(“m”),”a”),FieldAccess(Id(“m”),”b”)]))])]) Redeclared
Method: a Redeclared
Method: a 
 Program([ClassDecl(“y”,””,[VarDecl(“a”,IntType())]),ClassDecl(“x”,”y”,
[FuncDecl(“a”,[],([],[])),FuncDecl(“b”,[],([],[])),VarDecl(“b”,IntType())])]) Redeclared
Field: b Redeclared
Field: b 
Passed all tests! 
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
2
class ClassDecl:#name:str,parent:str,mem:List[Decl] class VarDecl(Decl): #name:str,typ:Type class FuncDecl(Decl): #name:str,param:List[VarDecl],body:Tuple(List[Decl],List[Expr]) class Type(ABC): #abstract class class IntType(Type) class FloatType(Type) class ClassType(Type):#name:str class Expr(ABC): #abstract class class Lit(Expr): #abstract class class IntLit(Lit): #val:int class Id(Expr): #name:str class FieldAccess(Expr): #exp:Expr,field:str and exceptions:
class UndeclaredIdentifier(Exception): #name:str class UndeclaredField(Exception): #name:str
Implement the methods of the following class Visitor to travel on the above AST to detect undeclared declarations (throw the exception UndeclaredIdentifier). Note that the redeclared declarations exception also is thrown if a redeclared declaration is detected: class StaticCheck(Visitor):
def visitProgram(self,ctx:Program,o:object): pass
def visitClassDecl(self,ctx:ClassDecl,o:object):pass
def visitVarDecl(self,ctx:VarDecl,o:object):pass
def visitFuncDecl(self,ctx:FuncDecl,o:object):pass
def visitIntType(self,ctx:IntType,o:object):pass
def visitFloatType(self,ctx:FloatType,o:object):pass
def visitClassType(self,ctx:ClassType,o:object):pass
def visitIntLit(self,ctx:IntLit,o:object):pass
def visitId(self,ctx:Id,o:object):pass
def visitFieldAccess(self,ctx:FieldAccess,o:object):pass
Your code starts at line 65 For example:
Test Result
Program([ClassDecl(“x”,””,[FuncDecl(“foo”,[],([VarDecl(“m”,ClassType(“x”))],
[FieldAccess(Id(“m”),”a”),FieldAccess(Id(“m”),”b”)])),VarDecl(“a”,IntType())])]) Undeclared
Field: b
Answer: (penalty regime: 0 %)
53 sel . isit( , o)
54
55
56
57
58
59
60
61 def visitIntType(self,ctx:IntType,o:object):pass def visitFloatType(self,ctx:FloatType,o:object):pass def visitClassType(self,ctx:ClassType,o:object):pass def visitIntLit(self,ctx:IntLit,o:object):pass
62
63 ▼
64 ▼
65
66
67
68 ▼ 69
70 ▼ 71
72 ▼
73
74
75
76 def visitId(self,ctx:Id,o:object): if ctx.name in o.get(‘classes’).get(o[‘class’]).get(‘methods’).get(o[‘method’]):
return o.get(‘classes’).get(o[‘class’]).get(‘methods’).get(o[‘method’]).get(ctx.name).name raise UndeclaredIdentifier(ctx.name)
def visitFieldAccess(self,ctx:FieldAccess,o:object):
curClass = self.visit(ctx.exp, o) while curClass != ”:
fields = o.get(‘classes’).get(curClass).get(‘fields’) if fields.get(ctx.field) != None: return fields.get(ctx.field) curClass = o.get(‘classes’).get(curClass).get(‘parent’)
raise UndeclaredField(ctx.field)

Test Expected Got
 Program([ClassDecl(“x”,””,[FuncDecl(“foo”,[],([VarDecl(“m”,ClassType(“x”))],
[FieldAccess(Id(“m”),”a”),FieldAccess(Id(“m”),”b”)])),VarDecl(“a”,IntType())])]) Undeclared
Field: b Undeclared
Field: b 
 Program([ClassDecl(“y”,””,[VarDecl(“a”,IntType()),FuncDecl(“foo”,[], ([VarDecl(“m”,ClassType(“y”))],[FieldAccess(Id(“m”),”a”)]))]),ClassDecl(“x”,””,
[FuncDecl(“foo”,[],([VarDecl(“m”,ClassType(“x”))],
[FieldAccess(Id(“m”),”a”),FieldAccess(Id(“m”),”b”)])),VarDecl(“a”,IntType())])]) Undeclared
Field: b Undeclared
Field: b 
Passed all tests! 
Chính xác Điểm cho bài nộp này: 1,00/1,00.
3
class ClassDecl:#name:str,parent:str,mem:List[Decl] class VarDecl(Decl): #name:str,typ:Type class FuncDecl(Decl): #name:str,param:List[VarDecl],body:Tuple(List[Decl],List[Expr]) class Type(ABC): #abstract class class IntType(Type) class FloatType(Type) class ClassType(Type):#name:str class Expr(ABC): #abstract class class Lit(Expr): #abstract class class IntLit(Lit): #val:int class Id(Expr): #name:str class FieldAccess(Expr): #exp:Expr,field:str and exceptions:
class UndeclaredIdentifier(Exception): #name:str class UndeclaredField(Exception): #name:str
Implement the methods of the following class Visitor to travel on the above AST to detect undeclared declarations (throw the exception UndeclaredIdentifier). Note that the redeclared declarations exception also is thrown if a redeclared declaration is detected: class StaticCheck(Visitor):
def visitProgram(self,ctx:Program,o:object): pass
def visitClassDecl(self,ctx:ClassDecl,o:object):pass
def visitVarDecl(self,ctx:VarDecl,o:object):pass
def visitFuncDecl(self,ctx:FuncDecl,o:object):pass
def visitIntType(self,ctx:IntType,o:object):pass
def visitFloatType(self,ctx:FloatType,o:object):pass
def visitClassType(self,ctx:ClassType,o:object):pass
def visitIntLit(self,ctx:IntLit,o:object):pass
def visitId(self,ctx:Id,o:object):pass
def visitFieldAccess(self,ctx:FieldAccess,o:object):pass
For example: Email: elearning@hcmut.edu.vn
Phát triển dựa trên hệ thống Moodle
Test Result
Program([ClassDecl(“x”,””,[VarDecl(“a”,IntType()),FuncDecl(“foo”,[],([VarDecl(“m”,ClassType(“x”))],
[FieldAccess(Id(“m”),”a”),FieldAccess(Id(“m”),”b”)]))])]) Undeclared
Field: b
Answer: (penalty regime: 0 %)
4 ▼
5
6
7
8
9 ▼ 10
11 for x in ctx.decl: o[x.name] = {} self.visit(x, o[x.name]) return o
def visitClassDecl(self,ctx:ClassDecl,o:object):
o[‘parent’] = ctx.parent # need update here to ref to the parent class o[‘fields’] = {}
12 o[‘methods’] = {}
12 o[ methods ] = {}
13 ▼
14
15
16 ▼
17 ▼
18
19
20
21 ▼
22 ▼
23
24
25 ▼26 ▼ for x in ctx.mem: self.visit(x, o)
def visitVarDecl(self,ctx:VarDecl,o:object): if o.get(‘fields’).get(ctx.name) != None: RedeclaredField(ctx.name) o[‘fields’][ctx.name] = ctx.typ
def visitFuncDecl(self,ctx:FuncDecl,o:object): if o.get(‘methods’).get(ctx.name) != None: RedeclaredMethod(ctx.name) o[‘methods’][ctx.name] = {} for x in ctx.param: if o.get(‘methods’).get(ctx.name).get(x.name) != None:
27 raise RedeclaredField(x name)
Test Expected Got
 Program([ClassDecl(“x”,””,[VarDecl(“a”,IntType()),FuncDecl(“foo”,[],
([VarDecl(“m”,ClassType(“x”))],
[FieldAccess(Id(“m”),”a”),FieldAccess(Id(“m”),”b”)]))])]) Undeclared
Field: b Undeclared
Field: b 
 Program([ClassDecl(“y”,””,[VarDecl(“a”,IntType())]),ClassDecl(“x”,”y”,
[FuncDecl(“foo”,[],([VarDecl(“m”,ClassType(“x”))],
[FieldAccess(Id(“m”),”a”),FieldAccess(Id(“m”),”b”)]))])]) Undeclared
Field: b Undeclared
Field: b 
 Program([ClassDecl(“y”,””,[VarDecl(“a”,IntType())]),
ClassDecl(“z”,””,[VarDecl(“b”,IntType())]),
ClassDecl(“t”,”y”,[VarDecl(“b”,IntType())]),
ClassDecl(“x”,”y”,[FuncDecl(“foo”,[],
([VarDecl(“m”,ClassType(“x”))],
[FieldAccess(Id(“m”),”a”),FieldAccess(Id(“m”),”b”)]))])]) Undeclared
Field: b Undeclared
Field: b 
Passed all tests! 
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
◄ Sequence Quiz
Chuyển tới…

Reviews

There are no reviews yet.

Be the first to review “CSE – Solved”

Your email address will not be published. Required fields are marked *