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

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:

Submission 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:

Exercises

  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. How do JavaScript and Rust treat the following:
    let x = 3;
    let x = 3;
    
  3. Describe how the languages Java and Ruby differ in their interpretations of the meaning of the keyword private. You can use an AI chatbot for help, but please trim down the long-winded applications those tools are known for, and give a concise explanation that proves you truly understand the difference.
  4. 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.
  5. 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.
  6. 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 exercise 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.
  7. 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.
  8. 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
  ├── .prettierrc.json
  ├── docs
  │   └── ...                 -- now with the companion website
  ├── examples
  │   └── ...                 -- add more example programs
  ├── src
  │   ├── (yourlanguagename).js
  │   ├── (yourlanguagename).ohm
  │   ├── compiler.js
  │   ├── core.js             -- new!
  │   ├── parser.js           -- as before
  │   ├── analyzer.js         -- with a completed static analyzer!
  │   ├── optimizer.js        -- still just the stub function
  │   └── generator.js        -- still just the stub function
  └── test
      ├── compiler.test.js    -- just test that you can parse and analyze
      ├── parser.test.js      -- as before, but improve from HW2 feedback
      └── analyzer.test.js    -- new!

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.