I've got to write a code parser but am really stuck

I have to read a file, process each token and output to a new file.

Reading and writing to files isn't a problem, it's processing each token.

What i've done is read the file and add each token to a vector.

I tried using a for loop which checks the current token and looks at other tokens in the vector. The problem is, is the loop is near the end and it's trying to look at other token that don't exist, the program crashes.

Is there a way i can look at the tokens without using a for loop?

An example is when the current token is an INT. I need to look at the token after the identifier to see if it's a '(' which means it's a function or a ',' or ';' which means it's a variable

There other occasions when i need to look ahead but if it's at the end of the loop i can't look forward

here is what i have so far

#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <sstream>
#include <stdio.h>

using namespace std;

int main()
{
	std::vector <std::string> tokens;
	std::vector <std::string> kinds;
	std::vector <std::string> spellings;
	
	int lineNo;
	
	std::string line, spelling, kind;
	std::string GetFileName =  "test1.cml";

	//std::cout << "Enter a filename to scan: \n";
	
	//get user input
	//cin >> GetFileName;
	
	//open the filename
	ifstream myFile( GetFileName.c_str() );
	
	//if the file doesn't exist give an error message and close program
	if (! myFile)
	{
		std::cout << "Error opening output fle"  << endl;
		return -1;
	}
	
	/* get each line from the file and process the line
	 * and add to a vector so each line can processed further
	 */
	while( getline( myFile, line ) )
	{
		tokens.push_back(line);	
	}
	
	myFile.close();
	
	ofstream cmi("test1.cmi");
	
	//if the file doesn't exist give an error message and close program
	if (!cmi)
	{
		std::cout << "Error opening output fle"  << endl;
		return -1;
	}
	
	for(int a=0; a < tokens.size(); a++)
	{
		//cout << tokens.at(a) << endl;
		
		string temp = tokens.at(a);
				
		std::size_t pos;
		pos= temp.find(":");
						
		if(pos != string::npos)
		{			
			kind =  temp.substr(0, pos);
			spelling = temp.substr(pos+1);
		}
		
		//cmi << "KIND: " << kind << endl;
		//cmi << "SPELLING: " << spelling << endl;
		
		kinds.push_back(kind);
		spellings.push_back(spelling);
		
	}
	
for(int a=0, b=0; a < kinds.size(), b < spellings.size(); a++, b++)
{
	//while(!kinds.back())
	//{
		if(kinds.at(a) == "INT")
		{
			if(kinds.at(a+2) == "LPARN")
				cout << "%function" << spellings.at(b) << spellings.at(b+1) << endl;
			if(kinds.at(a+2) == "SEMI" || kinds.at(a+2) == "COMMA" || kinds.at(a+2) == "RPARN")
				cout << "%variable" << spellings.at(b) << spellings.at(b+1) << endl;
		}
		
		if(kinds.at(a) == "VOID" && kinds.at(a+2) == "LPARN")
		{
			cout << "%procedure" << spellings.at(b+1) << endl;
		}
		
		if(kinds.at(a) == "LBRACE")
		{
			cout << "%begin" << endl;
		}

		if(kinds.at(a) == "RBRACE")
		{
			cout << "%end" << endl;
		}
		
		if(kinds.at(a) == "IF")
		{
			cout << "%if" << endl;
		}
		
		if(kinds.at(a) == "RETURN")
		{
			cout << "%return ";
			if(kinds.at(a+1) == "NUMBER")
				cout << "%num" << spellings.at(a+1) << endl;
		}
		
		if(kinds.at(a) == "IDENTIFIER" || kinds.at(a+1) == "ASSIGN")
		{
			cout << "assign";
			// this is where i get an error that the program has terminated in an unusual way
			//if(kinds.at(a+2) == "LPARN" && kinds.at(a+3) == "RPARN")
				//cout << "%assign " << spellings.at(a) << "%call" << spellings.at(a+2) << spellings.at(a+3) << endl;

		}

	//}
}
	
	cmi.close();

	return 0;
}

when accesing at an index say
kind.at(n)
just make sure that n>=0 and n<kind.size()

e.g. check in lines 85, 87, 88, 91, 118.
I didn't checked it completely, do it yourself.

This article has been dead for over six months. Start a new discussion instead.