Simple Interpreter

Pages: 123
Hey guys,

Does anyone have an example of an interpreter (pure C/C++) which can work out some basic maths and print text/numbers?

Thanks,
James
You mean a C interpreter?
Well, there's bc (www.gnu.org/software/bc/), which has a C-esque syntax and arbitrary precision arithmetic.
I don't know if it can do text operations, though.
No, more a Toy language? Only simple code though.

Read File
Store variables
Calculate maths
Basic functions: Print(), Exit(), Sleep() or whatever
Uh... I'm not sure I'm following. Do you want C, or a toy language? You can't have both.

If you want C, I just found this:
http://root.cern.ch/drupal/content/cint

If you want a language you can play around with, there's plenty. The first one that comes to mind is Python.
A language written in C/C++. Custom interpreter.
closed account (S6k9GNh0)
You need to explain what you want more specifically. From what I see, your wanting an interpreted language made in C/C++ (which seems to be irrelevant to what to what you want) which can perform can perform tasks similar to that of C++ and C.

Python, Perl, and Mathematica come to mind. Unfortunately, this is a C/++ forum concerning C++ only and as a result, I will not provide examples of this code.

NOTE: Mathematica is a bit...*different* but it's made in a C and technically does what you want....

NOTE 2: I'm actually not sure what Python is made out of... I can't seem to find any decent information about it.
Last edited on
I'm looking for a simple script which interprets a file. A file may look like this:
1
2
hInput = Input("Please enter your name");
Print("Your name is " + hInput);

I'm not looking for other languages. I'm looking for a way to parse my own language in C++.
Oh, so you want to parse your language.

Well, that's easy. Google Yacc or Bison. Bison has some features Yacc lacks, like generating a reentrant (aka pure) parser, and a few other things, but Yacc is public domain, if that's of interest to you.

PS: By the way, normally when it takes someone four posts to correctly convey something as simple as this, it's because there's something very wrong with the way it was being expressed. This is one such case.
Last edited on
Is there any examples without Yacc/Bison.
Well, there's Boost's Spirit, but that's probably not any simpler.
Otherwise, you'll just have to write the parser yourself.
I saw Boost's Spirit, it's a bit complex.

So there are no examples on how to open a file, parse and run commands?
The problem is not that it's not possible to "open a file, parse, and run commands". The problem is that you want that file to use your own syntax. If the file was, say, a Perl script, you could solve the problem by just embedding Perl and using the interpreter to run the script. But you want the script to be in your syntax, so you have few alternatives:
1. Use a Yacc/Bison-generated parser.
2. Use Boost Spirit.
3. Write your own parser.
I would strongly suggest the first option. You'll know for sure that there are no bugs in the parser and will be able to concentrate on different aspects of the interpreter. If you follow the tutorial in the Bison manual, you'll be able to write a short expression parser program after about an hour. It's an investment you won't regret.
I'll take a look into it then. Thanks guys!
So I took a look into Yacc and BISON however, I'd still prefer to find something which is pure C++.
Bison can generate C++ parsers (that is, written in C++), and their C parsers will compile on C++ without problems other than one or two harmless warnings.
Boost::spirit is simpler, but it has a steep learning curve and requires knowledge of advanced programming techniques that the average programmer will not know.

But there are plenty of examples online of simpler parsers (such as a caclulator) written in Boost::spirit.

You can easily write one in C or (more easily in) C++. It would be limited to handling your exact syntax.

Google around "recursive descent". For example:
http://en.wikipedia.org/wiki/Recursive_descent_parser

This is essentially how Yacc and Bison handle language construction, but it works in time with your code instead of writing a compiler...


There are other options like embedding a Tcl interpreter ( http://wiki.tcl.tk/ ) or a Scheme interpreter ( http://plt-scheme.org/ -- see the MzScheme interpreter). Both are easy to use and pretty small.

Python is also a good choice ( http://www.python.org/ ) -- it was written in C.

Sorry, I'll get to the toy language thread probably after you need it... :-\
Last edited on
Duoas, do you have any examples of a written one?

Kbw, that's an awesome example for maths, I like how it handles variables. I guess it shouldn't be too hard to write functions: sin, cos, tan etc?
Extending it to support some fixed functions like the trig functions should be straight forward. User defined functions is straight forward too, but a different matter.
Pages: 123