2002 Computer Science 330FC Language Implementation Your Marks for Assignment 3A All marking queries should initially be directed to the marker. Marking is done by running test cases, reading source code, sample programs, documentation, etc. Some of the markers test cases may check fine detail such as type checking errors, and markers have to take into account the level of importance of what is achieved. Getting the main idea is the most important thing. Markers also need to take into account that sometimes there are different ways of doing things, and students may get different results with the test cases (which might not even compile). Perhaps the test cases might need to be modified a bit. When students have done things differently, the hope is that they have provided some kind of documentation and justification for what they have done. ################################################################################ Basic Questions ################################################################################ ________________________________________________________________________________ 1 Comma expressions for initialisation and increment in for loops ________________________________________________________________________________ Grammar Stmt::= FOR LEFT DeclExprStmtOpt SEMICOLON ExprOpt SEMICOLON AssignExprListOpt RIGHT Stmt ; DeclExprStmtOpt::= AssignExprList | LocalDecl | /* Empty */ ; AssignExprList::= AssignExprList COMMA AssignExpr | AssignExpr ; Build parse tree correctly? Reprints correctly? Works for existing examples? Works for int i, j; for ( i = 0, j = 1; j < 10; i++, j++ ) println( "i = " + i + ", j = " + j ); Suitable examples provided to test? 1 (xxx/8 marks) Comma expressions ________________________________________________________________________________ 2 Nested function declarations ________________________________________________________________________________ Grammar permits both global and local function declarations? Build parse tree correctly? Reprint correctly? Put both global and local functions in the environment, and can find them again? Code for invocation builds correct environment, involving chaining down to the level in which the function was declared? Code in Runenv to chain down the appropriate level? Works for simple recursive examples (no nesting), invocation of function declared locally, and invocation of function one level out? void p( int a, b, c ) { println( "p( " + a + ", " + b + ", " + c + " );" ); void q( int d ) { println( "q( " + d + " ) in p( " + a + ", " + b + ", " + c + " );" ); void r( int e, f ) { println( "r( " + e + ", " + f + " ) in q( " + d + " ) in p( " + a + ", " + b + ", " + c + " );" ); println( "6! = " + fact( 6 ) ); }; r( a, d ); }; q( b ); }; p( 1, 2, 3 ); int fact( int n ) { if ( n == 0 ) return 1 else return n * fact( n - 1 ); }; println( "6! = " + fact( 6 ) ); Suitable examples provided to test? 2 (xxx/8 marks) Nested function ________________________________________________________________________________ 3 Type declarations ________________________________________________________________________________ Grammar for type declaration: TYPE IDENT = Type Grammar for use Type ::= IDENT Build parse tree correctly? Reprints correctly? Code for TypeDeclNode makes sense? Code for TypeIdentNode makes sense? Reprints correctly (no extra semicolon)? Does not have to cope with recursive types for full marks (these might be worth a bonus). Works for type a = int; a i = 3; println( "i should be 3, and is " + i ); type b = []int; b x = new [ 3 ]int; for ( i = 0; i < 3; i++ ) { x[ i ] = i; println( "x[" + i + "] = " + x[ i ] ); } Works for type a = int; a = 3; // Error - can't assign to type println( a ); // Error - can't print a type the above code should generate an error message, not print 3 or generate a run time exception. Suitable examples provided to test? 3 (xxx/8 marks) Type declarations ________________________________________________________________________________ 4 Enumerated type notation ________________________________________________________________________________ Grammar for enum type (Type ::= ENUM LEFT IdentListOpt RIGHT)? Build parse tree correctly? Reprints correctly? Code for EnumTypeNode, EnumType makes sense? Can use in type declarations, and simple variable declarations? Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); type weekDay = enum( mon, tue, wed, thu, fri ); day x, y; x = y; // Valid weekDay z; x = z; // Should give error enum( mon, tue, wed, thu, fri ) w; z = w; // Valid Suitable examples provided to test? 4 (xxx/5 marks) Enumerated type notation ________________________________________________________________________________ 5 Check for redeclaration of identifiers in the enumerated type. Enumerated type members in different environment from environment in which enumerated type created. ________________________________________________________________________________ Get appropriate error messages if declare identifier more than once. Get appropriate error messages if write "type day = enum( sun, mon, ... ); day x = mon;" Get appropriate error messages if write "mon = 4;" Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); type dup = enum( a, b, b, c ); // Error day x; int y; x = mon; // Error y = x; // Error x = 4; // Error Suitable examples provided to test? 5 (xxx/5 marks) Check for redeclaration in the enumerated type ________________________________________________________________________________ 6 Member selection for enumerated types ________________________________________________________________________________ Grammar (Primary ::= IDENT DOT IDENT)? Build parse tree correctly? Reprints correctly? code for member selection makes sense? Works (possibly int value) for type day = enum( sun, mon, tue, wed, thu, fri, sat ); day x0 = day.sun; day x1 = day.mon; day x2 = day.tue; day x3 = day.wed; day x4 = day.thu; day x5 = day.fri; day x6 = day.sat; println( x0 ); println( x1 ); println( x2 ); println( x3 ); println( x4 ); println( x5 ); println( x6 ); Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); day x; println( x.mon ); // Error x = day.day; // Error int y = day.mon; // Error println( garbage.mon ); // Error Suitable examples provided to test? 6 (xxx/15 marks) Member selection for enumerated types ________________________________________________________________________________ 7 Conversion of an enumerated type to a string ________________________________________________________________________________ Code for EnumValue makes sense? Works (text value) for type day = enum( sun, mon, tue, wed, thu, fri, sat ); day x0 = day.sun; day x1 = day.mon; day x2 = day.tue; day x3 = day.wed; day x4 = day.thu; day x5 = day.fri; day x6 = day.sat; println( x0 ); println( x1 ); println( x2 ); println( x3 ); println( x4 ); println( x5 ); println( x6 ); and prints mon, rather than 1. Suitable examples provided to test? 7 (xxx/5 marks) Conversion of an enumerated type to a string ________________________________________________________________________________ 8 Addition/subtraction of enumerated type values and increment/decrement of enumerated type variables ________________________________________________________________________________ Code for PlusNode, MinusNode, PreIncNode, PreDecNode, PostIncNode, PostDecNode make sense? Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); println( day.tue + 2 ); println( day.tue - 1 ); day x = day.tue; println( x++ ); println( x++ ); println( x++ ); println( x ); Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); println( day.mon + day.wed ); // Error println( 2 - day.mon ); // Error println( day.sat + 1 ); // Runtime error or sun or +infinity Suitable examples provided to test? 8 (xxx/5 marks) Addition/subtraction ________________________________________________________________________________ 9 Comparison of enumerated types ________________________________________________________________________________ Code for EqualNode, GreaterEqualNode, GreaterThanNode, LessEqualNode, LessThanNode, NotEqualNode, RelationNode make sense? Suitable examples provided to test? Works for for ( day x = day.mon; x <= day.fri; x++ ) println( x ); Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); type weekDay = enum( mon, tue, wed, thu, fri ); println( day.tue == weekDay.tue ); // Error println( day.tue * day.tue ); // Error 9 (xxx/5 marks) Comparison of enumerated types ________________________________________________________________________________ 10 Set type declaration ________________________________________________________________________________ Grammar (Type ::= SET LEFT Type RIGHT)? Build parse tree correctly? Reprints correctly? Code for SetTypeNode, SetType, SetValue make sense? Generate error messages of try and create set(real), set([]int), and other types that are not implemented. Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); type weekDay = enum( mon, tue, wed, thu, fri ); type daySet = set( day ); type weekDaySet = set( weekDay ); type charSet = set( char ); daySet s, t; weekDaySet u; s = t; t = u; // Error type intSet = set( int ); // Error unless implemented type realSet = set( real ); // Error unless implemented type intType = int; type intSet2 = set( intType ); // Error unless implemented Suitable examples provided to test? 10 (xxx/5 marks) Set type declaration ________________________________________________________________________________ 11 Set constructors ________________________________________________________________________________ Grammar? Primary ::= NEW Type LEFTBRACE RangeListOpt RIGHTBRACE; RangeListOpt::= /* Empty */ | RangeList; RangeList::= Range | RangeList COMMA Range; Range::= Expr | DOTDOT Expr | Expr DOTDOT Expr | Expr DOTDOT | DOTDOT; Code for Multiple, RangeListNode, RangeNode make sense? Build parse tree correctly? Reprints correctly? Can create sets with a list of values? Implement both sets of char and enumerated types? Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); type daySet = set( day ); daySet x, y; x = new daySet{ day.mon, day.wed, day.fri }; println( x ); y = new daySet{ day.mon, day.wed .. day.fri }; println( y ); Works for type charSet = set( char ); charSet x, y; x = new charSet { 'A'..'Z', 'a'..'z' }; println( x ); y = new charSet { '0'..'9', 'a'..'f', 'A'..'F', '.', ',' }; println( y ); Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); type daySet = set( day ); day x = day.mon, y = day.tue, z = day.wed; daySet s; s = new daySet{ x, y, z }; println( s ); s = new daySet{ x, y, x }; println( s ); Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); type daySet = set( day ); day x = day.mon, y = day.tue, z = day.wed; daySet s; s = new daySet{ ..x, z.. }; println( s ); Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); type daySet = set( day ); daySet s; s = new daySet{ day.mon, 'x', 'y' }; // Error s = new daySet{ 1, 2, 3 }; // Error println( s ); Suitable examples provided to test? 11 (xxx/20 marks) Set constructors ________________________________________________________________________________ 12 Set union, difference, intersection, and complement ________________________________________________________________________________ Grammar? Build parse tree correctly? Code in DiffNode, IntersectNode, SetNode, UnionNode makes sense? Reprints correctly? Works for type month = enum( jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec ); type monthSet = set( month ); monthSet x = new monthSet{ month.mar .. month.jul }; monthSet y = new monthSet{ month.may .. month.oct }; monthSet z = new monthSet{ month.jan, month.mar, month.may, month.jul, month.sep, month.nov }; println( x | y ); println( x & y ); println( x | y & z ); println( x \ y ); println( ~ x ); Suitable examples provided to test? 12 (xxx/5 marks) Set union, difference, intersection, and complement ________________________________________________________________________________ 13 Set membership ________________________________________________________________________________ Grammar? Build parse tree correctly? Reprints correctly? Code in InNode makes sense? Works for type bango = enum( zero, ichi, ni, san, shi, go, roku, shichi, hachi, kyu, ju ); type bangoSet = set( bango ); bangoSet x = new bangoSet{ bango.ichi .. bango.roku, bango.hachi }; for ( bango i = bango.zero; i < bango.ju; i++ ) if ( i in x ) println( i ); type charSet = set( char ); charSet s = new charSet { 'A'..'Z', 'a'..'z', '(', ')', '[', ']', '{', '}' }; for ( char i = ' '; i <= '~'; i++ ) if ( i in s ) println( i ); Suitable examples provided to test? 13 (xxx/5 marks) Set membership ________________________________________________________________________________ 14 Comparisons of sets ________________________________________________________________________________ Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); type daySet = set( day ); daySet x = new daySet{ day.mon .. day.thu }; daySet y = new daySet{ day.wed .. day.fri }; println( x == x ); println( x == y ); println( x < y ); println( x <= y ); println( x & y < x ); println( x < x ); println( x <= x ); println( x | y > x ); Suitable examples provided to test? 14 (xxx/5 marks) Comparisons of sets ________________________________________________________________________________ 15 Switch statements with integer, char, and enumerated type values ________________________________________________________________________________ Grammar makes use of RangeList, etc, and permits expressions? Build parse tree correctly? Reprints correctly? Checks the types of ranges agree with the type of the index? Works for for ( char c = ' '; c < '~'; c++ ) switch ( c ) { 'A'..'Z': println( "Upper " + c ); 'a'..'z': println( "Lower " + c ); '0'..'9': println( "Digit " + c ); ' '..'~': println( "special " + c ) } Works for for ( int i = -25; i < 25; i++ ) switch ( i ) { 1, 3, 5, 7, 9: println( "Odd " + i ); 2, 4, 6, 8: println( "Even " + i ); .. -1: println( "Negative " + i ); ..: println( "Large " + i ) } Works for type day = enum( sun, mon, tue, wed, thu, fri, sat ); for ( day d = day.sun; d <= day.sat; d++ ) switch ( d ) { day.mon..day.fri: println( "Weekday " + d ); ..: println( "holiday " + d ) } Works for char c1 = 'a', c2 = 'z'; char C1 = 'A', C2 = 'Z'; for ( char d = ' '; d <= '~'; d++ ) switch ( d ) { c1..c2: println( "lower " + d ); C1..C2: println( "upper " + d ); ..: println( "other " + d ) } Works for switch ( 32 ) { ' ': // Error println( "space" ); ..: println( "other" ) } Suitable examples provided to test? 15 (xxx/10 marks) Switch statements ################################################################################ Bonus Questions ################################################################################ ________________________________________________________________________________ 16 Structured type notation ________________________________________________________________________________ Grammar Type ::= STRUCT LEFT FieldDeclListOpt RIGHT FieldDeclListOpt::= FieldDeclList | /* Empty */ ; FieldDeclList::= FieldDeclList SEMICOLON FieldDecl | FieldDecl FieldDecl::= Type IdentList Build parse tree correctly? Reprints correctly? Works for type r = struct( int a, b; char c, d ); r x, y; x = y; Suitable examples provided to test? 16 (xxx/5 marks) Structured type notation ________________________________________________________________________________ 17 Recursively defined structured types ________________________________________________________________________________ Works for type list = struct( int value; list next ); list x, y; x = y; Suitable examples provided to test? Equality of types for two identical but different recursive structure types works (Bonus bonus). 17 (xxx/5 marks) Recursively defined structured types ________________________________________________________________________________ 18 Field selection ________________________________________________________________________________ Grammar? Variable::= Primary DOT IDENT Build parse tree correctly? Reprints correctly? Works for type list = struct( int value; list next ); list x, y; x = new list; y = new list; x.value = 3; y.value = 4; x.next = y; y.next = null; for ( list p = x; p != null; p = p.next ) println( p.value ); Suitable examples provided to test? 18 (xxx/5 marks) Field selection ________________________________________________________________________________ 19 Constructors for structures ________________________________________________________________________________ Grammar? Build parse tree correctly? Reprints correctly? Works for type list = struct( int value; list next ); list x, y; y = new List{ 4, null }; x = new list{ 3, y }; for ( list p = x; p != null; p = p.next ) println( p.value ); Suitable examples provided to test? 19 (xxx/5 marks) Constructors for structures ________________________________________________________________________________ 20 Set of integers ________________________________________________________________________________ Works for type intSet = set( int ); intSet x, y; x = new intSet{ .. 20, 30 .. 40 }; y = new intSet{ -20 .. 10, 35 .. }; println( x & y ); Suitable examples provided to test? 20 (xxx/15 marks) Set of integers ________________________________________________________________________________ 21 For loops to iterate through sets ________________________________________________________________________________ Grammar? Build parse tree correctly? Reprints correctly? Both iterate up and down, and combination? Suitable examples provided to test? 21 (xxx/5 marks) For loops to iterate through sets ################################################################################ The markers may give up to 10 marks, to about one student in 10, for good documentation, good design, additional features implemented (above those specified for bonus marks), additional test examples, coping well with errors (including syntax errors), etc. ################################################################################ extra (xxx/10 marks) Insight, Design, additional work, etc. ################################################################################ ################################################################################ Enter raw total mark here: Not Marked ################################################################################ Your raw mark is considered to be out of 80. However students may obtain more than full marks due to increases in marks for some sections, attempting the bonus topics, etc. Your adjusted mark, taking into account bonus and penalty marks will be computed separately, from the time of submission. ################################################################################ Comments: ################################################################################