Interpreter

1,051 views

Published on

The slides from a presentation I did on the Interpreter design pattern at the Melbourne Patterns group.

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,051
On SlideShare
0
From Embeds
0
Number of Embeds
19
Actions
Shares
0
Downloads
34
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Interpreter

  1. 1. Interpreter Abhijit Hiremagalur Cogent Consulting
  2. 2. Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language [GOF, 243]
  3. 3. Domain Language Grammar Object Hierarchy
  4. 4. Composite + interpret()
  5. 5. Reverse Polish Notation
  6. 6. 3+4 is expressed as 34+
  7. 7. 3-4+5
  8. 8. (3 - 4) + 5 or 3 - (4 +5) Which one is it?
  9. 9. 543-+ 5 (4 3 -) + 5 (1) + 6
  10. 10. The code
  11. 11. ‘41 2 1 - +’ equals 42
  12. 12. Stack what? [] (41) => [41] (2) => [41, 2] (1) => [41, 2, 1] (-) => [41, 1] (+) => [42]
  13. 13. Easy to implement, change & extend?
  14. 14. Doesn’t address parsing
  15. 15. Use something else if grammar is complex
  16. 16. Refactor to an Interpreter when...
  17. 17. “Numerous methods on a class combine elements of an implicit language”
  18. 18. Replace Implicit Language with Interpreter [Kerievsky]
  19. 19. “Define classes for the elements of the implicit language so that instances may be combined to form interpretable expressions.”
  20. 20. A.K.A ? • Little language • Macro • Domain specific language
  21. 21. Domain Specific Language (DSL)
  22. 22. Internal vs External DSL
  23. 23. Spec productSpec = parse(“price below 9.00f and not coloured white”); Spec productSpec = both(priceIsBelow(9.00f)).and(not(coloured(WHITE)));
  24. 24. Share symbols with Flyweight
  25. 25. Move operations to Visitor
  26. 26. The end.

×