예제 #1
0
t_token AlexicalAnalizer::nextToken(void) {
  t_token token;
  int token2 = -1;

  // Trata comentários
  skipSeparators();
  // Alfa numerico
  if (isalpha(next_char)) {
    // Palavras Reservadas
    char text[MAX_STRING_SIZE];
    // init text
    for (int i=0; i<MAX_STRING_SIZE ;i++) { text[i] = '\0'; }
    int i = 0;
    do {
      text[i++] = next_char;
      next_char = readChar();
    } while (isalnum(next_char) || next_char == '_');
    // discover alpha type
    token = searchKeyWord(text);
    // if is a id, add to table
    if (token == ID) { token2 = searchName(text); }
  }
  // Numeral
  else if (isdigit(next_char)) {
    int n = 0;
    do {
      n = n * 10 + (next_char - '0');
      next_char = readChar();
    } while (isdigit(next_char));

    // update token variables
    token = NUMERAL;
    token2 = addIntConst(n);
  }
  // Stringval
  else if (next_char == '"') {
    char string[MAX_STRING_SIZE + 1];
    int i = 0;
    // to remove '"' from string
    next_char = readChar();
    // store value of string on variable
    do {
      string[i++] = next_char;
      next_char = readChar();
    } while (next_char != '"');
    next_char = readChar();

    // update token variables
    token = STRINGVAL;
    token2 = addStringConst(string);
  } else {
    switch (next_char) {
    /************
     * SIMBOLOS *
     ************/
    case '.':
      next_char = readChar();
      token = DOT;
      break;
    // COLON
    case ':':
      next_char = readChar();
      token = COLON;
      break;
    // SEMI_COLON
    case ';':
      next_char = readChar();
      token = SEMI_COLON;
      break;
    // COMMA
    case ',':
      next_char = readChar();
      token = COMMA;
      break;
    // 'Squares'
    case '[':
      next_char = readChar();
      token = LEFT_SQUARE;
      break;
    case ']':
      next_char = readChar();
      token = RIGHT_SQUARE;
      break;
    // Braces
    case '{':
      next_char = readChar();
      token = LEFT_BRACES;
      break;
    case '}':
      next_char = readChar();
      token = RIGHT_BRACES;
      break;
    // Parenthesis
    case '(':
      next_char = readChar();
      token = LEFT_PARENTHESIS;
      break;
    case ')':
      next_char = readChar();
      token = RIGHT_PARENTHESIS;
      break;
    // Less_than and Less_or_equal
    case '<':
      next_char = readChar();
      token = LESS_THAN;
      if (next_char == '=') {
        token = LESS_OR_EQUAL;
        next_char = readChar();
      }
      break;
    // Greater_than and Greater_or_equal
    case '>':
      next_char = readChar();
      token = GREATER_THAN;
      if (next_char == '=') {
        token = GREATER_OR_EQUAL;
        next_char = readChar();
      }
      break;
    // Not_equal
    case '!':
      next_char = readChar();
      if (next_char == '=') {
        token = NOT_EQUAL;
        next_char = readChar();
      } else {
        token = NOT;
      }
      break;
    // Equals and equal_equal
    case '=':
      next_char = readChar();
      if (next_char == '=') {
        token = EQUAL_EQUAL;
        next_char = readChar();
      } else {
        token = EQUALS;
      }
      break;
    // Plus and plus_plus
    case '+':
      next_char = readChar();
      if (next_char == '+') {
        token = PLUS_PLUS;
        next_char = readChar();
      } else {
        token = PLUS;
      }
      break;
    // Minus and minus_minus
    case '-':
      next_char = readChar();
      if (next_char == '-') {
        token = MINUS_MINUS;
        next_char = readChar();
      } else {
        token = MINUS;
      }
      break;
    // Times
    case '*':
      next_char = readChar();
      token = TIMES;
      break;
    // Divide
    case '/':
      next_char = readChar();
      token = DIVIDE;
      break;
    /********************
     * Tokens Regulares *
     ********************/
    // Character
    case '\'':
      next_char = readChar();
      // update token variable
      token = CHARACTER;
      token2 = addCharConst(next_char);
      next_char = readChar();
      if ('\'' != next_char)
        token = UNKNOWN;
      next_char = readChar();
      break;
    }
  }
  // setup last token variables
  last_token = token;
  last_token2 = token2;

  return token;
}
t_token nextToken(void){
    tokenString = "";
    hasTokenSecundario = false;
    // loop do estado inicial para pular os separadores
    while(isspace(nextChar)){
        nextChar = readChar();
    }
    
    if(nextChar == EOF){
        token = ENDFILE;
        return token;
    }
    
    if( isalpha(nextChar) ){
        do {
            tokenString = tokenString + nextChar;
            nextChar = readChar();
        } while( isalnum(nextChar) || nextChar == '_' );
        token = searchKeyWord(tokenString);
        //std::cout << "Read:" << text << ", Token: " << token;
        if(token == IDT){
            tokenSecundario = searchName(tokenString);
            hasTokenSecundario = true;
            //std::cout << ", Token Secundario IDT " << tokenSecundario << std::endl;
        }
        else{
            //std::cout << std::endl;
        }
    }
    else if( isdigit(nextChar) ){
        char numeral[MAX_NUM_LEN+1];
        int i = 0;
        //std::cout << "Read: ";
        do{
            tokenString = tokenString + nextChar;
            numeral[i++] = nextChar;
            //std::cout << nextChar;
            nextChar = readChar();
        }while( isdigit(nextChar) );
        numeral[i] = '\0';
        token = NUMERAL;
        //std::cout << ", Token: " << token;
        tokenSecundario = addIntConst(atoi(numeral));
        hasTokenSecundario = true;
        //std::cout << ", Token Secundario Const: " << tokenSecundario << std::endl;
    } else if( nextChar == '"' ){
        tokenString = "";
        nextChar = readChar();
        while(nextChar != '"'){
            tokenString += nextChar;
            nextChar = readChar();
        }
        token = STRINGVAL;
        char* cstr = new char[tokenString.size()+1];
        memcpy(cstr, tokenString.c_str(), tokenString.size()+1);
        tokenSecundario = addStringConst(cstr);
        hasTokenSecundario = true;
        nextChar = readChar();
        //std::cout << "Read: " << tokenString << ", Token: " << token << ", Token Secundario Const: " << tokenSecundario << std::endl;
    }else if( nextChar == '\''){
        nextChar = readChar();
        tokenString = nextChar;
        token = CHARACTER;
        tokenSecundario = addCharConst(nextChar);
        hasTokenSecundario = true;
        //std::cout << "Read: " << '\'' << nextChar << '\'' << ", Token: " << token << ", Token Secundario Const: " << tokenSecundario << std::endl;
        nextChar = readChar(); //pular o '
        nextChar = readChar();
    }
    else{ //SIMBOLOS
        //std::cout << "Read: " << nextChar;
        tokenString = nextChar;
        switch(nextChar){
            case ':':
                nextChar = readChar();
                token = COLON;
                break;
            case ';':
                nextChar = readChar();
                token = SEMI_COLON;
                break;
            case ',':
                nextChar = readChar();
                token = COMMA;
                break;
            case '[':
                nextChar = readChar();
                token = LEFT_SQUARE;
                break;
            case ']':
                nextChar = readChar();
                token = RIGHT_SQUARE;
                break;
            case '{':
                nextChar = readChar();
                token = LEFT_BRACES;
                break;
            case '}':
                nextChar = readChar();
                token = RIGHT_BRACES;
                break;
            case '(':
                nextChar = readChar();
                token = LEFT_PARENTHESIS;
                break;
            case ')':
                nextChar = readChar();
                token = RIGHT_PARENTHESIS;
                break;
            case '&':
                nextChar = readChar();
                if(nextChar == '&'){
                    tokenString += nextChar;
                    token = AND;
                    nextChar = readChar();
                }
                else{
                    token = UNKNOWN;
                }
                break;
            case '|':
                nextChar = readChar();
                if(nextChar == '|'){
                    tokenString += nextChar;
                    token = OR;
                    nextChar = readChar();
                }
                else{
                    token = UNKNOWN;
                }
                break;
            case '=':
                nextChar = readChar();
                if( nextChar == '=' )
                {
                    tokenString += nextChar;
                    token = EQUAL_EQUAL;
                    nextChar = readChar();
                } else {
                    token = EQUALS;
                } break;
            case '<':
                nextChar = readChar();
                if( nextChar == '=' )
                {
                    tokenString += nextChar;
                    token = LESS_OR_EQUAL;
                    nextChar = readChar();
                } else {
                    token = LESS_THAN;
                } break;
            case '>':
                nextChar = readChar();
                if( nextChar == '=' )
                {
                    tokenString += nextChar;
                    token = GREATER_OR_EQUAL;
                    nextChar = readChar();
                } else {
                    token = GREATER_THAN;
                } break;
            case '!':
                nextChar = readChar();
                if( nextChar == '=' )
                {
                    tokenString += nextChar;
                    token = NOT_EQUAL;
                    nextChar = readChar();
                } else {
                    token = NOT;
                } break;
            case '+':
                nextChar = readChar();
                if( nextChar == '+' )
                {
                    tokenString += nextChar;
                    token = PLUS_PLUS;
                    nextChar = readChar();
                } else {
                    token = PLUS;
                } break;
            case '-':
                nextChar = readChar();
                if( nextChar == '-' )
                {
                    tokenString += nextChar;
                    token = MINUS_MINUS;
                    nextChar = readChar();
                } else {
                    token = MINUS;
                } break;
            case '*':
                nextChar = readChar();
                token = TIMES;
                break;
            case '/':
                nextChar = readChar();
                token = DIVIDE;
                break;
            case '.':
                nextChar = readChar();
                token = DOT;
                break;
            default:
                token = UNKNOWN;
        }
        //std::cout << ", Token: " << token << std::endl;
    }
    
    return token;
}