CSE – Solved

$ 24.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) / 7-Name / Name 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 2 giờ
Điểm 4,00/4,00
Điểm 10,00 của 10,00 (100%)
1
class VarDecl(Decl): #name:str,typ:Type class ConstDecl(Decl): #name:str,val:Lit class Type(ABC): #abstract class class IntType(Type) class FloatType(Type) class Lit(ABC): #abstract class class IntLit(Lit): #val:int and exception RedeclaredDeclaration:
class RedeclaredDeclaration(Exception): #name:str
Implement the methods of the following class Visitor to travel on the above ASST to detect redeclared declarations (throw exception RedeclaredDeclaration): class StaticCheck(Visitor):
def visitProgram(self,ctx:Program,o:object): pass
def visitVarDecl(self,ctx:VarDecl,o:object):pass
def visitConstDecl(self,ctx:ConstDecl,o:object):pass
def visitIntType(self,ctx:IntType,o:object):pass
def visitFloatType(self,ctx:FloatType,o:object):pass
def visitIntLit(self,ctx:IntLit,o:object):pass
Your code starts at line 40 For example:
Test Result
x = Program([VarDecl(“a”,IntType()),ConstDecl(“b”,IntLit(3)),VarDecl(“a”,FloatType())]) a
Answer: (penalty regime: 0 %)

TestTest ExpectedExpected GotGot
 x = Program([VarDecl(“a”,IntType()),ConstDecl(“b”,IntLit(3)),VarDecl(“a”,FloatType())]) a a 
 x = Program([VarDecl(“b”,IntType()),ConstDecl(“b”,IntLit(3)),VarDecl(“a”,FloatType())]) b b 
 x = Program([VarDecl(“a”,IntType()),ConstDecl(“c”,IntLit(3)),VarDecl(“c”,FloatType())]) c c 
 x = Program([VarDecl(“a”,IntType()),ConstDecl(“b”,IntLit(3)),VarDecl(“c”,FloatType())]) 
Passed all tests! 
Chính xác Điểm cho bài nộp này: 1,00/1,00.
2
class VarDecl(Decl): #name:str,typ:Type class ConstDecl(Decl): #name:str,val:Lit class Type(ABC): #abstract class class IntType(Type) class FloatType(Type) class Lit(ABC): #abstract class class IntLit(Lit): #val:int and exceptions:
class RedeclaredVariable(Exception): #name:str class RedeclaredConstant(Exception): #name:str
Implement the methods of the following class Visitor to travel on the above ASST to detect redeclared declarations (throw the exception corresponding to the second declaration with the same name): class StaticCheck(Visitor):
def visitProgram(self,ctx:Program,o:object): pass
def visitVarDecl(self,ctx:VarDecl,o:object):pass
def visitConstDecl(self,ctx:ConstDecl,o:object):pass
def visitIntType(self,ctx:IntType,o:object):pass
def visitFloatType(self,ctx:FloatType,o:object):pass
def visitIntLit(self,ctx:IntLit,o:object):pass
Your code starts at line 45 For example:
Test Result
x = Program([VarDecl(“a”,IntType()),ConstDecl(“b”,IntLit(3)),VarDecl(“a”,FloatType())]) Redeclared Varaible: a
Answer: (penalty regime: 0 %)

Test Expected Got
 x =
Program([VarDecl(“a”,IntType()),ConstDecl(“b”,IntLit(3)),VarDecl(“a”,FloatType())]) Redeclared Varaible:
a Redeclared Varaible:
a 
 x =
Program([VarDecl(“b”,IntType()),ConstDecl(“b”,IntLit(3)),VarDecl(“a”,FloatType())]) Redeclared Constant:
b Redeclared Constant:
b 
 x =
Program([VarDecl(“a”,IntType()),ConstDecl(“c”,IntLit(3)),VarDecl(“c”,FloatType())]) Redeclared Varaible:
c Redeclared Varaible:
c 
 x =
Program([VarDecl(“a”,IntType()),ConstDecl(“b”,IntLit(3)),VarDecl(“c”,FloatType())]) 
Passed all tests! 
Chính xác Điểm cho bài nộp này: 1,00/1,00.
3
class VarDecl(Decl): #name:str,typ:Type class ConstDecl(Decl): #name:str,val:Lit class FuncDecl(Decl): #name:str,param:List[VarDecl],body:List[Decl] class Type(ABC): #abstract class class IntType(Type) class FloatType(Type) class Lit(ABC): #abstract class class IntLit(Lit): #val:int and exceptions:
class RedeclaredVariable(Exception): #name:str class RedeclaredConstant(Exception): #name:str class RedeclaredFunction(Exception): #name:str
Implement the methods of the following class Visitor to travel on the above AST to detect redeclared declarations (throw the exception corresponding to the second declaration with the same name) in the same scope: class StaticCheck(Visitor):
def visitProgram(self,ctx:Program,o:object): pass
def visitVarDecl(self,ctx:VarDecl,o:object):pass
def visitConstDecl(self,ctx:ConstDecl,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 visitIntLit(self,ctx:IntLit,o:object):pass
Your code starts at line 55 For example:
Test Result
x = Program([VarDecl(“a”,IntType()),ConstDecl(“b”,IntLit(3)),FuncDecl(“a”,[],[])]) Redeclared Function: a
Answer: (penalty regime: 0 %)
8 etu
19 ▼
20
21
22
23
24 ▼ 25
26 ▼ 27
28 ▼
29
30
31
32
33
34
35
36
37 def visitFuncDecl(self,ctx:FuncDecl,o:object):
n=ctx.name listVar=ctx.param listbody=ctx.body name=[] if n in o:
raise RedeclaredFunction(n) for nameVar in listVar:
name+=[self.visit(nameVar,name)] for nameBody in listbody:
name+=[self.visit(nameBody,name)] return n
def visitIntType(self,ctx:IntType,o:object):pass def visitFloatType(self,ctx:FloatType,o:object):pass
38 def visitIntLit(self ctx:IntLit o:object):pass

Test Expected Got
 x = Program([VarDecl(“a”,IntType()),ConstDecl(“b”,IntLit(3)),FuncDecl(“a”,[],[])]) Redeclared Function:
a Redeclared Function:
a 
 x = Program([VarDecl(“b”,IntType()),FuncDecl(“a”,[VarDecl(“a”,FloatType())], [ConstDecl(“c”,IntLit(3)),VarDecl(“b”,IntType()),VarDecl(“c”,IntType())])]) Redeclared Variable:
c Redeclared Variable:
c 
 x = Program([VarDecl(“b”,IntType()),FuncDecl(“a”, [VarDecl(“m”,FloatType()),VarDecl(“b”,IntType()),VarDecl(“m”,FloatType())],
[ConstDecl(“c”,IntLit(3)),VarDecl(“d”,IntType())])]) Redeclared Variable:
m Redeclared Variable:
m 
 x = Program([VarDecl(“b”,IntType()),FuncDecl(“a”, [VarDecl(“m”,FloatType()),VarDecl(“b”,IntType()),VarDecl(“d”,FloatType())],
[ConstDecl(“c”,IntLit(3)),VarDecl(“d”,IntType())])]) Redeclared Variable:
d Redeclared Variable:
d 
 x = Program([VarDecl(“b”,IntType()),FuncDecl(“a”, [VarDecl(“m”,FloatType()),VarDecl(“b”,IntType()),VarDecl(“d”,FloatType())],
[ConstDecl(“c”,IntLit(3)),FuncDecl(“d”,[],[])])]) Redeclared Function:
d Redeclared Function:
d 
 x = Program([VarDecl(“b”,IntType()),FuncDecl(“a”, [VarDecl(“m”,FloatType()),VarDecl(“b”,IntType()),VarDecl(“d”,FloatType())], [ConstDecl(“c”,IntLit(3)),FuncDecl(“foo”,[VarDecl(“x”,IntType())],
[VarDecl(“x”,IntType())])])]) Redeclared Variable:
x Redeclared Variable:
x 
Passed all tests! 
Chính xác Điểm cho bài nộp này: 1,00/1,00.

Câu hỏi 4
Chính xác
Điểm 1,00 của 1,00
Let AST of a programming language be defined as follows:
class Program: #decl:List[Decl] class Decl(ABC): #abstract class class VarDecl(Decl): #name:str,typ:Type class ConstDecl(Decl): #name:str,val:Lit 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 Expr(ABC): #abstract class class Lit(Expr): #abstract class class IntLit(Lit): #val:int class Id(Expr): #name:str and exceptions:
class RedeclaredVariable(Exception): #name:str class RedeclaredConstant(Exception): #name:str class RedeclaredFunction(Exception): #name:str class UndeclaredIdentifier(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 visitVarDecl(self,ctx:VarDecl,o:object):pass
def visitConstDecl(self,ctx:ConstDecl,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 visitIntLit(self,ctx:IntLit,o:object):pass
def visitId(self,ctx:Id,o:object):pass
Your code starts at line 65 For example:
Test Result
x = Program([VarDecl(“a”,IntType()),ConstDecl(“b”,IntLit(3)),FuncDecl(“a”,[],([],[]))]) Redeclared Function: a
Answer: (penalty regime: 0 %)
9 ▼ o a e a i list a :
30
31 ▼ 32
33 ▼
34
35
36
37
38 name[0]+=[self.visit(nameVar,name)] for nameBody in listbody:
name[0]+=[self.visit(nameBody,name+o)] for id in listId:
self.visit(id,name+o)

39 d f i itI tT ( lf t I tT bj t)

Chính xác
Điểm cho bài nộp này: 1,00/1,00.
◄ Name Quiz
Chuyển tới…
y

Reviews

There are no reviews yet.

Be the first to review “CSE – Solved”

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