Download presentation
Presentation is loading. Please wait.
Published byMay Fowler Modified over 8 years ago
1
1 Assignment 2: AspectJ
2
2 The simple language Tiny program y=2; x=2+y*3; z=x+y*2; –A program is a sequence of assignments; –Expressions on the right hand side of assignment statements are arithmetic expressions; –Assume variables are always of integer types; –Arithmatic operators include +, -, *, and /. There are no other operators. –Assume input is always correct.
3
3 Parsing The parser creates an abstract syntax tree represented in Program class public class UseParser { public static void main(String[] args) { try { File inputFile = new File("calc.tiny"); Calc2Parser parser = new Calc2Parser(new Calc2Scanner( new FileReader(inputFile))); Program pm = (Program) parser.parse().value; System.out.println(pm.toString()); } catch (Exception e) { e.printStackTrace(); }
4
4 Parsing result - z - + - x - * - y - 2 … Program.java Assignment.java Expr.java
5
5 Program class consists of a vector of statements public class Program { private Vector statements; public static Hashtable varTable=new Hashtable (); public Program(Statement s) { statements = new Vector (); statements.add(s); } public Program(Statement s, Program p) { statements = p.getStatements(); statements.add(s); } public String toString(){ … }
6
6 Assignment class public class Assignment extends Statement{ private String lhs; private Expr rhs; public Assignment(String l, Expr r){ lhs=l; rhs=r; Program.varTable.put(l,r); } public String getLHS(){ return lhs;} public Expr getRHS() {return rhs; } }
7
7 Your tasks Print out the parsed program y = 2; x = (2+y*3); z = (x+(y*2)); –Hint: Suppress printout in the parser by skipping calls to System.out.println(..) in UseParser –Hint: Define your own print method in classes such as Expr, Assignment. Print out the total number of expressions in this program –Hint: count calls to constructors Evaluate the program y===>2 x===>8 z===>12 –Hint: add evaluation method into Expr class Print indented trace of the evaluation process –Hint: check JoinPointTraceAspect in account example –Count the nested level
8
8 Your output 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 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)==>4 (8+(2*2))==>12 Number of Expressions:11
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.