コード例 #1
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
void CheckParalist(SymbolTable &table,int num,SymbolTable &callTable)
{
    Emit tmp;
    SymbolItem* item;
    for (int i=0;i!=num;++i) {
        item=table.GetItem(i);
        if (symbol==RPARENT) ErrorHandler("function call doesn't fit defination");//Error paralist doesn't fit defination
        if (item->GetKind()== parask) {
            SymbolItem* tmpitem=callTable.GetItem(token);
            if (!(IsBasicVar(*tmpitem, callTable) || IsArrayVar(*tmpitem))) ErrorHandler("var should pass address not expression");//Error var should pass address
            if (IsBasicVar(*tmpitem, callTable)) tmp.SetD1(token);
            else if (IsArrayVar(*tmpitem)) tmp.SetD1(GetArrayOffset(callTable));
            else ;//Error var pare should have iden to be parameter
            GetNextSym();
            if (symbol!=COMMA && symbol!=RPARENT) ErrorHandler("para not fit defination");//Error para should have iden yo be parameter
            tmp.SetTP( pushet);
            tmp.SetD1(tmp.GetD1()+"[");
            tmp.emitResult();
            GetNextSym();
            continue;
        }
        tmp.SetTP( pushet);
        tmp.SetD1(Expression(callTable));
        tmp.emitResult();
        if (symbol!=COMMA && symbol!=RPARENT) ErrorHandler("para not fit defination");//Error para doesn't fit the defination
        GetNextSym();
    }
    if (num==0) {
        GetNextSym();
    }
}
コード例 #2
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
bool CheckItem(string str,bool isArray,SymbolTable &table)
{
    SymbolItem* item = table.GetItem(str);
    if (item==NULL) return false;
    if (isArray) return (item->GetKind()== varsk && IsArrayVar(*item));
    return false;
}
コード例 #3
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
bool CheckItem(string str,SymbolKind kind,SymbolType type,SymbolTable &table)
{
    SymbolItem* item = table.GetItem(str);
    if (item->GetKind()== parask && kind== varsk) return true;//if var is para then return true
    if (kind== varsk && table.GetName()==str && table.GetKind()== funcsk) return true;//if var is the name of this table, and current table is a function then return true.
    if (item->GetKind()!=kind) return false;
    if (item->GetType()!=type) return false;
    return true;
}
コード例 #4
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
void Statement(SymbolTable &table)
{
    switch (symbol) {
        case IFTK:
            IfStatement(table); break;
        case REPTTK:
            RepeatStatement(table); break;
        case BEGINTK:
            CompoundStatement(table); break;
        case READTK:
            ReadStatement(table); break;
        case WRITETK:
            WriteStatement(table); break;
        case FORTK:
            ForStatement(table); break;
        case ENDTK: break;
        case IDEN:
            if (!IsFunctVar(*table.GetItem(token)) && !IsProceVar(*table.GetItem(token))) AssignStatement(table);
            else FunctionCall(table);
            break;
        default: break;//Warning : find empty statement. programer should comnfirm twice.
    }
}
コード例 #5
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
void AssignStatement(SymbolTable &table)
{
    Emit tmp;
    tmp.SetTP( assignet);
    if (symbol!=IDEN) ErrorHandler("Assign should have iden to be assigned");//Error assign should have iden
    SymbolItem* item = table.GetItem(token);
    if (item==NULL && token!=table.GetName()) ErrorHandler("iden not exist while assign");//Error iden not exist;
    if (IsBasicVar(*item, table)) tmp.SetD1(token);
    else if (IsArrayVar(*item)) tmp.SetD1(GetArrayOffset(table));
    else ;//Error This must be somthing I don't expect.
    GetNextSym();
    if (symbol!=ASSIGN) ErrorHandler("assign must have :=") ;//Error should be assign
    GetNextSym();
    tmp.SetS1(Expression(table));
    tmp.emitResult();
}
コード例 #6
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
void ForStatement(SymbolTable &table)
{
    Emit tmp;
    if (symbol!=FORTK) ErrorHandler("for should begin with for");//Error for statement should begin with for
    GetNextSym();
    if (symbol!=IDEN) ErrorHandler("for should have iden as i");//Error for should have an iden to be iterator
    if (!IsBasicVar(*table.GetItem(token), table)) ErrorHandler("iden in for not exist");//Error for iden isn't int the table
    //if (token==table.GetName()) ;//Error function name can not be a iterator. Ok..I will use function name to be a variable
    tmp.SetTP( assignet);
    tmp.SetD1(token);
    string iter(token);
    GetNextSym();
    if (symbol!=ASSIGN) ErrorHandler("for should have :=") ;//Error iden should be assign
    GetNextSym();
    tmp.SetS1(Expression(table));
    tmp.emitResult();
    if (symbol!=TOTK && symbol!=DOWNTOTK) ErrorHandler("for should have to downto");//Error for should have to/downto
    string val = (symbol==TOTK?"1":"-1");
    GetNextSym();
    string cond = Expression(table);
    string labelbegin = "@" + NextTempName(table);
    string labelend = "@" +NextTempName(table);
    interResult<<labelbegin<<endl;
    if (val=="1") tmp.SetTP(greet);
    else tmp.SetTP(lsset);
    tmp.SetS2(cond);
    string desttmp = NextTempName(table);
    tmp.SetS1(tmp.GetD1());
    tmp.SetD1(desttmp);
    tmp.emitResult();
    tmp.SetTP( jzet);
    tmp.SetS1(desttmp);
    tmp.SetD1(labelend);
    tmp.emitResult();
    if (symbol!=DOTK) ErrorHandler("for should have do");//Error for should have do
    GetNextSym();
    Statement(table);
    tmp.SetTP(addet);
    tmp.SetS1(iter);
    tmp.SetS2(val);
    tmp.SetD1(iter);
    tmp.emitResult();
    tmp.SetTP( jmpet);
    tmp.SetD1(labelbegin);
    tmp.emitResult();
    interResult<<labelend<<endl;
}
コード例 #7
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
void ReadStatement(SymbolTable &table)
{
    Emit tmp;
    if (symbol!=READTK) ErrorHandler("read should begin with read");//Error should begin with read
    GetNextSym();
    if (symbol!=LPARENT) ErrorHandler("read should have (");//Error read should have (
    GetNextSym();
    if (symbol!=IDEN) ErrorHandler("read should have at least 1 iden");//Error read should read as least 1 iden
    while (symbol!=RPARENT) {
        if (symbol==LBRACK || symbol==LBRACE || symbol==LPARENT) ErrorHandler("according to syntax, can not read into array or function or procedure");
        if (!IsBasicVar(*table.GetItem(token), table)) ErrorHandler("should read at least 1 symbol");//Error should be iden recorded in symbol list
        //if (table.GetName()==token) ;//Error can not read vaule to a function !!!! This is very important....can function symbol be read when excute this function?
        tmp.SetTP( readet);
        tmp.SetD1(token);
        tmp.emitResult();
        GetNextSym();
        if (symbol==COMMA) GetNextSym();
        if (symbol!=IDEN && symbol!=RPARENT) ErrorHandler("Error while reading");//Error something unknown in read statement
    }
    GetNextSym();
}
コード例 #8
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
string Factor(SymbolTable &table)
{
    string rect="",desttmp;
    Emit tmp;
    tmp.SetTB(&table);
    if (symbol==INTCON) {
        rect=token;
        GetNextSym();
        return rect;
    }
    if (symbol==CONSTTK) {
        ErrorHandler("Can not use char as factor");
    }
    if (symbol==LPARENT)
    {
        GetNextSym();
        rect=Expression(table);
        if (symbol!=RPARENT) ErrorHandler("if factor start with ( then it should end with )");//Error should have )
        GetNextSym();
        return rect;
    }
    if (symbol==IDEN){
        SymbolItem* item = table.GetItem(token);
        bool isfunc=false;//if it is func then should not get next symbol
        if (item==NULL) ;//Error
        if (IsArrayVar(*item)) desttmp=GetArrayOffset(table);
        else if (IsFunctVar(*item)) {
            desttmp=FunctionCall(table);
            isfunc=true;
        }
        else if (IsBasicVar(*item, table)) desttmp=token;
        else if (item->GetKind()==constsk) desttmp=token;
        else ;//Error do not know what it is
        if (!isfunc) GetNextSym();
        return desttmp;
    }
    //Error is not a factor
    return "ERROR";
}
コード例 #9
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
string FunctionCall(SymbolTable &table)
{
    Emit tmp;
    SymbolTable* targetfunc=NULL;
    for (int i=0;i!=ptable;++i) if (symbolTableList[i].GetFather()==&table&&symbolTableList[i].GetName()==token) {
        targetfunc=&symbolTableList[i];
        break;
    }
    tmp.SetTB(targetfunc);
    string desttmp;
    tmp.SetS1(token);
    GetNextSym();
    if (symbol!=LPARENT) ErrorHandler("function call should have (");//Error function call should have (
    SymbolTable* target = GetTableByName(tmp.GetS1());
    SymbolItem* item = table.GetItem(tmp.GetS1());
    GetNextSym();
    CheckParalist(*target,item->GetValue(),table);
    tmp.SetTP( callet);
    tmp.SetS2(IntToString(item->GetValue()));
    desttmp = NextTempName(table);
    tmp.SetD1(desttmp);
    tmp.emitResult();
    return desttmp;
}
コード例 #10
0
ファイル: Parser.cpp プロジェクト: junpengxiao/PL0Compiler
bool CheckItem(string str,SymbolKind kind,SymbolTable &table)
{
    SymbolItem* item = table.GetItem(str);
    if (item->GetKind()!=kind) return false;
    return true;
}