Languages and Translation (CS 1011010102)



Languages and Translation (CS 362)Fall 2017 11/2/17 (100 points)Name __________KEY__________________Fill in the blank for the short questions regarding types, L-values, and R-values of a variable. Assume addresses assignments of d=x1000, e=x1004, f=x1008, g=x100a.[12 pts]float d, e;float * f;float * g;d = 12; // the constant 12 is a(n) __integer/float__ type because ______has no decimal—syntax is integer/ compiler determines it based on assignment in this case___ .e = d*3; // uses the __r-value__ of variable d in the assignment and the __l-value__ of e.f = &d; // f is set to the ___l-value___ of variable dg = f; // after the assignment, g and f are __aliases__ of variable __d__.What would be printed by the following cout statement? cout << d <<” “<< e <<” “<< f <<” “<< *g; 12.0 36.0 0x1000 12.0Express the following relational and algebraic expression into a sequence of pushes, binary and unary operations for a stack machine, like our project code generation. Use high level “if-then” control structures as appropriate for short circuit evaluation. The method top() can refer to the top of the stack without popping. Method pop() can also be used to ensure there is no memory leak on the stack. For example the expression “a*(b+c)” should be written as push(a);push(b);push(c);binary(+);binary(*);Your answer makes the evaluation order of operations clear. [8 pts]a > 0 && b >0 && a < b + c*dpush(a);push(0);binary(<);if(top()) { pop(); push(b); push(0); binary(<); if(top()){ pop(); push(a); push(b); push(c); push(d); binary(*); binary(+): binary(<): }}//top() holds the cond resultinstead of pops, end block with binary(&)Give 3 reasons why FORTRAN is still a viable language.[5 pts]Dusty decks—legacy softwareefficientNewly revised for use in supercomputing Show the semantic actions for compiling a high level language REPEAT-UNTIL (Pascal’s variant for DO-WHILE, repeatedly execute the loop body until the condition is true) to generate the low level unconditional branches (goto), conditional branches (if-goto) and labels. Indicate what might need to be communicated via $$, $1, $2, etc. to pass information to other reductions. Use some form of print or cout<< to indicate the code emission as well.[10 pts]<stmt> :== <repeat> <stmts> UNTIL <cond-part> {cout << “if ( ! mem[top--]) goto L”<<$1<<”;”<<endl;}<repeat> :== REPEAT { int target = nextlabel(); cout << “L”<<target<<”:”<<endl; $$ = target;}Given the FORTRAN 90 two dimensional array declarationREAL, DIMENSION (10:15,1:10) Mshow actual, final addresses for the two elements below using both row-major and column-major Assume the array has base address 1000 Recall row-major storage has its rightmost subscript varying the fastest; leftmost the slowest.[10 pts]Address for M(10,5) in row-major =___1000 + 5*4 = 1016___Address for M(12,2) in row major = 1000+[(12-10)*10 +(2-1)]*4 = 1084__ and in column major = __1000 + [(2-1)*6 + (12-10)] *4 = 1032___________What is the adjusted base address (virtual origin) to minimize the arithmetic operations necessary to calculate the indexing for an arbitrary M(I,J)? __1000 + [(I-10)*10 + (J-1)] * 4 = 1000 + [I*10 - 100 + J -1 ] * 4 =1000 + [I*10 +J -101] * 4 =1000 – 404 + [I*10 +J]*4 =596 + [I*10 +J]*4Compute the amount of storage in bytes required by the PL/I Employee structure declaration below. An array of employees is declared. FIXED(n) means you want up to n digits in an integer; the number of bytes would be 1, 2, 4 or 8 depending on how many digits such integers can hold.[12 pts]1 Employee (100), 2 Name, total bytes for the Name element _47____ 3 Last CHARACTER(25), 3 First CHARACTER (15), 3 Middle CHARACTER (1), 3 Suffix CHARACTER (6) 2 Age FIXED (3), total bytes for the Age element _2______ 2 Address, 3 Street, 4 Number FIXED(6), offset in bytes to the Number element __49____ 4 St_Name CHARACTER(24), 3 City CHAR(18), 3 State CHAR(2), total bytes for one Employee element ____~120______ 3 Zip CHAR(10), 2 N_Dependents FIXED(2), 2 SSN CHARACTER(9); total bytes for the entire array ___11000_____ d) Show how to refer to the State of the I-th employee.________Employee(I).Address.State__________ Assume you are learning a new language as saw a void function and a for loop control structure with the body indented like Python. void foo(x) x = x + 1void main() k = 3 m = 5 for I = k-1 .. m*2 step 1 loop print i foo(i) m = m-1[8 pts]One very likely output when main() is called would be 2 3 4 5 6 7 8 9 10 Give two other possible output sequences based on other semantics of for loops and parameter passing when main() is called. Clearly explain why each deviant sequence would be possible. With recalculating the m*2 ending value 2 3 4 Because i=1, m*2=8, i=3, m*2=6, i=4, m*2=4, next i is > 4If call-by-reference is used then 2 4 6 8 10Would be generated as the index is getting bumped twice.Parameter passing. Show the output of the program for each of the different parameter passing schemes. If the scheme is illegal in the given context, indicate so.[10 pts]Mechanismix[2]X[3]A,B by reference:328A by value, B by value:248A by name, B by name:344A in, B inout (like Ada):228A, B value-result (argument address is reevaluated upon exit)342int i;int x[5];void foo (int a, int b){a = a+1;b = b/2;}void maini = 2; x[0] = 1; x[1] = 2; x[2] = 4; x[3] = 8; x[4] = 16;foo (i, x[i]);cout << i << x[2] << x[3];endTrue/False.[9 pts]__T__ An array is like a math function that maps the index(es) to the values stored within.__ F __ An array index always starts at 0 for all languages.__ T __ Some languages allow any value, including strings as their index, resulting in an associative array, hash table or dictionary.__ F __ Row major is more efficient in space and access than column major.__ T __ Record structures are equivalent to the mathematics Cartesian product.__ F __ Record elements are accessed by numeric indexes like arrays.__ T __ Records in object oriented languages are replaced by class definitions.__ F __ Early languages like COBOL and PL/I limited the composition and nesting levels of records and arrays.__ T __ C unions and Pascal variant records could define variables of different types to share the same memory.Describe the difference between structure equivalence and name equivalence for types. Give a small example. Why is one more desirable than the other? [6 pts]structure = variables are considered same type if the internal structures are the same (type order/size)name = type structure must be named, and variables are only same type if they are defined with the same name.examples will varyName is likely more desirable. If you went to the trouble to define them differently, then the intention is probably the variables ought to be different.Describe Dijkstra’s Guarded If. [5 pts]A series of conditions and statements. One condition must evaluate to true; error otherwise. If more than one condition evaluates to true, it is non-deterministic which statement will executeShow how to map a traditional IF cond THEN stmt1 ELSE stmt2 into a guarded if.If cond -> stmt1;! cond -> stmt2;fiGive two different scenarios for a switch/case structure in which the underlying implementation would need to be different for efficiency.[5 pts]cases dense and small range, use a jump tablecases sparse and wide ranging, use multi-way if ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download