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) / 6-AST / AST 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 5 ngày 19 giờ
Điểm 6,00/6,00
Điểm 10,00 của 10,00 (100%)
Câu hỏi 1
Chính xác
Điểm 1,00 của 1,00
Given the grammar of MP as follows:
program: vardecls EOF; vardecls: vardecl vardecltail; vardecltail: vardecl vardecltail | ; vardecl: mptype ids ‘;’ ;
mptype: INTTYPE | FLOATTYPE;
ids: ID ‘,’ ids | ID;
INTTYPE: ‘int’;
FLOATTYPE: ‘float’;
ID: [a-z]+ ;
Please copy the following class into your answer and modify the bodies of its methods to count the terminal nodes in the parse tree? Your code starts at line 10. class TerminalCount(MPVisitor):
def visitProgram(self,ctx:MPParser.ProgramContext):
return None
def visitVardecls(self,ctx:MPParser.VardeclsContext):
return None
def visitVardecltail(self,ctx:MPParser.VardecltailContext):
return None
def visitVardecl(self,ctx:MPParser.VardeclContext):
return None
def visitMptype(self,ctx:MPParser.MptypeContext):
return None
def visitIds(self,ctx:MPParser.IdsContext):
return None
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
Test Expected Got
“int a;” 4 4
“””int a,b;””” 6 6
“int a;float b;” 7 7
“int a,b;float c;” 9 9
“int a,b;float c,d,e;” 13 13
Passed all tests!
Chính xác Điểm cho bài nộp này: 1,00/1,00.
Câu hỏi 2
Chính xác
Điểm 1,00 của 1,00
Given the grammar of MP as follows:
program: vardecls EOF; vardecls: vardecl vardecltail; vardecltail: vardecl vardecltail | ; vardecl: mptype ids ‘;’ ;
mptype: INTTYPE | FLOATTYPE;
ids: ID ‘,’ ids | ID;
INTTYPE: ‘int’;
FLOATTYPE: ‘float’;
ID: [a-z]+ ;
Please copy the following class into your answer and modify the bodies of its methods to return the height of the parse tree? Your code starts at line 10. class TerminalCount(MPVisitor):
def visitProgram(self,ctx:MPParser.ProgramContext):
return None
def visitVardecls(self,ctx:MPParser.VardeclsContext):
return None
def visitVardecltail(self,ctx:MPParser.VardecltailContext):
return None
def visitVardecl(self,ctx:MPParser.VardeclContext):
return None
def visitMptype(self,ctx:MPParser.MptypeContext):
return None
def visitIds(self,ctx:MPParser.IdsContext):
return None
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
Test Expected Got
“int a;” 4 4
“””int a,b;””” 5 5
“int a;float b;” 5 5
“int a,b;float c;” 5 5
“int a,b;float c,d,e;” 7 7
Passed all tests!
Chính xác Điểm cho bài nộp này: 1,00/1,00.
3
program: exp EOF; exp: term ASSIGN exp | term; term: factor COMPARE factor | factor; factor: factor ANDOR operand | operand; operand: ID | INTLIT | BOOLIT | ‘(‘ exp ‘)’;
INTLIT: [0-9]+ ;
BOOLIT: ‘True’ | ‘False’ ;
ANDOR: ‘and’ | ‘or’ ;
ASSIGN: ‘+=’ | ‘-=’ | ‘&=’ | ‘|=’ | ‘:=’ ;
COMPARE: ‘=’ | ‘<>’ | ‘>=’ | ‘<=’ | ‘<‘ | ‘>’ ; ID: [a-z]+ ; and AST classes as follows: class Expr(ABC):
class Binary(Expr): #op:string;left:Expr;right:Expr class Id(Expr): #value:string class IntLiteral(Expr): #value:int class BooleanLiteral(Expr): #value:boolean
Please copy the following class into your answer and modify the bodies of its methods to generate the AST of a MP input? class ASTGeneration(MPVisitor):
def visitProgram(self,ctx:MPParser.ProgramContext):
return None
def visitExp(self,ctx:MPParser.ExpContext):
return None
def visitTerm(self,ctx:MPParser.TermContext):
return None
def visitFactor(self,ctx:MPParser.FactorContext):
return None
def visitOperand(self,ctx:MPParser.OperandContext):
return None
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
Test Expected Got
“a := b := 4” Binary(:=,Id(a),Binary(:=,Id(b),IntLiteral(4))) Binary(:=,Id(a),Binary(:=,Id(b),IntLiteral(4)))
“””a
+= b -= a and
(b > 3)””” Binary(+=,Id(a),Binary(-
=,Id(b),Binary(and,Id(a),Binary(>,Id(b),IntLiteral(3))))) Binary(+=,Id(a),Binary(-
=,Id(b),Binary(and,Id(a),Binary(>,Id(b),IntLiter
“a or b and True” Binary(and,Binary(or,Id(a),Id(b)),BooleanLiteral(True)) Binary(and,Binary(or,Id(a),Id(b)),BooleanLiteral
Passed all tests!
Chính xác Điểm cho bài nộp này: 1,00/1,00.
4
program: vardecls EOF; vardecls: vardecl vardecltail; vardecltail: vardecl vardecltail | ; vardecl: mptype ids ‘;’ ;
mptype: INTTYPE | FLOATTYPE;
ids: ID ‘,’ ids | ID;
INTTYPE: ‘int’;
FLOATTYPE: ‘float’;
ID: [a-z]+ ; and AST classes as follows:
class Program:#decl:list(VarDecl) class Type(ABC): pass class IntType(Type): pass class FloatType(Type): pass class VarDecl: #variable:Id; varType: Type class Id: #name:str
Please copy the following class into your answer and modify the bodies of its methods to generate the AST of a MP input? class ASTGeneration(MPVisitor):
def visitProgram(self,ctx:MPParser.ProgramContext):
return None
def visitVardecls(self,ctx:MPParser.VardeclsContext):
return None
def visitVardecltail(self,ctx:MPParser.VardecltailContext):
return None
def visitVardecl(self,ctx:MPParser.VardeclContext):
return None
def visitMptype(self,ctx:MPParser.MptypeContext):
return None
def visitIds(self,ctx:MPParser.IdsContext):
return None
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
Test Expected
“int a;” Program([VarDecl(Id(a),IntType)])
“””int a,b;””” Program([VarDecl(Id(a),IntType),VarDecl(Id(b),IntType)])
“int a;float b;” Program([VarDecl(Id(a),IntType),VarDecl(Id(b),FloatType)])
“int a,b;float c;” Program([VarDecl(Id(a),IntType),VarDecl(Id(b),IntType),VarDecl(Id(c),FloatType)])
“int a,b;float c,d,e;” Program([VarDecl(Id(a),IntType),VarDecl(Id(b),IntType),VarDecl(Id(c),FloatType),VarDecl(Id(d),FloatType
Passed all tests!
Chính xác Điểm cho bài nộp này: 1,00/1,00.
5
program: vardecl+ EOF; vardecl: mptype ids ‘;’ ;
mptype: INTTYPE | FLOATTYPE;
ids: ID (‘,’ ID)*;
INTTYPE: ‘int’;
FLOATTYPE: ‘float’;
ID: [a-z]+ ; and AST classes as follows:
class Program:#decl:list(VarDecl) class Type(ABC): pass class IntType(Type): pass class FloatType(Type): pass class VarDecl: #variable:Id; varType: Type class Id: #name:str
Please copy the following class into your answer and modify the bodies of its methods to generate the AST of a MP input? class ASTGeneration(MPVisitor):
def visitProgram(self,ctx:MPParser.ProgramContext):
return None
def visitVardecl(self,ctx:MPParser.VardeclContext):
return None
def visitMptype(self,ctx:MPParser.MptypeContext):
return None
def visitIds(self,ctx:MPParser.IdsContext):
return None
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
6
program: exp EOF; exp: (term ASSIGN)* term; term: factor COMPARE factor | factor; factor: operand (ANDOR operand)*; operand: ID | INTLIT | BOOLIT | ‘(‘ exp ‘)’;
INTLIT: [0-9]+ ;
BOOLIT: ‘True’ | ‘False’ ;
ANDOR: ‘and’ | ‘or’ ;
ASSIGN: ‘+=’ | ‘-=’ | ‘&=’ | ‘|=’ | ‘:=’ ;
COMPARE: ‘=’ | ‘<>’ | ‘>=’ | ‘<=’ | ‘<‘ | ‘>’ ; ID: [a-z]+ ; and AST classes as follows: class Expr(ABC):
class Binary(Expr): #op:string;left:Expr;right:Expr class Id(Expr): #value:string class IntLiteral(Expr): #value:int class BooleanLiteral(Expr): #value:boolean
Please copy the following class into your answer and modify the bodies of its methods to generate the AST of a MP input? class ASTGeneration(MPVisitor):
def visitProgram(self,ctx:MPParser.ProgramContext):
return None
def visitExp(self,ctx:MPParser.ExpContext):
return None
def visitTerm(self,ctx:MPParser.TermContext):
return None
def visitFactor(self,ctx:MPParser.FactorContext):
return None
def visitOperand(self,ctx:MPParser.OperandContext):
return None
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
Test Expected Got
“a := b := 4” Binary(:=,Id(a),Binary(:=,Id(b),IntLiteral(4))) Binary(:=,Id(a),Binary(:=,Id(b),IntLiteral(4)))
“””a
+= b -= a and
(b > 3)””” Binary(+=,Id(a),Binary(-
=,Id(b),Binary(and,Id(a),Binary(>,Id(b),IntLiteral(3))))) Binary(+=,Id(a),Binary(-
=,Id(b),Binary(and,Id(a),Binary(>,Id(b),IntLiter
“a or b and True” Binary(and,Binary(or,Id(a),Id(b)),BooleanLiteral(True)) Binary(and,Binary(or,Id(a),Id(b)),BooleanLiteral
Passed all tests!
Chính xác
Điểm cho bài nộp này: 1,00/1,00.
◄ AST Quiz
Chuyển tới…
Reviews
There are no reviews yet.