| | |
writing a code parser
![]() |
•
•
Join Date: Dec 2008
Posts: 57
Reputation:
Solved Threads: 0
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
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
C++ Syntax (Toggle Plain Text)
#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; }
![]() |
Similar Threads
- Problems writing code for my project (Python)
- Code Snippet: A simple interpreter called 'minibas' (C++)
- error while i am writing the code to file upload (ASP.NET)
- Code Snippet: Console calculator Part 2 : The Parser (C#)
- "simple" file parser i am making (Python)
- Interface for XML parser (C++)
- Beware writing code without a plan (C)
- code tags (DaniWeb Community Feedback)
Other Threads in the C++ Forum
- Previous Thread: C++ game?
- Next Thread: How to store string words into individual variable?
Views: 425 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment beginner binary c++ c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error exception file files filestream forms fstream function functions game givemetehcodez graph graphics gui helpwithhomework homework iamthwee input int lazy link linked-list linker list loop looping loops math matrix member memory newbie number object objects opengl output parameter path pointer pointers problem program programming project python random read reading recursion recursive reference search sort spoonfeeding string strings struct student studio template templates text time tree url variable vc++ vector video visual win32 window windows winsock wxwidgets





