Lab 5
The idea behind this lab is to develop functions that will be useful in developing your intuition for the syntax of the DFLY language.
More specifically, you must develop a simple "quiz" engine that displays legal DFLY expressions, and then challenges the user to produce the parsed representation.
This lab does not require you to do "testing" in the standard sense on any function except for the unparse function; the other functions you’re writing will be probabilistic, and will be difficult to test in a straightforward way.
Please note that the word "term" here means the same thing as "expression".
Copy your ExprC definition from Assignment 3. If you haven’t finished it yet, go ahead and do that first. Just the ExprC part.
The random and random-seed functions can be used to pick random numbers. Read their docs.
Develop the function randomSymbol that produces a random symbol from a fixed set of size 8. That is: define a fixed set of symbols, and write a function that returns one of them randomly.
Develop the function randomBaseTerm that produces a random expression that does not contain any other expressions. Choose randomly from the set of non-self-referential expressions. Use the randomSymbol function to generate identifiers.
Develop the function randomTerm that accepts a maximum depth and produces an expression tree whose depth does not exceed the given one. Use the randomBaseTerm function in order to "bottom out". For function definitions and applications, use a random arity in the range 0-3.
Please don’t worry about the fact that the terms you’re generating won’t evaluate correctly. For instance, they may add numbers to functions, or try to make a function call to a boolean, or have terrible arity issues. That’s no problem. The only thing that matters is that the range of your randomTerm function includes all well-formed expression trees with variables chosen from your specified set, and with no more than three parameters to functions, and with no more than three arguments to function calls.
Develop the unparse function that takes a parsed expression and produces concrete syntax that corresponds to this. This function is testable. Test it!
Develop the quiz function that generates a random term, prints its concrete syntax, and returns the term.
- Use this code to quiz yourself:
(define secret (quiz)) (define (reveal) secret) Each time you run the program, you get one challenge. When you call reveal, you see the answer.
Now: quiz yourself!