コード例 #1
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
int GetParaList(SymbolTable &table)
{
    bool ispara=false;
    int num=0;//The number of para
    if (symbol==VARTK) {
        ispara=true;
        GetNextSym();
    }
    do {
        if (symbol!=IDEN) ErrorHandler("para list should split vars via ,");//Error while define paralist ,should have iden
        if (ispara) table.Push(token,  parask);
        else table.Push(token,  varsk);
        num++;
        GetNextSym();
        if (symbol==COLON) break;
        if (symbol!=COMMA) ErrorHandler("para list should split vars via ,");//Error iden should be split by comma
        GetNextSym();
    } while (true);
    GetNextSym();
    if (symbol==INTTK) table.SetType( integerst);
    else if (symbol==CHARTK) table.SetType( charst);
    else ;//Error should have basic type
    table.FillBack();
    GetNextSym();
    if (symbol==SEMICN) {
        GetNextSym();
        num+=GetParaList(table);
    }
    return num;
}
コード例 #2
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
void FuncHeader(SymbolTable &table)
{
    if (symbol!=FUNCTK) ErrorHandler("func should begin with function");//Error symbol should be func
    GetNextSym();
    if (symbol!=IDEN) ErrorHandler("function should have iden as name");//Error function should have iden
    symbolTableList[ptable].ReInitial(table.GetLevel()+1,&table,token, funcsk);
    ptable++;
    table.Push(token,  funcsk);
    GetNextSym();
    if (symbol!=LPARENT) ErrorHandler("function define should have ( ");//Error function should have (
    int num=0;
    GetNextSym();
    if (symbol!=RPARENT) num=GetParaList(symbolTableList[ptable-1]);
    symbolTableList[ptable-1].SetParaNum(num);
    if (symbol!=RPARENT) ErrorHandler("function defien should have )");//Error function should have )
    GetNextSym();
    if (symbol!=COLON) ErrorHandler("function define should have :");//Error fcuntion should have return value
    GetNextSym();
    if (symbol==INTTK) table.SetType( integerst);
    else if (symbol==CHARTK) {
        table.SetType( charst);
        symbolTableList[ptable-1].SetFuncType(charst);
    }
    else ;//Error function should have return value;
    table.SetValue(num);
    table.FillBack();
    GetNextSym();
    if (symbol!=SEMICN) ErrorHandler("function define should have integer or char as type");//Error function should end with ;
    GetNextSym();
}
コード例 #3
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
void VarDefinition(SymbolTable &table)
{
    if (symbol!=IDEN) ErrorHandler("Error defination shold begin with iden");//Throw Error while define var, expect iden
    table.Push(token,  varsk);
    do {
        GetNextSym();
        if (symbol==COLON) break;
        if (symbol!=COMMA) ErrorHandler("multi-var should be seperate via ,");
        GetNextSym();
        if (symbol!=IDEN) ErrorHandler("Error defination should be iden, but you used "+token); //throw error while define var
        table.Push(token,  varsk);
    } while (true);
    GetNextSym();
    if (symbol==INTTK) table.SetType( integerst);
    else if (symbol==CHARTK) table.SetType( charst);
    //table.SetKind(varsk);
    else if (symbol==ARRAYTK) {
        GetNextSym();
        if (symbol!=LBRACK) ErrorHandler("Error while define arr,miss [");//Throw Error expect [
        GetNextSym();
        if (symbol!=INTCON) ErrorHandler("[k] k should be number");// Throw Error expect number
        table.SetTypeValue( arrOfCh, number);
        GetNextSym();
        if (symbol!=RBRACK) ErrorHandler("define arr, miss ]");//Throw Error expect ]
        GetNextSym();
        if (symbol!=OFTK) ErrorHandler("define arr miss of");//Throw Error expect of
        GetNextSym();
        if (symbol!=INTTK && symbol!=CHARTK) ErrorHandler("define arr miss char or integer"); //Throw error expect char or integer
        if (symbol==INTTK) table.SetType( arrOfInt);
    } else ErrorHandler("var type illegal");
    table.FillBack();
    GetNextSym();
    if (symbol==IDEN) ErrorHandler("miss ;");
}
コード例 #4
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
string NextTempName(SymbolTable &table)
{
    string rect="ttemp";
    rect=rect+IntToString(tempGenerator++);
    table.Push(rect, varsk);
    table.SetType(integerst);
    table.FillBack();
    return rect;
}
コード例 #5
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
void ConstDefinition(SymbolTable &table)
{
    if (symbol!=IDEN) ErrorHandler("Const defination shoule begin with iden");//Throw Error while define const value
    table.Push(token,  constsk);
    GetNextSym();
    if (symbol!=EQL) ErrorHandler("Const defination should use = as symbol");//Throw Error while define const value
    GetNextSym();
    if (symbol==CHARCON) {
      table.SetTypeValue( charst, (int)token[0]);
      table.FillBack();
      GetNextSym();
      return;
    }
    if (symbol==MINU) {
        GetNextSym();
        number*=-1;
    } else if (symbol==PLUS) GetNextSym();
    if (symbol!=INTCON) ErrorHandler("Const should be integer or char");//throw Error while define int const
    table.SetTypeValue( integerst, number);
    table.FillBack();
    GetNextSym();
}
コード例 #6
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
void ProcHeader(SymbolTable &table)
{
    if (symbol!=PROCETK) ErrorHandler("Proc should begin with procedure");//Error procedure should begin with procedure
    GetNextSym();
    if (symbol!=IDEN) ErrorHandler("procedue should have iden as name");//Error procedure should have iden
    symbolTableList[ptable].ReInitial(table.GetLevel()+1,&table,token, procsk);
    ptable++;
    table.Push(token,  procsk);
    GetNextSym();
    if (symbol!=LPARENT) ErrorHandler("proc define should have (");//Error procedure should have (
    int num=0;//The number of para
    GetNextSym();
    if (symbol!=RPARENT) num=GetParaList(symbolTableList[ptable-1]);
    symbolTableList[ptable-1].SetParaNum(num);
    table.SetTypeValue( integerst, num);
    table.FillBack();
    if (symbol!=RPARENT) ErrorHandler("Proc define should have )") ;//Error procedure should have )
    GetNextSym();
    if (symbol!=SEMICN) ErrorHandler("Proc head def should end with ;");//Error Procedure should end with ;
    GetNextSym();
}
コード例 #7
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
void SubProgram(SymbolTable &table)
{
    if (symbol==CONSTTK) ConstDeclaration(table);
    if (symbol==VARTK) VarDeclaration(table);
    while (symbol==PROCETK || symbol==FUNCTK) {
        if (symbol==PROCETK) ProcDeClaration(table);
        else if (symbol==FUNCTK) FuncDeClaration(table);
    }
    if (symbol!=BEGINTK) ErrorHandler("SubProgram should begin with begin"); //Throw Error while define Subprogram, can not find begin tk.
    if (table.GetKind()==procsk) interResult<<"Proc ";
    if (table.GetKind()==funcsk) interResult<<"Func ";
    interResult<<table.GetName();
    for (int i=0;i!=ptable;++i) if ((&symbolTableList[i])==&table) {
        interResult<<' '<<i<<endl;
        break;
    }
    CompoundStatement(table);
    table.Push(table.GetName(), varsk);
    table.SetType(table.GetFuncType());
    table.FillBack();
}