27
Expression Evaluation: Expression Binary Variable Value Never need an instance of Expression

Expression Evaluation:

  • Upload
    gilles

  • View
    22

  • Download
    0

Embed Size (px)

DESCRIPTION

Expression Evaluation:. Never need an instance of Expression. Expression. Binary. Variable. Value. Expression Evaluation:. Abstract syntax definition:. Expression = Variable | Value | Binary Variable = String id Value = int intValue | boolean boolValue - PowerPoint PPT Presentation

Citation preview

Page 1: Expression Evaluation:

Expression Evaluation:

Expression

Binary Variable Value

Never need an instanceof Expression

Page 2: Expression Evaluation:

Expression Evaluation:

• Abstract syntax definition:

Expression = Variable | Value | Binary Variable = String id Value = int intValue | boolean boolValue Binary = Operator op; Expression term1, term2 Operator = BooleanOp | RelationalOp | ArithmeticOp

Italics: type name; Non-italics: member nameReview Section 2.3

Page 3: Expression Evaluation:

Abstract Syntax Class Hierarchy

Expression

Variable Value Binary

IntValue BoolValue UndefValue

Instead of primitives

Page 4: Expression Evaluation:

Abstract Syntax Class Hierarchy (2)

Operator

ArithmeticOp RelationalOp BooleanOp

AddOp SubOp MultOp DivOp

LessOp EqualOp GreaterOp . . .

AndOp OrOp

Page 5: Expression Evaluation:

Evaluation:

M: Expression x ∑ ValueM(Expression e, State σ)e.M(σ) = e (this) if e is a Value σ(e) if e is a Variable ApplyBinary(e.op, M(e.term1, σ), M(e.term2, σ)) if e is a Binary ApplyUnary(e.op, M(e.term1, σ)) if e is a Unary

Page 6: Expression Evaluation:

What “state” means:

• By State we mean the set of all instance variable values.

• By σ(e) we mean the value of an Expression, e, given its current data state as σ.

Example: e is the Expression x + 4 current value of x is 3 (σ(e)) current value of e is 7 M(e,σ(e))

Meaning of e

Page 7: Expression Evaluation:

Expression.java

public abstract class Expression { public abstract Value M(State sigma);}

M (meaning) == evaluate

Page 8: Expression Evaluation:

Command Pattern:

Application Menu

1 * +clicked()

MenuItem

1 *

+Execute()

Command

1

1

+Execute()

PasteCommand

ducument.Paste()

Abstract (italics)

Page 9: Expression Evaluation:

Motivation:

• GUI needs behaviour without knowing what kind.• Command turns a request for behaviour into an object• Concrete subclasses specify receiver-action pairs.• Binary encapsulates a receiver (op) and an action

(apply())

Page 10: Expression Evaluation:

Variable.java

public class Variable extends Expression {// Variable = String id String id;

public boolean equals (Object obj) { String s = ((Variable) obj).id; return id.equalsIgnoreCase(s); // case-insensitive identifiers }

public int hashCode ( ) { return id.hashCode( ); }

public Value M(State sigma) { return (Value)(sigma.get(this)); }}

Page 11: Expression Evaluation:

Value.java (1)

public class Value extends Expression {// Value = int intValue | boolean boolValue | Undefined

static public Value create(int i ) { return new IntValue(i); } static public Value create(boolean b ) { return new BooleanValue(b); } static public Value create( ) { return new UndefValue(); }

// only UndefValue class needs to override this public boolean isUndef() { return false; } public int intVal() { return -1; } // allows override public boolean boolVal() { return false; } // allows override public Value M(State sigma) { return this; }

. . . }

Page 12: Expression Evaluation:

Value.java (2)

public class Value extends Expression {// Value = int intValue | boolean boolValue | Undefined . . . class IntValue extends Value { int intVal; public IntValue(int i) { intVal = i; } public int intVal() { return intVal; } }

class BooleanValue extends Value { boolean boolVal; public BooleanValue(boolean b) { boolVal = b; } public boolean boolVal() { return boolVal; } }

class UndefValue extends Value { public UndefValue() { } public boolean isUndef() { return true; } }}

In Chapter 3, Value had a Type instancevariable. This isad hoc polymorphism.

Now we create the actual subclasses

Page 13: Expression Evaluation:

Factory Pattern:

• Value is an example of a Factory Pattern.

• Factory patterns can be used in following cases:

1. When a class does not know which class of objects it must create.2. A class specifies its sub-classes to specify which objects to create.3. In programmer’s language (very raw form), you can use factory pattern where you have to create an object of any one of a number of sub-classes of some class depending on the data provided.

NOTE: Patterns are archetypes of typical programming constructs.There is a whole study of what these archetypes are and how they should be used. eg: http://www.allapplabs.com/java_design_patterns/factory_pattern.htm

Page 14: Expression Evaluation:

Weird syntax:

• The book code for Value is “weird” and invalid.

• But this works strangely enough

public Value extends Expression { public Value(int n) { return new IntValue(n);} . . .}

public Value extends Expression { public Value Value(int n) {return new IntValue(n);} . . .}

// clientValue x = Value(3);// the actual type of x is IntValue

Page 15: Expression Evaluation:

Binary.java

public class Binary extends Expression {// Binary = Operator op; Expression term1, term2 Operator op; Expression term1, term2;

public Binary(Operator o, Expression t1, Expression t2) { op = o; term1 = t1; term2 = t2; }

public Value M(State sigma) { return op.apply(term1.M(sigma), term2.M(sigma)); }}

Only the Operator class needs be public

Page 16: Expression Evaluation:

Operator.java

public abstract class Operator { public abstract Value apply(Value v1, Value v2);}

abstract class ArithmeticOp extends Operator { }abstract class RelationalOp extends Operator { }abstract class BooleanOp extends Operator { }

NOTE: In a full implementation of Jay, we need to do type checkingand the above three abstract classes help in this. For our purposesthey don’t hurt so leave them there.

Page 17: Expression Evaluation:

AddOp.java

class AddOp extends ArithmeticOp { public Value apply(Value v1, Value v2) { if (v1.isUndef() || v2.isUndef()) return new Value(); return new Value(v1.intVal() + v2.intVal()); }}

Page 18: Expression Evaluation:

State.java// State.javaimport java.util.*;

public class State { private Hashtable<Variable,Value> contents = null; public State () { contents = new Hashtable<Variable,Value>(); } public Enumeration keys() { return contents.keys(); } public Value get(Variable v) { return contents.get(v); } public State (Variable key, Value value) { if ( contents == null ) contents = new Hashtable<Variable,Value>(); contents.put(key,value); } public State onion( State t) { // union for ( Enumeration<Variable> e = t.keys(); e.hasMoreElements(); ) { Variable v = e.nextElement(); contents.put(v, t.get(v)); } return this; }}

Page 19: Expression Evaluation:

State.java (2)

• Text implementation uses inheritance after complaining about Vector Stack

• Onion(): This is really just union. Any ideas? • Hashtable.put(k,v) replaces any existing value if an

attempt is made put a duplicate key.

Page 20: Expression Evaluation:

Concordance:

Roses are redViolets are blueSugar is sweetSo are you

Are: 1, 2, 4Blue: 2Is: 3Red: 1Roses: 1So: 4Sugar: 3Sweet: 3Violets: 2You: 4

Blue: 2Red: 1Roses: 1So: 4Sugar: 3Sweet: 3Violets: 2You: 4

Without nonsense words

Page 21: Expression Evaluation:

Concordance Uses:

• Searching large documents like Bible

• Variants include indexes, no nonsense concordance

• Cross-reference listing of program: what functions use what variables and call what other funtions

Page 22: Expression Evaluation:

OOD Methodology(?):

• List all nouns (sounds like ER) found is problem description

ConcordanceWords (String)Line numbers (int)DocumentReport

Page 23: Expression Evaluation:

ULM Diagram:

+Concordance()+Concordance(in allowDupl : bool)+add(in word : Object, in number : Object) : void+keys() : Enumeration+elements(in word : Object) : Enumeration

-allowDupl : bool-dict : Dictionary

Concordance

+Document(in input : BufferedReader, in senstive : Boolean, in dict : Concordance)

Document

+Report(in dict : Concordance, out out : PrintWriter)

Report

Page 24: Expression Evaluation:

Concordance.java

public class Concordance { private Dictionary<,String,Vector<Integer>> dict = new Hashtable <String,Vector<Integer>> (); private boolean allowDupl = true; public Concordance (boolean allowDupl ) { this.allowDupl = allowDupl; } public Concordance ( ) { this(true); } public void add (String word, Integer line) { Vector<Integer> set = dict.get(word); if (set == null) // word not in Concordance { set = new Vector<Integer> ( ); dict.put(word, set); } if (allowDupl || !set.contains(line))

set.addElement(line); } public Enumeration<String> keys( ) { return dict.keys( ); } public Enumeration< Vector<Integer>> elements (String word) { return (dict.get(word)).elements( ); }}

Sets exist in Java butnot ones that allow forduplicate entries.

Page 25: Expression Evaluation:

Document.java

public class Document {

public Document (BufferedReader input, boolean sensitive, Concordance dict) throws IOExecption { String line; for (int number = 1; (line = input.readLine())!=null; number++) { if (!sensitive) line = line.toLowerCase( ); Enumeration<String> e = new StringTokenizer(line, " \t\n.,!?;:()[]{}"); while (e.hasMoreElements( )) dict.add(e.nextElement( ), new Integer(number)); } } }

Page 26: Expression Evaluation:

Report.java

public class Report {

public Report (Concordance dict, PrintStream out) { Enumeration<String> e = dict.keys( ); while (e.hasMoreElements( )) { String word = e.nextElement( ); out.print(word + ":"); Enumeration<Vector<Integer>> f = dict.elements(word); while (f.hasMoreElements( )) out.print(" " + f.nextElement( )); out.println( ); } }

}

Exercise: Add code to the above so that an HTML document is produced.

Page 27: Expression Evaluation:

Things learned:

• A good library is a good thing.• Enumerators have numerable uses