講解:command-language、Java、BigInteger、JavaPython|Python

Assignment 2. Sets.For this assignment an interpreter has to be written for a calculator with thecommand-language shown further down. All sentences in this command-languageare statements for operations with sets of arbitrary big natural numbers.The calculator should be able to store an arbitrary number of variables (A variablebeing a combination of a name and a value.).Proceed as follows:1. Make a specification for an Identifier ADT and a Set ADT. The typeIdentifier will be the type of the name and the type Set will be the typeof the value of each variable stored by the calculator.2. From the API use (1) the class BigInteger to implement the arbitrary bignatural numbers and (2) the class HashMap to store the arbitrarynumber of variables.3. Implement the class List according to the specification given in theListInterface. The given class Node has to be used in the implementationof this class List. We provide a class ListTest which you can optionally useto test your implementation. You can find this class on Blackboard.Instructions on how to use it are at the end of this document.4. Implement the class Set with the class List.5. Use instances of the class APException in case you want to throw anexception.6. Make, using the objects, a design for the interpreter. Use the method ofrecursive descent (see the example at the end).7. Get approval for the self-made interfaces, as well as the design of theinterpreter, at the meetings with the assistant.8. Once your design and interfaces have been approved, and you haveimplemented the interfaces, you can start with programming. First,completely write-out the parser part of your design. Which means, write aprogram that reads lines of input, and does nothing in case of a correctcommand, but returns a clear error-message in case there are incorrectcommands.9. Only when the parser works, must you proceed to extend it to an interpreter.The interpreter must recognize and execute commands written in thecommand-language specified below.You can find the interface ListInterface, the class Node and the class APExceptionon Blackboard.With the aid of the command-language specified further down, we can manipulatethe sets of natural numbers and the variables that are contained in the calculator.Only identifiers are allowed as names for variables. There are four operators that can be used on sets in this language:+ : A + B = { x | x ∈ A ∨ x ∈ B } union* : A * B = { x | x ∈ A ∧ x ∈ B } intersection : A B = { x | x ∈ A ∧ x B } complement| : A | B = { x | x ∈ A + B ∧ x A * B } symmetric difference= { x | x ∈ (A + B) (A * B) }The operator * has a higher priority than +, | and -, whom have the samepriority. The operators are left-associative.Example: A B * Set1 + D is to be evaluated as (A ? (B * Set1)) + D.There are two commands available. For each of those an example is given:1. Set1 = A + B Set1 * (D + Set2)Calculate the set that is the result of the expression to the right of the=-sign and associate the variable with the identifier Set1 with this Set.2. Set1 + Set2Calculate the set that is the result of the expression, and print theelements of this set: separated by spaces on a single line on thestandard output.Do not print any other text or commas because this will interferewith the automated test.Syntax command-languageWe use the EBNF-notation for describing the syntax-portion of the commandlanguagegrammar. Furthermore the signs ‘’ are used for descriptions (forexample ).program = { statement } ;A program is any arbitrary number of statements (commands) ended by theend of file.statement = assignment | print_statement | comment ;A statement is an assignment-statement, a print-statement or a comment-line.assignment = identifier = expression ;An assignment statement is an identifier, followed by the = sign, after whichthere is an expression, followed by an end-of-line. print_statement = ? expression ;A print statement is a ? followed by an expression and ended with an end-ofline.comment = / ;A comment is a line of text that starts with the / -sign and is closed by anend-of-line.identifier = letter { letter | number } ;An identifier starts with a letter and only consists of letters and numbers.expression = term { additive_operator term } ;An expression is a term, followed by zero or more terms. All terms areseparated by an additive-operator.term = factor { multiplicative_operator factor } ;A term is a factor, followed by 0 or more factors. All factors are separated bya multiplicative-operator.factor = identifier | complex_factor | set ;A factor is an identifier, a complex factor or a set.complex_factor = ( expression ) ;A complex factor is an expression between round brackets.set = { row_natural_numbers } ;A set is a row of natural numbers between accolades.row_natural_numbers = [ natural_number { , natural_number } ] ;A row of natural 代寫command-language留學(xué)生作業(yè)、代做Java編程作業(yè)、幫寫B(tài)igInteger作業(yè)、代做Java語言作業(yè)numbers is empty or a summation of one or more naturalnumbers separated by commas.additive_operator = + | | | ? ;An additive operator is a +, a | or a - sign.multiplicative_operator = * ;A multiplicative operation is a * -sign.natural_number = positive_number | zero ;A natural number is a positive number or zero.positive_number = not_zero { number } ;A positive number does not start with a zero, does not have a sign, and has 1or more numbers.number = zero | not_zero ; A number is a zero or not a zero.zero = 0 ;Zero is the number 0.not_zero = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ;Not-zero is the number from the range 1 up to and including 9.letter = A | B | C | D | E | F | G | H | I | J | K | L | M |N | O | P | Q | R | S | T | U | V | W | X | Y | Z |a | b | c | d | e | f | g | h | i | j | k | l | m |n | o | p | q | r | s | t | u | v | w | x | y | z ;A letter is a capital letter or a small letter.Remarks1. Spaces in natural numbers and identifiers are not allowed. Elsewhere, spaceshave no function and they can be added to any line to increase readability.2. When a faulty statement is discovered, then a clear error message needs to beprinted, followed by a new-line. Explanatory error messages like this arenecessary in the test-phase to discover what the program exactly does (this isusually something else than what it should do).3. The program has to read from standard input and write to standard output.The output must consist of one-line error-messages or one-line withnumbers, separated with spaces and ending with an end-of-line after the lastnumber.4. Comments have to be ignored.Recursive descentWe illustrate how to make a design by showing how a factor can be recognizedwith the method recursive descent. recursive descent means that the methodsthat parse the input call each other recursively.We can, for example, design a method factor() that:1. returns a set of natural numbers as the result of the function if there isgrammatically correct input, and2. gives an exception, and throws an APException if there is grammaticallyincorrect input.When the method factor() parses a complex factor, the right expression has tobe parsed, for which a term has to be parsed, for which another correct factor has tobe parsed. So after 3 calls in between, factor() is called again (in other wordsthere is recursion). A first sketch of the method factor() is:Set factor () throws APException {/* factor() reads, if possible, a correct factor of the input. If this succeeds this factor is evaluated and the resulting set is returned. If this fails, then an error-message is given and a APException is thrown.*/ if (the next character is a letter) { read an identifier retrieve the set that belongs with that identifier } else if (the next character is {) { read a set } else if (the next character is () { determine the set that is the result of the complex factor } else give a clear error-message throw APException } RETURN the value of the set}Also, pay attention to the similarity between the syntax-definition of a factor andthe design of factor(). The syntax-rules are a very thoroughanalysis/description of the input and only have to be converted to a Java during thedesign process. Testing List implementationIn order to help you implement the ListInterface, we provide you with a unit test ListTest.java. Youcan download it from Canvas. Usage of this test is completely optional. You wont get any points forusing it, but testing might help you to implement the list faster and with less errors.If you name your implementation of the ListInterface List.java, you can automatically use these tests(if you have a different class name, you need to change all occurrences of the type “List” in the testfile accordingly).Test framework: JUnitThe unit test uses JUnit (junit.org). Follow this?tutorial?on?how?to?use?JUnit?in?Eclipse. If you want tolearn more about JUnit, look at the documentation.QuickstartIn short, you need to follow these steps to use our test suite in Eclipse:1. Right-click on the package in which you want to create the test and select New > JUnit TestCase.2. Fill “ListTest” into the name field and click Finish.3. Eclipse prompts you to add JUnit to the build path. Approve this by clicking OK.4. Paste the content from ListTest.java on Blackboard into this newly generated file.5. Right-click on the newly generated file ListTest.java and select Run as > JUnit Test. Eclipsewill open a list of tests which were passed and failed.If anything is unclear follow the detailed tutorial athttps://courses.cs.washington.edu/courses/cse143/11wi/eclipse‐tutorial/junit.shtmlMore testingThe provided tests are not complete. You can add additional tests for lists in the same file. If you want to create atest for another class, you can use the file as a template. 轉(zhuǎn)自:http://ass.3daixie.com/2018101920717026.html

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容