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

📄 语法分析器.cpp

📁 词法分析器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
            a=lexan();
        }
    

    return a;
   }


}




int  Switch(int Vt){
    switch(Vt) {
    case ';':
    case 0:
        return 0;              
    case ID:
    case NUM:
        return 1;
    case '+':
        return 2;
    case '-':
        return 3;
    case '*':
        return 4;
    case '/':
        return 5;
    case '^':
        return 6;
    case '(':
        return 7;
    case ')':
        return 8;
    default:
        return 9;
    }
}
int  compare(int Vt1,int Vt2){
    int i,j;
    i=Switch(Vt1);
    j=Switch(Vt2);
    if(i==9||j==9) return 0;
    return priority[i][j];

}
double expparse(){
    int k=1;
    int j;
    int Q;
    int a;
    bool flag=true;
    int s[100];
    double tempvalue[50];
    int    tempvi=0;
    double temp1[5];
    char*  temp2[5];
    char*  p=new char[10];
    p="operator";
    s[1]=0;
    stack ss;
    do{ 
        state=0;
        start=0;
        a=lexan();
        if(flag==true&&a==';'){

            cout<<"line"<<lineno<<":\t语法错误:表达式缺少右边的值"<<endl;

        }
        flag=false;
        if(a==ID&&symtable[lexical_value].type==0)
            cout<<"line"<<lineno<<":\t语法错误:该变量还没有定义"<<endl;


        if(s[k]!=Vn) j=k;
        else j=k-1;
        while(compare(s[j],a)==1){
            do{
                Q=s[j];
                j--;
                if(s[j]==Vn) j--;

            }while(compare(s[j],Q)!=3);
            for(int i=k;i>j;i--){
                int temp3;
                if(s[i]==ID||s[i]==NUM){
                    ss.pop(temp3);
                    temp1[i-j]=symtable[temp3].value;
                    temp2[i-j]=symtable[temp3].lexptr;

                }
                else if(s[i]==Vn){
                    ss.pop(temp3);
                    temp1[i-j]=tempvalue[temp3];
                    temp2[i-j]=NULL;
                }
                else{ 
                    temp1[i-j]=s[i];
                    temp2[i-j]=p;
                }


            }
            if(k-j==3){
                if(temp1[1]=='('&&temp2[1]==p&&temp1[3]==')'&&temp2[3]==p&&temp2[2]!=p){
                    temp1[0]=temp1[2];
                    if(temp2[2]==NULL)
                        cout<<'('<<temp1[2]<<')'<<'='<<temp1[2]<<endl;
                    else 
                        cout<<'('<<temp2[2]<<')'<<'='<<temp2[2]<<endl;
                }
                else if(temp2[1]!=p&&temp2[3]!=p&&temp2[2]==p){
                    switch(int(temp1[2])) {
                    case '+':
                        temp1[0]=temp1[1]+temp1[3];
                        if(temp2[1]==NULL){
                            if(temp1[1]>=0)
                                cout<<temp1[1]<<'+';
                            else
                                cout<<'('<<temp1[1]<<')'<<'+';
                        }
                       
                        else 
                            cout<<temp2[1]<<'+';
                        if(temp2[3]==NULL){
                            if(temp1[3]>=0)
                                cout<<temp1[3]<<'='<<temp1[0]<<endl;
                            else
                                cout<<'('<<temp1[3]<<')'<<'='<<temp1[0]<<endl;
                        }
                        else 
                            cout<<temp2[3]<<'='<<temp1[0]<<endl;

                        break;
                    case '-':
                        temp1[0]=temp1[1]-temp1[3];
                        if(temp2[1]==NULL)
                            cout<<temp1[1]<<'-';
                        else 
                            cout<<temp2[1]<<'-';
                        if(temp2[3]==NULL){
                            if(temp1[3]>=0)
                                cout<<temp1[3]<<'='<<temp1[0]<<endl;
                            else
                                cout<<'('<<temp1[3]<<')'<<'='<<temp1[0]<<endl;
                        }
                        else 
                            cout<<temp2[3]<<'='<<temp1[0]<<endl;


                        break;
                    case '*':
                        temp1[0]=temp1[1]*temp1[3];
                        if(temp2[1]==NULL)
                            cout<<temp1[1]<<'*';
                        else 
                            cout<<temp2[1]<<'*';
                        if(temp2[3]==NULL){
                            if(temp1[3]>=0)
                                cout<<temp1[3]<<'='<<temp1[0]<<endl;
                            else
                                cout<<'('<<temp1[3]<<')'<<'='<<temp1[0]<<endl;
                        }
                        else 
                            cout<<temp2[3]<<'='<<temp1[0]<<endl;

                        break;
                    case '/':
                        if(temp1[3]==0){
                            cout<<"line"<<lineno<<":\t语法错误: 表达式有错误(除数为零)"<<endl;
                            while(a!=59&&a!=LY_EOF) {         
                                state=0;
                                start=0;
                                a=lexan();
                            }

                            return a;


                        }
                        temp1[0]=temp1[1]/temp1[3];
                        if(temp2[1]==NULL)
                            cout<<temp1[1]<<'/';
                        else 
                            cout<<temp2[1]<<'/';
                        if(temp2[3]==NULL){
                            if(temp1[3]>=0)
                                cout<<temp1[3]<<'='<<temp1[0]<<endl;
                            else
                                cout<<'('<<temp1[3]<<')'<<'='<<temp1[0]<<endl;
                        }
                        else 
                            cout<<temp2[3]<<'='<<temp1[0]<<endl;

                        break;

                    case '^':
                        temp1[0]=pow(temp1[1],temp1[3]);
                        if(temp2[1]==NULL)
                            cout<<temp1[1]<<'^';
                        else 
                            cout<<temp2[1]<<'^';
                        if(temp2[3]==NULL){
                            if(temp1[3]>=0)
                                cout<<temp1[3]<<'='<<temp1[0]<<endl;
                            else
                                cout<<'('<<temp1[3]<<')'<<'='<<temp1[0]<<endl;
                        }
                        else 
                            cout<<temp2[3]<<'='<<temp1[0]<<endl;

                        break;

                    default:
                        cout<<"line"<<lineno<<":\t语法错误: 表达式有错误"<<endl;
                        while(a!=59&&a!=LY_EOF) {         
                            state=0;
                            start=0;
                            a=lexan();
                        }

                        return a;


                    }
                }
                else{
                    cout<<"line"<<lineno<<":\t语法错误: 表达式有错误"<<endl;
                    while(a!=59&&a!=LY_EOF) {         
                        state=0;
                        start=0;
                        a=lexan();
                    }

                    return a;




                }
            }
            else if(k-j==2){
                if(temp1[1]=='-'&&temp2[1]==p&&temp2[2]==NULL){
                    temp1[0]=-temp1[2];
                    cout<<temp1[0]<<'='<<temp1[0]<<endl;
                }
                else{
                    cout<<"line"<<lineno<<":\t语法错误: 表达式有错误"<<endl;
                    while(a!=59&&a!=LY_EOF) {         
                        state=0;
                        start=0;
                        a=lexan();
                    }

                    return a;
                }




            }
            else if(k-j==1){
                if(temp2[1]!=NULL&&temp2[1]!=p){
                    temp1[0]=temp1[1];
                    cout<<temp2[1]<<'='<<temp1[1]<<endl;
                }
                else{
                    cout<<"line"<<lineno<<":\t语法错误: 表达式有错误"<<endl;
                    while(a!=59&&a!=LY_EOF) {         
                        state=0;
                        start=0;
                        a=lexan();
                    }

                    return a;
                }

            }
            else {
                cout<<"line"<<lineno<<":\t语法错误: 表达式有错误"<<endl;
                while(a!=59&&a!=LY_EOF) {         
                    state=0;
                    start=0;
                    a=lexan();
                }

                return a;


            }

            k=j+1;
            s[k]=Vn;
            tempvalue[tempvi]=temp1[0];
            ss.push(tempvi);
            tempvi++;

        }
        if(compare(s[j],a)==2||compare(s[j],a)==3){
            k++;
            s[k]=a;
            if(a==ID||a==NUM)
               ss.push(lexical_value);
        }
        else{
            cout<<"line"<<lineno<<":\t语法错误: 表达式有错误"<<endl;
            while(a!=59&&a!=LY_EOF) {         
                state=0;
                start=0;
                a=lexan();
            }

            return a;



        }


    }while(a!=';');
    return temp1[0];


}




void main(){
    char *filename=new char[20];
    file_buf[1024]=LY_EOF;
    file_buf[2049]=LY_EOF;
    while(1){

        cout<<"请输入你要编译的文件名(退出请输入exit):"<<endl;
        cin>>filename;
        if(!strcmp(filename,"exit")) return;


        lexeme_beginning=0;
        forward=0;
        state=0;
        start=0;
        lastchar=-1;
        lastentry=0;
        lineno=1;
        for(int i=0;i<500;i++){
            symtable[i].type=0;
            symtable[i].lexptr=NULL;
        }

        // get the file length 

        int fh;
        if( ( fh = _open( filename, _O_BINARY, _S_IREAD ))  != -1 ) {
            filelen = _filelength( fh ) ;
        }
        _close( fh );
        filelen=filelen%1024;

        fp=fopen(filename,"rt");
        if(fp==NULL){
            cout<<"Can not open the file."<<endl;
            return;
        }
        fread(file_buf,1,1024,fp);


        while (parse()!=LY_EOF);
        fclose(fp);
    }



}

⌨️ 快捷键说明

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