writing a code parser

Reply

Join Date: Dec 2008
Posts: 57
Reputation: AdRock is an unknown quantity at this point 
Solved Threads: 0
AdRock AdRock is offline Offline
Junior Poster in Training

writing a code parser

 
0
  #1
Nov 16th, 2009
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
  1. #include <fstream>
  2. #include <iostream>
  3. #include <vector>
  4. #include <cstring>
  5. #include <string>
  6. #include <sstream>
  7. #include <stdio.h>
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13. std::vector <std::string> tokens;
  14. std::vector <std::string> kinds;
  15. std::vector <std::string> spellings;
  16.  
  17. int lineNo;
  18.  
  19. std::string line, spelling, kind;
  20. std::string GetFileName = "test1.cml";
  21.  
  22. //std::cout << "Enter a filename to scan: \n";
  23.  
  24. //get user input
  25. //cin >> GetFileName;
  26.  
  27. //open the filename
  28. ifstream myFile( GetFileName.c_str() );
  29.  
  30. //if the file doesn't exist give an error message and close program
  31. if (! myFile)
  32. {
  33. std::cout << "Error opening output fle" << endl;
  34. return -1;
  35. }
  36.  
  37. /* get each line from the file and process the line
  38. * and add to a vector so each line can processed further
  39. */
  40. while( getline( myFile, line ) )
  41. {
  42. tokens.push_back(line);
  43. }
  44.  
  45. myFile.close();
  46.  
  47. ofstream cmi("test1.cmi");
  48.  
  49. //if the file doesn't exist give an error message and close program
  50. if (!cmi)
  51. {
  52. std::cout << "Error opening output fle" << endl;
  53. return -1;
  54. }
  55.  
  56. for(int a=0; a < tokens.size(); a++)
  57. {
  58. //cout << tokens.at(a) << endl;
  59.  
  60. string temp = tokens.at(a);
  61.  
  62. std::size_t pos;
  63. pos= temp.find(":");
  64.  
  65. if(pos != string::npos)
  66. {
  67. kind = temp.substr(0, pos);
  68. spelling = temp.substr(pos+1);
  69. }
  70.  
  71. //cmi << "KIND: " << kind << endl;
  72. //cmi << "SPELLING: " << spelling << endl;
  73.  
  74. kinds.push_back(kind);
  75. spellings.push_back(spelling);
  76.  
  77. }
  78.  
  79. for(int a=0, b=0; a < kinds.size(), b < spellings.size(); a++, b++)
  80. {
  81. //while(!kinds.back())
  82. //{
  83. if(kinds.at(a) == "INT")
  84. {
  85. if(kinds.at(a+2) == "LPARN")
  86. cout << "%function" << spellings.at(b) << spellings.at(b+1) << endl;
  87. if(kinds.at(a+2) == "SEMI" || kinds.at(a+2) == "COMMA" || kinds.at(a+2) == "RPARN")
  88. cout << "%variable" << spellings.at(b) << spellings.at(b+1) << endl;
  89. }
  90.  
  91. if(kinds.at(a) == "VOID" && kinds.at(a+2) == "LPARN")
  92. {
  93. cout << "%procedure" << spellings.at(b+1) << endl;
  94. }
  95.  
  96. if(kinds.at(a) == "LBRACE")
  97. {
  98. cout << "%begin" << endl;
  99. }
  100.  
  101. if(kinds.at(a) == "RBRACE")
  102. {
  103. cout << "%end" << endl;
  104. }
  105.  
  106. if(kinds.at(a) == "IF")
  107. {
  108. cout << "%if" << endl;
  109. }
  110.  
  111. if(kinds.at(a) == "RETURN")
  112. {
  113. cout << "%return ";
  114. if(kinds.at(a+1) == "NUMBER")
  115. cout << "%num" << spellings.at(a+1) << endl;
  116. }
  117.  
  118. if(kinds.at(a) == "IDENTIFIER" || kinds.at(a+1) == "ASSIGN")
  119. {
  120. cout << "assign";
  121. // this is where i get an error that the program has terminated in an unusual way
  122. //if(kinds.at(a+2) == "LPARN" && kinds.at(a+3) == "RPARN")
  123. //cout << "%assign " << spellings.at(a) << "%call" << spellings.at(a+2) << spellings.at(a+3) << endl;
  124.  
  125. }
  126.  
  127. //}
  128. }
  129.  
  130. cmi.close();
  131.  
  132. return 0;
  133. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 358
Reputation: dkalita will become famous soon enough dkalita will become famous soon enough 
Solved Threads: 56
dkalita's Avatar
dkalita dkalita is offline Offline
Posting Whiz
 
0
  #2
Nov 17th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 425 | Replies: 1
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC