Question:
Making a interpreter programming language?
1970-01-01 00:00:00 UTC
Making a interpreter programming language?
Seven answers:
?
2016-11-05 09:46:48 UTC
Dragon Programming Language
?
2016-03-12 02:21:51 UTC
Designing and implementing a programming language is not inherently difficult. You will need to learn how to do lexical analysis, parsing and code generation. If your language is fairly simple, this is an easy project. If your langauge is very complex, this is a very big project (probably bigger than one person in a reasonable amount of time). I suggest that you choose an established high-level language as your target, rather than a machine language. In other words, the output of your compiler should be C, C++ or Java code. You should pick the one that most closely fits your goals for the runtime design of your language. For example, if your language will have garbage collection, choose Java which already has implemented garbage collection for you. Good luck! I am looking forward to your more detailed questions as you dig deeper into this project.
Jonathan
2013-07-18 15:45:37 UTC
No, you should NOT use ANTLR, Lex, or Yacc.



You need to start with simple, recursive descent parsers and learn what is meant by left-recursive and right-recursive productions and how to convert from left-recursive form to right-recursive form (which you definitely want to use with recursive descent parsers.)



Recursive descent parsers are trivial to write (you simply write one subroutine for each production) once you have the right-recursive productions written down.



As I told you earlier, the FIRST THING you should do is to write an expression analyzer -- one that can accept an algebraic expression, parse it, and execute it (by interpreting it.) That is step #1 for ANYONE learning about parsing, compiling, etc. It is LEVEL 1. You need to get to LEVEL 1 first. By then, you will learn a lot and that will make your questions much more clear and precise and you will then have moved in the right direction and have some intuition about where next to go.



Learn about productions, recursive descent parsing, and expression analysis first. I've a very old page, but perhaps still useful, to this effect. See 1st link below. To see a simple, completed expression parser and execution engine for an algebraic expression calculator, see the 2nd link below: It includes a trial run of a couple of expressions, too.
2013-07-18 13:56:51 UTC
The first edition "Red" Dragon is probably a better place to start. Your choices will depend on just what you want to achieve. Just about any language can be used from JavaScript upwards. If you are purely interested in the idea as a concept (as opposed to some practical purpose at the end) then Lisp/Scheme is probably a good choice but it does not really matter to begin with. You should start with very basic steps and build on those once you have understood and mastered them.



http://www.codeproject.com/Articles/345888/How-to-write-a-simple-interpreter-in-JavaScript

http://rpal.sourceforge.net/oneday.html

http://www.rubyinside.com/writing-an-interpreter-in-15-minutes-with-ruby-1825.html

http://supportweb.cs.bham.ac.uk/docs/tutorials/docsystem/build/tutorials/antlr/antlr.html



Etc.
godfatherofsoul
2013-07-18 13:46:26 UTC
I think I responded to your other post. Really, the only different between interpreted and compiled languages is when you do you compilation. And, with scripted languages like Lua, you can still precompile your code.



As for tools, I still think the first thing you need to understand is assembly language (assuming you know enough C++ or Java to write your compiler) and how machine code is generated from it. Once you understand assembly, then take a look and grammars and what it takes to write one. Look for the book Concepts of Programming Languages by Sebesta which has chapters that are a good overview of how creating a grammar works. Once you have one, then you can worry about the analyzing tools to build your compiler.



I think you understand the basics. A compiler is a program that parses your language based on the grammar you define, then spits out binary code based on the semantics of your program.
2013-07-18 13:34:20 UTC
What you want is a scripting language. I highly suggest LUA because it works very well for C++ and there is a wrapper for C#. I highly suggest using C++ for games it has access to the DirectX libraries and is not managed like C#.
Brendan
2013-07-18 15:04:00 UTC
Hello Michael,



Like you I also set out to create my own language from a very young age; however I set out to write a compiler. The only thing that differs is the back end.



I think that C# or Java would be the best choice for you to write your interpreter. I would avoid C\C++ unless you are already comfortable with the language. I believe in writing all my code by hand so I do not use tools like Lex and Yacc, besides if it is your first time you should write it by hand any way it is a good learning experience.



As you know the first and easiest thing to do is write a lexer (also called a scanner). Next is the parser and AST. If you are writing it by hand you should write a recursive decent parser first and then perhaps a shift reduce parser later if you want more speed. After that comes the symbol table and semantic analysis. Then comes the back end which in your case would be the Interpreter itself. One other thing, make sure you have good error handling and descriptive error messages.



I started out with the book Writing Compilers and Interpreters An Applied Approach Using C++ by Ronald Mak. There is also a new version of the book in Java. It shows you how to write a both a compiler and interpreter for the Pascal programming language.



I also used several online sources. This site (http://lambda.uta.edu/cse5317/notes/node1.html) was very helpful to me. Here (http://blogs.msdn.com/b/jmstall/archive/2005/02/06/368192.aspx?Redirected=true) is another helpful source. It is a C# compiler written in C#. I’m sure you have an idea of how the language syntax will be, but it is a good idea to write it out in BNF or EBNF before you start writing the parser. I have written a simple calculator in Java that has a small lexer and parser if you wish to look at it as an example, or you have any questions feel free to send me an email at shockna1@yahoo.com.



I’m sure you can do it if you are really determined. Good luck!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...