LMU ☀️ CMSI 3802
LANGUAGES AND AUTOMATA II
HOMEWORK #3 Due: 2024-03-15

Learning Objectives

In this assignment you will demonstrate:

Readings, Videos, and Self-Study

Although your team will be turning in only one submission, every team member is individually responsible for each of the following:

Skim:

Watch:

Instructions

As usual, turn in a single submission for your entire group via BrightSpace. Remember to not simply partition the work among team members, as this reduces your learning.

Your team's BrightSpace submission should be an attached PDF or text file with:

  1. The names of every student
  2. For each student, an affidavit that each of the readings and watchings above were individually completed
  3. Answers to each ot the “Problems to Turn In,” numbered and neatly typeset
  4. A link to the public GitHub repository hosting your team’s project

Problems to Turn In

Solutions to these problems will appear in the beautifully formatted PDF that you will submit to BrightSpace. For problems involving code to write, host your code on GitHub or Replit or OneCompiler (or similar) and provide the link to the hosting site in your BrightSpace submission.

  1. Classify the following as a syntax error, static semantic (contextual) error, or not a compile time error. In the case where code is given, assume all identifiers are declared, have the expected type, and are in scope. All items refer to the Java language.

    1. x+++-y
    2. x---+y
    3. incrementing a read-only variable
    4. code in class C accessing a private field from class D
    5. Using an uninitialized variable
    6. Dereferencing a null reference
    7. null instanceof C
    8. !!x
    9. x > y > z
    10. if (a instanceof Dog d) {...}
    11. var s = """This is weird""";
    12. switch = 200;
    13. x = switch (e) {case 1->5; default->8;};
  2. Here’s a code fragment in some generic language:
    var x = 3;          // line 1
    function f() {      // line 2
      print(x);         // line 3
      var x = x + 2;    // line 4
      print(x);         // line 5
    }                   // line 6
    f();                // line 7
    
    You are going to play the role of a language designer here. Assume static, nested scoping. For each of the following outputs, define precise rules that might lead to the given output. For example, if the output were 3 then 5, you would say “Variables come into scope after their declaration and before their declaration identifiers refer to whatever is already in scope.”
    1. undefined
      NaN
    2. Error on line 3: x is not declared
    3. 75354253672
      75354253674
    4. 3
      -23482937128
    5. Error on line 4: x used in its own declaration
  3. Some languages do not require the parameters to a function call to be evaluated in any particular order. Is it possible that different evaluation orders can lead to different arguments being passed? If so, give an example to illustrate this point, and if not, prove that no such event could occur.
  4. Describe in your own words how the Carlos language allows handles recursive structs. Describe what kinds of restrictions the language definition imposes and why. Describe how the compiler enforces the restrictions. Write well. Use technical vocabulary accurately. An AI assistant can help you get your grammar and spelling right, though it is unlikely to get the right answer.
  5. Some languages do not have loops. Write a function, using tail recursion (and no loops) to compute the minimum value of an array or list in Python, C, JavaScript, and in either Go, Erlang, or Rust (your choice). Obviously these languages probably already have a min-value-in-array function in a standard library, but the purpose of this problem is for you to demonstrate your understanding of tail recursion. Your solution must be in the classic functional programming style, that is, it must be stateless. Use parameters, not nonlocal variables, to accumulate values. Assume the array or list contains floating-point values.
  6. Your friend creates a little JavaScript function to implement a count down, like so:
    function countDownFrom10() {
      let i = 10;
      function update() {
        document.getElementById("t").innerHTML = i;
        if (i-- > 0) setTimeout(update, 1000);
      }
      update();
    }
    

    Your other friend says “Yikes, you are updating a non-local variable! Here is a better way:”

    function countDownFromTen() {
      function update(i) {
        document.getElementById("t").innerHTML = i;
        if (i-- > 0) setTimeout(update(i), 1000);
      }
      update(10);
    }
    
    What does your second friend’s function do when called? Why does it fail? Your friend is on the right path though. Fix their code and explain why your fix works.
  7. Find as many linter errors as you can in this Java source code file (C.java):
    import java.util.HashMap;
    
    class C {
        static final HashMap<String, Integer> m = new HashMap<String, Integer>();
    
        static int zero() {
            return 0;
        }
    
        public C() {
        }
    }
    

    You can use SonarLint or FindBugs or FindSecBugs or PMD or whatever you prefer. You might even need to use a combination of tools because it is possible no tool finds them all. (Please note you are not expected to already know what all the issues are here. The idea is to practice with tools and have good discussions with teammates. Find as many as you can, and read and understand each problem that is reported to you so you learn (1) what kinds of potential bugs and security problems can exist even in compilable and runnable code, and (2) the kinds of things that a static analyzer can detect.)

For Your Project

Continue your compiler project in the public GitHub repository you created in the first assignment. You will be expanding your repo to have the following:

  .
  ├── .gitignore
  ├── README.md
  ├── LICENSE
  ├── package.json            -- configuration because this is a Node.js app
  ├── .prettierrc.json        -- (optional, you don’t have to have one)
  ├── docs
  │   └── ...                 -- now with the companion website
  ├── examples
  │   └── ...                 -- lots of example programs
  ├── src
  │   ├── (yourlanguagename).js
  │   ├── (yourlanguagename).ohm
  │   ├── compiler.js         -- slightly expanded to include the static analyzer
  │   ├── core.js             -- new!
  │   ├── parser.js           -- as before
  │   ├── analyzer.js         -- with a completed static analyzer!
  │   ├── optimizer.js        -- still contains just the stub function
  │   └── generator.js        -- still contains just the stub function
  └── test
      ├── compiler.test.js    -- expanded form before
      ├── parser.test.js      -- as before, but improve from HW2 feedback
      └── analyzer.test.js    -- add in tests for context checking

Your tasks for this assignment are twofold. First, you are to implement the static analyzer for your compiler. Second, you are to start a companion website for your language.

Grading Rubric

To help you get a good score, here are all the things you will be graded on.