Homework 5 Solutions - Programming Languages | CS 4700, Assignments of Programming Languages

Material Type: Assignment; Class: Programming Languages; Subject: Computer Science; University: Utah State University; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 07/30/2009

koofers-user-ek2
koofers-user-ek2 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS4700 Homework #5
Note, these exercises may be done in groups of one, two, or three. Working with
someone else is strongly recommended. If more than one person is involved, list all the
names on ONE set of answers. Groups may change throughout the term.
Working in groups is a BIG plus for you. Take advantage of it. If you work in groups, you
must work in the group for the ENTIRE assignment. It is considered cheating if you work
with someone else for some of the answers, but turn in an individual copy of the answers.
It is an all or nothing situation. You can't work together on some questions and alone on
some. Sometimes I see an individual whose name is listed in two groups. This is strictly
forbidden and is considered cheating. You cannot work in two groups.
Assignments are due at the beginning of class and should be typed.
Chapter 5
1. Here are some type and variable declarations in Pascal-like syntax. State which
variables are equivalent under (a) name equivalence (b) declaration equivalence
(c) structural equivalence. Note that anything that is name equivalent is also
declaration equivalent, and anything that is declaration equivalent is also
structurally equivalent. Note also, that two variables declared in the same
declaration (consisting of a variable list and a single type specifier) are name
equivalent (even if the type is elaborated explicitly and not given a user defined
name).
We say two types are declaration equivalent if they lead back to same declaration
through renaming. (If the exact same unnamed type is repeated on a separate line,
the two variables are NOT declaration equivalent.)
type
range = -5 ..5;
levels = 0..10;
table1 = array[range] of char;
table2 = table1;
table3 = array[levels] of char;
var
x,y : array[-5..5] of char;
z: table1;
w: table2;
q,r,s: table3;
i: range;
j:-5..5;
m: table1;
3. Some programming languages are typeless. What are the obvious advantages and
disadvantages of not having statically declared types in a language?
4. Consider this Pascal-like program in which each method has the following syntax:
header, declarations (including declarations of local procedures), body:
program Example;
var
pf3
pf4

Partial preview of the text

Download Homework 5 Solutions - Programming Languages | CS 4700 and more Assignments Programming Languages in PDF only on Docsity!

CS4700 Homework

Note, these exercises may be done in groups of one, two, or three. Working with someone else is strongly recommended. If more than one person is involved, list all the names on ONE set of answers. Groups may change throughout the term. Working in groups is a BIG plus for you. Take advantage of it. If you work in groups, you must work in the group for the ENTIRE assignment. It is considered cheating if you work with someone else for some of the answers, but turn in an individual copy of the answers. It is an all or nothing situation. You can't work together on some questions and alone on some. Sometimes I see an individual whose name is listed in two groups. This is strictly forbidden and is considered cheating. You cannot work in two groups. Assignments are due at the beginning of class and should be typed.

Chapter 5

  1. Here are some type and variable declarations in Pascal-like syntax. State which variables are equivalent under (a) name equivalence (b) declaration equivalence (c) structural equivalence. Note that anything that is name equivalent is also declaration equivalent, and anything that is declaration equivalent is also structurally equivalent. Note also, that two variables declared in the same declaration (consisting of a variable list and a single type specifier) are name equivalent (even if the type is elaborated explicitly and not given a user defined name). We say two types are declaration equivalent if they lead back to same declaration through renaming. (If the exact same unnamed type is repeated on a separate line, the two variables are NOT declaration equivalent.) type range = -5 ..5; levels = 0..10; table1 = array[range] of char; table2 = table1; table3 = array[levels] of char; var x,y : array[-5..5] of char; z: table1; w: table2; q,r,s: table3; i: range; j:-5..5; m: table1;
  2. Some programming languages are typeless. What are the obvious advantages and disadvantages of not having statically declared types in a language?
  3. Consider this Pascal-like program in which each method has the following syntax: header, declarations (including declarations of local procedures), body: program Example; var

x: integer; y:float; procedure First; begin writeln(x, “ “, y); end; procedure Second; var x: integer; y: string; procedure Third; var x:integer; begin { Third } x := 99; First end; { Third } begin { Second } x := 88; y = “happy”; Third end { Second } begin { Example } x := 77; y = 10.7; First; Second end. { Example } a) What will be printed by this program if we assume static scope? b) What will be printed by this program if we assume dynamic scope?

  1. For the following C code in which all parameters are passed by value, what is produced under static scoping? Under dynamic scoping? int x =1; // Global char y = 'a'; // Global void p(void) { double x=2.5; cout << y; { int y[10];} } void q(void) { int y=42; cout << x; p(); } void main() { char x = 'b'; q(); }
  2. A discriminated union would look something like: Type SHAPE = union (form: SHAPETYPE){ circle: (diameter:real); triangle: (leftside: integer; rightside: integer; angle: real); rectangle:( side1: integer; side2:integer);
  1. Indicate one disadvantage of each of the following methods of storage reclamation. a) explicit disposal b) reference counting c) mark-sweep garbage collection