Using c++ to run a code

I need help on writing a C++ program that reads another "made up" program from a text file.

For example:
1
2
3
4
5
a=10 - 2;
b = 20 - 5 * a /  4;
print  b a;
a=b;
print a:


I'm just really confused on how to start it, especially reading it line by line (if that's how you do it). Basically, I'm just totally confused.

Can anyone give me some kind of algorithm on how it should work? I'm sure stacks are involved and stuff like that.
Do you want to convert the lines into a C++ compilable words?

If yes, it means you need a specific converter firstly. You can use C++ to first implement the converter to translate the lines into C++. What I can think is the converter itself is the main issue. The converter may be very specific because of the input file.


b2ee
Last edited on
So your trying to make your own interpreted language?
First you need to design the syntax to follow(keywords, symbols, arithmetic, etc...), then you have your translator (c++ program) read the file and because you dictate the syntax, you will know what type of information to expect.

Do you know how to read text from a file in c++?
I guess..

I just have to write a C++ program that reads an user program in "made up coding language" from a file and execute it.


These are some of the rules of the "made up coding language":

-The language is case sensitive.
-It has two kinds of statements: an assignment statement and a print statement.
-A statement is terminated by a semicolon.
-There is one reserved word “print”.
-You can define variables, reserved words can not be used as variable names, otherwise, the variable name is a combination of letters(a ..z, A.. Z). For example, x, aBc, Klasses are valid, but A1, print are not.
-The assignment statement takes the form “variable = expression”, it assigns the result of the expression to the variable.
-The expression contains integer numbers, variables and binary operators +(add), –(subtract), *(multiply), and /(quotient). For example, 10+3/59, 2*a/pbs + 10 -30 are valid expressions, 10 + + b – c is not.
-The standard rules on operator precedence apply.
-Blanks can appear anywhere just like the way in C++.
-An assignment statement such as xy=abc+3/50 is valid, so is xy = abc + 3 / 50.
-A print statement takes the form “print variable(s)”, it displays the value(s) of the variables on the screen.
-The variable(s) can be one variable, or more than one variable separated by blanks. For example, print a; print a b cbs; are valid print statements.

-For assignment statement, the expression to the right of the equal sign is evaluated and the result is assigned to the variable to the left of the equal sign; for print statement, the name of the variable and the value of it with a equal sign in between will be displayed, one pair per line.

-Your program will assign the value 2 to variable A, assign the value 0(A – 2) to variable c, and stops while trying to evaluate A/c (2/0). An output like “RUN TIME ERROR: divide by 0” should appear on the screen and your program will stop and the rest of the user program is ignored. If you want to be more user friendly, you can indicate where but that is not mandatory. The use program died because it tries to divide by zero. If a variable is referenced without having previously assigned a value, a default value of zero shall be used.



I'm just struggling to start and get an idea of how this is going to work.
@Mathhead, yes I do.
Start here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[]) {
	if( argc < 2 ) {
		cerr << "Missing argument. What file are you trying to run?\n";
		return 1;
	}
	
	ifstream in( argv[1] ); //open a file stream
	char c;
	int line = 1;
	
	while(true) {
		in.get(c); //read the file char by char
		if( in.eof() ) //read until end-of-file
			break;
		if( !in.good() ) {
			cerr << "File error on line " << line << '\n';
			return 10;
		}
		
		//Do Stuff Here!
		//for testing we will print the char's out just to make sure we are reading the file correctly 
		cout << c;

		if( c == '\n' )
			line++; //increase line count
	}
	
	in.close();
	return 0;
}
Last edited on
I would read by character, but you could just as easily read the file by word or by line.
Topic archived. No new replies allowed.