?? analyze.cpp
字號:
#include "StdAfx.h"
#include ".\analyze.h"
Analyze::Analyze(void)
{
fr.setFileName("test.txt");
fr.OpenFile();
strcpy(strToken, "");
ch = ' ';
size = 0;
}
int Analyze::getNextWord()
{
if( fr.IsEnd() )
return -1;
if( fr.IsLineEnd() )
fr.ReadLine();
return StepForward();
}
void Analyze::Close()
{
fr.CloseFile();;
}
void Analyze::addInt()
{
_global.pushWaiting("realOpt", strToken, integer, 0);
}
void Analyze::addReal()
{
_global.pushWaiting("realOpt", strToken, real, 0);
}
void Analyze::addBoolean()
{
_global.pushWaiting("realOpt", strToken, boolean, 0);
}
int Analyze::StepForward()
{
strcpy(strToken, "");
int code = 0;
int value = 0;
fr.SkipBlank();
ch = fr.getFirstChar();
if(IsDigit() || ch == '.' )
{
while(IsDigit())
{
Concat();
ch = fr.getFirstChar();
}
if( ch == '.' )
{
Concat();
ch = fr.getFirstChar();
if(!IsDigit())
{
this->ProcError();
return -1;
}
while(IsDigit())
{
Concat();
ch = fr.getFirstChar();
}
Retract();
addReal();
if(strToken[0] == '.')//$Real
{
return 200;
}
return 200;
}
Retract();//$Int
addInt();
return 100;
}
else if( IsLetter() )
{
Concat();
ch = fr.getFirstChar();
while(IsDigit() || IsLetter())
{
Concat();
ch = fr.getFirstChar();
}
if( strcmp(strToken, "END") == 0 )
{
if( ch == '.')//$END.
{
return 1420;
}
else
{
Retract();
}
}
code = Reserve();
if( code == -1 ) //非保留字
{
value = InsertID();//$ID
return 700;
}
else
{
if(code == 301)//$TRUE
{
addBoolean();
return 301;
}
else if(code == 302)//$FALSE
{
addBoolean();
return 302;
}
else if( code == 601)
{
// record<<"($OPE_LOG, 1)"<<endl;
return 601;
}
else if(code == 602)
{
// record<<"($OPE_LOG, 2)"<<endl;
return 602;
}
else if( code == 603)
{
// record<<"($OPE_LOG, 3)"<<endl;
return 603;
}
// record<<"($"<<strToken<<",-)"<<endl;
return code;
}
}
else if( ch == ':')
{
ch = fr.getFirstChar();
if( ch == '=')
{
// record<<"($ASSIGN, -)"<<endl;
return 1500;
}
else
{
Retract();
return 1800;
}
}
else if( ch == '=')
{
// record<<"($OPE_REL, 5)"<<endl;
return 505;
}
else if( ch == '>')
{
ch = fr.getFirstChar();
if( ch == '=')
{
// record<<"($OPE_REL, 3)"<<endl;
return 504;
}
Retract();
// record<<"($OPE_REL, 4)"<<endl;
return 503;
}
else if( ch == '<')
{
ch = fr.getFirstChar();
if( ch == '=')
{
// record<<"($OPE_REL, 2)"<<endl;
return 502;
}
else if( ch == '>')
{
// record<<"($OPE_REL, 6)"<<endl;
return 506;
}
Retract();
//record<<"($OPE_REL, 1)"<<endl;
return 501;
}
else if( ch == '+')
{
// record<<"($OPE_ARI, 1)"<<endl;
return 401;
}
else if( ch == '-')
{
// record<<"($OPE_ARI, 2)"<<endl;
return 402;
}
else if( ch == '/')
{
ch = fr.getFirstChar();
if( ch == '/' )//注釋行
{
fr.ReadLine();
return StepForward();
}
Retract();
// record<<"($OPE_ARI, 4)"<<endl;
return 404;
}
else if( ch == '*')
{
// record<<"($OPE_ARI, 3)"<<endl;
return 403;
}
else if( ch == '[')
{
// record<<"($LPAR_M, -)"<<endl;
return 1000;
}
else if( ch == ']')
{
// record<<"($RPAR_M, -)"<<endl;
return 1100;
}
else if( ch == ';')
{
// record<<"($SEMICOLON, -)"<<endl;
return 1600;
}
else if( ch == '(')
{
// record<<"($LPAR_S, -)"<<endl;
return 800;
}
else if( ch == ')')
{
// record<<"($RPAR_S, -)"<<endl;
return 900;
}
else if( ch == '{')
{
// record<<"($LPAR_L, -)"<<endl;
return 1200;
}
else if( ch == '}')
{
// record<<"($RPAR_L, -)"<<endl;
return 1300;
}
else if( ch == ',')
{
return 1700;
}
else
{
ProcError();
return -1;
}
}
void Analyze::ProcError()
{
record<<"error:錯誤產生在第"<<fr.getLineNumber()<<"行處";
if( strlen(strToken) == 0 )
record<<ch<<endl;
record<<strToken<<endl;
}
int Analyze::CheckIDExist()
{
for( int i = 0 ; i < _global.v_table.size() ; i++)
if( _global.v_table[i].name.compare((string)strToken) == 0 )//equal
return i;
return -1;
}
int Analyze::InsertID()
{
if( _global.global_type == definition)
{
int s = this->CheckIDExist();
if(s != -1)
{
return s;
}
_global.pushWaiting("newID", (string)strToken, 0, 0);
}
else if( _global.global_type == program )
{
int s = this->CheckIDExist();
if(s != -1)
{
_global.pushWaiting("checkID", _global.v_table[s].name, _global.v_table[s].type, 0);
return s;
}
cout<<"符號不存在"<<endl;
_global.global_type = error;
}
}
void Analyze::Retract()
{
fr.backwardReadPosition(1);
ch = ' ';
}
int Analyze::Reserve()
{
return _sm.getInt((string)strToken);
}
int Analyze::Concat()
{
strncat(this->strToken, &ch, 1);
if(strlen(strToken) >= STRING_SIZE)
return 1;
return 0;
}
int Analyze::IsDigit()
{
int temp = (int)ch;
if(temp <= 57 && temp >= 48)
return 1;
return 0;
}
int Analyze::IsLetter()
{
int temp = (int)ch;
if(temp <= 90 && temp >= 65)
return 1;
if(temp <= 122 && temp >= 97)
return 1;
return 0;
}
anaStatus::anaStatus()
{
status_num = 0;
next = NULL;
}
anaSymbol::anaSymbol()
{
symbol = 0;
next = NULL;
}
anaStack::anaStack()
{
content = NULL;
_symbol = NULL;
}
void anaStack::outputStatus()
{
cout<<"輸出狀態棧的內容: ";
anaStatus *first = content;
while(first != NULL)
{
cout<<first->status_num<<",";
first = first->next;
}
}
void anaStack::outputSymbol()
{
symbol_mapping temp;
string name;
cout<<"輸出符號棧的內容: ";
anaSymbol *first = _symbol;
while(first != NULL)
{
name = temp.getName(first->symbol);
cout<<name<<",";
first = first->next;
}
}
int anaStack::IsSymbolEmpty()
{
if(_symbol == NULL)
return 1;
return 0;
}
int anaStack::IsEmpty()
{
if(content == NULL)
return 1;
return 0;
}
void anaStack::PopSymbolIn(anaSymbol *src)
{
anaSymbol *temp = new anaSymbol();
temp->symbol = src->symbol;
temp->next = _symbol;
_symbol = temp;
}
void anaStack::PopIn(anaStatus *src)
{
anaStatus *temp = new anaStatus();
temp->status_num = src->status_num;
temp->next = content;
content = temp;
}
void anaStack::PushSymbolOut(anaSymbol *dst, int num)
{
anaSymbol *temp = _symbol;
while( num > 0 )
{
_symbol = _symbol->next;
delete temp;
temp = _symbol;
num--;
}
}
void anaStack::PushOut(anaStatus *dst, int num)
{
anaStatus *temp = content;
while( num > 0 )
{
content = content->next;
delete temp;
temp = content;
num--;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -