How to make a simple script parser?

I am trying to figure out how I can read a command from a script file, then perform the command in my program.

I am going to be given a text file that will look kind of like this:

read(A,’A.txt’)
read(B,’B.txt’)
read(C,’C.txt’)
R1=A+B
R2=A*B
R3=A+B*C
R4=(A+B)*C
R5=(A+B)*R4+A
write(R3,’out3.txt’)
write(R4,’out4.txt’)
write(R5,’out5.txt’)

(read is reading a file, the R's are expressions to evaluate, write is writing to a file)

and I need my program to read each line, perform the specified command, then move on to the next.

One suggestion I had was to remove all symbols, except for '.', then store the results in a stringstream. If I did that, then maybe I could make if statements that check the first word in the stringstream, and perform that action.

I just can't figure this out because I will not know the names of the input or the output files in the program and I'm not sure how to check each individual word in a string stream.

If anyone could help me with this I would be very appreciative.

you can iterate a string with [] and check each character. You can also search for specific characters. If you make the format friendly (consider, spaces or commas or other symbols between things?) you can break it up easier.

I mean you know file names are between ' ' symbols... you can look for ' as one part of your code.
Last edited on
It's usually painful to handle languages with no actual definition - especially when you're trying to define them in code without any plan.

The first step is to define your language's grammar. This will help you determine how sophisticated a solution is required, and it will help you translate the definition into code. I recommend posting the result here when you're done.
Last edited on
Ok, well this is what I have come up with so far:

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
36
37
38
39
40
41
42
43
44
45
46
ifstream Script("Script.txt");
	string line, word;
	ifstream files[100];
	int j = 0;
	int names[100];

	while(getline(Script, line))
	{
		if(line[0] == 'r' && line[1] == 'e' && line[2] == 'a' && line[3] == 'd')
			line = Format(line);
		else if (line[0] == 'w' && line[1] == 'r' && line[2] == 'i' && line[3] == 't' && line[4] == 'e')
			line = Format(line);

		stringstream ss(line);
		ss.seekg(0,ios::end);
		int size = ss.tellg();
		ss.seekg(0,ios::beg);


		string words[size];
		int i = 0;
		while (ss >> word)
		{
			words[i] = word;
			i++;
		}



		if(words[0] == "read")
		{

			string fileName = words[2];
			cout << "File name: " << words[2] << " j: " << j << endl;
			files[j].open(fileName.c_str());
			if(!files[j])
							cout << "FILE ERROR!" << endl;

			string test;
			while(getline(files[j], test)) //This part was just to make sure that it was opening the right
				cout << test << endl;//files

			++j;
		}

	}


This successfully opens the correct files and displays them. The part I am confused about now is how I would make it where I can designate a name to each file. So when I try to do a+b, it knows to use a,txt and b.txt.

Maybe if the expression is R1 = a + b I could just tell it to add ".txt" to a and b, then do the operation? Does that sound right, or is there a better way to do it?
It isn't about better ways to do a specific task. Its more about the whole design and approach... what exactly do you want your script language to support? If its just like 4 or 5 simple commands, yea that would work and its fine.
Topic archived. No new replies allowed.