College of Computing and Information Technology
Theory of Programming Languages
Fall Semester 2024/2025
Assignment #1 from Sebesta (due 27/2/2025)
Please answer in your own handwriting. Try to make your handwriting as clear,
clean, and organized as possible.
[1-1] What is an example of lack of orthogonality in C?
[1-2] Why is readability important to writability?
[1-3] Strings are supported by all modern programming languages, but no consensus
has emerged among language designers on how strings should be classified. What
consequences have it for the programmer to have strings as simple types or as
structured types, respectively?
[1-4] What is the disadvantage of having too many features in a language?
[3-1] What would the tokens be in the following statement: if (a < 5) then b = a
[3-2] Use the grammar shown below to draw the parse tree for “3 * - 9 + 5 =”
< Calc >
< Expr >
< Value >
< Digit >
< Oper >
< Sign >
→ < Expr > =
→ < Value > | < Value > < Oper > < Expr >
→ < Digit > | < Sign >< Digit >
→3|5|9
→+|-|*|/
→-|+
[3-3] Do a leftmost derivation of the string a = b * (c + a) given the shown grammar:
Write each step in the derivation on a separate line.
S ID = E
ID a | b | c
E E + E | E * E | ( E) | ID
[4-1] An identifier in a programming language can consist of only letters (upper and
lowercase) and digits, and must start with a capital letter and end with a digit. Draw a
finite automaton (state diagram) for this and be sure to properly denote the accepting
states.
[4-2] Perform the pairwise disjointness test for the
following grammar rules:
[5-1] Consider the following C program:
void fun(void){
int a, b, c; /* definition 1*/
………
while (………) {
int b, c, d; /* definition 2 */
a. A aB | b |cBB
b. B aB | bA | aBb
c. C aaA | b | caB
College of Computing and Information Technology
Theory of Programming Languages
Fall Semester 2024/2025
Assignment #1 from Sebesta (due 27/2/2025)
………
(1)
while(………) {
int c, d, e; /* definition 3 */
………
(2)
}
………
(3)
}
………
(4)
}
For each of the 4 marked points in this function, list each visible variable, along with
the number of the definition statement that defines it.
[5-2] Assume the following Ada program was compiled and executed using static
scoping rules. What value of x is printed in procedure Sub1? Under dynamic scoping
rules, what value of x would be printed? (show how you reached the result in both
cases)
procedure Main is
x: Integer;
procedure Sub1 is
begin -- of Sub1
Put(x);
end; -- of Sub1
procedure Sub2 is
x: Integer;
begin -- of Sub2
x := 10;
Sub1;
end; -- of Sub2
begin -- of Main
x:=5;
Sub2;
end; -- of Main
[5-3] Explain with examples what “coercion” is.
[6-1] Multidimensional arrays can be stored in row-major order or in column-major
order. Develop the access function of a column-major order arrangement for 2dimensional arrays.
[6-2] Describe the two main problems with using pointers in C, providing code
examples as needed.
[6-3] Discuss implementation choices of character strings in various programming
languages. Mention at least three choices.
[7]
College of Computing and Information Technology
Theory of Programming Languages
Fall Semester 2024/2025
Assignment #1 from Sebesta (due 27/2/2025)
1. Describe short-circuit evaluation of expressions in programming languages,
giving at least two examples (one should be a numeric expression and another
should be Boolean).
2. Consider the following C program:
int fun(int *i) {
*i += 5;
return 4;
}
void main() {
int x = 3;
x = x + fun(&x);
}
What is the value of x after the assignment statement in main, assuming
a. operands are evaluated left-to-right.
b. operands are evaluated right-to-left.
[8]
1. In the Java example:
if (sum == 0)
if (count == 0)
result = 0;
else result = 1;
Which if gets the else? How could you force the else to go with the other if?
2. In old versions of FORTRAN, you could only use one statement in the if-clause
(right after the 'if' keyword and the condition). Explain why this was a bad design
choice and how the problem was solved in C.
[9]
1. In most Fortran IV implementations, parameters were passed by reference, using access path
transmission only. State both the advantages and disadvantages of this design choice.
2. Consider the following program written in C syntax:
void swap(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}
void main() {
int value = 2, list[5] = {1, 3, 5, 7, 9};
swap(value, list[0]);
swap(list[0], list[1]);
swap(value, list[value]);
}
For each of the following parameter-passing methods, what are all of the
values of the variables value and list after each of the three calls to
College of Computing and Information Technology
Theory of Programming Languages
Fall Semester 2024/2025
Assignment #1 from Sebesta (due 27/2/2025)
swap?
a. Passed by value
b. Passed by reference
c. Passed by value-result
3.
Present one argument against providing both static and dynamic local variables in subprograms.
4. Consider the following program written in C syntax:
void fun (int first, int second) {
first += first;
second += second;
}
void main() {
int list[2] = {1, 3};
fun(list[0], list[1]);
}
For each of the following parameter-passing methods, what are the values of the list array after
execution?
a. Passed by value
b. Passed by reference
c. Passed by value-result