Docsity

CS 476 Assignment 1: Developing a Natural Language Calculator using JavaCC, Assignments of Programming Languages

Toggle sidebar
bg1
CS 476 - Programming Language Design - Spring 2007
Homework #1
Due date: February 20th, 2007, 11:59 pm
General description
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.
Part 1
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:
1. Tell me the sum of 2.5 and -3
2. Tell me the difference between 10 and 5.1
3. Yell the square root of 2
4. Yell the product of the square of 5 and the ratio of 10 over 3
As you can infer from example 4, mathematical expressions can be nested arbitrarily. Since
the form of the language corresponds to a prefix notation, parentheses are not necessary.
The following table contains a list of the valid expressions and their mathematical meaning.
X and Y are floating point numbers.
Expression Meaning
the sum of X and Y
X๎‚ƒY
the difference between X and Y
Xโˆ’Y
the product of X and Y
Xโˆ—Y
the ratio of X over Y
X/Y
the opposite of X
โˆ’X
the inverse of X
1
X
the square of X
X2
the square root of X
๎‚
X
1
pf3

Partial preview of the text

Download CS 476 Assignment 1: Developing a Natural Language Calculator using JavaCC and more Assignments Programming Languages in PDF only on Docsity!

CS 476 - Programming Language Design - Spring 2007

Homework

Due date: February 20th, 2007, 11:59 pm

General description

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.

Part 1

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:

  1. Tell me the sum of 2.5 and -
  2. Tell me the difference between 10 and 5.
  3. Yell the square root of 2
  4. Yell the product of the square of 5 and the ratio of 10 over 3 As you can infer from example 4, mathematical expressions can be nested arbitrarily. Since the form of the language corresponds to a prefix notation, parentheses are not necessary. The following table contains a list of the valid expressions and their mathematical meaning. X and Y are floating point numbers. Expression Meaning

the sum of X and Y X ๎‚ƒ Y

the difference between X and Y X โˆ’ Y

the product of X and Y X โˆ— Y

the ratio of X over Y X / Y

the opposite of X โˆ’ X

the inverse of X

X

the square of X X^2

the square root of X ๎‚ X

Part 2

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.