Introduction to Simple Language Programs
In this content, you will explore the basics of writing programs in a simple language, focusing on assignments, arithmetic expressions, parsing, and creating abstract syntax trees. The examples provided demonstrate how to work with variables, operators, and sequences of assignments. Learn about the structure of programs, parsing results, and the implementation of classes like Program and Assignment. Enhance your understanding of programming concepts through practical examples and explanations.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
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. 2
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(); } } } 3
Parsing result for z=x+y*2 <?xml version="1.0" encoding="UTF-8" ?> - <program> - <assignment> <lhs>z</lhs> - <rhs> - <expr> <op>+</op> - <left> - <expr> <primitive>x</primitive> </expr> </left> - <right> - <expr> <op>*</op> - <left> - <expr> <primitive>y</primitive> </expr> </left> - <right> - <expr> <primitive>2</primitive> </expr> </right> </expr> </right> </expr> </rhs> </assignment> Program.java Assignment.java Expr.java 4
Program class consists of a vector of statements public class Program { private Vector <Statement> statements; public static Hashtable<String, Expr> varTable=new Hashtable<String, Expr>(); public Program(Statement s) { statements = new Vector <Statement>(); statements.add(s); } public Program(Statement s, Program p) { statements = p.getStatements(); statements.add(s); } public String toString(){ } } 5
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; } } 6
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 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 2==>2 3==> ? 3==>3 (2*3)==>6 (2+(2*3))==>8 Number of Expressions:11 7
Print out the program Print out the parsed program y = 2; x = (2+(y*3)); z = (x+(y*2)); public class UseParser { public static void main(String[] args) throws Exception{ File inputFile = new File("calc.tiny"); Calc2Parser parser = new Calc2Parser (new Calc2Scanner( new FileReader(inputFile))); Suppress printout in the parser by skipping calls to System.out.println(..) in UseParser Program pm = (Program) parser.parse().value; System.out.println(pm.toString()); } } void around() : call(* *..print*(..)) && !within(A2) { } Note that call(* System.out.println(..)) Won t work 8
Print out the parsed program public class Expr { private Integer value; private boolean primitive = false; private String id; private Expr left; private Expr right; private String op; public Expr() { super(); } public Expr(Expr l, Expr r, String o) { left = l; right = r; op = o; } Define your own print method in classes such as Expr, Assignment. Note that the method will be added inside the Expr class. public String Expr.print(){ String result=""; if (this.isPrimitive() && this.getValue() != null) { return this.getValue().toString(); } else if (this.isPrimitive() && this.getID() != null) { result=this.getID(); } else { String op = this.getOp(); Expr left = this.getLeft(); Expr right = this.getRight(); result= "("+left.print()+op+right.print()+")"; } return result; } public Expr(Integer i) { value = i; primitive = true; } } 9
Print out the total number of expressions in this program count calls to constructors static int Expr.exprCount=0; after(Expr e): execution( Expr.new(..)) && this(e) { e.exprCount++; } 10
Evaluate the program y===>2 x===>8 z===>12 public Integer Expr.evaluate() { Integer val; if (this.isPrimitive() && this.getValue() != null) { return this.getValue(); } else if (this.isPrimitive() && this.getID() != null) { val = Program.varTable.get(this.getID()).evaluate(); } else { String op = this.getOp(); Expr left = this.getLeft(); Expr right = this.getRight(); if (op.equals("+")) { val = left.evaluate() + right.evaluate(); } else if (op.equals("*")) { val = left.evaluate() * right.evaluate(); add evaluation method into Expr class public class Expr { private Integer value; private boolean primitive = false; private String id; private Expr left; private Expr right; private String op; public Expr() { super(); } public Expr(Expr l, Expr r, String o) { left = l; right = r; op = o; } } public Expr(Integer i) { value = i; primitive = true; } 11
Print indented trace of the evaluation process check JoinPointTraceAspect in account example Count the nested level before(): call(* *.evaluate()){ indentLevel++; Expr exp = (Expr) thisJoinPoint.getTarget(); for (int i = 0, sp = indentLevel * 3; i < sp; i++) System.out.print(" "); System.out.println(exp.print() + "==> ?"); } 12