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

📄 语法分析器.cpp

📁 词法分析器
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                return symtable[j].token;

            case  12:
                ch=nextchar();
                if(ch>='0'&&ch<='9') state=13;
                else state=fail();
                break;
            case  13:
                ch=nextchar();
                if(ch>='0'&&ch<='9') state=13;
                else if (ch=='.')  state=14;
                else if (ch=='E')  state=16;
                else state=fail();
                break;
            case  14:
                ch=nextchar();
                if(ch>='0'&&ch<='9') state=15;
                else {
                    cout<<"line"<<lineno<<":\terror"<<endl;
                    retract();
                    lexeme_beginning=forward;
                    return ERROR;
                }
                break;
            case  15:
                ch=nextchar();
                if(ch>='0'&&ch<='9') state=15;
                else if(ch=='E')  state=16;
                else state=fail();
                break;
            case  16:
                ch=nextchar();
                if(ch=='+'||ch=='-') state=17;
                else if(ch>='0'&&ch<='9') state=18;
                else state=fail();
                break;
            case  17:
                ch=nextchar();
                if(ch>='0'&&ch<='9') state=18;
                else state=fail();
                break;
            case  18:
                ch=nextchar();
                if(ch>='0'&&ch<='9') state=18;
                else state=19;
                break;
            case  19:
             
                retract();
                i=0;
                while (lexeme_beginning!=forward) {
                    if(lexeme_beginning==1024) lexeme_beginning++;
                    else if(lexeme_beginning==2049) lexeme_beginning=0;
                    else lexbuf[i++]=file_buf[lexeme_beginning++];
                }
                lexbuf[i]='\0';
                if(!(j=search(lexbuf))) {
                    lexical_value=insert(lexbuf,NUM);
                    if(lexical_value==0) return LY_EOF;
                }
                else 
                    lexical_value=j;
                lexeme_beginning=forward;
                
                return NUM;
            case  20:
                ch=nextchar();
                if(ch>='0'&&ch<='9') state=21;
                else state=fail();
                break;
            case  21:
                ch=nextchar();
                if(ch>='0'&&ch<='9') state=21;
                else if(ch=='.') state=22;
                else state=fail();
                break;
            case  22:
                ch=nextchar();
                if(ch>='0'&&ch<='9') state=23;
                else {
                    cout<<"line"<<lineno<<":\terror"<<endl;
                    retract();
                    lexeme_beginning=forward;
                    return ERROR;
                }
                break;
            case  23:
                ch=nextchar();
                if(ch>='0'&&ch<='9')  state=23;
                else state=24;
                break;
            case  24:
               
                retract();
                i=0;
                while (lexeme_beginning!=forward) {
                    if(lexeme_beginning==1024) lexeme_beginning++;
                    else if(lexeme_beginning==2049) lexeme_beginning=0;
                    else lexbuf[i++]=file_buf[lexeme_beginning++];
                }
                lexbuf[i]='\0';
                if(!(j=search(lexbuf))) {
                    lexical_value=insert(lexbuf,NUM);
                    if(lexical_value==0) return LY_EOF;
                }
                else 
                    lexical_value=j;
                lexeme_beginning=forward;
              
                return NUM;
            case  25:
                ch=nextchar();
                if(ch>='0'&&ch<='9') state=26;
                else state=fail();
                break;
            case  26:
                ch=nextchar();
                if(ch>='0'&&ch<='9') state=26;
                else state=27;
                break;
            case  27:
               
                retract();
                i=0;
                while (lexeme_beginning!=forward) {
                    if(lexeme_beginning==1024) lexeme_beginning++;
                    else if(lexeme_beginning==2049) lexeme_beginning=0;
                    else lexbuf[i++]=file_buf[lexeme_beginning++];
                }
                lexbuf[i]='\0';
                if(!(j=search(lexbuf))) {
                    lexical_value=insert(lexbuf,NUM);
                    if(lexeme_beginning==0) return LY_EOF;
                }
                else
                    lexical_value=j;
                lexeme_beginning=forward;
                
                return NUM;
            case  40:
                ch=nextchar();
                if(ch=='/') state=41;
                else if(ch==34) state=48;
                else if(ch==39) state=50;
                else state=fail();
                break;
            case  41:
                ch=nextchar();
                if(ch=='*') state=43;
                else if(ch=='/') state=42;
                else state=44;
                break;
            case  42:
                ch=nextchar();
                if(ch==LY_EOF) return LY_EOF;
                else if(ch=='\n')  { state=47;  lineno++;}
                else state=42;
                break;
            case  43:
                ch=nextchar();
                if(ch==LY_EOF) return LY_EOF;
                else if(ch=='*') state=45;
                else state=43;
                break;
            case  44:
                retract();
                if(!search("/")) {
                    lexical_value=insert("/",'/');
                    if(lexical_value==0) return LY_EOF;
                }
                lexeme_beginning=forward;
                return '/';
            case  45:
                ch=nextchar();
                if(ch==LY_EOF) return LY_EOF;
                else if(ch=='/') state=46;
                else if(ch=='\n') {lineno++; state=43;}
                else state=43;
                break;
            case  46:
                lexeme_beginning=forward;
                return NOTE;
            case  47:
                lexeme_beginning=forward;
                return NOTE;
            case  48:
                ch=nextchar();
                if(ch==LY_EOF) return LY_EOF;
                else if(ch==34) state=49;
                else state=48;
                break;
            case  49:
                lexeme_beginning=forward;
                return NOTE;
            case  50:
                ch=nextchar();
                if(ch==LY_EOF) return LY_EOF;
                else if(ch==39) state=52;
                else if(ch==92) state=54;
                else state=51;
                break;
            case  51:
                ch=nextchar();
                if(ch==LY_EOF) return LY_EOF;
                else if(ch==39) state=52;
                else state=53;
                break;
            case  52:
                lexeme_beginning=forward;
                return NOTE;
            case  53:
                retract();
                lexeme_beginning=forward;
                cout<<"line"<<lineno<<":\terror"<<endl;
                return ERROR;
            case  54:
                ch=nextchar();
                if(ch==LY_EOF) return LY_EOF;
                else state=51;
                break;
            default:
                ch=nextchar();
                lexbuf[0]=ch;
                lexbuf[1]='\0';
                if(!search(lexbuf)) {
                    lexical_value=insert(lexbuf,ch);
                    if(lexical_value==0) return LY_EOF;
                }
                lexeme_beginning=forward;
                return ch;

        }


    }
}



double expparse();
int  compare(int Vt1,int Vt2);
int  Switch(int Vt);




int parse(){
    int a;
    int b,temp;
    state=0;
    start=0;

    a=lexan();
    if(a==LY_EOF) return a;
    if(a>=310&&a<=313){
        state=0;
        start=0;

        b=lexan();
        while(1){
            if(b!=ID) {
                cout<<"line"<<lineno<<":\t语法错误:定义不完整"<<endl;
                while(b!=59&&b!=LY_EOF) {         
                    state=0;
                    start=0;
                    b=lexan();
                }

                return b;
            }
            else{
                if(symtable[lexical_value].type!=0)
                    cout<<"line"<<lineno<<":\t语法错误:重复定义了同一个变量"<<endl;
                else
                    symtable[lexical_value].type=a;
                temp=lexical_value;
                state=0;
                start=0;
                b=lexan();
                if(b==59) return b;
                if(b==EL) {
                    state=0;
                    start=0;
                    b=lexan();
                    if(b!=ID&&b!=NUM){
                        cout<<"line"<<lineno<<":\t语法错误:表达式不完整"<<endl;
                        while(b!=59&&b!=LY_EOF) {         
                            state=0;
                            start=0;
                            b=lexan();
                        }

                        return b;


                    }
                    else{
                        symtable[temp].value=symtable[lexical_value].value;

                    }
                    state=0;
                    start=0;
                    b=lexan();
                    if(b==59)  return b;
                    if(b!=',') {
                        cout<<"line"<<lineno<<":\t语法错误:本程序暂不能处理"<<endl;
                        while(b!=59&&b!=LY_EOF) {         
                            state=0;
                            start=0;
                            b=lexan();
                        }

                        return b;


                    }



                }
                else if(b!=','){
                    cout<<"line"<<lineno<<":\t语法错误:定义语句有错(可能是缺少;)"<<endl;
                    while(b!=59&&b!=LY_EOF) {         
                        state=0;
                        start=0;
                        b=lexan();
                    }

                    return b;


                }
                state=0;
                start=0;
                b=lexan();



            }

        }

    }


    else if(a==ID){
        if(symtable[lexical_value].type==0)
            cout<<"line"<<lineno<<":\t语法错误:该变量还没有定义"<<endl;
        temp=lexical_value;
        state=0;
        start=0;
        a=lexan();
        if(a==EL) {
            symtable[temp].value=expparse();
            return a;
        }
        else{
            cout<<"line"<<lineno<<":\t语法错误: 本程序暂不能处理"<<endl;
            while(a!=59&&a!=LY_EOF) {         
                state=0;
                start=0;
                a=lexan();
            }

            return a;


        }

    }


    else if(a==NOTE){
        return a ;

    }
    else{
        cout<<"line"<<lineno<<":\t语法错误: 本程序暂不能处理"<<endl;
        while(a!=59&&a!=LY_EOF) {         
            state=0;
            start=0;

⌨️ 快捷键说明

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