CMSI 386/585
Quiz 1

The test is open-everything with the sole limitation that you neither solicit nor give help while the exam is in progress.

This is a take-home quiz. From the time you first look at it, you have 12 hours to complete it. Print out the exam and submit all answers on the printed sheets only. Sign the affadavit at the bottom of this page, or else you will get a zero.

ProblemYou gotOut of
10
20
10
10
20
10
10
10
TOTAL 100

I have not worked on this exam past the twelve-hour period allowed.

 

 

Date

  1. Here is a description of a language. Programs in this language are made up of a non-empty sequence of function declarations, followed by a single expression. Each function declaration starts with the keyword fun followed by the function's name (an identifier), then a parenthesized list of zero or more parameters (also identifiers) separated by commas, then the body, which is a sequence of one or more expressions terminated by semicolons with the sequence enclosed in curly braces. Expressions can be numeric literals, string literals, identifiers, function calls, or can be made up of other expressions with the usual binary arithmetic operators (plus, minus, times, divide) and a unary prefix negation and a unary postfix factorial ("!"). There's a conditional expression with the weird syntax "x if y else z". Factorial has the highest precedence, followed by negation, the multiplicative operators, the additive operators, and finally the conditional. Parentheses are used, as in most other languages, to group subexpressions. Numeric literals are non-empty sequences of decimal digits with an optional fractional part and an optional exponent part. String literals are doublequote-enclosed sequences of non-control characters with the backslash used to escape the double quote character and the backslash itself. Identifiers are those non-empty sequences of letters, decimal digits, underscores, at-signs, and dollar signs, beginning with a letter or dollar sign, that are not also reserved words. Function calls are as in C, with the arguments in a comma-separated list of expressions bracketed by parentheses. There are no comments in this language, and whitespace can be used liberally between tokens.

    Write an example program in this language that shows off everything described above.

  2. For the language in the previous problem, write both the microsyntax and the macrosyntax, using the notation I used in my notes on syntax.
  3. Classify each of the following Java fragments as (a) a lexical error, (b) a syntax error, (c) a static semantic error, (d) a dynamic semantic error, or (e) no error at all.
    1. class C {int x; void f(int x) {}}
    2. class C {void f(int x) {int x = 1;}}
    3. class C {String s = "Hawai`i";}
    4. class C {int x[] = new int[3]; System.out.println(x[10];)}
    5. class C {void f() {throw new IllegalStateException();}}
    or else, answer the above question for the following fragments of C90:
    1. {int x; int x;}
    2. {int x; {int x;}}
    3. {int x; struct x {};}
    4. void f() {return 5;}
    5. x +. 2;
    If you answer both the Java and C halves of this question, your score for this problem will be the worst of the two halves.
  4. Give the abstract syntax tree for the following fragment of, well, it doesn't matter whether you say this is C or Java.
      while (true) x . last[y*3>>2] >>= y^w * f(q |- w);
    
  5. Write regular expressions for
    1. A number from the set {82,86,87,59,57,871}.





    2. Strings that start with an a, end with one or more b's, which contain, in between, characters that are neither letters nor spaces.





    3. The set of all strings over {a,b,c} that do not contain two consecutive a's anywhere in the string.





    4. The set of all strings of any length that either contain exactly 10 digits (in '0'..'9') or contain exactly 11 digits (where the first one is a '1'). The following strings are examples: "13jef784y/9903:*88}", "2222222222", "000.009.2111", and 1(808)112-4444xx".
  6. Write a fragment of Ruby code that, after it is executed, we now have a new method called stutter that works on arrays as follows:
        [1,4,3].stutter     returns    [1,1,4,4,3,3]
    
  7. Write a Ruby class called Account, representing a kind of bank account with an account number that never changes once constructed, and an account balance which can be read and written. All writes cause, as a side effect, a message to be written to standard output. Also all writes that would make the balance negative are prohibited. Example:
        a = Account.new(14)
        a.id                 returns      14
        a.balance            returns       0
        a.balance = 100      prints "depsited 100 to account 14", returns 100
        a.balance = 20       prints "withdrew 80 from account 14", returns 20
        a.balance = -20      prints "overdrafts not allowed", returns -20
        a.balance            returns       20
    
  8. Consider this script:
        var a = [];
        for (var i = 0; i < 10; i++) {
            a[i] = function() {return i;}
        }
        var b = [];
        for (var j = 0; j < 10; j++) {
            b[j] = a[j]();
        }
    

    At the end of this script, what is the value of b? Explain in detail why this is. A skecth will be very helpful and improve your chances of getting full credit.