The syntax of a simple "while" language is shown below:
Statement ::= Assignment | Conditional | While | Compound
Assignment ::= Var := Expr
Conditional ::= if Expr then Statement else Statement
While ::= while Expr do Statement
Compound ::= begin Statement; ...; Statement end
Define classes, using the Composite pattern, for representing
programs in this language. For example, the following factorial
program
begin
fact := 1;
while (n > 1) do
begin
fact := fact * n;
n := n - 1
end
end
is constructed, using the classes you are expected to define,
by this Java declaration and initialization
Statement factorial = new Compound (
new Assignment ("fact", new Expr ()),
new While (new Expr (), new Compound (
new Assignment ("fact", new Expr ()),
new Assignment ("n", new Expr ()))));
To simplify the code, expressions are represented by a trivial
placeholder. Include in your classes methods to pretty print
a program. The expected output when object "factorial" is printed
is
begin
fact := expression;
while expression do
begin
fact := expression;
n := expression
end
end