fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. #include <istream>
  5. #include <string>
  6. #include <cstring>
  7. #include <vector>
  8. #include <map>
  9. #include <cstdlib>
  10. #include <limits>
  11. #include <cerrno>
  12. #include <regex>
  13.  
  14. using namespace std;
  15.  
  16. class DefList{
  17. public:
  18. int Definition_Count;
  19. vector<string> DefSymbolList;
  20. vector<int> Definition_Address;
  21. };
  22.  
  23. class ProgramText{
  24. public:
  25. int ProgramText_Count;
  26. vector<string> IARE_List;
  27. vector<string> Instruction_Address;
  28. };
  29.  
  30.  
  31. void parseFile(char* argv[]); // to read to input file.
  32. bool is_number(string& src); // to check if the string is number.
  33. bool is_IARE(string& iare); // to check if the string is IARE type.
  34. bool is_symbol(string& symbol); // to check if the string is symbol.
  35. void syntaxError(int position, int errorCode);
  36. bool is_multipleDef(map<string , int>& FreqSymDef , string& defSym);
  37.  
  38. /* parse Defition Symbol, Use List, Program Text. Set flag: valid = 1; fail = 0 */
  39. int parseDefList(vector<DefList>& Vector_DefList) ;
  40. int parseUseList(vector< vector<string> >& Vector_UseList);
  41. int parseProgramText(int& num_ProgTxt , vector<ProgramText>& Vector_Program_Text);
  42.  
  43. void SymbolTable(vector<DefList>& Vector_DefList, vector<int>& baseAdding);
  44.  
  45.  
  46.  
  47.  
  48. /* Containers */
  49. // to store all line;
  50. vector<string> inputLine;
  51.  
  52.  
  53. // to store all token
  54. vector<string> tokenVector;
  55. // to record positions of each token.
  56. vector<int> rowVector; // to store the number of row on each token
  57. vector<int> columnVector; // to store the number of vector on each token
  58. //
  59. map<string , int> FreqSymDef; // frequency of defining a symbol <symbol token , frequency>
  60. map<string , int> Count_Sym_Module; // log the position where the symbol is <symbol token , modulePos>
  61. map<string , int> DictSymAddress; // Dictionary for {Symbol : Address}
  62.  
  63.  
  64.  
  65.  
  66. void parseFile(char* argv[]);
  67.  
  68. // Index Area
  69. int token_index = 0; // token index when parsing
  70. int num_token; // numbers of token in the file
  71.  
  72. /* Main Function */
  73. int main (int argc , char* argv[]){
  74.  
  75. if (argc != 2){
  76. cout << "The format of input file is invalid." << endl;
  77. exit(0);
  78.  
  79. }
  80.  
  81. parseFile(argv);
  82.  
  83. return 0;
  84. }
  85.  
  86. /* Read File and Parse it */
  87. void parseFile(char* argv[]){
  88. // Stream the input file
  89. string text;
  90. ifstream inputFile (argv[1], ios::binary);
  91. if (inputFile.is_open()) {
  92. text.assign((std::istreambuf_iterator<char>(inputFile)) , (std::istreambuf_iterator<char>()));
  93.  
  94. vector<int> lengthLine; // log the length of each line
  95.  
  96.  
  97. /* Parse the token */
  98. /* Log the position (row , column) */
  99. int row = 0; // Initialization
  100. int column = 0;
  101.  
  102. string token = "";
  103. for (int i = 0 ; i < text.size() ; i++){
  104. row += 1;
  105.  
  106. if (text[i] != ' ' || text[i] != '\t' || text[i] != '\n'){
  107.  
  108. if (text[i + 1] == ' ' || text[i + 1] == '\t' || text[i + 1] == '\n'){
  109. tokenVector.push_back(token);
  110. token = "";
  111. }
  112. column += 1;
  113. token += text[i];
  114. }
  115. else if (text[i] == ' '){
  116. column += 1;
  117. token = "";
  118. }
  119. else if (text[i] == '\t'){
  120. column +=1;
  121. token = "";
  122. }
  123. else if (text[i] == '\n'){
  124. column = 0;
  125. token = "";
  126. }
  127. }
  128.  
  129.  
  130.  
  131. for (int i = 0 ; i < tokenVector.size() ; i++){
  132. cout<<tokenVector[i]<<endl;
  133. }
  134.  
  135.  
  136.  
  137.  
  138.  
  139. /*
  140.  
  141. // test read all line in the inputFile:
  142.  
  143.   cout<<inputLine.size()<<endl; // number of line
  144.  
  145.   for (int i = 0 ; i < inputLine.size() ; i++){
  146.   for (int j = 0 ; j < inputLine[i].size() ; j++){
  147.   cout<<"line: "<<i+1<<" offset: "<<j+1<<" token: "<<inputLine[i][j]<<endl;
  148.   }
  149.   }
  150.  
  151. */
  152.  
  153.  
  154.  
  155.  
  156. /*
  157.   while ( getline(inputFile, line)) {
  158.  
  159.   row += 1;
  160.   lengthLine.push_back(line.length());
  161.   stringstream input(line);
  162.  
  163.   // Start to Parse the Token
  164.   int column = -1; // Initialization: be ware of the situation like input-3
  165.   for (string token; input >> token;){
  166.   column = line.find(token, column + 1); // find the position of the token in the line
  167.   tokenVector.push_back(token); // store up the token
  168.   // Record the position
  169.   rowVector.push_back(row);
  170.   columnVector.push_back(column + 1); // reason: the end of the parsed token.
  171.   }
  172.   }
  173. */
  174.  
  175. /*
  176.  
  177.   // log the last position in the input file
  178.   int lastRow = row;
  179.   int lastColumn = lengthLine.back() + 1;
  180.   rowVector.push_back(lastRow);
  181.   columnVector.push_back(lastColumn);
  182. */
  183.  
  184. inputFile.close();
  185. }
  186. else {
  187. cout << "Failed to open the file." << endl;
  188. }
  189.  
  190. /*
  191. // test parsing
  192.   cout<<tokenVector.size()<<endl;
  193.   cout<<columnVector.size()<<endl;
  194.   cout<<rowVector.size()<<endl;
  195.  
  196.   for (int i = 0 ; i < tokenVector.size() ; i++){
  197.   cout<<tokenVector[i]<<":"<<rowVector[i]<<":"<<columnVector[i]<<endl;
  198.   }
  199. // end
  200. */
  201.  
  202. }
Success #stdin #stdout 0s 4356KB
stdin
Standard input is empty
stdout
The format of input file is invalid.