Senin, 08 April 2013

Concepts of Programming Languages Assignment - Chapter 7

Review Questions

3. What is a prefix operator?
prefix operators is the operators that precede the operands

5. What is a nonassociative operator?
Operator with illegal expression

 9. What is a coercion?
 A coercion is the implicit type convertion
 
10. What is a conditional expression?
Conditional expression is a statement that uses if-then-else statements
 
11. What is an overloaded operator?
An overloaded operator is the multiple uses of an operator
 
14.  What is a mixed-mode expression?
A mixed-mode expression is one that has operands of different types
 
21. What is the purpose of a compound assignment operator?
To specifying a commonly needed form of assignment

24. What two languages include multiple assignments?
Perl, Ruby and Lua
 
28. What is a cast?
Cast is explicit type conversions. To specify a cast, the desired type is placed in parentheses just before the expression to be converted

Problem Set

1. Run the code given in Problem 13 (in the problem set) on some system that supports C to determine the values of sum1 and sum2. Explain the result.
Suppose Type1 is a subrange of Integer. It may be useful for the difference between Type1 and Integer to be ignored by the compiler in an expression

3. Do you think the elimination of overloaded operators in your favorite language would be beneficial? Why or why not?
Yes, I think that the elimination of overloaded operators in my favorite language would be beneficial. Elimination of overloaded operators will result in:
- Enhance and increase readability.
- Minimize the compiler overhead (choosing the correct operator meaning).

4. Would it be a good idea to eliminate all operator precedence rules and require parentheses to show the desired precedence in expressions? Why or why not?
No, it's not a good idea to eliminate all operator precedence rules. Although this idea sounds good, but in fact it affects the flexibility provided by the language since only one way is used to show the precedence in expressions. Moreover, using parentheses will future more affects writability and in some cases readability.

7. Describe a situation in which the add operator in a programming language would not be commutative.
An expression such as a + fun(b)

8. Describe a situation in which the add operator in a programming language would not be associative.
Consider the integer expression A + B + C. Suppose the values of A, B, and C are 20,000, 25,000, and -20,000, respectively. Further suppose that the machine has a maximum integer value of 32,767. If the first addition is computed first, it will result in overflow. If the second addition is done first, the whole expression can be correctly computed.

11. Write an BNF description of precendence and associativity rules defined for the expressions in Problem 9. Assume the only operands are the names a, b, c, d, and e.

<expr> → <expr> or <e1> | <expr> xor <e1> | <e1>
<e1> → <e1> and <e2> | <e2>
<e2> → <e2> = <e3> | <e2> /= <e3> | <e2> < <e3>
| <e2> <= <e3> | <e2> > <e3> | <e2> >= <e3> | <e3>
<e3> → <e4>
<e4> → <e4> + <e5> | <e4> – <e5> | <e4> & <e5> | <e4> mod <e5> | <e5>
<e5> → <e5> * <e6> | <e5> / <e6> | not <e5> | <e6>
<e6> → a | b | c | d | e | const | ( <expr> )

15. Explain why it is difficult to eliminate functional side effects in C.
One reason functional side effects would be difficult to remove from C is that all of C’s subprograms are functions, providing the ability of returning only a single data value (though it could be an array). The problem is that in many cases it is necessary (or at least convenient) to return more than one data value, which is done through the use of pointer actual parameters, which are a means of creating functional side effects

Concepts of Programming Languages Assignment - Chapter 6

Review Questions

1.  What is a descriptor?
A descriptor is the collection of the attributes of a variable

2. What are the advantages and disadvantages of decimal data types?
The advantages of decimal data types is being able to precisely store decimal values, at least those within a restricted range, which can’t be done with floating-point. And the disadvantages of decimal data types are that the range of values is restricted because no exponents are allowed, and their representation in memory is mildly wasteful

3. What are the design issues for character sting types?
The two most important design issues that are specific to character string types are the following:
- Should strings be simply a special kind of character array or a primitive type?
- Should strings have static or dynamic length?

11.How does JavaScript support sparse arrays?
JavaScript sparse arrays means that the subscript values need not be contiguous

12. What languages support negative superscripts?
c++

13. What languages support array slices with stepsized?
Phyton, Perl, and Ruby

14. What array initialization feature is available in Ada that is not available in other common imperative languages?
Ada provides two mechanisms for initializing arrays in the declaration statement: by listing them in the order in which they are to be stored, or by directly assigning them to an index position using the => operator, which in Ada is called an arrow

17. Define row major order and column major order.
Row major order = the elements of the array that have as their first subscript the lower bound value of
that subscript are stored first, followed by the elements of the second value of the first subscript, and so forth.
Column major order = the elements of an array that have as their last sub-script the lower bound value of that subscript are stored first, followed by the elements of the second value of the last subscript, and so forth

20.What is the structure of an associative array?
hashes

23. What is the primary difference between a record and a tuple?
In tuple, the elements are not named

32. What are the design issues for unions?
The primary design issues that are particular to union types are the following:
- Should type checking be required? Note that any such type checking must be dynamic.
- Should unions be embedded in records?

35. What are the design issues for pointer types?
The primary design issues particular to pointers are the following:
- What are the scope and lifetime of a pointer variable?
- What is the lifetime of a heap-dynamic variable?
- Are pointers restricted as to the type of value to which they can point?
- Are pointers used for dynamic storage management, indirect addressing, or both?
- Should the language support pointer types, reference types, or both?

43. What is a compatible type?
A compatible type is one that either is legal for the operator or is allowed under language rules to be implicitly converted by compiler-generated code to a legal type.

44. Define type error.
type error the application of an operator to an operand of an inappropriate type

45. Define strongly typed.
widely acknowledged as being a highly valuable language characteristic

Problem Set

1. What are the arguments for and against four signed integer sized in Java?
Bytes =1 byte, short = 2 bytes, integer = 4 bytes, long  = 8 bytes. As a result, depending on the domain of the variable required, data types are used.

2. How are negative integers stored in memory?
A negative integer could be stored in sign-magnitude notation, in which the sign bit is set to indicate negative and the remainder of the bit string represents the absolute value of the number. Sign-magnitude notation does not lend itself to computer arithmetic. Most computers now use a notation called twos complement to store negative integers, which is convenient for addition and subtraction.

5. What disadvantages are there in implicit dereferencing of pointers, but only in certain contexts? For example, consider the implicit dereference of a pointer to a record in Ada when it is used to reference a record field.
When implicit dereferencing of pointers occurs only in certain contexts, it makes the language slightly less orthogonal. The context of the reference to the pointer determines its meaning. This detracts from the readability of the language and makes it slightly more difficult to learn

7. Compare the pointer and reference type variable in C++.
A C++ reference type variable is a constant pointer that’s always implicitly dereferenced

15. What are the arguments for and against Head management’s single-size cells and variable-size cells?
Integer values stored in decimal waste storage in binary memory computers, simply as a result of the fact that it takes four binary bits to store a single decimal digit, but those four bits are capable of storing 16 different values. Therefore, the ability to store six out of every 16 possible values is wasted. Numeric values can be stored efficiently on binary memory computers only in number bases that are multiples of 2. If humans had
developed hands with a number of fingers that was a power of 2, these kinds of problems would not occur

21. In what way is dynamic type checking better than static type checking?
Static checking reduces programmer flexibility

22. Explain how the dangling-pointer problem can be removed using the locks-and-keys approach.
In the locks-and-keys approach, pointer values are represented as ordered pairs (key, address), where the key is an integer value. Heap-dynamic variables are represented as the storage for the variable plus a header cell that stores an integer lock value. When a heap-dynamic variable is allocated, a lock value is created and placed both in the lock cell of the heap-dynamic variable and in the key cell of the pointer that is specified in the call to new

Concepts of Programming Languages Assignment - Chapter 5

Review Questions

1. What are the design issues for names?
- Are names case sensitive?
- Are the special words of the language reserved words or keywords?

4. What is an alias?
a hindrance to readability because it allows a variable to have its value changed by an assignment to a different variable

5. Which category of C++ reference variable is always aliases?
union types

6. What is the l-value of a variable? What is the r-value?
l-value is an address of a variable
r-value is a variable’s value

7. Define binding and binding time.
binding is an association between an attribute and an entity, such as between a variable and its type or value, or between an operation and a symbol. Binding time is the time at which a binding takes place

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

16. What is the referencing environment of a statement?
referencing environment of a statement is the collection of all variables that are visible in the statement

18. What is block?
Such variables are typically stack dynamic, so their storage is allocated when the section
is entered and deallocated when the section is exited

Problem set

1. Decide which of the following identifier names is valid in C language. Support your decision
Student & Student123
because no consist of special character and number at the first of names

2. What is l-value? Write a statement in C language which gives the compile time error “l-value required”.
The address of a variable is sometimes called its l-value
scanf(“%d”,num);

4. Why is the type declaration of a variable necessary? What is the value range of the int type variable in Java?
because it must include an initial value, byte

5. Approaches to building a lexical analyzer:
- Write a formal description of the token patterns of the language using a descriptive language related to regular expressions. These descriptions are used as input to a software tool that automatically generates a lexical analyzer. There are many such tools available for this. The oldest of these, named lex, is commonly included as part of UNIX systems
- Design a state transition diagram that describes the token patterns of the language and write a program that implements the diagram
- Design a state transition diagram that describes the token patterns of the language and hand-construct a table-driven implementation of the state diagram

6.  State transition diagram is a directed graph. The nodes of a state diagram are labeled with state names.
The arcs are labeled with the input characters that cause transitions among the states

8.a.Assuming static scooping, in the following, which declaration of x is the correct one for a reference to x?
b.Repeat part a, but assume dynamic scoping.

(a) i. Sub1
ii. Sub126
iii. Main
(b) i. Sub1
ii. Sub1
iii. Sub1

9.  The differences between top-down and bottom-up parsers:
- Syntax analyzers are either top-down, meaning they construct left most derivations and a parse tree in top-down order, which mean the tree built from the root downward to the leaves
- Bottom-up meaning case they construct the reverse of a rightmost derivation and a parse tree in bottom-up order, which mean the parse tree is built from leaves upward to the root