Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. 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
1. 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. a. IF next-is-empty THEN move ELSE IF next-is-wall THEN turnright turnright move END IF END IF b. WHILE true DO turnright IF next-is-enemy THEN TurnAround ELSE skip END IF turnleft END WHI C. WHILE next-is-enemy DO infect TurnAround move turnright D WHIL d. IF next-is-friend THEN turnright turnright WHILE true DO infect END WHILE END IF e. 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 IF2. 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 * Reports the number of calls to primitive instructions (move, turnleft, 3 * turnright, infect, skip) in a given (@code Statement}. 4 * 5 * @param s 6 * the (@code Statement) 7 * @return the number of calls to primitive instructions in {@code s) 8 * @ensures
9 * countOfPrimitiveCalls = 10 * [number of calls to primitive instructions in s] 11 *12 */ 13 public static int countOfPrimitiveCalls(Statement s) { 14 int count = O; 15 switch (s.kind()) { 16 case BLOCK: { l7 /* l8 * Add up the number of calls to primitive instructions 19 * in each nested statement in the BLOCK. 20 */ 21 22 // TODO fill in case 23 24 break; 25 l 26 case IF: { 27 /* 28 * Find the number of calls to primitive instructions in 29 * the body of the IF. 30 */ 31 32 // TODO fill in case 33 34 break; 35 ) 36 case IF_ELSE: ( 37 /* 38 * Add up the number of calls to primitive instructions in 39 * the "then" and "else" bodies of the IF_ELSE. 40 */ 41 42 // TODO fill in case 43 44 break
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started