






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!























COP-5555 Programming Language Principles Practice Final Questions
Add procedures to the denotational description of Tiny. Don’t panic. We will consider procedures with no parameters, and with no local variables, but the procedures are recur- sive. Two new constructs need to be handled: a procedure declaration, and a procedure call.
Complete the following:
CC[
CC[
Hints:
The solutions are one-liners.
Treat the procedure declaration as an asignment to the procedure name I.
Draw a display of the run-time environment, at the point marked "HERE" in the code shown below, for each of the following two techniques:
procedure A; begin {A} ... end;
procedure B(procedure E); var x:integer;
procedure C; begin {C} x := 5; <----------- HERE! end;
begin {B} E; B(C); end;
begin {main} B(A) end;
On the next two pages are two copies of the AST for a small Tiny program. Place next to each tree node the necessary attributes. For most nodes, this means four inherited attributes (on the node’s left), and five synthesized attributes (on the right). Fill in the actual values of ALL the attributes, according to the attribute grammar in your class notes, i.e. show the result of propagating the values through the functional graph. DO NOT depict the functional graph itself. Please adher to the convention adopted in class: the attributes are, from left to right: code, error, next, top, type.
Note: The first copy of the tree is for you to scratch up; the second is there in case you wish to produce a final clean copy.
program
<id:teeny> ; <id:teency>
assign
if
<id:x> read
=
assign
<id:x> <int:1>
<id:x> <int:0>
<id:x>
Add the Algol ’for’ loop to the the denotational description of Tiny. The Algol for
loop has the following syntax:
Statement -> ’for’ ’
For example:
for i:= 0, 8, 9, 8..19, 7..3 do output(i)
Note: ranges are intended to count ’upwards’ only. Complete the following:
CC[<’for’ <id:x> IE 1... IEn C>] =
Write an attribute grammar for Roman numerals. In case you don’t remember how roman numerals work, the following rules should refresh your memory:
Below is an AST grammar for roman numerals.
R → < ’roman’ R E > → ’end’ E → < ’+’ E E > → < ’-’ E E > → ’M’ # 1000 → ’D’ # 500 → ’C’ # 100 → ’L’ # 50 → ’X’ # 10 → ’V’ # 5 → ’I’ # 1
Below is a sample AST, produced from the input string ’LIV , CMI’. Note that there are two roman numerals: 50+(5-1)=54, and (1000-100)+1=901. Notice the reversed order of the subtraction. roman ____/ ____ /
roman + / \ /
end + - I / \ /
L - C M /
I V
The ultimate goal is produce one attribute, a ’file’ attribute, which will contain the values of all the roman numerals in the tree. Determine the following: ATT (attributes), SATT (synthesized attributes), IATT (inherited attributes).
Determine the attributes that are attached to each tree node, by specifying the func- tions S and I, as in the class notes. Then write the complete attribute grammar.
Suppose that you are given an AST and its standardized version ST.
a) Is it possible to uniquely destandardize the ST, and reproduce the AST? If so then write a destandardizer in pseudocode format for destandardizing a lambda expres- sion to an RPAL AST. If it can’t be done, explain why not.
b) Is it possible to uniquely deparse the AST, and produce the RPAL source? If not, explain why not. If it can be done, discuss a strategy for producing the RPAL source. Illustrate it with pseudo-code if appropriate.
Describe the addition of the ‘‘loop--while’’ construct to your Tiny compiler. Describe the changes you would make to your parser by giving the pseudo-code of the relevant portion of your recursive descent parser. Show pseudo-code describing how your constrainer would analyze this construct, and show pseudo-code describing how your code generator would generate code for it.
The ‘‘loop--while’’ construct has the following form:
loop S while B : T repeat
Both S and T are statement; B is a boolean expression. S is executed at least once. After each execution of S, expression B is tested: if true, T and then S are executed, and B is examined again; if false, the iteration stops.
Choose, among the two statements below, the one you believe is closest to the truth, and complete the statement you’ve chosen.
Attribute grammars are closer to the denotational approach than the opera- tional approach, because...
Attribute grammars are closer to the operational approach than the denota- tional approach, because...
Suppose we were to change the description of the binop node in the denotational description of the CSE machine to each of the two descriptions shown below. In each case determine whether the new description gives different results from the description given in the course notes, and if so, give a sample RPAL program (in AST form) whose output will be altered by the change. Show your RPAL program’s output before and after the change.
Augment the denotational semantic description of Tiny with conditional expres- sions. Conditional expressions can be used anywhere ordinary expressions are used, and they are used to choose between two expressions, rather than between two statements. The syntax is that of the conditional expression in the C language. For example:
(y <= 12)? 3 : 4
In this case, the value of the conditional expression will be either 3 or 4, depending on whether or not y is less or equal to 12. Whichever value is the case, either 3 or 4, is the resulting value of the expression. Conditional expressions can be nested arbitrarily. The ":" clause is required.
Draw a picture of the run-time environment, at the point marked "HERE" in the code shown below, for each of the following two techniques:
program main; procedure A; begin {A} ... end; procedure B(procedure E); var x:integer; procedure C; begin {C} x := 5; <----------- HERE! end; begin {B} E; B(C); end; begin {main} B(A) end;
Add a swap statement to the denotational description of Tiny. The swap statement is a double assignment among two variables (not expressions). For example, ’a :=: b’ swaps the values of variables a and b. Specifically, complete the following:
CC[<:=: I1 I2>] =
‘‘Implement’’ the swap statement, i.e. describe the changes necessary to the RPAL ver- sion of the denotational description of Tiny, in order to add the swap statement to the lan- guage.
Consider the following Tiny program:
program sum: assign s:=0; assign i:=1; while not(i=10) do assign s := s + read; assign i := i + 1; od; output s end sum.
Using the grammar on page 90 of your notes, build the AST for this program. Place next to each tree node the necessary attributes. For most nodes, this means four inherited attributes (on the node’s left), and five synthesized attributes (on the right). Fill in the actual values of ALL the attributes, according to the attribute grammar in your class notes, i.e. show the result of propagating the values through the functional graph. DO NOT depict the functional graph itself. Please adher to the convention adopted in class: the attributes are, from left to right: code, error, next, top, type.