开发者

Creating a language interpreter

开发者 https://www.devze.com 2022-12-22 16:16 出处:网络
I am trying to understand how a language interpreter works. Can you guys point me the general lines on how an interpreter works?

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 开发者_Python百科x, but these are symbols that does not exist.

If you guys can give me the direction...

Thanks for any help.


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/


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.


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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消