Advertisement
Night_Wood

CSE 307 HW5 Markdown

Apr 23rd, 2018
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. # stonyBrookScript
  2.  
  3. For this assignment, we add support for variables, assignments, conditionals, loops, and output to our language.
  4. For now, our language has only a single scope for variables: *a global static scope*.
  5.  
  6. ## Variables and Assignments
  7.  
  8. Variable names must begin with an ASCII letter, which may be followed by zero or more ASCII letters, digits and underscore.
  9. A regular expression that matches variable names is `'[A-Za-z][A-Za-z0-9_]*'`.
  10.  
  11. Our expressions from the previous assignment need to be extended in two ways:
  12.  
  13. 1. We need to add support for assignments to variables
  14. * For example, `x = 1`
  15. * Including assignment to indexed list variables, for example: `x[2] = 5` where `x` was a variable that contained a list
  16. * For example, `x` was previously assigned a list: `x=[1,2,3];` after the assignment `x[2] = 5`, `x` contains `[1,2,5]`
  17.  
  18. 2. We need to add support for variables used in expressions
  19. * For example, if `x` was assigned `1`, then `print(x)` will print `1`
  20. * Similarly, we should evaluate indexed variables if they occur in a place where their value is needed
  21. * For example, if `x` was assigned `[1,2,5]`, then `print(l[0]+l[1]+l[1+1])` should print `8`
  22.  
  23. Evaluating a variable for its value should have the following behavior:
  24. * **If** the variable has had a value assigned to it, the value should be returned.
  25. * **Else**, a `Semantic Error` should be generated and the program should stop.
  26.  
  27. When an indexed list variable is used in an expression, then both the list and the index are evaluated to their value
  28. and the indexed list expression is evaluated.
  29. If the variable is not a list (or a string), or the index is not a integer number, then a `Semantic Error` should be produced.
  30. If the index is outside the bounds of the array, a `Semantic Error` should be produced.
  31.  
  32. ## Statements
  33.  
  34. The new types of statements are:
  35.  
  36. * **Block:** a block statement consists of zero-or-more statements enclosed in curly braces {...}. When the block executes, each of the statements in the block is executed in sequential order.
  37.  
  38. * **Assignment:** an assignment statement consists of an expression, an equals sign, an expression, and a semicolon. When the assignment statement executes, the left expression is assigned the value evaluated of the right-expression.
  39.  
  40. * **Print:** a print statement consists of the "print" keyword, a left-parenthesis, an expression, a right-parenthesis, and a semicolon. When the print statement executes, the expression is evaluated for its value.
  41.  
  42. * **Conditional statements:**
  43.  
  44. * **If statements:** consists of the keyword `if`, a left-parenthesis, an expression, a right-parenthesis, and a body block statement. When the `if` statement executes, if the expression is `True`, the body block statement is executed.
  45.  
  46. * **If-else statements:** consists of the keyword `if`, a left-parenthesis, an expression, a right-parenthesis, a body block statement, an `else` keyword and a body block statement . When the `if-else` statement executes, if the expression is `True`, the first body block statement is executed, else the second body is executed. The `else` goes with the closest previous `if` in the same block.
  47.  
  48. - **While:** a `while` statement consists of the keyword `while`, a left-parenthesis, an expression, a right-parenthesis, and a body block statement. Executing the `while` statement begins by evaluating the condition for its value. If that value is `False`, the `while` statement terminates. Otherwise, the `while` statement executes its body block statement, and execution repeats.
  49.  
  50. ## Running
  51.  
  52. An input program consists of a single outermost block statement.
  53. Executing the program consists of executing this block.
  54.  
  55. Your interpreter will be called with a single argument.
  56. This argument will be a file containing an input program.
  57. * If the program contains syntax error, your interpreter should print out: `Syntax Error` and stop.
  58. * Otherwise, you should execute the program.
  59. * If the execution of the program causes a semantic error, then your interpreter should print out `Semantic Error` and stop.
  60.  
  61. Execution of the program may cause print statements to execute and the output from print statements should be sent to standard output. **No other output should be produced!**
  62.  
  63. ## Sample Input and Output
  64.  
  65. ### Example 1
  66.  
  67. ```c
  68. {
  69.  
  70. number = 33;
  71. isPrime = 1;
  72. i = 2;
  73.  
  74. while(isPrime==1 and i<number){
  75.  
  76. if (number%i==0) {
  77. isPrime = 0;
  78. }
  79. i = i + 1;
  80. }
  81.  
  82. if(isPrime==1){
  83. print("isPrime is true");
  84. } else {
  85. print("isPrime is false");
  86. }
  87. }
  88. ```
  89.  
  90. The output from this file should be only:
  91.  
  92. ```c
  93. isPrime is false
  94. ```
  95.  
  96. ### Example 2
  97.  
  98. ```c
  99. {
  100. data = [ [ 100, 42 ], [ 100, 50 ], [ 123, 456 ], [ 300, 9000 ] ];
  101. result = [ 0, 0, 0, 0 ];
  102. i = 0;
  103.  
  104. while (i < 4) {
  105. a = data[i][0];
  106. b = data[i][1];
  107.  
  108. if (a > 0){
  109. while (b > 0){
  110. if (a > b){
  111. a = a - b;
  112. } else {
  113. b = b - a;
  114. }
  115. }
  116. }
  117. result[i] = a;
  118. i = i + 1;
  119. }
  120. print(result);
  121. }
  122. ```
  123.  
  124. The program will print:
  125.  
  126. ```c
  127. [2, 50, 3, 300]
  128. ```
  129.  
  130. ### Example 3
  131.  
  132. Printing strings should execute as in python (no quotes around single strings printed and single quotes around strings in lists):
  133.  
  134. ```c
  135. {print("a");}
  136. ```
  137.  
  138. The output should be:
  139.  
  140. ```c
  141. a
  142. ```
  143.  
  144. Another input:
  145.  
  146. ```c
  147. {print([1, "a", 2]);}
  148. ```
  149.  
  150. the output should be:
  151.  
  152. ```c
  153. [1, 'a', 2]
  154. ```
  155.  
  156. ## Tips
  157.  
  158. To complete this assignment, I suggest you:
  159. * Create a global map/dictionary to store the values of variables.
  160. * Implement the `evaluate()` method of the `Variable` class so that it returns the values of variables.
  161. * Implement the execute method of each of the statements.
  162.  
  163. ## Submission
  164.  
  165. You should include your name and ID as the first line (commented out) in your python file.
  166. The program must run under Python 3.x.
  167. The program must be named `main.py`.
  168. Your program will be run with a command like:
  169.  
  170. ```sh
  171. python3 main.py inputProgram.txt
  172. ```
  173.  
  174. ## Grading
  175.  
  176. There are a total of 50 points.
  177. Your program will be run against 5 use cases using similar programs to the sample input.
  178. If it generates a correct result you will get 10 points for each use case.
  179. If the solution is incorrect, you will lose the points for that use case.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement