Anda di halaman 1dari 3

Principles of Programming Languages Assignment 1. Deadline 3.05.

2007, 4 pm
This assignment contributes 25% to your course mark. Please make sure that you: read carefully before you start and ask for clarication as soon as possible if you do not understand the questions; answer all three questions; this assignment consists of three pages; note that this is individually assessed coursework and you are not allowed to work in groups; the standard rules for late submissions, plagiarism and collusion apply. Please provide references whenever you use other peoples work, even if it is just to back up your own points of view; note that form and presentation of your work contribute to the mark; submit your assignment to the Informatics Departmental Oce in due time using the appropriate and completed cover sheet.

1. Language Evaluation [42 marks] (a) List without further comment the criteria discussed in the lectures that can be used to evaluate and compare programming languages. (b) For each language feature explained below say which language evaluation criterion this feature supports the most. In each case, briey explain your answer. i. The use of byte code and a virtual machine as in Java. ii. Ability to dene and use functions that can be invoked with an arbitrary number of arguments as e.g. used in C#. iii. Design-by-contract as in the language Eiel where methods can be dened with boolean expressions as pre- and postconditions. When a method is invoked at runtime, the precondition is checked before and the postcondition after the method body is actually executed. If any of the conditions is violated, a specic exception is thrown. (c) perl is a widely used scripting language. The following quip is widely known: Any program compiles in perl. Evaluate perl based on this statement using our language evaluation criteria. Address and motivate potential trade-os the language designers possibly had to deal with. Note that it is not required to know anything about perl other than the information given here and you should not list concrete perl features. 1

2. Abstract Syntax [43 marks] Consider the following abstract grammar for arithmetic (exp), string (s exp) expressions as well as statements / commands (com). n intval x var e exp s s exp ::= ::= m stringval h charval e+e | ee | e | n | x m|x| s ++ s | toString e | hs x=e|x=s|x=h| c; c | for x = e to e do c endfor integer expressions string values and variables string concatenation integer to string conversion append character to string assignment sequential composition for-loop

c com

::=

The abstract carriers in use are: intval representing integer values, stringval representing string values, charval representing character values, and var representing variables (that are untyped). Note that we write character values inside single quotes, e.g. a and string values in double quotes e.g, this is a string containing blanks. The meaning of the abstract syntax is described in comments. Appending a character to a string has the obvious meaning, for instance f irst = rst. A for-loop in abstract syntax for x = e1 to e2 do c endfor is supposed to have the same eect as the Java loop for (int x = e1; x <= e2; x = x+1) c. Let ast com be a phrase in abstract syntax dened as follows: y = ; for i = 1 to 2 + 3 do y = y ; y = toString i ++ y endfor (a) What is the value of variable y after the execution of the statement ast provided that the meaning of the abstract syntax is as described above? (b) Draw an abstract syntax tree of the abstract phrase ast given above. (c) Note that the abstract grammar above has three dierent types of assignment constructs. Simplify the grammar so that only

one assignment construct is needed. Ignore typing issues as the abstract carriers do not represent well-typed phrases anyway. (d) Design some abstract syntax, extending the original abstract grammar (without changes performed in (c)) above, to include the following features: a predened function ascii that takes a character and returns its ascii value which is an integer; a command that checks whether a given character value is in a given string expression, and if so executes a given command (otherwise it does nothing). 3. Variables and Binding [15 marks] (a) What is the type of i in ast? What scope would you suggest i should have in ast if the abstract syntax is supposed to be interpreted as described earlier. (b) What is the latest possible address-to-variable binding time for the variables of the language described in Question 2? (c) Explain briey for each of the four categories of variable lifetime how appropriate its usage would be for the variables in the language described in Question 2. Note that appropriateness may depend on whether types are bound statically or dynamically.

Anda mungkin juga menyukai