-1

How to write a simple interpreter for a simple language in C++ that gets a .txt file as input and runs that.

** Just for + - / * out functions **

Input (Source code) :

int a,b,c;
a=2;
b=4;
c=a+b;
out c;

Interpreter output :

6

How i can do this ? Any help or source code appreciated.

Sorry if u didn't get my question, My English is so bad. Just ignore the " Interpreter " for a sec . I want to write an console program in C++ that gets a txt file as source code and recognize the basic math and "out" command . is it possible in C++ or not ?

Emperor280
  • 97
  • 1
  • 2
  • 5
  • Are you allowed to use yacc and lex for this ? – Paul R Oct 09 '15 at 06:35
  • @Paul R I don't think so. – Emperor280 Oct 09 '15 at 07:06
  • There is not too many possible answers: there are specific math to solve this questions and the way to implement that is not wider than for any other question. How to remove this "Hold"? – Adrian Maire Oct 09 '15 at 07:16
  • If you don't want to reinvent the wheel: http://boost-spirit.com/ – hyde Oct 09 '15 at 07:16
  • Look into embeddable interpreters like [Lua](http://lua.org/) or [Guile](http://www.gnu.org/software/guile/). See also [this](http://stackoverflow.com/a/27554300/841108) & [that](http://stackoverflow.com/a/25317439/841108) & [that](http://stackoverflow.com/a/29816531/841108) and the references I'm giving there. – Basile Starynkevitch Oct 09 '15 at 07:17
  • @AdrianMaire The question is basically "how do I create interpreter in C++ for a language like this?", and I'd say that is definitely in the "too broad" category. There are just too many valid ways to do it, especially with language like C++. – hyde Oct 09 '15 at 07:22
  • 1
    @Emperor280 your question is off-topic here because it is not related to source code. And it would be off-topic on [Programmers](http://programmers.stackexchange.com/) because it is too broad and does not show any prior research – Basile Starynkevitch Oct 09 '15 at 07:22
  • @hyde: No, look my answer, the path is quite defined. You could also define which of the syntax to use, but I did not get so far. Supposing for example a LALR(1) you could even set a pseudo-algorithm. – Adrian Maire Oct 09 '15 at 07:24
  • @AdrianMaire: the bulk of any interpreter is not [parsing](https://en.wikipedia.org/wiki/Parsing), but interpreting, I.e. defining the [semantics](https://en.wikipedia.org/wiki/Semantics_%28computer_science%29) of the scripting language, then coding the interpreter. – Basile Starynkevitch Oct 09 '15 at 07:25
  • @AdrianMaire The question asks about C++, you answer doesn't really cover that. But perhaps it's the question, which should be edited to use [tag:pseudocode], or just not have "language" tag in it. – hyde Oct 09 '15 at 07:27
  • @hyde: probably you are right and the C++ tag should be removed or switched to pseudo-code. I think we should be more careful in tagging new users, that is very de-motivating. Usually an explanation/correction is more constructive. – Adrian Maire Oct 09 '15 at 07:37
  • *"is it possible in C++ or not ?"*, obviously it is possible... In about 100 different ways. If you want to do it yourself from scratch, work in small steps (as always in programming). You *could* start by writing a tokenizer, which recognizes your two keywords (`int` and `out`), numbers and operators (`=+-*/`). You could create a token class, and then have `std::vector`, where you put the tokens. Once you have code which can create such a vector (and give syntax/parse error message when parsing fails), you can start looking into what to do with the tokens... – hyde Oct 09 '15 at 09:46

1 Answers1

3

An interpreter is usually made in 3 steps:

Lexical Recognize specific simple shemes, like keyword, numbers, symbols, etc. The standard way is to define a regular expression for each lexer. There are well defined algorithm there outside to transform a reg-exp in a state machine so you may recognize any work in your input.

This is made by:

  • Create a state machine from each regexp
  • Join all state machines
  • Make the state machine deterministic
  • Make the state machine minimal.

Syntax In this part, you get the sequence of lexer as input and create a tree with them. Depending on the complexity of your language, there are different types: top->down or down->top. (speaking about LR, LL, LALR, etc.)

file
|...
|- c=a+b
|  |- a+b
      |- a
      |- +
      |- b
   |- =
   |-c

Semantic Go trough your tree and make the operations, so for example in the a+b, you get the a, the b and you sum them, then you return to the above node, and set the value to c.

Final note: Be careful to design a powerful error/warning mechanism from the starting: type of error, full description, line and char where the mistake is detected, level of the error/warning, etc.

Also, could be interesting to provide for each node the parsed input (string), the interpreted content (LEX_NUMBER) and the interpreted value (4).

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85