CS 4150 Midterm


LEARN FROM MY MISTAKES
Dalibor Labudovic
CS 4150 Midterm
All answers to discussion questions need to be in complete sentences and grammatically correct! 80/100
  1. Compare static type binding and dynamic type binding. Be sure to use the four criteria (readability, writability, reliability, and cost).
    1. Comparing static type binding and dynamic type binding is determining whether a type binding has the ability to be changed. Static type binding obviously cannot be changed, under the four criteria’s: readability is low, writability is high ( variables have to utilized in another way to extend size ) , reliability is high (because following as the type is being used in one way and then in a different when the size has to altered) and cost is high. My thoughts on cost being high are the limitation a static type binding would pose on scalability. Dynamic type binding can be changed and under the four criteria’s: readability is high (a variable is hard to follow) , writability is low because it is declared once and size changes with its needs and lastly cost is low because code can be written once and is ready for scalability. 7/9
  2. Discuss the role of the lexical analyzer in the class project and how it accomplishes that role. The role of the lexical analyzer in the class project is process of converting a sequence of characters into a sequence of tokens. A "scanner" is used for the first stage of a lexer. A lexer is generally combined with a parser, which together analyze the syntax of a programming language. 9/9










  1. What is the difference between name equivalence and structural equivalence for type checking? What are the advantages of each? Be sure to use the four criteria in your discussion.
    1. The difference between name equivalence and structural equivalence for type checking can be summarized in their advantages. Name equivalence is a structure where each type declaration introduces a new type, district from all others, under the four criteria’s, this type checking will reduce readability and writability because name types can be case sensitive and therefore harder to follow. An advantage of name equivalence is the reliability in the structure as it allows different types to be utilized. Structural equivalence for type checking utilizes to check if two types as the same if they consist of the same component. From the four criteria’s perspective, advantages include readability and writability because these structures are relatively easy to follow due of consistency in the structure. 8/9
  2. What is a dangling reference? Write some code (pseudocode) that will create a dangling reference.
    1. A dandling reference is when a pointer is referencing an invalid memory location.
Main
{
String a = “hello”;
String b = a;
Free (a);
Print(b);
} 10/10








  1. What is a memory leak (garbage)? Write some code (pseudocode) that will create a memory leak.
    1. A memory leak is when an object is stored in memory but cannot be accessed by running code
Void Somefunction(void)
{
Int* a = malloc(sizeof(10));
}
Int Main(void)
{
Somefunction();
} 10/10
























  1. Using the grammar for the “project language”, construct a left-most derivation to show that the following is an element of this language.
MODULE A ; BEGIN x := 1 ; END A .
<program> MODULE <id> ; BEGIN <statement_sequence> ; END <id>
MODULE A; BEGIN <statement_sequence> ; END <id>
MODULE A; BEGIN <statement> ; END <id>
MODULE A; BEGIN <assignment_statement> ; END <id>
MODULE A; BEGIN <id> <assignment_operator><expression> ; END <id>
MODULE A; BEGIN x :=<expression> ; END <id>
MODULE A; BEGIN x := <literal_integer>; END <id>
MODULE A; BEGIN x := 1; END A 10/10






















  1. Modify the attached grammar (grammar 1) to add a binary exponential operator (**) that has higher precedence than either + or * and is right associative.


    1. <assign> -> <id> = <expr>
    2. <id> -> A | B | C
    3. <expr> -> <term> + < expr>| <term> * <factor> | <term> You modified the grammar!
    4. <term> -> <factor>**<term> | <<factor>
    5. <factor> -> ( <expr> ) | <id> 7/9


  1. Discuss the difference between row-major and column-major storage formats. Give an example of a case where the storage format being used impacts the run-time efficiency of some code.
    1. The differences between row-major and column-major storage formats is the when the two types of storage formats are interacting. Imaging a program A is running on row-major storage format and program B is running on column-major storage format. When program A refers to a row in its code and passes that to Program B, program B will interpret this as a column. A case in which a storage format being used to impacts the run time efficiency of some code would be a matrix style array in which a place in the matrix is referred to. Let’s say in the game of Chess. 6/9
  2. Give an example in pseudocode where there would be a difference in the value of a boolean expression depending upon whether short-circuit evaluation or full evaluation is used.
    1. Short-circuit
      1. If( 0 == 1 && 2+2==4)
Print(“this statement will not print, it will return false”);
Else if(2+2=4 || 0 == 1)
Print(“this statement will print, it will return true”);
    1. Full-circuit
      1. If(2+2 = 4 & 0=1)
Return false
Else
Return true ?? 2/9




  1. This question refers to the following code.
    1. What are the values of x, y, and z in each of the subprograms A, B, and C assuming static scoping?
      1. Subprogram A (x = 3, y= 1, z = 7) Subprogram B (x =3 , y=1 , z=7 )
Subprogram C(x =4 , y=2, z= ERROR)
    1. What are the values of x, y, and z in subprogram C assuming dynamic scoping and main calls subprogram A which calls subprogram C.
      1. Subprogram C (x=4 ,y=2 , z=7 ) 11/16

procedure Main is
x: Integer := 4;
y: Integer := 6;
procedure A is
x: Integer := 3;
z: Integer := 5;
procedure B is
y: Integer := 1;
z: Integer := 7;
begin
end B;
begin
end A;
procedure C is
y: Integer := 2;
begin
end C;
begin

end Main;
Grammar 1:
<assign> -> <id> = <expr>
<id> -> A | B | C
<expr> -> <expr> + < term> | <term>
<term> -> <term> * <factor> | <factor>
<factor> -> ( <expr> ) | <id>


Grammar for the project language
Parser


<program> → MODULE <id> ; BEGIN <statement_sequence> END <id> .
<statement_sequence> → <statement> ; | <statement_sequence> <statement> ;
<statement> → <assignment_statement> | <print_statement> | <while_statement> | <if_statement> | <until_statement>
<if_statement> → IF <boolean_expression> THEN <statement_sequence> ELSE <statement_sequence> END
<while_statement> → WHILE <boolean_expression> DO <statement_sequence> END
<assignment_statement> → <id> assignment_operator <expression>
<print_statement> → WriteInt ( <expression> )
<until_statement> → REPEAT <statement_sequence> UNTIL <boolean_expression>
<boolean_expression> → <relational_operator> <expression> <expression>
<relational_operator> → le_operator | lt_operator | ge_operator | gt_operator | eq_operator | ne_operator
<expression> → <arithmetic_operator> <expression> <expression> | <id> | literal_integer
<arithmetic_operator> → add_operator | sub_operator | mul_operator | div_operator


Lexical Analyzer


id → letter
literal_integer → digit literal_integer | digit
assignment_operator → :=
le_operator → <=
lt_operator → <
ge_operator → >=
gt_operator → >
eq_operator → =
ne_operator → #
add_operator → +
sub_operator → -
mul_operator → *
div_operator → /

Popular posts from this blog

CS3401 Practice Quiz 2 Part 2

CS3401 Practice Quiz 2 Part 1