Skim Chapter 3 and read Chapter 4 in Scott.
Do all of the exercises marked "Check Your Understanding"
in a social setting with classmates or friends, but do not
turn these in.
Submit solutions on paper for all but the Hana code,
but make sure everything is in your CVS repository.
You'll have at least
/homework/cmsi488/hw2.tex
/homework/cmsi488/scratch/src/main/javacc/Polynomial.jj
/homework/cmsi488/scratch/src/test/java/edu/lmu/cs/your-id/PolynomialTest.java
/homework/cmsi488/hana/
- Write a grammar (using the notation introduced in this class)
that is amenable to LL(1) parsing for polynomials
whose sole variable is x and for which all coefficients
are integers, and all
exponents are non-negative integers. The following
strings must be accepted.
- 2x
- 2x^3+7x+5
- 3x^8 - x + x^2
- 3 x - x ^ 3 +2
- -9x ^ 5 - 0 + 4x^100
- -3x^1 + 8 x ^ 0
- Write a command-line application, using JavaCC, that evaluates
polynomials from the language you defined above. The first argument should
be the polynomial and the second is the value at which to evaluate
the polynomial. Here are some example runs:
$ eval "2x" 10
20.000000
$ eval "2x^3+7x+5" 2
35.000000
$ eval "3x^8-x+x^2" 1
3.000000
$ eval " 3 x - x ^ 3 +2" 0
2.000000
Make sure to do a good job on this one — you'll get differentiation
on the midterm.
- Write a unit test for the polynomial evaluator
in the last problem. This means
that if you already did the last problem, and you crammed a lot of
logic into a main() method that you did something wrong. Make
sure your evaluator is architected as a class which contains a method
that returns (as a double) the evaluation of a polynomial (given
as a string) at a given value (a double). The main() method should
call that evaluate() method, and your unit test should test
said evaluate() method.
- Begin a project for a Hana compiler. Base it on the one for
Carlos that was discussed in class. For this assignment, you should
build the project structure, implement the semantic classes
leaving the semantic analysis methods as stubs, and write
a complete abstract syntax tree generator.