April 7, 2013

KBP chapter 5


Rickvian Aldi / 02PCT / 1601253441
Assignment By Mr. Tri Djoko Wahjono

Review questions

3. In what way are reserved words better than keywords?
- As a language design choice, reserved words are better
than keywords because the ability to redefine keywords can be confusing.
These statements declare the program variable Real to be of Integer type
and the variable Integer to be of Real type.3 In addition to the strange
appearance of these declaration statements, the appearance of Real and Integer
as variable names elsewhere in the program could be misleading to program
readers.


4. What is an alias?

-When more than one variable name can be used to access the same memory location, the variables are called aliases.


5. Which category of C++ reference variables is always aliases?

-Two pointer variables are aliases when they point to the same memory
location. The same is true for reference variables. This kind of aliasing is simply
a side effect of the nature of pointers and references. When a C++ pointer is set
to point at a named variable, the pointer, when dereferenced, and the variable’s
name are aliases.


6. What is the l-value of a variable? What is the r-value?

-A variable’s value is sometimes called its r-value
-To access the r-value, the l-value must be determined first.

9. Define static binding and dynamic binding.

-An explicit declaration is a statement in a program that lists variable names
and specifies that they are a particular type. An implicit declaration is a means
of associating variables with types through default conventions, rather than
declaration statements. In this case, the first appearance of a variable name in a
program constitutes its implicit declaration. Both explicit and implicit declarations
create static bindings to types.

-With dynamic type binding, the type of a variable is not specified by a declaration
statement, nor can it be determined by the spelling of its name. Instead,
the variable is bound to a type when it is assigned a value in an assignment statement.


13. Define lifetime, scope, static scope, and dynamic scope.

-The lifetime of a variable is the time during which the variable is bound
to a specific memory location.

-The scope of a variable is 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.

-the method of binding names to nonlocal variables called static scoping.

-Dynamic scoping is based on the calling sequence of subprograms, not
on their spatial relationship to each other. Thus, the scope can be determined
only at run time.


15. What is the general problem with static scoping?

-First, in most cases it allows
more access to both variables and subprograms than is necessary. It is simply
too crude a tool for concisely specifying such restrictions. Second, and perhaps
more important, is a problem related to program evolution. Software is highly
dynamic—programs that are used regularly continually change. These changes
often result in restructuring, thereby destroying the initial structure that
restricted variable and subprogram access.
-Designers are encouraged to use far more globals than are necessary.


18. What is a block?

-allows a section of code to have its own local variables whose scope is minimized. Such variables
are typically stack dynamic, so their storage is allocated when the section
is entered and deallocated when the section is exited. Such a section of code
is called a block.


19. What is the purpose of the let constructs in functional languages?

-These constructs have two parts:
-The first of which is to bind names to values, usually specified as
expressions.
-The second part is an expression that uses the names defined in the
first part. Programs in functional languages are comprised of expressions, rather
than statements. Therefore, the final part of a let construct is an expression, rather than a statement.


23. What are the advantages of named constants?

-named constants are useful to increase readbility of program
for example, by using name pi instead of the constant 3.14159265

Problem Sets

1. Which of the following identifier forms is most readable? Support your decision.
SumOfSales
sum_of_sales
SUMOFSALES

answers :
sum_of_sales
seperated by underscore will give more space, also seperate each wort, so it will be more readable than those are not seperated.


2.Some programming languages are typeless. What are the obvious advantages
and disadvantages of having no types in a language?

answers :
-advantages : the programmer allowed to code programs quickly without worrying about data types
-disadvantages : it is slow for parser that has to keep track on whats on the variable.


5. Describe a situation when a history-sensitive variable in a subprogram is
useful.

answer :
-Globally accessible variables are often used throughout the execution
of a program, thus making it necessary to have them bound to the same
storage during that execution. Sometimes it is convenient to have subprograms
that are history sensitive.


10. Consider the following C program:

void fun(void) {
int a, b, c; /* definition 1 */
. . .
while (. . .) {
int b, c, d; /*definition 2 */
. . . <-------- 1
while (. . .) {
int c, d, e; /* definition 3 */
. . . <-------2
}
. . . <-------3
}
. . . <-------4
}
For each of the four marked points in this function, list each visible variable,
along with the number of the definition statement that defines it.

answers :
1 : a,b,c <definition 1> ; b,c,d <definiton 2>
2 : a,b,c <definition 1> ; b,c,d <definiton 2> ; c,d,e <definition 3>
3 : a,b,c <definition 1> ; b,c,d <definiton 2>
4 : a,b,c <definition 1>