示例#1
0
void GetToken(void)
{
	//int		n	=0;
	// Simply reads in the next statement and places it in the
	// token buffer.

	ParseWhitespace();

	switch (chr_table[*src])
	{
	case LETTER:
		//token_type = IDENTIFIER;
		tok.type=IDENTIFIER;
		GetIdentifier();
		break;
	case DIGIT:
		//token_type = DIGIT;
		tok.type=DIGIT;
		GetNumber();
		break;
	case SPECIAL:
		//token_type = CONTROL;
		tok.type=CONTROL;
		GetPunctuation();
		break;
	}

	//printf("token: %s\n", tok.ident);

	if (!*src && inevent)
	{
		err("Unexpected end of file");
	}
}
示例#2
0
void GetToken(void)
{
  int i;

  // Simply reads in the next statement and places it in the
  // token buffer.

  ParseWhitespace();
  i=0;

  switch (chr_table[*src])
  {
    case  LETTER: { token_type = IDENTIFIER; GetIdentifier();  break; }
    case   DIGIT: { token_type = DIGIT;      GetNumber();      break; }
    case SPECIAL: { token_type = CONTROL;    GetPunctuation(); break; }
  }

  if (!*src && inevent)
    err("Unexpected end of file");
}
bool LexicalAnalysis::Analyze()
{
	if (!m_file.is_open())
	{
		cout << "File loading error" << endl;
		return false;
	}

	int temp = -1;
	while (1)
	{
		//move ptr to the beginning of the next word
		if (!DeleteNonsense())
			return true;

		//analyze the word
		pair<Token, string> data;
		//the end
		if (IsEnd(*m_bufferMoved))
		{
			return true;
		}
		//begin with the number
		else if (IsDecimalNumber(*m_bufferMoved))
		{
			data.first = GetNumber(data.second);
			if (data.first == ERROR)
			{
				cout << "ERROR:" << data.second;
				continue;
			}
			++m_wordNum;
		}
		//begin with the alpha
		else if (IsAlphaOrUnderline(*m_bufferMoved))
		{
			data.first = GetKeywordOrIdentifier(data.second);
			if (data.first == ERROR)
			{
				cout << "ERROR:" << data.second;
				continue;
			}
			++m_wordNum;
		}
		//begin with the punctuation
		else
		{
			data.first = GetPunctuation(data.second);
			if (data.first == ERROR)
			{
				cout << "ERROR:" << data.second;
				continue;
			}
		}
		//save the word
		m_data.push_back(data);

		if (temp != m_lineNum)
		{
			temp = m_lineNum;
			std::cout << "第" << temp << "行" << endl;
			m_fout << "第" << temp << "行" << endl;
		}

		std::cout << "\t" << m_keyword[data.first] << "\t" << data.second << endl;
		m_fout << "\t" << m_keyword[data.first] << "\t" << data.second << endl;
	}

	return true;
}