1. Consider the following fragment of C code.
int x;
float y;
int *pi;
float *pf;
x = 12345;
y = (float) x; /* 1 */
printf("%f", y);
pi = &x; /* 2 */
pf = (float *) pi; /* 3 */
y = *pf; /* 4 */
printf("%f", y);
a) Explain as precisely as possible, from an implementation point of view, what gets copied into the left side by the assignment operator at positions 1, 2, 3, and 4 in the above code. Include in your explanation any effects caused by the "typecasts" in the code.
b)Will the output of the two "printf("%f", y);" statements be the same in both cases? Explain why or why not?
2. Some languages, (e.g., Ada, FORTRAN90) not only support array to array assignment, but support assignment of "slices" of arrays. Consider the Ada declarations below.
type MY_ARRAY_TYPE is array (10 .. 100) of INTEGER; --Definition of type
A, B : MY_ARRAY_TYPE; -- Declaration of variables
A "slice" such as A(15 .. 35) represents the sub-array consisting of all elements of A indexed by the corresponding range. Assignments such as A := B, A(15 .. 17) := B(55 .. 57), and A(20 .. 30) := A(21 .. 31) are all valid. In general A(I .. J) := B(K .. L) is valid if I, J, K, and L are integer variables lying within the index range of MY_ARRAY_TYPE and satisfy J - I = L - K.
a) What checks are done at compile time?
b) What checks are done at run time?
c) What code is generated (use pseudocode or English) for
Name1[exp1...exp2] := Name2[exp3...exp4]
3. Suppose that you have been given the task of writing a program which will implement a "four function" integer calculator. Specifically, your program should accept non-negative integers, the four binary arithmetic operators +,-,*,/, parentheses, and the equal sign (=), and it should compute and print the value of the resulting expression. It should observe the usual precedence and associativity of operators---all left associative, with + and - having lower precedence than * and /---and the equal sign (=) should be used to signal the end of expression. (There is no "unary minus" operator.)
For example, 37 - 52*(181 + 2) + 750 = should result in 8729 being printed.
You have further been told to construct this program as an implementation of a programming language, with a scanner and a parser which does the arithmetic as it parses (i.e., an interpreter).
a) What tokens will your scanner need to recognize? Explain your choices.
b) Give a correct BNF grammar for your language.
c) Using a pseudocode reasonably close to a standard language (such as C, C++, Java, Pascal, Ada) sketch the implementation of a scanner for this program. Explain your design and explain what the parser should expect to get from your scanner.
b)Will the output of the two "printf("%f", y);" statements be the same in both cases? Explain why or why not?
2. Some languages, (e.g., Ada, FORTRAN90) not only support array to array assignment, but support assignment of "slices" of arrays. Consider the Ada declarations below.
type MY_ARRAY_TYPE is array (10 .. 100) of INTEGER; --Definition of type
A, B : MY_ARRAY_TYPE; -- Declaration of variables
A "slice" such as A(15 .. 35) represents the sub-array consisting of all elements of A indexed by the corresponding range. Assignments such as A := B, A(15 .. 17) := B(55 .. 57), and A(20 .. 30) := A(21 .. 31) are all valid. In general A(I .. J) := B(K .. L) is valid if I, J, K, and L are integer variables lying within the index range of MY_ARRAY_TYPE and satisfy J - I = L - K.
a) What checks are done at compile time?
b) What checks are done at run time?
c) What code is generated (use pseudocode or English) for
Name1[exp1...exp2] := Name2[exp3...exp4]
3. Suppose that you have been given the task of writing a program which will implement a "four function" integer calculator. Specifically, your program should accept non-negative integers, the four binary arithmetic operators +,-,*,/, parentheses, and the equal sign (=), and it should compute and print the value of the resulting expression. It should observe the usual precedence and associativity of operators---all left associative, with + and - having lower precedence than * and /---and the equal sign (=) should be used to signal the end of expression. (There is no "unary minus" operator.)
For example, 37 - 52*(181 + 2) + 750 = should result in 8729 being printed.
You have further been told to construct this program as an implementation of a programming language, with a scanner and a parser which does the arithmetic as it parses (i.e., an interpreter).
a) What tokens will your scanner need to recognize? Explain your choices.
b) Give a correct BNF grammar for your language.
c) Using a pseudocode reasonably close to a standard language (such as C, C++, Java, Pascal, Ada) sketch the implementation of a scanner for this program. Explain your design and explain what the parser should expect to get from your scanner.
4. The following grammar is expressed in EBNF:
<goal> ::= [<a>] {xy} <b>
<a> ::= z {z}
<b> ::= x [y] z
Note: recall that { obj } means to repeat obj zero or more times, and [ obj ] means that obj is optional (occurs zero or one time).
a) Translate the grammar into BNF. You may need to add nonterminals. You may use the alternative symbol "|" but not the empty string in your BNF.
b) Translate the grammar into a single syntax diagram.