Returning letter
  
 
I am writing the coding which is display the contain of the file is number,letter,...etc but I stuck on the keyword part
Let say i have if is written in the file and that if is see it should be display keyword not indent
can someone help me out plz
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
 
  | 
/
#include "LexicalAnalysis.h"
LexicalAnalysis::LexicalAnalysis(string in) : input(in), charClass(ERROR), nextChar(' '), lexeme("")
{
	getChar();
}
LexicalAnalysis::~LexicalAnalysis()
{
}
void LexicalAnalysis::setNewInput(string in) 
{
	input=in;
	getChar();
}
void LexicalAnalysis::getChar()
{
	if (input.size()>0)
    {
	  nextChar = input[0];
	  input.erase(0,1);
	}
	else nextChar = '$';
	charClass = ERROR;
	if ( (nextChar > 64 && nextChar <91) || (nextChar > 96 && nextChar <123) )
		charClass = LETTER;
	if ( nextChar > 47 && nextChar <58 )
		charClass = DIGIT;
	if (nextChar == ' ') charClass = SPACE;
	if (nextChar == '$') charClass = STOP;
    if (nextChar == 'i') charClass = PLUS_CODE;
}
void LexicalAnalysis::addChar()
{
	lexeme+=nextChar;
}
int LexicalAnalysis::lex()
{
  lexeme="";
  while (charClass == SPACE) getChar();
  if (charClass == ERROR) {addChar(); getChar(); return ERROR;}
  if (charClass == STOP) { return STOP;}
  switch (charClass) {
    case LETTER:
      addChar();
      getChar();
      while (charClass == LETTER || charClass == DIGIT)
      {
        addChar();
        getChar();
      }
      return IDENT;
	  break;
	case DIGIT: 
      addChar();
      getChar();
      while (charClass == DIGIT) {
        addChar();
        getChar();
      }
      return INT_LIT;
      break;
    case PLUS_CODE:
      addChar();
      getChar();
      return PLUS_CODE;
	  break;
  }
    return 0;
}
  |  | 
 
 
 
 
Topic archived. No new replies allowed.