There is a small programming language, called Tiny, which can have a sequence of assignment statements. We have a parser (tiny.jar) that can parse Tiny programs like calc.tiny into an abstract syntax tree. The expressions can have "+" and "*" operators only. The syntax tree is represented by classes such as Program, Statement, Assignment, Expr, etc. Those classes are also included in tiny.jar. A2/UseParser.java has the main method to run the parser.
Once you have put the jar file, the java programs, and calc.tiny into your local directory, you can try the following commands. You should be able to see the xml printout that describes the parse tree of the program:
E:\440\A2>javac -classpath tiny.jar;<yourAspectJDir>\lib\aspectjrt.jar *.java
E:\440\A2>java -classpath .;tiny.jar;<yourAspectJDir>\lib\aspectjrt.jar UseParser
Now you can start to write an aspect called A2.aj. We will run the following commands to check your program. Here are instructions to install AspectJ on windows.
E:\440\A2>ajc -1.5 -classpath tiny.jar;<yourAspectJDir>\lib\aspectjrt.jar *.java A2.aj
E:\440\A2>java -classpath .;tiny.jar;<yourAspectJDir>\lib\aspectjrt.jar UseParser
Your task is to write an aspect A2.aj to perform the following five tasks. Please note that you are not supposed to modify any code we have provided.
You can assume that the tiny program is always a correct one. It consists of a sequence of assignment statements. Variables are of Integer type, and they will be assigned values before they are used. The expressions have "+" and "*" operators only.
Current program print out parsed Tiny programs in xml format. Write A2.aj so that the following lines can be printed by running UseParser class. Note that correct parentheses should be added to the printout. Also, the original XML printout should be suppressed.
y = 2;
x = (2+(y*3));
z = (x+(y*2));
The number of expressions should include also the sub expressions. Note that primitives such as x and 2 are also expressions. For our sample input calc.tiny, you should print out the following:
Number of Expressions:11
The parser generates a parse tree represented in the Program object. However, it is not evaluated yet. Write the evaluation function in A2.aj so that when you run UseParser, it will print out the value for each variable in the assignment statement. For example, it will print out the following for the calc2.tiny program:
y===>2
x===>8
z===>12
Add more advices in A2.aj, so that you can print out the evaluation process for the right hand side of the assignment statement. That is, you will evaluate the primitives first, either a value or a variable, then you will perform the arithmetic operations.
For the given calc.tiny program, your output should be the following. It prints the assignment statement first. Next it shows which expression it is trying to evaluate. Finally, it prints the number of assignment statements in this program.
Assignment 0 is: y=2
2==> ?
2==>2
Assignment 1 is: x=(2+(y*3))
(2+(y*3))==> ?
2==> ?
2==>2
(y*3)==> ?
y==> ?
2==> ?
2==>2
2==>2
3==> ?
3==>3
(2*3)==>6
(2+(2*3))==>8
Assignment 2 is: z=(x+(y*2))
(x+(y*2))==> ?
x==> ?
(2+(2*3))==> ?
2==> ?
2==>2
(2*3)==> ?
2==> ?
2==>2
3==> ?
3==>3
(2*3)==>6
(2+(2*3))==>8
8==>8
(y*2)==> ?
y==> ?
2==> ?
2==>2
2==>2
2==> ?
2==>2
(2*2)==>4
(8+(2*2))==>12
Number of Expressions:11