Please post your practice final solutions to this thread.
Best, Chris
Harrison Hwang, Bill Li, Christina Ng.
Question 6: Briefly explain how C struct are implemented in memory by the compiler.
The C struct is stored in memory sequentially, memory size is allocated based on the sizes of the types defined in the struct. Characters and booleans occupy one byte, and ints occupy 4 bytes, doubles occupy 8 bytes. In a 32 bit machines, memory is reserved as multiples of 4 bytes and in a 64bit machine each word will take up 8 bytes. for example <nowiki>
Struct example { int atomic_number; double atomic_weight; _Bool metallic; } e
</nowiki> For a 64bit system, the minimum size allocated would be 8+8+8 = 24bytes.
If the ((packed)) attribute is included, the lowest amount of memory would be allocated: 4+8+1 = 13 bytes
(Edited: 2021-12-06)Mark Masulis Andre Domingo Raymond Lin
E1 --> E2 + T E1.val := sum(E2.val, T.val) E1 --> E2 - T E1.val := difference(E2.val, T.val) E --> T E.val := T.val
F --> float F.val := float.val F1 --> -F2 F1.val := inverse(F2.val)
T1 --> T2 * F T1.val := product(T2.val, F.val) T --> F T.val := F.val
(Edited: 2021-12-06)<pre> Nicholas Semaan and Yuanye Yang
Applicative order evaluates subexpressions when, i.e. just before, the procedure is applied. Normal order passes subexpressions as they are, without evaluation, and proceeds with the evaluation only when the corresponding formal parameter is actually to be itself evaluated.
<u>'''Example:'''</u> def double x = (plus x x) def average x y = (divide (plus x y) 2) <u>'''Normal:'''</u> (double (average 2 4)) => (plus (average 2 4) (average 2 4)) => (plus (divide (plus 2 4) 2) (average 2 4)) => (plus (divide 6 2) (average 2 4)) => (plus 3 (average 2 4)) => (plus 3 (divide (plus 2 4) 2)) => (plus 3 (divide 6 2)) => (plus 3 3) => 6 <u>'''Applicative:'''</u> double (average 2 4) => double (divide (plus 2 4) 2) => double (divide 6 2) => double 3 => plus 3 3 => 6
<u>'''Macro Example:'''</u> If your Scheme lacks the conditional special form when, you could define when as the following macro:
(define-syntax when (lambda (test . branch) (list 'if test (cons 'begin branch))))
</pre>
(Edited: 2021-12-06)Luksawee, Alec Moore, Kyle Domen, Ameya Dighe
Pass by value: replace parameters in the body of the procedure by the corresponding argument value
(define (add x) lambda(y)( (set! x (+ x y)) ) ) ) (define add1 (add 2)) (add1 5)
Pass by reference - callee gives a direct reference to the variable and both caller and callee point to the same variable in memory.
void increment(int %count) { count++; }
int main() { int count = 0; increment(count); printf("%d", count); }
pass by value result- do pass by value and then take result and copy it back to location. ex. begin integer n; procedure p(k:int) begin n: n+1 k: k+4 print(n) end
main n = 0 p(n) print(n) end
which the call by value result it will be 1 4
(Edited: 2021-12-06)Team Members: Brandon Ong, Aaron Xiang, and Chris McCain
Structured programming is programming that emphasizes modularization of code, structured types, descriptive variable and constant names, and extensive comments
PHP Example for multi-level returns:
function two_return(){
return 2;
}
function testReturns(){
for(i = 0; i < 100; i++){
for(j = 0; j < 20; j++){
if(j == 20){
echo "Return 1 at ";
echo j;
echo "\n";
return 1;
}
if(i == 50){
echo "Return 2 at ";
echo i;
echo "\n";
two_return();
}
}
}
}
Scheme Continuations: Use the function call-with continuation or call/cc to set a single parameter procedure to receive and manipulate an environment. After call/cc, the procedure is run in the supplied environment and once call/cc completes the execution resumes.
(Edited: 2021-12-06)<nowiki> Shaoyue Liu, Thanh Le Question 3: What is short circuit evaluation? Give a C example of short circuit versus non short circuit evaluation.
If we have a boolean expression, we can often determine its value without having to completely evaluate it.
Short circuit evaluation: #include <stdio.h> int main() { int a = 10; int b = -1;
#does not check the second condition after the first conditon failed
if (a != 10 && b == -1) {
printf("Don't print this line.");
}
else {
printf("Print this.");
}
return 0;
}
Considering the program above, if we are using non short circuit evaluation, the program would evaluate the second condition after the first condition failed.
</nowiki>
(Edited: 2021-12-06)Sweet Zhang, Tomer Erlich
The compiler will look for implementations of '' that will take the types (int, float), it could be that '' is overloaded and has an implementation of (float, float) since int can be coerced into type float by int -> (float) int, therefore (7, 3.14) would evaluate to float. The same process would then apply to implementations of +, which would find +(float, float) therefore +((7,3.14), (float)5) -> float
(Edited: 2021-12-06)Team Members: Angela Pham, Jesse Dong, Ian Chavez, Arun Murugan
Question 2: Jason Hammar Leo Alciso Edward Phan
Attribute grammars are a framework for defining semantics of programming languages in a syntax-directed fashion.
S-attributed Grammars are grammars where the left-hand side consists of a non-terminal that is dependent on the attribute of a non-terminal on the right-hand side. Example: A->B > A.Val = B.Val
L-attributed Grammars are grammars that have a non-terminal on the right-hand side which has its value depend on another attribute on the right hand side of the same production rule. Example: A->BC > B.Val = C.Val
A side effect is something a programming language has where it influences subsequent computation in any way other than by returning a value for use in the surrounding context. i.e. In some languages, making a variable on the global scope may affect the return value of the function.
(Edited: 2021-12-06)