

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Due date: February 20th, 2007, 11:59 pm
In this assignment, you will develop a small calculator that executes commands given in natural language. This will help you familiarize with context-free grammars and tools that can be used to build sophisticated applications.
Write a context-free grammar that recognizes the language described below. You don't necessarily have to submit the grammar with your solution, but you can submit it if you are not able to complete the rest of the assignment, to receive partial credit. The language to be represented is a small subset of English, composed by imperative sentences describing commands to be performed. A sentence begins either with the command tell me or yell , followed by the description of a mathematical expression to be calculated. Examples:
the inverse of X
Implement a system using the grammar you wrote in the first part of the assignment and the tool JavaCC. JavaCC allows you to attach fragments of Java code to the production rules of a context-free grammar. The tool automatically generates a parser that recognizes the language described by the grammar and executes the corresponding code. JavaCC is freely available at the following website: https://javacc.dev.java.net/ The installation of JavaCC is straightforward. To complete this assignment, the main tool (javacc) is sufficient, you don't need to use the jjtree and jjdoc utilities also provided in the package. Your system will parse sentences provided by the user, perform the appropriate arithmetic calculations, and return the result in a format dependent on the command ("tell me" or "yell"). Examples: Input Output Tell me the sum of 2.5 and -3 The result is -0. Tell me the difference between 10 and 5.1 The result is 4. Yell the square root of 2 THE RESULT IS 1.4142135623730951!!! Yell the product of the square of 5 and the ratio of 10 over 3 THE RESULT IS 83.33333333333334!!! qpwoei rieoq fqeij!!! Sorry, I couldn't understand your command. For more examples of input/output behavior, you can download and run the reference implementation from the class' website. Note: if you find any mistake or odd behavior in the reference system, please report it to the TA as soon as possible. To run the reference system, use the command: java -jar NLCalculator.jar Tips and additional requirements: โ Read carefully the examples in the directory "SimpleExamples" of the JavaCC package. They are very useful to understand how JavaCC works. โ Make your system case insensitive and whitespace insensitive. This can be done playing with the options and the SKIP directive. โ Put your code in a file named NLCalculator.jj and call the main class defined into it NLCalculator. JavaCC will generate many .java files, among them there should be a file called NLCalculator.java. โ Make sure your system reads the sentences from the standard input. Each sentence should be delimited by a newline character. โ The system should exit upon the additional commands quit , exit , or when the end of file is reached.