Lecture thumbnail 0:02 / 4:00 In this section of the course.
We’re going to take a look at the interpreter design pattern.
And to be honest, the interpreter design pattern is a separate field of computer science.
It would actually end up us having another ten hour course simply describing how to build interpreters
in their own right.
So here what I’m going to do is I’m going to try and explain it from the high level.
So essentially the idea of interpretation is the idea of processing textual input.
So for example, the source code you write in your program or the regular expressions that you write,
this is all textual input and somebody needs to process this textual input to turn your source code
into executables, to turn your regular expressions into actual state machines.
So all of this has to be parsed and turned into object oriented structures.
Now the reason we were talking about structures is because we are working with C Sharp, which is an
Oop language.
If this was a design patterns in C course, I wouldn’t be talking about Oop structures.
I would be talking about just structures generally.
So some examples of the kind of stuff that you do with interpreters is anything related to programming.
So programming language, compilers, interpreters and integrated development environments.
They all use this interpreter pattern, for example, Ides or something like Resharper, which performs
static analysis.
They have to interpret source code, so they have to turn the source code into some sort of object oriented
representation that they can subsequently walk, traverse and figure out what’s going on.
Another example is structured file format.
So you have file formats like HTML or XML for example, and similar you have to have components which
can actually turn those formats once again into object oriented representations.
So an example of XML, for example, we have Xlink which can actually parse the whole thing, parse
an XML file and then present it with a link like API so that you can perform searches on the link document.
Very useful.
Another example, a simpler example is numeric expressions.
So when you are writing source code or you’re working with Matlab or some computer algebra system and
you’re writing three plus four over five, somebody has to interpret that.
Somebody has to read this whole expression.
They have to figure out that four over five has to be computed before the addition to three and so on
and so forth.
Another example is regular expressions, which is kind of like a language within a language, effectively.
So regular expressions, once again, somebody has to parse them, they have to turn into the appropriate
state machine and then you apply that state machine whenever, for example, you want to apply or check
whether a particular textual expression actually corresponds to this regular expression.
So the whole overriding goal here is turning strings, turning text into object oriented constructs
based on a rather complicated process.
And that’s the kind of process that we’re going to not cover in any great depth, because once again,
the best way to learn about interpretation and all the rest of it is to read a book, or better yet,
take a computer science degree where one of the topics and I don’t promise this because so many universities
have stopped teaching compiler theory, but one of the topics should be called compiler theory, and
that’s where you learn all of the different approaches.
Another, another I suppose variation is go get a job with a company like Jetbrains like I did, and
that will teach you a lot about parsing, indexing and that sort of thing.
So the interpreter design pattern is a very broad design pattern, but generally it’s a component which
knows how to process structured text data, and it does so in two stages typically.
So first of all, it takes the text and it breaks it up, sort of splits it into separate lexical tokens.
And this is called Lexing.
And then the second part is the interpretation of the sequences of tokens, and that is called parsing.
Stop Play Play Play Start Play information alert