Assignment #5
Pengajar : Tri Djoko
Wahjono
Review Question :
11. What are
the advantages and disadvantages of dynamic type binding?
=>
The
advantage is flexibility (generic program units).
12. Define static, stack-dynamic, explicit
heap-dynamic, and implicit
heap-dynamic variables.What are their advantages and
disadvantages?
=>
Static:
bound to memory cells before execution begins and remains bound to the same
memory cell throughout the execution.
Stack-dynamic:
storage bindings are created for variables when their declaration statements
are elaborated.
Explicit
heap-dynamic: allocated and deallocated by explicit directives, specified
by the programmer, which take effect during execution.
Implicit
heap-dynamic variables: Allocation and deallocation caused by assignment
statements.
13. Define lifetime, scope, static scope, and dynamic
scope.
=>
Lifetime: A time
during which the variable is bound to a specific memory location. The lifetime
begins when it is bound to a specific cell and ends when it is unbound from
that cell.
Scope: The
range of statements in which the variable is visible. A variable is visible in
a statement if it can be referenced in that statement.
Static scope: is based on
program text and to connect a name reference to a variable , you (or the compiler)
must find the declaration.
Dynamic scope: Based on
calling sequences of program units, not their textual layout (temporal versus
spatial). References to variables are connected to declarations by searching
back through the chain of subprogram calls that forced execution to this point.
14. How is a reference to a nonlocal variable in a static-scoped
program
connected to its definition?
=> A reference to
a non-locally variable in a static-scoped language with nested subprograms
requires a two step access process:
1. Find the correct activation record instance
2. Determine the correct offset within that
activation record instance
15. What is the general problem with static scoping?
=>
Usually too much access. Scope structure destroyed as program
evolves.
Problem Set :
1. Which of the following identifier forms is most readable? Support
your
decision.
sumOfSales
sum_of_sales
SUMOFSALES
=> I'm choose sum_of_sales, because
it's easier to see the spaces
between words and easier to read.
2. Some programming languages are typeless. What are the obvious
2. Some programming languages are typeless. What are the obvious
advantages
and disadvantages of having no types in
a language.
=>
* Advantages :
allow users to write sloppy programs faster.
* Disadvantages : cannot control the data
and variables, compiler cannot detect any mistake.
3. Write
a simple assignment statement with one arithmetic operator in some language you
know. For each component of the statement, list the various bindings that are
required to determine the semantics when the statement is executed. For each
binding, indicate the binding time used for the language.
=> (C++)
int count;count = count + 5;
Possible types for count: set at language design time. Type of count: bound at compile time.
Set of possible values of count: bound at compiler design time. Value of count: bound at execution time with this statement. Set of possible meanings for the operator symbol ““:*bound at language definition time.*Meaning of the operator symbol “” in this statement: bound at compile time.
Internal representation of the literal “5”: bound at compiler design time.
Set of possible values of count: bound at compiler design time. Value of count: bound at execution time with this statement. Set of possible meanings for the operator symbol ““:*bound at language definition time.*Meaning of the operator symbol “” in this statement: bound at compile time.
Internal representation of the literal “5”: bound at compiler design time.
4. Dynamic type binding is closely related to implicit heap-dynamic
variables. Explain this relationship.
=> Both are related to the assignment and the statement.
5. Describe a situation when a history-sensitive variable in a subprogram is
useful.
=> Both are related to the assignment and the statement.
5. Describe a situation when a history-sensitive variable in a subprogram is
useful.
=>History sensitive variables may be useful in data
manipulation
subprograms, where some operation is performed on a variable, and
the function exits, then the function is called again. This way,
the function
doesn't have to take the variable as a parameter, but only
to return it.
to return it.