Given the following BL statements, draw the corresponding abstract syntax trees as…
- User
Given the following BL statements, draw the corresponding abstract syntax trees as defined by the mathematical model of StatementKernel. See Slides 5-12 in Abstract Syntax Trees for some examples. IF next-is-empty THEN move ELSE IF next-is-wall THEN turnright turnright move END IF END IF WHILE true DO turnright IF next-is-enemy THEN TurnAround ELSE skip END IF turnleft END WHILE WHILE next-is-enemy DO infect TurnAround move turnright END WHILE IF next-is-friend THEN turnright turnright WHILE true DO infect END WHILE END IF IF next-is-not-empty THEN turnleft turnleft ELSE WHILE next-is-empty DO move END WHILE IF next-is-enemy THEN infect END IF skip END IF Using recursion, complete the body of the following static method. Note the use of a Java switch statement. See the Statement slides (44-49) for the syntax, purpose, and behavior of this construct. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 /**
- Reports the number of calls to primitive instructions (move, turnleft,
- turnright, infect, skip) in a given {@code Statement}.
- @param s
-
code
the {@code Statement} - @return the number of calls to primitive instructions in {@code s}
- @ensures
- countOfPrimitiveCalls =
- [number of calls to primitive instructions in s]
/ public static int countOfPrimitiveCalls(Statement s) { int count = 0; switch (s.kind()) { case BLOCK: { / * Add up the number of calls to primitive instructions * in each nested statement in the BLOCK. */
code// TODO - fill in case break; } case IF: { /* * Find the number of calls to primitive instructions in * the body of the IF. */ // TODO - fill in case break; } case IF_ELSE: { /* * Add up the number of calls to primitive instructions in * the "then" and "else" bodies of the IF_ELSE. */ // TODO - fill in case break; } case WHILE: { /* * Find the number of calls to primitive instructions in * the body of the WHILE. */ // TODO - fill in case break; } case CALL: { /* * This is a leaf: the count can only be 1 or 0. Determine * whether this is a call to a primitive instruction or not. */ // TODO - fill in case break; } default: { // this will never happen...can you explain why? break; } } return count; - Assistant