Due
Date:
Midnight, morning of Monday April 7
Reading: Chapter 8 in the text.
This assignment
is a self-contained Java application that parses logical expressions and
displays them as trees. In the next assignment, we will use the result in an
interesting way: to check whether expressions are tautologies or whether they
can be simplified. An example of using this grammar, in applet form, may be
found at:
http://www.cs.hmc.edu/~keller/javaExamples/taut/
Requirement:
Implement
a Java application that parses logic expressions according to the grammar given
below. Each expression is one input line. The result is printed following the
input. Any number of input expressions can be entered. For example, if the input is comprised
of the following two lines,
(a+b)' = a'*b'a + a'*b = a + b+aabc
then
the output will be
Tree for: (a+b)' = a'*b'= ' + a b * ' a ' b Tree for: a + a'*b = a + b= + a * ' a b + a bTree for: +a*** error: invalid inputTree for: abc*** error: garbage after valid input: bc
The
result of parsing, if the expression is syntactically correct, is a syntax tree
constructed using the LogicTree class that is provided to you (in a09 we will
add more functionality to this and related classes). A LogicTree is convertible
to a String for printing. If the input is not correct, then the result is an
instance of the sub-class Failure. This will also be the case if the input has
a correct prefix, but is followed by garbage.
Grammar:
The
root symbol is E, representing the syntactic category of logical Expressions:
The
productions are, where [...] means an optional occurrence of ... and {...}
means 0 or more occurrences of ... :
E ® I [ = I ] //
Expression, = is if-and-only-if
I ® S [ > I ] //
Implication, > is implies
S ® P { + P } //
Sum, + is or
P ® L { * L } //
Product, * is and
L ® U { ' } //
Literal ( ' is single quote for not)
C -> 0 | 1
// Constant (0 for false, 1 for true)
V -> a|b|c|d|e|f|g|h|i|j|k|l|m| //
Variable
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
http://www.cs.hmc.edu/courses/current/cs60/a08/
cs60submit *.java . . .