Advertisement
canezzy

lexicalAnalysis.cpp

Mar 27th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.32 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4.  
  5. #include "common.h"
  6. #include "LexicalAnalysis.h"
  7. #include "Token.h"
  8.  
  9. using namespace std;
  10.  
  11.  
  12. void LexicalAnalysis::initialize()
  13. {
  14.     programBufferPosition = 0;
  15.     fsm.initStateMachine();
  16. }
  17.  
  18.  
  19. bool LexicalAnalysis::Do()
  20. {
  21.     while(1)
  22.     {
  23.         Token token = getNextTokenLex();
  24.         switch (token.getType())
  25.         {
  26.             case T_ERROR:
  27.                 errorToken = token;tokenList.push_back(token);
  28.                 return false;
  29.             case T_END_OF_FILE:
  30.                 tokenList.push_back(token);
  31.                 return true;
  32.             case T_WHITE_SPACE:
  33.                 continue;
  34.             default:
  35.                 tokenList.push_back(token);
  36.                 break;
  37.         }
  38.     }
  39. }
  40.  
  41.  
  42. bool LexicalAnalysis::readInputFile(string fileName)
  43. {
  44.     inputFile.open(fileName, ios_base::binary);
  45.  
  46.     if (!inputFile)
  47.         return false;
  48.    
  49.     inputFile.seekg(0, inputFile.end);
  50.     int length = (int)inputFile.tellg();
  51.     inputFile.seekg (0, inputFile.beg);
  52.     programBuffer.resize(length);
  53.     if (length > 0)
  54.         inputFile.read(&programBuffer.front(),length);
  55.     inputFile.close();
  56.     return true;
  57. }
  58.  
  59.  
  60. Token LexicalAnalysis::getNextTokenLex()
  61. {
  62.     int currentState = START_STATE;
  63.     int nextState = 0;
  64.     int lastFiniteState = 0;
  65.    
  66.     // position in stream
  67.     int counter = 0;
  68.     int lastLetterPos = programBufferPosition;
  69.  
  70.     Token token;
  71.  
  72.     while(true)
  73.     {
  74.         char letter;
  75.         unsigned int letterIndex = programBufferPosition + counter;
  76.    
  77.         if (letterIndex < programBuffer.size())
  78.         {
  79.             letter = programBuffer[letterIndex];
  80.         }
  81.         else
  82.         {
  83.             // we have reached the end of input file, force the search letter to invalid value
  84.             letter = -1;
  85.             if (programBufferPosition >= programBuffer.size())
  86.             {
  87.                 // if we have reached end of file and printed out the last correct token
  88.                 // create EOF token and exit
  89.                 token.makeEofToken();
  90.                 return token;
  91.             }
  92.         }
  93.        
  94.         nextState = this->fsm.getNextState(currentState, letter);
  95.         counter ++;
  96.  
  97.         if (nextState > IDLE_STATE)
  98.         {
  99.             // change the current state
  100.             currentState = nextState;
  101.  
  102.             if (nextState == START_STATE)
  103.                 throw runtime_error("\nException: Infinite state detected! There is something very wrong with the code !\n");
  104.  
  105.             // remember last finite state
  106.             lastFiniteState = nextState;
  107.             lastLetterPos = programBufferPosition + counter;
  108.         }
  109.         else if (nextState == INVALID_STATE)
  110.         {
  111.             // eof character read, generate token defined with last finite state
  112.             if (lastFiniteState != IDLE_STATE)
  113.             {
  114.                 // token recognized, make token
  115.                 token.makeToken(programBufferPosition, lastLetterPos, programBuffer, lastFiniteState);
  116.                 programBufferPosition = lastLetterPos;
  117.                 return token;
  118.             }
  119.             else
  120.             {
  121.                 // error occurred, create error token
  122.                 token.makeErrorToken(programBufferPosition + counter - 1, programBuffer);
  123.                 programBufferPosition = programBufferPosition + counter - 1;
  124.                 return token;
  125.             }
  126.         }
  127.         else
  128.         {
  129.             // final state reached, state machine is in IDLE state
  130.             // calculate the number of characters needed for the recognized token
  131.             int len = lastLetterPos - programBufferPosition;
  132.  
  133.             // create the token
  134.             if (len > 0)
  135.             {
  136.                 // token recognized, make token
  137.                 token.makeToken(programBufferPosition, lastLetterPos, programBuffer, lastFiniteState);
  138.                 programBufferPosition = lastLetterPos;
  139.                 return token;
  140.             }
  141.             else
  142.             {
  143.                 // error occurred, create error token
  144.                 token.makeErrorToken(programBufferPosition + counter - 1, programBuffer);
  145.                 programBufferPosition = programBufferPosition + counter - 1;
  146.                 return token;
  147.             }
  148.         }
  149.     }
  150.  
  151.     return token;
  152. }
  153.  
  154.  
  155. TokenList& LexicalAnalysis::getTokenList()
  156. {
  157.     return tokenList;
  158. }
  159.  
  160.  
  161. void LexicalAnalysis::printTokens()
  162. {
  163.     if (tokenList.empty())
  164.     {
  165.         cout << "Token list is empty!" << endl;
  166.     }
  167.     else
  168.     {
  169.         printMessageHeader();
  170.         TokenList::iterator it = tokenList.begin();
  171.         for (; it != tokenList.end(); it++)
  172.         {
  173.             (*it).printTokenInfo();
  174.         }
  175.     }
  176. }
  177.  
  178.  
  179. void LexicalAnalysis::printLexError()
  180. {
  181.     if (errorToken.getType() != T_NO_TYPE)
  182.     {
  183.         printMessageHeader();
  184.         errorToken.printTokenInfo();
  185.     }
  186.     else
  187.     {
  188.         cout << "There are no lexical errors!" << endl;
  189.     }
  190. }
  191.  
  192.  
  193. void LexicalAnalysis::printMessageHeader()
  194. {
  195.     cout << setw(LEFT_ALIGN) << left << "Type:";
  196.     cout << setw(RIGHT_ALIGN) << right << "Value:" << endl;
  197.     cout << setfill('-') << setw(LEFT_ALIGN+RIGHT_ALIGN+1) << " " << endl;
  198.     cout << setfill(' ');
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement