Designing a small object-oriented interpreter

I’m building a small interpreter for a language I created.

The grammar, lexer and syntax analyzer are already done.

What’s confusing me is the interpreter part. I know interpreter pattern, but it seems too much verbose (more than it needs to be), I don’t feel it’s the right approach.

What are other alternatives I have to design my interpreter? (it’s only the interpreter, not the parser).

The language that will be interpreted is a very small imperative language with if, while, attribution statement, input statement and output statement.

The language I’m using to create the interpreter is object-oriented, so I need an object-oriented approach.

I won’t use any framework or similar, it’s from scratch.


If you used a parser generator to implement parsing, usually it’s possible to associate code actions to grammar rules. These code actions also receive the values bound to the lexical tokens and the values returned by other rules referenced.

Typically this is used to construct an abstract syntax tree (AST) for the code being parsed.

You then can descend along the AST and evaluate its nodes.
This can also be done in an OO fashion, like for example using OO classes for the nodes, each with an “evaluate” method, or using a Visitor pattern, where again you can use OOP as it suits you.

Note that you can use a similar approach also if you are writing your own parser.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *