📄 pascalsintanalyzer.cpp
字号:
case TT_KW_PRINT:
{
PushBack();
InstrPrint();
break;
}
case TT_KW_READ:
{
PushBack();
InstrRead();
break;
}
case TT_KW_IF:
{
PushBack();
InstrIf();
break;
}
case TT_WORD:
if (IsProc(GetStrValue()))
{
PushBack();
ApelProcedura();
}
else
{
PushBack();
Atribuire();
}
break;
default:
throw error(SET_EXPECTED, CString("Instruction"));
}
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::InsertSymbol(Symbol &s)
{
SymbolList *list;
if (!m_SymbTable.Lookup(s.m_sName , list))
list = new SymbolList();
list->AddTail(s);
m_SymbTable[s.m_sName]=list;
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.8 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
BOOL CPascalCompiler::IsProc(CString &str)
{
SymbolList *list;
if (!m_SymbTable.Lookup(str,list))
return FALSE;
BOOL is = FALSE;
POSITION pos = list->GetHeadPosition ();
while (pos != NULL)
{
Symbol s= list->GetNext (pos);
if (s.m_nClass == CT_PROCEDURE)
is = TRUE;
}
return is;
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.8 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::Atribuire()
{
// Variabila
Variabila();
if (NextToken()!=TT_IS)
throw error(SET_EXPECTED, CString(":="));
Expr();
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.8 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::Variabila()
{
if (NextToken()!=TT_WORD)
throw error(SET_EXPECTED, CString("identifier"));
int val = NextToken();
if (val=='[')
{
Expr();
if (NextToken()!=']')
throw error(SET_EXPECTED, CString("]"));
return;
}
if (val=='.')
{
if (NextToken()!=TT_WORD)
throw error(SET_EXPECTED, CString("identifier"));
return;
}
PushBack();
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.8 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::Expr()
{
while (1)
{
Termen();
int val = NextToken();
if (val!='+' && val != '-')
{
PushBack();
break;
}
}
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.8 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::Termen()
{
while (1)
{
Factor();
int val = NextToken();
if (val!='*' && val != '/' && val!=TT_KW_MOD && val != TT_KW_DIV)
{
PushBack();
break;
}
}
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.8 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::Factor()
{
int val = NextToken();
if (val == TT_NUMBER)
return;
if (val == TT_STRING)
return;
if (val == '(')
{
Expr();
if (NextToken()!=')')
throw error(SET_EXPECTED, CString(")"));
return;
}
if (val == TT_WORD)
if (IsFunc(GetStrValue()))
{
PushBack();
ApelFunc();
return;
}
else
{
PushBack();
Variabila();
return;
}
throw error(SET_EXPECTED, CString("number or constant or identifier or function or variable "));;
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.8 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
BOOL CPascalCompiler::IsFunc(CString &str)
{
SymbolList *list;
if (!m_SymbTable.Lookup(str,list))
return FALSE;
BOOL is = FALSE;
POSITION pos = list->GetHeadPosition ();
while (pos != NULL)
{
Symbol s= list->GetNext (pos);
if (s.m_nClass == CT_FUNCTION)
is = TRUE;
}
return is;
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.8 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::ApelFunc()
{
if (NextToken()!=TT_WORD)
throw error(SET_EXPECTED, CString("identifier"));
if (NextToken()=='(')
{
while (1)
{
Expr();
if (NextToken()!=',')
{
PushBack();
break;
}
}
if (NextToken()!=')')
throw error(SET_EXPECTED, CString(")"));
}
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.8 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::InstrWhile()
{
if (NextToken()!=TT_KW_WHILE)
throw error(SET_EXPECTED, CString("while"));
Conditie();
if (NextToken()!=TT_KW_DO)
throw error(SET_EXPECTED, CString("do"));
Instr();
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.8 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::Conditie()
{
int val;
while (1)
{
if (NextToken()!=TT_KW_NOT)
PushBack();
ExprLogica();
val = NextToken();
if (val != TT_KW_AND && val != TT_KW_OR)
{
PushBack();
break;
}
}
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::ExprLogica()
{
Expr();
int val = NextToken();
switch (val)
{
case '=':break;
case '<':break;
case '>':break;
case TT_GTE:break;
case TT_LWE:break;
case TT_NE:break;
default: throw error(SET_EXPECTED, CString("logical operator"));;
}
Expr();
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::InstrRepeat()
{
if (NextToken()!=TT_KW_REPEAT)
throw error(SET_EXPECTED, CString("repeat"));
Instr();
if (NextToken()!=TT_KW_UNTIL)
throw error(SET_EXPECTED, CString("until"));
Conditie();
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::InstrIf()
{
if (NextToken()!=TT_KW_IF)
throw error(SET_EXPECTED, CString("if"));
Conditie();
if (NextToken()!=TT_KW_THEN)
throw error(SET_EXPECTED, CString("then"));
Instr();
if (NextToken()==TT_KW_ELSE)
{
Instr();
}
else PushBack();
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::InstrFor()
{
if (NextToken()!=TT_KW_FOR)
throw error(SET_EXPECTED, CString("for"));
Variabila();
if (NextToken()!=TT_IS)
throw error(SET_EXPECTED, CString(":="));
Expr();
int val = NextToken();
if (val==TT_KW_TO)
{
// varianta To
;
}
else
if (val==TT_KW_DOWNTO)
{
// varianta DownTo
;
}
else throw error(SET_EXPECTED, CString("to or downto"));;
Expr();
if (NextToken() == TT_KW_STEP)
{
Expr();
}
else PushBack();
if (NextToken()!=TT_KW_DO)
throw error(SET_EXPECTED, CString("do"));
Instr();
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::InstrCase()
{
if (NextToken() != TT_KW_CASE)
throw error(SET_EXPECTED, CString("case"));
Expr();
if (NextToken() != TT_KW_OF)
throw error(SET_EXPECTED, CString("of"));
ListaAltern();
if (NextToken() != TT_KW_END)
throw error(SET_EXPECTED, CString("end"));
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::ListaAltern()
{
while (1)
{
while (1)
{
if (NextToken()!=TT_NUMBER)
throw error(SET_EXPECTED, CString("number"));
if (NextToken()!=',')
{
PushBack();
break;
}
}
if (NextToken()!=':')
throw error(SET_EXPECTED, CString(":"));
Instr();
if (NextToken()!=';')
{
PushBack();
break;
}
}
if (NextToken() == TT_KW_OTHERWISE)
{
Instr();
}
else
PushBack();
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::ApelProcedura()
{
if (NextToken()!= TT_WORD)
throw error(SET_EXPECTED, CString("identifier"));
if (NextToken()=='(')
{
while (1)
{
Expr();
if (NextToken()!=',')
{
PushBack();
break;
}
}
if (NextToken()!=')')
throw error(SET_EXPECTED, CString(")"));
}
else
PushBack();
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::InstrPrint()
{
if (NextToken()!=TT_KW_PRINT)
throw error(SET_EXPECTED, CString("print"));
if (NextToken()!='(')
throw error(SET_EXPECTED, CString("("));
while (1)
{
if (NextToken()!=TT_STRING)
{
PushBack();
Expr();
}
if (NextToken()!=',')
{
PushBack();
break;
}
}
if (NextToken()!=')')
throw error(SET_EXPECTED, CString(")"));
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.11 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
void CPascalCompiler::InstrRead()
{
if (NextToken()!=TT_KW_READ)
throw error(SET_EXPECTED, CString("read"));
if (NextToken()!='(')
throw error(SET_EXPECTED, CString("("));
while (1)
{
Variabila();
if (NextToken()!=',')
{
PushBack();
break;
}
}
if (NextToken()!=')')
throw error(SET_EXPECTED, CString(")"));
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.13 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/
CString CPascalCompiler::GetErrStr(CPascalCompiler::error &e)
{
CString estr;
switch (e.m_nType)
{
case SET_GENERAL: estr = _T("Syntax ERROR");break;
case SET_EXPECTED: estr.Format("%s expected",e.m_sData);break;
default: estr = _T("Syntax ERROR");
}
CString str;
str.Format("%s at : %d", estr, LineNo ());
return str;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -