Writing an Interpreter – Programming Languages


So let’s do it! We’re just going to get started writing an interpreter together. I’m going to write the first half of it, and for this quiz, you’re going to finish it off. We’re going to write a recursive procedure called interpret that’s going to walk over abstract syntax trees and figure out what they mean. What figure out what they mean is, calls the graphics library to render a webpage. That’s the meaning of HTML. The reason why we’re operating on trees instead of just 1, this is mostly just how I’m labeling it. But remember, that HTML can be a collection of elements rather than just 1. For example, you could write out a webpage that said, “Hello, Friend”, and it would be a list of 2 elements. So I want to make sure that we’re prepared to take a list instead of just a single thing. We’ll immediately dispense with that however by considering them one at a time. So now, for example, we’ll just do the “Hello”. Remember for us though, it would look something like this. We’ve decided to represent our abstract syntax trees as tuples. So I’m going to need to pull out the 0th element of this tree to see what sort of thing it is. So I’m going to pull out the nodetype. In our running example, it’s word-element, but it could be Javascript element, tag element–bunch of possibilities. If it is a word element, I know what to do. I’m going to call our graphics library and say print out a word, and the word that I want you to print out is the second child of the tree. So in this example, it’s “Hello,”. Another possibility is that someone has given us a tag element– something like this–bold tag begins, some HTML in the center, bold tag ends. Well, we went to all the work to storing this information in our parser, so let’s get it out now. The name of the tag we’re entering is the first part of the abstract syntax tree. In this strong text example, it’s just a b. There may well be some number of tag arguments. For the bold tag, no arguments, but for something like the anchor tag, there might be more. Then there’s a list of HTML elements that we’ll have to interpret later– strong text, in this example, and then there’s the closing tag name. Here’s what I want you to do for the quiz– 2 different things. I want you to check that the tags match and interpret the subtree. If the trees don’t match, use graphics.warning to make that really obvious. Otherwise, interpret the subtree. Hint: call interpret recursively.

2 thoughts on “Writing an Interpreter – Programming Languages

  1. do you reinvent basic from 1964 the second time? if you can recommend me something like pcbasic or bwbasic, it will be appreciated. i wrote reviews about both of 'em on sourceforge, but using a different name. shortly
    pcbasic works on 64bit but is too slow
    bwbasic is faster but crashes on 64bit

Leave a Reply

Your email address will not be published. Required fields are marked *