⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 accidenceanalysis.cpp

📁 自己在学习编译原理时用vc++写的算符优先分析器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
						break;
					}
				if(Wordtype==CTable::KEYWORD)Wordtype=CTable::IDENTIFIER;
				else
				{
					output.Add(wNumber);
					Temp.Format("%.2d.关键字:",output.GetSize());
					destination+=Temp;
					destination+=CurrString;
					destination+=_T("\r\n");
				}
				break;
			case CTable::IDENTIFIER:
				nSize=m_pTable->m_IdentifierTable.GetSize();
				for(nIndex=0;nIndex<nSize;nIndex++)
					if(CurrString==m_pTable->m_IdentifierTable.GetAt(nIndex))
					{
						wNumber=CTable::IDENTIFIER+nIndex;
						Wordtype=CTable::UNKNOWN;
						break;
					}
				if(Wordtype==CTable::IDENTIFIER)
				{
					m_pTable->m_IdentifierTable.Add(CurrString);
					wNumber=CTable::IDENTIFIER+nSize;
					Wordtype=CTable::UNKNOWN;
				}
				output.Add(wNumber);
				Temp.Format("%.2d.标识符:",output.GetSize());
				destination+=Temp;
				destination+=CurrString;
				destination+=_T("\r\n");
				break;
			case CTable::OPERATOR:
				nSize=m_pTable->m_OperatorTable.GetSize();
				for(nIndex=0;nIndex<nSize;nIndex++)
				{
					CurrString=m_pTable->m_OperatorTable.GetAt(nIndex);
					if(CurrString==source.Mid(nCurrPos,CurrString.GetLength()))
					{
						nCurrPos+=CurrString.GetLength();
						wNumber=CTable::OPERATOR+nIndex;
						Wordtype=CTable::UNKNOWN;
						break;
					}
				}
				if(Wordtype==CTable::OPERATOR)Wordtype=CTable::SEPARATOR;
				else
				{
					output.Add(wNumber);
					Temp.Format("%.2d.运算符:",output.GetSize());
					destination+=Temp;
					destination+=CurrString;
					destination+=_T("\r\n");
				}
				break;
			case CTable::SEPARATOR:
				nSize=m_pTable->m_SeparatorTable.GetSize();
				for(nIndex=0;nIndex<nSize;nIndex++)
				{
					CurrString=m_pTable->m_SeparatorTable.GetAt(nIndex);
					if(CurrString==source.Mid(nCurrPos,CurrString.GetLength()))
					{
						nCurrPos+=CurrString.GetLength();
						wNumber=CTable::SEPARATOR+nIndex;
						Wordtype=CTable::UNKNOWN;
						break;
					}
				}
				if(Wordtype==CTable::SEPARATOR)
				{
					nError++;
					wNumber=CTable::ERRORCODE;
					CurrString.Format("|错误:未知分隔符 %c |",source.GetAt(nCurrPos));
					nCurrPos++;
					Wordtype=CTable::UNKNOWN;
				}
				output.Add(wNumber);
				Temp.Format("%.2d.分隔符:",output.GetSize());
				destination+=Temp;
				destination+=CurrString;
				destination+=_T("\r\n");
				break;
			case CTable::CHARACTER:
				CurrString=_T("");
				while((chCurrChar=source.GetAt(++nCurrPos))!='\'')
				{
					CurrString+=chCurrChar;
					if(chCurrChar=='\\')CurrString+=source.GetAt(++nCurrPos);
				}
				nCurrPos++;
				nSize=m_pTable->m_CharacterTable.GetSize();
				for(nIndex=0;nIndex<nSize;nIndex++)
					if(CurrString==m_pTable->m_CharacterTable.GetAt(nIndex))
					{
						wNumber=CTable::CHARACTER+nIndex;
						Wordtype=CTable::UNKNOWN;
						break;
					}
				if(Wordtype==CTable::CHARACTER)
				{
					m_pTable->m_CharacterTable.Add(CurrString);
					wNumber=CTable::CHARACTER+nSize;
					Wordtype=CTable::UNKNOWN;
				}
				output.Add(wNumber);
				Temp.Format("%.2d.单字符:",output.GetSize());
				destination+=Temp;
				destination+=('\''+CurrString+'\'');
				destination+=_T("\r\n");
				break;
			case CTable::STRING:
				CurrString=_T("");
				while((chCurrChar=source.GetAt(++nCurrPos))!='\"')
				{
					CurrString+=chCurrChar;
					if(chCurrChar=='\\')CurrString+=source.GetAt(++nCurrPos);
				}
				nCurrPos++;
				nSize=m_pTable->m_StringTable.GetSize();
				for(nIndex=0;nIndex<nSize;nIndex++)
					if(CurrString==m_pTable->m_StringTable.GetAt(nIndex))
					{
						wNumber=CTable::STRING+nIndex;
						Wordtype=CTable::UNKNOWN;
						break;
					}
				if(Wordtype==CTable::STRING)
				{
					m_pTable->m_StringTable.Add(CurrString);
					wNumber=CTable::STRING+nSize;
					Wordtype=CTable::UNKNOWN;
				}
				output.Add(wNumber);
				Temp.Format("%.2d.字符串:",output.GetSize());
				destination+=Temp;
				destination+=('\"'+CurrString+'\"');
				destination+=_T("\r\n");
				break;
			case CTable::INTEGER:
				nCount=0;
				if(chCurrChar=='\0')
				{
					dwCurrNum=0;
					if(CurrString.GetLength()>1)
						if(CurrString.GetAt(1)=='x')
							if((nSize=CurrString.GetLength())<3)nCount++;
							else
								for(nIndex=2;nIndex<nSize;nIndex++)
								{
									if(dwCurrNum&0xF0000000)
									{
										nCount++;
										break;
									}
									dwCurrNum<<=4;
									chCurrChar=CurrString.GetAt(nIndex);
									if(chCurrChar>='0' && chCurrChar<='9')dwCurrNum+=(chCurrChar-'0');
									else if(chCurrChar>='A' && chCurrChar<='F')dwCurrNum+=(chCurrChar-'A'+10);
									else if(chCurrChar>='a' && chCurrChar<='f')dwCurrNum+=(chCurrChar-'a'+10);
									else
									{
										nCount++;
										break;
									}
								}
						else
						{
							nSize=CurrString.GetLength();
							for(nIndex=1;nIndex<nSize;nIndex++)
							{
								if(dwCurrNum&0xE0000000)
								{
									nCount++;
									break;
								}
								chCurrChar=CurrString.GetAt(nIndex);
								if(chCurrChar>='0' && chCurrChar<='7')(dwCurrNum<<=3)+=(chCurrChar-'0');
								else
								{
									nCount++;
									break;
								}
							}
						}
				}
				if(nCount)
				{
					nError++;
					wNumber=CTable::ERRORCODE;
					CurrString=_T("|错误:未知的整型数 ")+CurrString+_T(" |");
					Wordtype=CTable::UNKNOWN;
				}
				else
				{
					nSize=m_pTable->m_IntegerTable.GetSize();
					for(nIndex=0;nIndex<nSize;nIndex++)
						if(dwCurrNum==m_pTable->m_IntegerTable.GetAt(nIndex))
						{
							wNumber=CTable::INTEGER+nIndex;
							Wordtype=CTable::UNKNOWN;
							break;
						}
					if(Wordtype==CTable::INTEGER)
					{
						m_pTable->m_IntegerTable.Add(dwCurrNum);
						wNumber=CTable::INTEGER+nSize;
						Wordtype=CTable::UNKNOWN;
					}
				}
				output.Add(wNumber);
				Temp.Format("%.2d.整型数:",output.GetSize());
				destination+=Temp;
				destination+=CurrString;
				destination+=_T("\r\n");
				break;
			case CTable::REAL:
				fCurrNum=0;
				nCount=0;
				if((CurrString=chCurrChar)==".")nCount++;
				while(++nCurrPos<nLength)
				{
					chCurrChar=source.GetAt(nCurrPos);
					if(chCurrChar>='0' && chCurrChar<='9')CurrString+=chCurrChar;
					else if(chCurrChar=='.')
					{
						CurrString+=chCurrChar;
						nCount++;
					}
					else if((chCurrChar>='A' && chCurrChar<='Z') || (chCurrChar>='a' && chCurrChar<='z'))
					{
						CurrString+=chCurrChar;
						nCount+=0x1000;
					}
					else break;
				}
				if(CurrString.GetAt(0)=='0' && (nCount&0x00FF)==0)
				{
					Wordtype=CTable::INTEGER;
					chCurrChar='\0';
					break;
				}
				if(nCount>1 || CurrString.GetAt(CurrString.GetLength()-1)=='.')
				{
					nError++;
					wNumber=CTable::ERRORCODE;
					CurrString=_T("|错误:未知的实型数 ")+CurrString+_T(" |");
					Wordtype=CTable::UNKNOWN;
				}
				else
				{
					for(nIndex=0;nIndex<CurrString.GetLength();nIndex++)
					{
						if((chCurrChar=CurrString.GetAt(nIndex))!='.')(fCurrNum*=10)+=(chCurrChar-'0');
						else
						{
							double fTempNum=0;
							for(int i=CurrString.GetLength()-1;i>nIndex;i--)(fTempNum/=10)+=(CurrString.GetAt(i)-'0');
							fCurrNum+=fTempNum/10;
							break;
						}
					}
					if(nCount || fCurrNum>0xFFFFFFFF)
					{
						nSize=m_pTable->m_RealTable.GetSize();
						for(nIndex=0;nIndex<nSize;nIndex++)
							if(fCurrNum==m_pTable->m_RealTable.GetAt(nIndex))
							{
								wNumber=CTable::REAL+nIndex;
								Wordtype=CTable::UNKNOWN;
								break;
							}
						if(Wordtype==CTable::REAL)
						{
							m_pTable->m_RealTable.Add(fCurrNum);
							wNumber=CTable::REAL+nSize;
							Wordtype=CTable::UNKNOWN;
						}
					}
					else
					{
						dwCurrNum=(DWORD)fCurrNum;
						Wordtype=CTable::INTEGER;
						break;
					}
				}
				output.Add(wNumber);
				Temp.Format("%.2d.实型数:",output.GetSize());
				destination+=Temp;
				destination+=CurrString;
				destination+=_T("\r\n");
			}
		while(Wordtype!=CTable::UNKNOWN);
	}

	return nError;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -