33
Interpreter Kurt de Leon & Brian Guadalupe April 19, 2018 1/17

Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Interpreter

Kurt de Leon & Brian Guadalupe

April 19, 2018

1/17

Page 2: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Introduction

Page 3: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

What is this?

From Gamma, et al.

Given a language, define a representation for its grammar along with an interpreter

that uses the representation to interpret sentences in the language.

• A behavioral pattern

• Unfortunately, also ignored by programmers :(

2/17

Page 4: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

What is this?

From Gamma, et al.

Given a language, define a representation for its grammar along with an interpreter

that uses the representation to interpret sentences in the language.

• A behavioral pattern

• Unfortunately, also ignored by programmers :(

2/17

Page 5: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

What is this?

From Gamma, et al.

Given a language, define a representation for its grammar along with an interpreter

that uses the representation to interpret sentences in the language.

• A behavioral pattern

• Unfortunately, also ignored by programmers :(

2/17

Page 6: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

What is this?

From Gamma, et al.

Given a language, define a representation for its grammar along with an interpreter

that uses the representation to interpret sentences in the language.

• A behavioral pattern

• Unfortunately, also ignored by programmers :(

2/17

Page 7: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

When to use this?

The Interpreter design pattern works best when

• the grammar is simple

• efficiency is not a concern

Basically...

Use the Interpreter design pattern when you have a language to interpret.

3/17

Page 8: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

When to use this?

The Interpreter design pattern works best when

• the grammar is simple

• efficiency is not a concern

Basically...

Use the Interpreter design pattern when you have a language to interpret.

3/17

Page 9: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Motivation

Page 10: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Consider this problem...

• Suppose you want to create your own personal assistant

(maybe for your smartphone, laptop, fridge, etc.)

• Often takes a query written in plain English as input

• How do you process such queries?

Take note!

For now, only simple calculations are supported.

4/17

Page 11: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Consider this problem...

• Suppose you want to create your own personal assistant

(maybe for your smartphone, laptop, fridge, etc.)

• Often takes a query written in plain English as input

• How do you process such queries?

Take note!

For now, only simple calculations are supported.

4/17

Page 12: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Consider this problem...

• Suppose you want to create your own personal assistant

(maybe for your smartphone, laptop, fridge, etc.)

• Often takes a query written in plain English as input

• How do you process such queries?

Take note!

For now, only simple calculations are supported.

4/17

Page 13: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Defining the language

We can define the language recursively, as follows:

Backus-Naur form

〈expression〉 ::= 〈plus〉 | 〈minus〉 | 〈integer〉

〈plus〉 ::= 〈expression〉 ’plus’ 〈expression〉

〈minus〉 ::= 〈expression〉 ’minus’ 〈expression〉

〈integer〉 ::= 〈unsigned-integer〉 | ’+’ 〈unsigned-integer〉 | ’−’ 〈unsigned-integer〉

〈unsigned-integer〉 ::= 〈digit〉 | 〈unsigned-integer〉 〈digit〉

〈digit〉 ::= ’0’ | ’1’ | ’2’ | ’3’ | ’4’ | ’5’ | ’6’ | ’7’ | ’8’ | ’9’

5/17

Page 14: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Defining the language

We can define the language recursively, as follows:

Backus-Naur form

〈expression〉 ::= 〈plus〉 | 〈minus〉 | 〈integer〉

〈plus〉 ::= 〈expression〉 ’plus’ 〈expression〉

〈minus〉 ::= 〈expression〉 ’minus’ 〈expression〉

〈integer〉 ::= 〈unsigned-integer〉 | ’+’ 〈unsigned-integer〉 | ’−’ 〈unsigned-integer〉

〈unsigned-integer〉 ::= 〈digit〉 | 〈unsigned-integer〉 〈digit〉

〈digit〉 ::= ’0’ | ’1’ | ’2’ | ’3’ | ’4’ | ’5’ | ’6’ | ’7’ | ’8’ | ’9’

5/17

Page 15: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Defining the language

plus

1minus

20plus

67Figure 1: The abstract syntax tree for the query “7 plus 6 minus 20 plus 1”

6/17

Page 16: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Consequences

Page 17: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Consequences

Using the Interpreter pattern can lead into the following pros and cons:

1. Easy to change and extend the grammar

• Through inheritance

2. Implementing the grammar is also easy

• Classes defining nodes in the abstract syntax tree have similar implementations

3. Complex grammars are hard to maintain

• Because it defines at least one class for every rule in the grammar

• Other techniques may be more appropriate to deal with complex grammars

4. Support new ways to interpret expressions

• Add pretty-printing and type checking functionality, for example

• Consider using the Visitor pattern if you intend to provide more new ways to

interpret an expression

7/17

Page 18: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Consequences

Using the Interpreter pattern can lead into the following pros and cons:

1. Easy to change and extend the grammar

• Through inheritance

2. Implementing the grammar is also easy

• Classes defining nodes in the abstract syntax tree have similar implementations

3. Complex grammars are hard to maintain

• Because it defines at least one class for every rule in the grammar

• Other techniques may be more appropriate to deal with complex grammars

4. Support new ways to interpret expressions

• Add pretty-printing and type checking functionality, for example

• Consider using the Visitor pattern if you intend to provide more new ways to

interpret an expression

7/17

Page 19: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Consequences

Using the Interpreter pattern can lead into the following pros and cons:

1. Easy to change and extend the grammar

• Through inheritance

2. Implementing the grammar is also easy

• Classes defining nodes in the abstract syntax tree have similar implementations

3. Complex grammars are hard to maintain

• Because it defines at least one class for every rule in the grammar

• Other techniques may be more appropriate to deal with complex grammars

4. Support new ways to interpret expressions

• Add pretty-printing and type checking functionality, for example

• Consider using the Visitor pattern if you intend to provide more new ways to

interpret an expression

7/17

Page 20: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Consequences

Using the Interpreter pattern can lead into the following pros and cons:

1. Easy to change and extend the grammar

• Through inheritance

2. Implementing the grammar is also easy

• Classes defining nodes in the abstract syntax tree have similar implementations

3. Complex grammars are hard to maintain

• Because it defines at least one class for every rule in the grammar

• Other techniques may be more appropriate to deal with complex grammars

4. Support new ways to interpret expressions

• Add pretty-printing and type checking functionality, for example

• Consider using the Visitor pattern if you intend to provide more new ways to

interpret an expression

7/17

Page 21: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Consequences

Using the Interpreter pattern can lead into the following pros and cons:

1. Easy to change and extend the grammar

• Through inheritance

2. Implementing the grammar is also easy

• Classes defining nodes in the abstract syntax tree have similar implementations

3. Complex grammars are hard to maintain

• Because it defines at least one class for every rule in the grammar

• Other techniques may be more appropriate to deal with complex grammars

4. Support new ways to interpret expressions

• Add pretty-printing and type checking functionality, for example

• Consider using the Visitor pattern if you intend to provide more new ways to

interpret an expression

7/17

Page 22: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Implementation

Page 23: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Structure

CalculatorClient

CalculatorContext

PlusExpression

+ interpret(Context)

MinusExpression

+ interpret(Context)

IntegerExpression

+ interpret(Context)

non-terminal terminal

«interface» Expression

+ interpret(Context)

8/17

Page 24: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Participants

1. CalculatorClient

• builds an abstract syntax tree of the expression

• invokes the interpret() method

2. CalculatorContext

• contains information that is global to the interpreter (e.g., variables, current state)

3. Expression

• declares an interpret() method that is common to all nodes in the AST

4. PlusExpression, MinusExpression

• non-terminal expressions

5. IntegerExpression

• terminal expression

9/17

Page 25: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

CalculatorClient

1 import java.util.Scanner;23 public class CalculatorClient {4 public static void main(String[] args) {5 Scanner sc = new Scanner(System.in);6 while (true) {7 // get expr from standard input8 System.out.print("Enter an expression: ");9 String expr = sc.nextLine();

10 // build the abstract syntax tree11 Expression ast = parse(expr);12 // interpret the AST13 String result = Integer.toString(ast.interpret());14 // output the result15 System.out.println("Result: " + result);16 }17 }1819 // Note that the Interpreter pattern DOES NOT supply a parser of its own!20 public static Expression parse(String inputExpr) {21 // do stuff here22 }23 }

10/17

Page 26: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

CalculatorContext

1 public class CalculatorContext {

2 /*

3 * The CalculatorContext class contains information that is global

4 * to the interpreter.

5 * For example, if we want to extend our simple calculator in order

6 * to support variables, we could put a HashMap of variables, along

7 * with theirrespective values, so that the interpreter can either

8 * recall the value of a variable or store a new value to it.

9 */

10 }

11/17

Page 27: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Expression

1 public interface Expression {

2 public int interpret();

3 }

12/17

Page 28: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

PlusExpression

1 public class PlusExpression implements Expression {

2

3 private Expression leftExpr, rightExpr;

4

5 public PlusExpression(Expression left, Expression right) {

6 this.leftExpr = left;

7 this.rightExpr = right;

8 }

9

10 @Override

11 public int interpret() {

12 return leftExpr.interpret() + rightExpr.interpret();

13 }

14 }

13/17

Page 29: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

MinusExpression

1 public class MinusExpression implements Expression {

2

3 private Expression leftExpr, rightExpr;

4

5 public MinusExpression(Expression left, Expression right) {

6 this.leftExpr = left;

7 this.rightExpr = right;

8 }

9

10 @Override

11 public int interpret() {

12 return leftExpr.interpret() - rightExpr.interpret();

13 }

14 }

14/17

Page 30: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

IntegerExpression

1 public class IntegerExpression implements Expression {

2

3 private int number;

4

5 public IntegerExpression(String s) {

6 this.number = Integer.parseInt(s);

7 }

8

9 @Override

10 public int interpret() {

11 return number;

12 }

13 }

15/17

Page 31: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

But there’s a catch...

• The Gang of Four book did not address the issue of parsing the input (i.e.

converting the language into an AST), because they said it’s a separate problem

altogether

• Very difficult to find an easy way to generate ASTs

• Without parsing, the Interpreter pattern is essentially useless!

16/17

Page 32: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

Demo

Page 33: Interpreterpenoy.admu.edu.ph/.../classes/cs124_2017/interpreter.pdf · 2018. 4. 19. · Using the Interpreter pattern can lead into the following pros and cons: 1.Easy to change and

References

Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.

Design Patterns: Elements of Reusable Object-Oriented Software.

Addison-Wesley, Reading, MA, 1995.

Jeffrey Kegler.

The Interpreter Design Pattern, March 2013.

Available at https://jeffreykegler.github.io/

Ocean-of-Awareness-blog/individual/2013/03/interpreter.html.

17/17