5

I am trying to understand how a language interpreter works. Can you guys point me the general lines on how an interpreter works?

I mean, suppose I have some lines written like this

10  x = 200;
20  for r = x to 1000 step 1
25  z = r + 32;
30  print z;
40  next r;
50  end;

what's the best way to build an interpreter that could run something like that?

Having a large matrix containing all functions allowed and searching for a match? The first line, for example: it is assigning 200 to a variable x, but these are symbols that does not exist.

If you guys can give me the direction...

Thanks for any help.

Duck
  • 34,902
  • 47
  • 248
  • 470
  • 2
    Writing an interpreter (or a compiler) for that sake is something that's really complicated. I'm no expert on the subject but in my university they have a course that's named compiler-technique and several of my friends attend to that course. If I remember correctly it consists of 4 steps, first was writing some code-patterns out (like you already have), then they use a program to look at it and find patterns and they build tokens that can be used to construct a tree out of the code. Then they build the compiler. As said I don't know much about it, but I've been told that it's not easy at all. – Alxandr Mar 10 '10 at 00:10
  • thanks.. that's the point. I am trying to get a general idea. – Duck Mar 10 '10 at 00:12
  • 1
    The canonical question for compiler and interpreter resources is http://stackoverflow.com/questions/1669/learning-to-write-a-compiler. And there are some resources listed there (like the Crenshaw tutorial) which are pitched at a very simple level. – dmckee --- ex-moderator kitten Mar 10 '10 at 00:23
  • 1
    Many duplicates, some are in the Related panel to the right; http://stackoverflow.com/questions/1669/, http://stackoverflow.com/questions/256629/, http://stackoverflow.com/questions/294852/, http://stackoverflow.com/questions/1255820/ – Dour High Arch Mar 10 '10 at 01:17

6 Answers6

3

Compiler creation is a complex topic (an interpreter can be seen as a special compiler).

You have to first parse it - try to understand the syntax and then create some internal representation (abstract syntax tree) and then create some execution logic.

Wikpedia suggests http://mcs.une.edu.au/~comp319/

johannes
  • 15,807
  • 3
  • 44
  • 57
2

I know this is an old thread but most of the related questions are marked as duplicate or closed. So, here are my two cents.

I am surprised no one has mentioned xtext yet. It is available as Eclipse plugin and IntelliJ plugin. It provides not just the parser like ANTLR but the whole pipeline (including parser, linker, typechecker, compiler) needed for a DSL. You can check it's source code on Github for understanding how, an interpreter/compiler works.

Mann
  • 307
  • 2
  • 14
1

you can find an open source 'Gold Parsing System' at http://goldparser.org. :)

there are some explained concepts on their site too where you can learn some rudimentary basics of the process.

Shawn Kovac
  • 1,425
  • 15
  • 17
1

Perhaps you are talking about creating a DSL. You might find this helpful (if you are ok with spending $$)

http://gilesbowkett.blogspot.com/2010/03/create-your-own-programming-language.html

rmk
  • 4,395
  • 3
  • 27
  • 32
1

Learn about tools such as lex/flex and yacc/bison. These are most popular tools for building compilers in open software world. Many well known open source programs are written using them (including PHP, gcc, doxygen). You'll find a lot of free books and tutorials. They not only show how to use lex and yacc tools, but also explain general ideas behind compilers.

rici
  • 234,347
  • 28
  • 237
  • 341
mip
  • 8,355
  • 6
  • 53
  • 72
  • 1
    Don't forget ANTLR (http://www.antlr.org/) a tat more sophisticated than lex or bison but quite relevant. – mjv Mar 10 '10 at 00:21
  • Yes ANTLR is worth mentioning. – mip Mar 10 '10 at 00:23
  • I think lex/yacc is much easier for a beginner. Plus, shift-reduce parsing is a little easier to understand. The OP wants to learn, so maybe lex/yacc is much better for that purpose; I have used both and find that lex/yacc is much better as a beginner. – rmk Mar 10 '10 at 18:30
1

I'm interested in learning more about this as well. I found Douglas Crockford's JavaScript parser interesting, though from what I understand he's using a different method than is typical for parsing languages. It's not the full picture for interpreting and compiling, but I found it helpful to see some actual parsing implementation and the resulting restructuring of the code.

Bob
  • 7,851
  • 5
  • 36
  • 48