Struggling with Interpreter Pattern

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Struggling with Interpreter Pattern

Post by Luke »

I have been reading the gang of four book as well as PHP: Objects, Patterns, and Practice. Both of them have chapters devoted to the Interpreter Pattern. The PHP book even has an appendix with a sample parser with an impressive amount of example code to help understand how and why the interpreter pattern is used. But for whatever reason, even with all that help, I can't seem to wrap my head around this pattern. I know this is a really vague question, and I apologize for that, but I'm just basically wondering if anybody can help me understand this pattern and how, exactly, it works. I am having trouble figuring out what problem it solves. Because according to the PHP book, at least what I understand of it, the interpreter doesn't necessarily incorporate a parser. The parser seems to be out of the scope of the actual pattern. So what IS the interpreter pattern? Can anybody point me in the right direction? What am I missing?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Struggling with Interpreter Pattern

Post by requinix »

The interpreted thing is what you get after the parsing and lexing: a tree of sorts. "a + b" would exist as an Addition expression with the operands "a" and "b". The power comes from the recursion: the two operands are themselves expressions that get evaluated. It's easier to see with more complicated expressions:

Code: Select all

a * b + 3 * c + 5

      +
  *           +
a   b     *     5
        3   c
That tree is implemented using objects (rather than, like, arrays or something). Those objects have a method to interpret themselves - to get a value from whatever they represent. You've got Number expressions that evaluate to themselves (obviously), and you've got Variable expressions that evaluate to whatever the value of the variable is, and you've got the recursive Addition expressions that evaluate to the left expression plus the right expression, and finally recursive Multiplication expressions that are the left expression times the right expression.

The interpreter pattern is the combination of the recursive tree-nature combined with the ability for things to return a "value" for themselves.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Struggling with Interpreter Pattern

Post by Luke »

I think that the interpreter pattern is more than I need then. I'm simply needing a parser. The file I am trying to parse is not complicated enough to require the interpreter pattern. It doesn't have variables or operators or anything like that. It just has a grammar of sorts.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Struggling with Interpreter Pattern

Post by requinix »

You might need a bit more than a parser. Technically speaking, a parser is what turns a sequence of characters into specific tokens. Like in PHP, "class" becomes T_CLASS. Once you get a stream of tokens the lexer kicks in and turns them into something you can work with: T_CLASS("class") T_STRING("c") "{" indicates the class definition of "c".
A full Interpreter could be too much for sure, but you'll probably still want the structuring part of it.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: Struggling with Interpreter Pattern

Post by Luke »

Oh... I thought it was the other way around. I need to rename my classes then because I have my lexer converting the text into tokens and the parser making sense of them.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Struggling with Interpreter Pattern

Post by Weirdan »

Luke wrote:Oh... I thought it was the other way around.
And you were right. http://en.wikipedia.org/wiki/Lexical_analysis
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Struggling with Interpreter Pattern

Post by requinix »

Weirdan wrote:
Luke wrote:Oh... I thought it was the other way around.
And you were right. http://en.wikipedia.org/wiki/Lexical_analysis
Oh jeez, I had them completely backwards. WTF. :banghead: See, that is why you don't trust just one person on the internet to give you answers.
Post Reply