![]() |
![]() |
|
Due
Oct.29 (Fri) 23:59 on D2L; bring hardcopy to next class.
The derivations are best written in a text-editor where you can copy/paste one line and replace a single non-terminal to create the next line. But for parse-trees, I recommend drawing them freehand.
Your name and the assignment-number must be in a comment at the start of the file, along with a link to this assignment page.
Reading: Scott §3.6.4; (Lambda expressions); §11;, but skip 11.4 and 11.5 (OCaml and Evaluation Order, respectively).
For this (and future) assignments, be sure you have bumped up your DrRacket language-level to Intermediate Student with lambda.
Hint: Using the name “keep?” for one of your parameters is a good, descriptive name.
removedall off-screen trucks). You may presume that truck-on-screen?1 is written and working.
Notice that several of the image-creating functions imported via (require 2htdp/image) are similar, in that they happen to take four arguments: two non-negative numbers v and w, a drawing mode (e.g. 'solid or #o162 (a transparency)) and a color. Two such examples are ellipse and rhombus.
Let’s write a function shapes-in-a-row which takes in a list containing such functions, and returns an image that is the result of calling each function with the arguments 54, 40, #o162, and 'lightGoldenrod, and putting all the images beside each other:
(check-expect (shapes-in-a-row |
list-of-(… -> …). This function and the next do not need to be tail-recursive — the goal of this exercise is to be comfortable with passing functions.
Using lambda, write a single expression (no defines) which calls your function and passes it a list containing the following two functions:
caution: Don't use the grammar of Example 2.8 by mistake.
<JsFunc> → function <JsIdent>( <JsIdents> ) { <JsBody> } this is one long rule which contains two newlines |
For the grammar rules below, write them as in class: in basic BNF using “→” (and “ε”), but not using Extended BNF constructs like ? and * (nor equivalently, written as opt and …).
As we consider how to define <JsIdents>, a comma-separated list of identifiers, we note that the book has something similar: in the example of <id_list> (Examples 2.20 and 2.21 in §2.3), they present rules for a non-empty list of comma-separated identifiers (and, ending in a semicolon). Removing the semicolon will be trivial, but we do need to figure out how to adapt it to allow the possibility of zero identifiers.
Working towards a solution, the programmer MC Carthy comes up with the following attempt for a nonterminal that generates repetitions of the letter “x”, separated by commas and ending in a semicolon (which we won't end with in our part (c), next):
<XS> → ; | x; | x, <XS> |
Prove that this grammar is not correct, by giving a derivation of some string which is not a valid comma-separated list-of-“x”s ending in a semicolon. As usual, for full credit give the shortest correct answer.5
Generating a grammar
Give a grammar for Java's boolean expressions. (That is: a grammar describing exactly what you are allowed to put inside the parentheses of a Java if (…) … statement.)
For this problem, assume that you already have grammar rules for a Java numeric expression (“NumExpr”, probably similar but, for Java, more-complete-than the book's Example 2.4, p.46), for a Java identifier (“Id”), and that the only boolean operators you want to handle are &&,||,!,== and the numeric predicates >, >=, and ==. (You do not need to worry about enforcing precedence, although you are welcome to.) For this problem, you may use the Extended BNF constructs ? and * (or equivalently, written as opt and …).
hints: Make sure you can derive expressions like!!!!!!falseand((((targetFound)))), in addition to the expression below. Make sure you cannot derive expressions likex false(two expressions not one),2 > true(> can only occur between numeric-expressions, not boolean-expressions), or(the empty string). You might want to ask yourself:For comparison, see that book Example 2.4, and what is allowed on either side of binary operators like “+”, and also what it allowed to follow the unary-negation “-”.
- What is allowed to follow a “!”?
- What is allowed on each side of “||”?
Using your grammar, give a parse tree (not a derivation) for:
a+2 > 5 || ((!false == x))
In the tree, for children of
NumExpr
and
Id,
you can just write “⋮” to skip from the non-terminal directly to its children,
since you didn't write the exact rules for those nonterminals.
I recommend drawing your tree freehand6.
; truck-on-screen? : truck -> boolean ; Is any part of `m` on-screen? ; (define (truck-on-screen? m) (overlap? (bullet-x m) (bullet-y m) (image-width TRUCK-IMAGE) (image-height TRUCK-IMAGE) (/ WORLD-WIDTH 2) (/ WORLD-HEIGHT 2) WORLD-WIDTH WORLD-HEIGHT)) |
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |