Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CSCI 3370: Programming Languages 1. Errors in a computer program can be classifi

ID: 648585 • Letter: C

Question

CSCI 3370: Programming Languages

1. Errors in a computer program can be classified according to when they are detected and, if they are detected at compile time, what part of the compiler detects them. Using your favorite imperative language, give an example of each of the following.

a. A lexical error, detected by the scanner

b. A syntax error, detected by the parser

c. A static semantic error, detected by semantic analysis

d. A dynamic semantic error, detected by code generated by the compiler

e. An error that the compiler can neither catch nor easily generate code to catch (this should be a violation of the language definition, not just a program bug)

(Use the same language for parts (a)-(d). You may use a different language for part

(e) -- this part could be challenging in Java, for example.)

2. In your local implementation of C, what is the limit on the size of integers (describe how you found the answer)? What happens in the event of arithmetic overflow? What are the implications of size limits on the portability of programs from one machine/compiler to another? Find out how the answers differ to these questions for any one of the following languages: Java, Ada, Pascal or Scheme?

3. Indicate the binding time (e.g., when the language is designed, when the program is linked, when the program starts execution, etc.,) for each of the following decisions in your favorite programming language and implementation. Explain any answers you think are open to interpretation.

o The number of built-in functions (abs, cosine, etc.).
o The variable declaration that corresponds to a particular variable reference

(say, its use in an expression).
o The maximum length allowed for a constant (literal) character string.
o The referencing environment for a subroutine that is passed as a parameter o The address of a particular library routine.
o The total amount of space occupied by program code and data.

4. Give two conceptually different concrete examples drawn from programming languages with which you are familiar in which a variable or some other object is live but not in scope. (Hint: consider how different abstraction and information hiding mechanisms may affect the visibility and accessibility of objects.)

Explanation / Answer

1.

Let the language chosen be Java, then the examples of each of the following is as follows:

a. Lexical error, detected by the scanner: ‘aa’ is the string and not the character so ‘a’ will not be assigned the data type char.

b. Syntax error: Missing ‘;’ at the end of a statement.

c. A static semantic error, detected by semantic analysis: arr[10.5], the array will become out of bounds.

d. A dynamic semantic error, detected by code generated by the compiler:

int [] array = new int [10];

array [10] = 0;

The array here, has been assigned value 0 which is not with in the bounds.

e. An error that the compiler can neither catch nor easily generate code to catch (this should be a violation of the language definition, not just a program bug:

int a;

if (false)

{

x =10;

system.out.println(“Error”);

}

Here, the value of x is unreachable, yet error will not be reported.

2.

In the local implementation of C, the limit on the size of the integers depends on the compiler. The size range varies from compiler to compiler.

In case of a 16-bit operating system, the size is: -32, 768 to 32, 767.

In case of a 32-bit operating system, the size is:   -2,147,483,648 to 2,147,483,647

In C, if an arithmetic overflow occurs the program terminates and stops responding. For e.g.: If the number is divided by zero, the array becomes out of bound.

In java, the size of integer is always 32 bits across different platforms.

In Ada, there is considerable variation across platforms, but there is a variety of constraints on range and behavior. The built-in integer type must be atleast -215 +1……215-1.

In Pascal, the integers run from -maxint to maxint.

In Scheme, there is arbitrary precision on all platforms.

3.

The binding time depends on the following times:

4.

The two conceptually different concrete examples drawn from a programming language, say Java in which a variable or some other object is live but not in scope can be given by using abstraction and encapsulation mechanism.

Suppose a private variable is declared in a class abc:

class abc

{

           int data;

           public int get data ()

           {

                   return data;

            }

}

When an object is instantiated, the private data cannot be accessed directly, even if scope is there. The public member functions can help to access the data.

Similarly, for abstraction, the unnecessary features can be hidden showing only essential features.